mirror of
https://github.com/open-mmlab/mmselfsup.git
synced 2025-06-03 14:59:38 +08:00
* [Fix]: Fix mmcls upgrade bug (#235) * [Feature]: Add multi machine dist_train (#232) * [Feature]: Add multi machine dist_train * [Fix]: Change bash to sh * [Fix]: Fix missing sh suffix * [Refactor]: Change bash to sh * [Refactor] Add unit test (#234) * [Refactor] add unit test * update workflow * update * [Fix] fix lint * update test * refactor moco and densecl unit test * fix lint * add unit test * update unit test * remove modification * [Feature]: Add MAE metafile (#238) * [Feature]: Add MAE metafile * [Fix]: Fix lint * [Fix]: Change LARS to AdamW in the metafile of MAE * [Fix] fix codecov bug (#241) * [Fix] fix codecov bug * update comment * [Refactor] Using MMCls backbones (#233) * [Refactor] using backbones from MMCls * [Refactor] modify the unit test * [Fix] modify default setting of out_indices * [Docs] fix lint * [Refactor] modify super init * [Refactore] remove res_layer.py * using mmcv PatchEmbed * [Fix]: Fix outdated problem (#249) * [Fix]: Fix outdated problem * [Fix]: Update MoCov3 bibtex * [Fix]: Use abs path in README * [Fix]: Reformat MAE bibtex * [Fix]: Reformat MoCov3 bibtex * [Feature] Resume from the latest checkpoint automatically. (#245) * [Feature] Resume from the latest checkpoint automatically. * fix windows path problem * fix lint * add code reference * [Docs] add docstring for ResNet and ResNeXt (#252) * [Feature] support KNN benchmark (#243) * [Feature] support KNN benchmark * [Fix] add docstring and multi-machine testing * [Fix] fix lint * [Fix] change args format and check init_cfg * [Docs] add benchmark tutorial * [Docs] add benchmark results * [Feature]: SimMIM supported (#239) * [Feature]: SimMIM Pretrain * [Feature]: Add mix precision and 16x128 config * [Fix]: Fix config import bug * [Fix]: Fix config bug * [Feature]: Simim Finetune * [Fix]: Log every 100 * [Fix]: Fix eval problem * [Feature]: Add docstring for simmim * [Refactor]: Merge layer wise lr decay to Default constructor * [Fix]:Fix simmim evaluation bug * [Fix]: Change model to be compatible to latest version of mmcls * [Fix]: Fix lint * [Fix]: Rewrite forward_train for classification cls * [Feature]: Add UT * [Fix]: Fix lint * [Feature]: Add 32 gpus training for simmim ft * [Fix]: Rename mmcls classifier wrapper * [Fix]: Add docstring to SimMIMNeck * [Feature]: Generate docstring for the forward function of simmim encoder * [Fix]: Rewrite the class docstring for constructor * [Fix]: Fix lint * [Fix]: Fix UT * [Fix]: Reformat config * [Fix]: Add img resolution * [Feature]: Add readme and metafile * [Fix]: Fix typo in README.md * [Fix]: Change BlackMaskGen to BlockwiseMaskGenerator * [Fix]: Change the name of SwinForSimMIM * [Fix]: Delete irrelevant files * [Feature]: Create extra transformerfinetuneconstructor * [Fix]: Fix lint * [Fix]: Update SimMIM README * [Fix]: Change SimMIMPretrainHead to SimMIMHead * [Fix]: Fix the docstring of ft constructor * [Fix]: Fix UT * [Fix]: Recover deletion Co-authored-by: Your <you@example.com> * [Fix] add seed to distributed sampler (#250) * [Fix] add seed to distributed sampler * fix lint * [Feature] Add ImageNet21k (#225) * solve memory leak by limited implementation * fix lint problem Co-authored-by: liming <liming.ai@bytedance.com> * [Refactor] change args format to '--a-b' (#253) * [Refactor] change args format to `--a-b` * modify tsne script * modify 'sh' files * modify getting_started.md * modify getting_started.md * [Fix] fix 'mkdir' error in prepare_voc07_cls.sh (#261) * [Fix] fix positional parameter error (#260) * [Fix] fix command errors in benchmarks tutorial (#263) * [Docs] add brief installation steps in README.md (#265) * [Docs] add colab tutorial (#247) * [Docs] add colab tutorial * fix lint * modify the colab tutorial, using API to train the model * modify the description * remove # * modify the command * [Docs] translate 6_benchmarks.md into Chinese (#262) * [Docs] translate 6_benchmarks.md into Chinese * Update 6_benchmarks.md change 基准 to 基准评测 * Update 6_benchmarks.md (1) Add Chinese translation of ‘1 folder for ImageNet nearest-neighbor classification task’ (2) 数据预准备 -> 数据准备 * [Docs] remove install scripts in README (#267) * [Docs] Update version information in dev branch (#268) * update version to v0.8.0 * fix lint * [Fix]: Install the latest mmcls * [Fix]: Add SimMIM in RAEDME Co-authored-by: Yuan Liu <30762564+YuanLiuuuuuu@users.noreply.github.com> Co-authored-by: Jiahao Xie <52497952+Jiahao000@users.noreply.github.com> Co-authored-by: Your <you@example.com> Co-authored-by: Ming Li <73068772+mitming@users.noreply.github.com> Co-authored-by: liming <liming.ai@bytedance.com> Co-authored-by: RenQin <45731309+soonera@users.noreply.github.com> Co-authored-by: YuanLiuuuuuu <3463423099@qq.com>
140 lines
4.3 KiB
Python
140 lines
4.3 KiB
Python
# Copyright (c) OpenMMLab. All rights reserved.
|
|
import torch
|
|
from mmcv.utils.parrots_wrapper import _BatchNorm
|
|
|
|
from mmselfsup.models.backbones import ResNet
|
|
from mmselfsup.models.backbones.resnet import BasicBlock, Bottleneck
|
|
|
|
|
|
def is_block(modules):
|
|
"""Check if is ResNet building block."""
|
|
if isinstance(modules, (BasicBlock, Bottleneck)):
|
|
return True
|
|
return False
|
|
|
|
|
|
def all_zeros(modules):
|
|
"""Check if the weight(and bias) is all zero."""
|
|
weight_zero = torch.equal(modules.weight.data,
|
|
torch.zeros_like(modules.weight.data))
|
|
if hasattr(modules, 'bias'):
|
|
bias_zero = torch.equal(modules.bias.data,
|
|
torch.zeros_like(modules.bias.data))
|
|
else:
|
|
bias_zero = True
|
|
|
|
return weight_zero and bias_zero
|
|
|
|
|
|
def check_norm_state(modules, train_state):
|
|
"""Check if norm layer is in correct train state."""
|
|
for mod in modules:
|
|
if isinstance(mod, _BatchNorm):
|
|
if mod.training != train_state:
|
|
return False
|
|
return True
|
|
|
|
|
|
def test_resnet():
|
|
"""Test resnet backbone."""
|
|
# Test ResNet50 norm_eval=True
|
|
model = ResNet(50, norm_eval=True)
|
|
model.init_weights()
|
|
model.train()
|
|
assert check_norm_state(model.modules(), False)
|
|
|
|
# Test ResNet50 with torchvision pretrained weight
|
|
model = ResNet(depth=50, norm_eval=True)
|
|
model.init_weights()
|
|
model.train()
|
|
assert check_norm_state(model.modules(), False)
|
|
|
|
# Test ResNet50 with first stage frozen
|
|
frozen_stages = 1
|
|
model = ResNet(50, frozen_stages=frozen_stages)
|
|
model.init_weights()
|
|
model.train()
|
|
assert model.norm1.training is False
|
|
for layer in [model.conv1, model.norm1]:
|
|
for param in layer.parameters():
|
|
assert param.requires_grad is False
|
|
for i in range(1, frozen_stages + 1):
|
|
layer = getattr(model, f'layer{i}')
|
|
for mod in layer.modules():
|
|
if isinstance(mod, _BatchNorm):
|
|
assert mod.training is False
|
|
for param in layer.parameters():
|
|
assert param.requires_grad is False
|
|
|
|
# Test ResNet18 forward
|
|
model = ResNet(18, out_indices=(0, 1, 2, 3, 4))
|
|
model.init_weights()
|
|
model.train()
|
|
|
|
imgs = torch.randn(1, 3, 224, 224)
|
|
feat = model(imgs)
|
|
assert len(feat) == 5
|
|
assert feat[0].shape == (1, 64, 112, 112)
|
|
assert feat[1].shape == (1, 64, 56, 56)
|
|
assert feat[2].shape == (1, 128, 28, 28)
|
|
assert feat[3].shape == (1, 256, 14, 14)
|
|
assert feat[4].shape == (1, 512, 7, 7)
|
|
|
|
# Test ResNet50 with BatchNorm forward
|
|
model = ResNet(50, out_indices=(0, 1, 2, 3, 4))
|
|
model.init_weights()
|
|
model.train()
|
|
|
|
imgs = torch.randn(1, 3, 224, 224)
|
|
feat = model(imgs)
|
|
assert len(feat) == 5
|
|
assert feat[0].shape == (1, 64, 112, 112)
|
|
assert feat[1].shape == (1, 256, 56, 56)
|
|
assert feat[2].shape == (1, 512, 28, 28)
|
|
assert feat[3].shape == (1, 1024, 14, 14)
|
|
assert feat[4].shape == (1, 2048, 7, 7)
|
|
|
|
# Test ResNet50 with layers 3 (top feature maps) out forward
|
|
model = ResNet(50, out_indices=(4, ))
|
|
model.init_weights()
|
|
model.train()
|
|
|
|
imgs = torch.randn(1, 3, 224, 224)
|
|
feat = model(imgs)
|
|
assert feat[0].shape == (1, 2048, 7, 7)
|
|
|
|
# Test ResNet50 with checkpoint forward
|
|
model = ResNet(50, out_indices=(0, 1, 2, 3, 4), with_cp=True)
|
|
for m in model.modules():
|
|
if is_block(m):
|
|
assert m.with_cp
|
|
model.init_weights()
|
|
model.train()
|
|
|
|
imgs = torch.randn(1, 3, 224, 224)
|
|
feat = model(imgs)
|
|
assert len(feat) == 5
|
|
assert feat[0].shape == (1, 64, 112, 112)
|
|
assert feat[1].shape == (1, 256, 56, 56)
|
|
assert feat[2].shape == (1, 512, 28, 28)
|
|
assert feat[3].shape == (1, 1024, 14, 14)
|
|
assert feat[4].shape == (1, 2048, 7, 7)
|
|
|
|
# zero initialization of residual blocks
|
|
model = ResNet(50, zero_init_residual=True)
|
|
model.init_weights()
|
|
for m in model.modules():
|
|
if isinstance(m, Bottleneck):
|
|
assert all_zeros(m.norm3)
|
|
elif isinstance(m, BasicBlock):
|
|
assert all_zeros(m.norm2)
|
|
|
|
# non-zero initialization of residual blocks
|
|
model = ResNet(50, zero_init_residual=False)
|
|
model.init_weights()
|
|
for m in model.modules():
|
|
if isinstance(m, Bottleneck):
|
|
assert not all_zeros(m.norm3)
|
|
elif isinstance(m, BasicBlock):
|
|
assert not all_zeros(m.norm2)
|