Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP][Feature] Add ViT-Adapter Model #2762

Merged
merged 17 commits into from
Mar 17, 2023
Prev Previous commit
Next Next commit
1116 0
  • Loading branch information
juncaipeng committed Nov 16, 2022
commit 7193096d387d9cafc9a04e9e4d30270201f89be5
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ model:
cffn_ratio: 0.25
deform_ratio: 1.0
interaction_indexes: [[0, 2], [3, 5], [6, 8], [9, 11]]
pretrained: pretrained_model/deit_tiny_patch16_224-a1311bcf_from_torch.pdparams
backbone_indices: [0, 1, 2, 3]
channels: 512
pool_scales: [1, 2, 3, 6]
dropout_ratio: 0.1
aux_loss: True
aux_channels: 256
pretrained: pretrained_model/upernet_deit_adapter_tiny_512_160_ade20k_from_torch.pdparams
#pretrained: pretrained_model/upernet_deit_adapter_tiny_512_160_ade20k_from_torch.pdparams
14 changes: 0 additions & 14 deletions paddleseg/models/backbones/vit_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,20 +256,6 @@ def __init__(self,
def init_weight(self):
utils.load_pretrained_model(self, self.pretrained)

def forward_features(self, x):
x, H, W = self.patch_embed(x)
cls_token = self.cls_token.expand(
x.shape[0], -1, -1) # stole cls_tokens impl from Phil Wang, thanks
x = paddle.concat([cls_tokens, x], axis=1)
x = self.pos_drop(x + self.pos_embed)
for blk in self.blocks:
x = blk(x, H, W)
return x

def forward(self, x):
x = self.forward_features(x)
return x


@manager.BACKBONES.add_component
class ViTAdapter(VisionTransformer):
Expand Down
10 changes: 5 additions & 5 deletions paddleseg/models/layers/vit_adapter_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ def _reset_parameters(self):
for i in range(self.n_points):
grid_init[:, :, i, :] *= i + 1

with paddle.no_grad():
grid_init = grid_init.reshape([-1])
self.sampling_offsets.bias = self.create_parameter(
shape=grid_init.shape,
default_initializer=paddle.nn.initializer.Assign(grid_init))
grid_init = grid_init.reshape([-1])
self.sampling_offsets.bias = self.create_parameter(
shape=grid_init.shape,
default_initializer=paddle.nn.initializer.Assign(grid_init))
self.sampling_offsets.bias.stop_gradient = True

constant_init(self.attention_weights.weight, value=0.)
constant_init(self.attention_weights.bias, value=0.)
Expand Down