Skip to content

[Refactor] Using mmcv transformer bricks to refactor vit.#571

Merged
xvjiarui merged 14 commits intoopen-mmlab:masterfrom
clownrat6:vit_refactor
Jun 17, 2021
Merged

[Refactor] Using mmcv transformer bricks to refactor vit.#571
xvjiarui merged 14 commits intoopen-mmlab:masterfrom
clownrat6:vit_refactor

Conversation

@clownrat6
Copy link
Copy Markdown
Contributor

@clownrat6 clownrat6 commented May 25, 2021

The foundation of this PR:
mmcv: open-mmlab/mmcv#978 (merged)
mmclassification: open-mmlab/mmpretrain#295

@clownrat6 clownrat6 requested a review from xvjiarui May 25, 2021 14:34
@xvjiarui
Copy link
Copy Markdown
Collaborator

We may exclude SETR from this PR. We will refactor #531 and #520

@codecov
Copy link
Copy Markdown

codecov bot commented Jun 16, 2021

Codecov Report

Merging #571 (a9cb251) into master (608f842) will decrease coverage by 0.49%.
The diff coverage is 67.96%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #571      +/-   ##
==========================================
- Coverage   85.95%   85.45%   -0.50%     
==========================================
  Files         101      101              
  Lines        5234     5220      -14     
  Branches      828      840      +12     
==========================================
- Hits         4499     4461      -38     
- Misses        561      586      +25     
+ Partials      174      173       -1     
Flag Coverage Δ
unittests 85.45% <67.96%> (-0.50%) ⬇️

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

Impacted Files Coverage Δ
mmseg/models/utils/timm_convert.py 7.40% <7.40%> (ø)
mmseg/models/backbones/vit.py 83.81% <89.33%> (-2.34%) ⬇️
mmseg/models/utils/__init__.py 100.00% <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 608f842...a9cb251. Read the comment docs.

sennnnn added 3 commits June 16, 2021 13:07
1. Fix low code coverage of vit.py;

2. Remove HybirdEmbed;

3. Fix doc string of VisionTransformer;
@@ -0,0 +1,53 @@
import logging
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is this file necessary?

Comment on lines +1 to +20
import collections.abc
from itertools import repeat


# From PyTorch internals
def _ntuple(n):

def parse(x):
if isinstance(x, collections.abc.Iterable):
return x
return tuple(repeat(x, n))

return parse


to_1tuple = _ntuple(1)
to_2tuple = _ntuple(2)
to_3tuple = _ntuple(3)
to_4tuple = _ntuple(4)
to_ntuple = _ntuple
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This file is not necessary. Use from torch.nn.modules.utils import _pair as to_2tuple instead.

Comment on lines +347 to +337
# We only implement the 'jax_impl' initialization implemented at
# https://github.com/rwightman/pytorch-image-models/blob/master/timm/models/vision_transformer.py#L353 # noqa: E501
trunc_normal_(self.pos_embed, std=.02)
trunc_normal_(self.cls_token, std=.02)
for n, m in self.named_modules():
if isinstance(m, Linear):
trunc_normal_(m.weight, std=.02)
if m.bias is not None:
if 'mlp' in n:
normal_init(m.bias, std=1e-6)
else:
constant_init(m.bias, 0)
elif isinstance(m, Conv2d):
kaiming_init(m.weight, mode='fan_in')
if m.bias is not None:
constant_init(m.bias, 0)
elif isinstance(m, (_BatchNorm, nn.GroupNorm, nn.LayerNorm)):
constant_init(m.bias, 0)
constant_init(m.weight, 1.0)
else:
raise TypeError('pretrained must be a str or None')
# Modified from ClassyVision
nn.init.normal_(self.pos_embed, std=0.02)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why remove the initialization?

else:
state_dict = checkpoint

if 'rwightman/pytorch-image-models' in pretrained:
Copy link
Copy Markdown
Collaborator

@xvjiarui xvjiarui Jun 17, 2021

Choose a reason for hiding this comment

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

If user downloaded the weight from timm and would like to init the model with path, the condition does not hold.

include:
- torch: 1.3.0+cpu
torchvision: 0.4.1+cpu
torch_version: 1.3.0
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reset this file.

with_cp (bool): Use checkpoint or not. Using checkpoint will save
some memory while slowing down the training speed. Default: False.
pretrain_style (str): Choose to use timm or mmcls pretrain weights.
Default: timm.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We should explain what options are supported, and add assert.

@xvjiarui xvjiarui merged commit 8f8abe3 into open-mmlab:master Jun 17, 2021
bowenroom pushed a commit to bowenroom/mmsegmentation that referenced this pull request Feb 25, 2022
…#571)

* [Refactor] Using mmcv bricks to refactor vit

* Follow the vit code structure from mmclassification

* Add MMCV install into CI system.

* Add  to 'Install MMCV' CI item

* Add 'Install MMCV_CPU' and 'Install MMCV_GPU CI' items

* Fix & Add

1. Fix low code coverage of vit.py;

2. Remove HybirdEmbed;

3. Fix doc string of VisionTransformer;

* Add helpers unit test.

* Add converter to convert vit pretrain weights from timm style to mmcls style.

* Clean some rebundant code and refactor init

1. Use timm style init_weights;

2. Remove to_xtuple and trunc_norm_;

* Add comments for VisionTransformer.init_weights()

* Add arg: pretrain_style to choose timm or mmcls vit pretrain weights.
michaelzhang-ai pushed a commit to michaelzhang-ai/mmsegmentation that referenced this pull request Mar 22, 2024
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.

2 participants