mmclassification/tests/test_backbone.py

166 lines
5.1 KiB
Python
Raw Normal View History

2020-06-04 02:22:53 +08:00
import pytest
2020-06-03 15:51:17 +08:00
import torch
2020-06-04 02:22:53 +08:00
from torch.nn.modules import GroupNorm
from torch.nn.modules.batchnorm import _BatchNorm
2020-06-03 15:51:17 +08:00
2020-06-07 22:40:36 +08:00
from mmcls.models.backbones import ShuffleNetv1
from mmcls.models.backbones.shufflenet_v1 import ShuffleUnit
2020-06-04 02:22:53 +08:00
def is_block(modules):
"""Check if is ResNet building block."""
2020-06-07 22:40:36 +08:00
if isinstance(modules, (ShuffleUnit, )):
2020-06-04 02:22:53 +08:00
return True
return False
def is_norm(modules):
"""Check if is one of the norms."""
if isinstance(modules, (GroupNorm, _BatchNorm)):
return True
return False
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
2020-06-07 22:40:36 +08:00
def test_shufflenetv1_shuffleuint():
with pytest.raises(ValueError):
# combine must be in ['add', 'concat']
ShuffleUnit(24, 16, groups=3, first_block=True, combine='test')
with pytest.raises(ValueError):
# inplanes must be divisible by groups
2020-06-07 22:40:36 +08:00
ShuffleUnit(64, 64, groups=3, first_block=True, combine='add')
2020-06-04 02:22:53 +08:00
with pytest.raises(AssertionError):
2020-06-07 22:40:36 +08:00
# inplanes must be equal tp = outplanes when combine='add'
ShuffleUnit(64, 24, groups=3, first_block=True, combine='add')
2020-06-04 02:22:53 +08:00
2020-06-07 22:40:36 +08:00
# Test ShuffleUnit with combine='add'
block = ShuffleUnit(24, 24, groups=3, first_block=True, combine='add')
x = torch.randn(1, 24, 56, 56)
2020-06-04 02:22:53 +08:00
x_out = block(x)
2020-06-07 22:40:36 +08:00
assert x_out.shape == torch.Size([1, 24, 56, 56])
2020-06-04 02:22:53 +08:00
2020-06-07 22:40:36 +08:00
# Test ShuffleUnit with combine='concat'
block = ShuffleUnit(24, 240, groups=3, first_block=True, combine='concat')
x = torch.randn(1, 24, 56, 56)
2020-06-04 02:22:53 +08:00
x_out = block(x)
2020-06-07 22:40:36 +08:00
assert x_out.shape == torch.Size([1, 240, 28, 28])
2020-06-04 02:22:53 +08:00
2020-06-07 22:40:36 +08:00
# Test ShuffleUnit with checkpoint forward
block = ShuffleUnit(
24, 24, groups=3, first_block=True, combine='add', with_cp=True)
x = torch.randn(1, 24, 56, 56)
2020-06-04 02:22:53 +08:00
x_out = block(x)
2020-06-07 22:40:36 +08:00
assert x_out.shape == torch.Size([1, 24, 56, 56])
2020-06-04 02:22:53 +08:00
2020-06-03 15:51:17 +08:00
2020-06-07 22:40:36 +08:00
def test_shufflenetv1_backbone():
2020-06-03 15:51:17 +08:00
with pytest.raises(ValueError):
# frozen_stages must in [-1, 1, 2, 3]
ShuffleNetv1(frozen_stages=10)
with pytest.raises(ValueError):
# the item in out_indices must in [0, 1, 2, 3]
ShuffleNetv1(out_indices=[5])
2020-06-07 22:40:36 +08:00
with pytest.raises(ValueError):
# groups must in [1, 2, 3, 4, 8]
ShuffleNetv1(groups=10)
2020-06-04 02:22:53 +08:00
2020-06-07 22:40:36 +08:00
# Test ShuffleNetv1 norm state
model = ShuffleNetv1()
2020-06-04 02:22:53 +08:00
model.init_weights()
model.train()
assert check_norm_state(model.modules(), False)
2020-06-07 22:40:36 +08:00
# Test ShuffleNetv1 with first stage frozen
2020-06-04 02:22:53 +08:00
frozen_stages = 1
2020-06-07 22:40:36 +08:00
model = ShuffleNetv1(frozen_stages=frozen_stages)
2020-06-04 02:22:53 +08:00
model.init_weights()
model.train()
2020-06-07 22:40:36 +08:00
for layer in [model.conv1]:
2020-06-04 02:22:53 +08:00
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
2020-06-07 22:40:36 +08:00
# Test ShuffleNetv1 with bn frozen
model = ShuffleNetv1(bn_frozen=True)
2020-06-04 02:22:53 +08:00
model.init_weights()
model.train()
2020-06-07 22:40:36 +08:00
for i in range(1, 4):
2020-06-04 02:22:53 +08:00
layer = getattr(model, f'layer{i}')
for mod in layer.modules():
if isinstance(mod, _BatchNorm):
assert mod.training is False
for params in mod.parameters():
params.requires_grad = False
2020-06-07 22:40:36 +08:00
# Test ShuffleNetv1 forward with groups=3
model = ShuffleNetv1(groups=3)
2020-06-03 15:51:17 +08:00
model.init_weights()
model.train()
2020-06-07 22:40:36 +08:00
for m in model.modules():
if is_norm(m):
assert isinstance(m, _BatchNorm)
2020-06-03 15:51:17 +08:00
imgs = torch.randn(1, 3, 224, 224)
feat = model(imgs)
2020-06-07 22:40:36 +08:00
assert len(feat) == 4
assert feat[0].shape == torch.Size([1, 240, 28, 28])
assert feat[1].shape == torch.Size([1, 480, 14, 14])
assert feat[2].shape == torch.Size([1, 960, 7, 7])
assert feat[3].shape == torch.Size([1, 960, 7, 7])
# Test ShuffleNetv1 forward with layers 1, 2 forward
model = ShuffleNetv1(groups=3, out_indices=(1, 2))
2020-06-04 02:22:53 +08:00
model.init_weights()
model.train()
for m in model.modules():
if is_norm(m):
assert isinstance(m, _BatchNorm)
imgs = torch.randn(1, 3, 224, 224)
feat = model(imgs)
2020-06-07 22:40:36 +08:00
assert len(feat) == 3
assert feat[0].shape == torch.Size([1, 480, 14, 14])
assert feat[1].shape == torch.Size([1, 960, 7, 7])
assert feat[2].shape == torch.Size([1, 960, 7, 7])
# Test ShuffleNetv1 forward with checkpoint forward
model = ShuffleNetv1(groups=3, with_cp=True)
2020-06-04 02:22:53 +08:00
model.init_weights()
model.train()
for m in model.modules():
2020-06-07 22:40:36 +08:00
if is_norm(m):
assert isinstance(m, _BatchNorm)
2020-06-04 02:22:53 +08:00
imgs = torch.randn(1, 3, 224, 224)
feat = model(imgs)
2020-06-07 22:40:36 +08:00
assert len(feat) == 4
assert feat[0].shape == torch.Size([1, 240, 28, 28])
assert feat[1].shape == torch.Size([1, 480, 14, 14])
assert feat[2].shape == torch.Size([1, 960, 7, 7])
assert feat[3].shape == torch.Size([1, 960, 7, 7])