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

[Fix] fix patch_embed and pos_embed mismatch error #685

Merged
merged 13 commits into from
Jul 19, 2021

Conversation

xiexinch
Copy link
Collaborator

@xiexinch xiexinch commented Jul 8, 2021

Thanks for your contribution and we appreciate it a lot. The following instructions would make your pull request more healthy and more easily get feedback. If you do not understand some items, don't worry, just make the pull request and seek help from maintainers.

Motivation

Fix patch_embed and pos_embed mismatch error and remove out_shape param.

Modification

  • In resize_pos_embed() function, interpolation will match padded input image.
  • Replace out_shape parameters with output_cls_token, user may choose whether append cls_token to output feature map.

Checklist

  1. Pre-commit or other linting tools are used to fix the potential lint issues.
  2. The modification is covered by complete unit tests. If not, please add more unit test to ensure the correctness.
  3. If the modification has potential influence on downstream projects, this PR should be tested with downstream projects, like MMDet or MMDet3D.
  4. The documentation has been modified accordingly, like docstring or example tutorials.

@codecov
Copy link

codecov bot commented Jul 8, 2021

Codecov Report

Merging #685 (9a4e1e8) into master (5097d55) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #685   +/-   ##
=======================================
  Coverage   85.78%   85.78%           
=======================================
  Files         105      105           
  Lines        5627     5627           
  Branches      915      916    +1     
=======================================
  Hits         4827     4827           
  Misses        621      621           
  Partials      179      179           
Flag Coverage Δ
unittests 85.76% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
mmseg/models/backbones/vit.py 84.84% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5097d55...9a4e1e8. Read the comment docs.

Comment on lines 383 to 384
x, H, W = self.patch_embed(
inputs), self.patch_embed.DH, self.patch_embed.DW
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may use hw_shape instead, to be consistent with Swin Transformer.

@@ -317,14 +316,13 @@ def init_weights(self):
constant_init(m.bias, 0)
constant_init(m.weight, 1.0)

def _pos_embeding(self, img, patched_img, pos_embed):
def _pos_embeding(self, downsampled_img_size, patched_img, pos_embed):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def _pos_embeding(self, downsampled_img_size, patched_img, pos_embed):
def _pos_embeding(self, x, hw_shape, pos_embed):

assert out_shape in ['NLC',
'NCHW'], 'output shape must be "NLC" or "NCHW".'
if output_cls_token:
assert with_cls_token is True, f'with_cls_token must be True if' \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to add this description to Docstring.

Comment on lines 357 to 360
pos_embed (torch.Tensor): pos_embed weights.
input_shpae (tuple): Tuple for (input_h, intput_w).
pos_shape (tuple): Tuple for (pos_h, pos_w).
patch_size (int): Patch size.
mode (str): Algorithm used for upsampling.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to descript the Args in more detail, and we don’t need abbreviations in the description, e.g. pos_embed should be position embedding.

@@ -371,7 +370,7 @@ def resize_pos_embed(pos_embed, input_shpae, pos_shape, patch_size, mode):
1, pos_h, pos_w, pos_embed.shape[2]).permute(0, 3, 1, 2)
pos_embed_weight = F.interpolate(
pos_embed_weight,
size=[input_h // patch_size, input_w // patch_size],
size=[input_h, input_w],
Copy link
Collaborator

@Junjun2016 Junjun2016 Jul 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
size=[input_h, input_w],
size=input_shpae,

so input_h, input_w = input_shpae is redundant.

patched_img (torch.Tensor): The patched image, it should be
shape of [B, L1, C].
hw_shape (tuple): The downsampled image resolution.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe output_shape is better.

I am not sure. @xvjiarui

self.interpolate_mode)
return self.drop_after_pos(patched_img + pos_embed)

@staticmethod
def resize_pos_embed(pos_embed, input_shpae, pos_shape, patch_size, mode):
def resize_pos_embed(pos_embed, input_shpae, pos_shape, mode):
"""Resize pos_embed weights.

Resize pos_embed using bicubic interpolate method.
Args:
pos_embed (torch.Tensor): pos_embed weights.
input_shpae (tuple): Tuple for (input_h, intput_w).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe output_shape is better.

I am not sure. @xvjiarui

@xvjiarui
Copy link
Collaborator

Some configs still have out_shape.
Please kindly delete them.

@xiexinch
Copy link
Collaborator Author

Some configs still have out_shape.
Please kindly delete them.

I have checked all vit configs, out_shape is not found.

@xvjiarui xvjiarui merged commit dff7a96 into open-mmlab:master Jul 19, 2021
bowenroom pushed a commit to bowenroom/mmsegmentation that referenced this pull request Feb 25, 2022
* fix patch_embed and pos_embed mismatch error

* add docstring

* update unittest

* use downsampled image shape

* use tuple

* remove unused parameters and add doc

* fix init weights function

* revise docstring

* Update vit.py

If -> Whether

* fix lint

Co-authored-by: Junjun2016 <hejunjun@sjtu.edu.cn>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants