Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ModelZoo
ResNet50_tensorflow
Commits
6e2129f6
Commit
6e2129f6
authored
Oct 18, 2022
by
Chaochao Yan
Committed by
A. Unique TensorFlower
Oct 18, 2022
Browse files
Internal change
PiperOrigin-RevId: 481938528
parent
09b3f5ab
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
9 additions
and
10 deletions
+9
-10
official/projects/vit/modeling/vit.py
official/projects/vit/modeling/vit.py
+9
-10
No files found.
official/projects/vit/modeling/vit.py
View file @
6e2129f6
...
...
@@ -26,7 +26,6 @@ from official.projects.vit.modeling.vit_specs import VIT_SPECS
from
official.vision.modeling.backbones
import
factory
from
official.vision.modeling.layers
import
nn_layers
layers
=
tf
.
keras
.
layers
...
...
@@ -67,9 +66,7 @@ class AddPositionEmbs(tf.keras.layers.Layer):
self
.
pos_embedding
=
self
.
add_weight
(
'pos_embedding'
,
pos_emb_shape
,
initializer
=
self
.
posemb_init
)
def
_interpolate
(
self
,
pos_embedding
:
tf
.
Tensor
,
from_shape
:
Tuple
[
int
,
int
],
def
_interpolate
(
self
,
pos_embedding
:
tf
.
Tensor
,
from_shape
:
Tuple
[
int
,
int
],
to_shape
:
Tuple
[
int
,
int
])
->
tf
.
Tensor
:
"""Interpolates the positional embeddings."""
logging
.
info
(
'Interpolating postional embedding from length: %d to %d'
,
...
...
@@ -84,9 +81,10 @@ class AddPositionEmbs(tf.keras.layers.Layer):
pos_embedding
=
self
.
pos_embedding
# inputs.shape is (batch_size, seq_len, emb_dim).
if
inputs
.
shape
[
1
]
!=
pos_embedding
.
shape
[
1
]:
pos_embedding
=
self
.
_interpolate
(
pos_embedding
,
from_shape
=
self
.
posemb_origin_shape
,
to_shape
=
self
.
posemb_target_shape
)
pos_embedding
=
self
.
_interpolate
(
pos_embedding
,
from_shape
=
self
.
posemb_origin_shape
,
to_shape
=
self
.
posemb_target_shape
)
pos_embedding
=
tf
.
cast
(
pos_embedding
,
inputs
.
dtype
)
return
inputs
+
pos_embedding
...
...
@@ -262,7 +260,8 @@ class VisionTransformer(tf.keras.Model):
class_name
=
'TruncatedNormal'
,
config
=
dict
(
stddev
=
.
02
)),
init_stochastic_depth_rate
=
init_stochastic_depth_rate
,
pos_embed_origin_shape
=
pos_embed_shape
,
pos_embed_target_shape
=
pos_embed_target_shape
)(
x
)
pos_embed_target_shape
=
pos_embed_target_shape
)(
x
)
if
pooler
==
'token'
:
x
=
x
[:,
0
]
...
...
@@ -303,8 +302,8 @@ def build_vit(input_specs,
del
norm_activation_config
backbone_type
=
backbone_config
.
type
backbone_cfg
=
backbone_config
.
get
()
assert
backbone_type
==
'vit'
,
(
f
'Inconsistent backbone type '
f
'
{
backbone_type
}
'
)
assert
backbone_type
==
'
legacy_
vit'
,
(
f
'Inconsistent backbone type '
f
'
{
backbone_type
}
'
)
backbone_cfg
.
override
(
VIT_SPECS
[
backbone_cfg
.
model_name
])
return
VisionTransformer
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment