test_mobilenet_v3.py 6.3 KB
Newer Older
limm's avatar
limm committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# Copyright (c) OpenMMLab. All rights reserved.
import pytest
import torch
from torch.nn.modules import GroupNorm
from torch.nn.modules.batchnorm import _BatchNorm

from mmpretrain.models.backbones import MobileNetV3
from mmpretrain.models.utils import InvertedResidual


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


def test_mobilenetv3_backbone():
    with pytest.raises(TypeError):
        # pretrained must be a string path
        model = MobileNetV3()
        model.init_weights(pretrained=0)

    with pytest.raises(AssertionError):
        # arch must in [small, large]
        MobileNetV3(arch='others')

    with pytest.raises(ValueError):
        # frozen_stages must less than 13 when arch is small
        MobileNetV3(arch='small', frozen_stages=13)

    with pytest.raises(ValueError):
        # frozen_stages must less than 17 when arch is large
        MobileNetV3(arch='large', frozen_stages=17)

    with pytest.raises(ValueError):
        # max out_indices must less than 13 when arch is small
        MobileNetV3(arch='small', out_indices=(13, ))

    with pytest.raises(ValueError):
        # max out_indices must less than 17 when arch is large
        MobileNetV3(arch='large', out_indices=(17, ))

    # Test MobileNetV3
    model = MobileNetV3()
    model.init_weights()
    model.train()

    # Test MobileNetV3 with first stage frozen
    frozen_stages = 1
    model = MobileNetV3(frozen_stages=frozen_stages)
    model.init_weights()
    model.train()
    for i in range(0, 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 MobileNetV3 with norm eval
    model = MobileNetV3(norm_eval=True, out_indices=range(0, 12))
    model.init_weights()
    model.train()
    assert check_norm_state(model.modules(), False)

    # Test MobileNetV3 forward with small arch
    model = MobileNetV3(out_indices=(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12))
    model.init_weights()
    model.train()

    imgs = torch.randn(1, 3, 224, 224)
    feat = model(imgs)
    assert len(feat) == 13
    assert feat[0].shape == torch.Size([1, 16, 112, 112])
    assert feat[1].shape == torch.Size([1, 16, 56, 56])
    assert feat[2].shape == torch.Size([1, 24, 28, 28])
    assert feat[3].shape == torch.Size([1, 24, 28, 28])
    assert feat[4].shape == torch.Size([1, 40, 14, 14])
    assert feat[5].shape == torch.Size([1, 40, 14, 14])
    assert feat[6].shape == torch.Size([1, 40, 14, 14])
    assert feat[7].shape == torch.Size([1, 48, 14, 14])
    assert feat[8].shape == torch.Size([1, 48, 14, 14])
    assert feat[9].shape == torch.Size([1, 96, 7, 7])
    assert feat[10].shape == torch.Size([1, 96, 7, 7])
    assert feat[11].shape == torch.Size([1, 96, 7, 7])
    assert feat[12].shape == torch.Size([1, 576, 7, 7])

    # Test MobileNetV3 forward with small arch and GroupNorm
    model = MobileNetV3(
        out_indices=(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12),
        norm_cfg=dict(type='GN', num_groups=2, requires_grad=True))
    for m in model.modules():
        if is_norm(m):
            assert isinstance(m, GroupNorm)
    model.init_weights()
    model.train()

    imgs = torch.randn(1, 3, 224, 224)
    feat = model(imgs)
    assert len(feat) == 13
    assert feat[0].shape == torch.Size([1, 16, 112, 112])
    assert feat[1].shape == torch.Size([1, 16, 56, 56])
    assert feat[2].shape == torch.Size([1, 24, 28, 28])
    assert feat[3].shape == torch.Size([1, 24, 28, 28])
    assert feat[4].shape == torch.Size([1, 40, 14, 14])
    assert feat[5].shape == torch.Size([1, 40, 14, 14])
    assert feat[6].shape == torch.Size([1, 40, 14, 14])
    assert feat[7].shape == torch.Size([1, 48, 14, 14])
    assert feat[8].shape == torch.Size([1, 48, 14, 14])
    assert feat[9].shape == torch.Size([1, 96, 7, 7])
    assert feat[10].shape == torch.Size([1, 96, 7, 7])
    assert feat[11].shape == torch.Size([1, 96, 7, 7])
    assert feat[12].shape == torch.Size([1, 576, 7, 7])

    # Test MobileNetV3 forward with large arch
    model = MobileNetV3(
        arch='large',
        out_indices=(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16))
    model.init_weights()
    model.train()

    imgs = torch.randn(1, 3, 224, 224)
    feat = model(imgs)
    assert len(feat) == 17
    assert feat[0].shape == torch.Size([1, 16, 112, 112])
    assert feat[1].shape == torch.Size([1, 16, 112, 112])
    assert feat[2].shape == torch.Size([1, 24, 56, 56])
    assert feat[3].shape == torch.Size([1, 24, 56, 56])
    assert feat[4].shape == torch.Size([1, 40, 28, 28])
    assert feat[5].shape == torch.Size([1, 40, 28, 28])
    assert feat[6].shape == torch.Size([1, 40, 28, 28])
    assert feat[7].shape == torch.Size([1, 80, 14, 14])
    assert feat[8].shape == torch.Size([1, 80, 14, 14])
    assert feat[9].shape == torch.Size([1, 80, 14, 14])
    assert feat[10].shape == torch.Size([1, 80, 14, 14])
    assert feat[11].shape == torch.Size([1, 112, 14, 14])
    assert feat[12].shape == torch.Size([1, 112, 14, 14])
    assert feat[13].shape == torch.Size([1, 160, 7, 7])
    assert feat[14].shape == torch.Size([1, 160, 7, 7])
    assert feat[15].shape == torch.Size([1, 160, 7, 7])
    assert feat[16].shape == torch.Size([1, 960, 7, 7])

    # Test MobileNetV3 forward with large arch
    model = MobileNetV3(arch='large', out_indices=(0, ))
    model.init_weights()
    model.train()

    imgs = torch.randn(1, 3, 224, 224)
    feat = model(imgs)
    assert len(feat) == 1
    assert feat[0].shape == torch.Size([1, 16, 112, 112])

    # Test MobileNetV3 with checkpoint forward
    model = MobileNetV3(with_cp=True)
    for m in model.modules():
        if isinstance(m, InvertedResidual):
            assert m.with_cp
    model.init_weights()
    model.train()

    imgs = torch.randn(1, 3, 224, 224)
    feat = model(imgs)
    assert len(feat) == 1
    assert feat[0].shape == torch.Size([1, 576, 7, 7])