File size: 4,012 Bytes
d7a991a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) OpenMMLab. All rights reserved.
import pytest
import torch
from mmcv.utils.parrots_wrapper import _BatchNorm

from mmpose.models.backbones import VGG


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_vgg():
    """Test VGG backbone."""
    with pytest.raises(KeyError):
        # VGG depth should be in [11, 13, 16, 19]
        VGG(18)

    with pytest.raises(AssertionError):
        # In VGG: 1 <= num_stages <= 5
        VGG(11, num_stages=0)

    with pytest.raises(AssertionError):
        # In VGG: 1 <= num_stages <= 5
        VGG(11, num_stages=6)

    with pytest.raises(AssertionError):
        # len(dilations) == num_stages
        VGG(11, dilations=(1, 1), num_stages=3)

    with pytest.raises(TypeError):
        # pretrained must be a string path
        model = VGG(11)
        model.init_weights(pretrained=0)

    # Test VGG11 norm_eval=True
    model = VGG(11, norm_eval=True)
    model.init_weights()
    model.train()
    assert check_norm_state(model.modules(), False)

    # Test VGG11 forward without classifiers
    model = VGG(11, 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, 128, 56, 56)
    assert feat[2].shape == (1, 256, 28, 28)
    assert feat[3].shape == (1, 512, 14, 14)
    assert feat[4].shape == (1, 512, 7, 7)

    # Test VGG11 forward with classifiers
    model = VGG(11, num_classes=10, out_indices=(0, 1, 2, 3, 4, 5))
    model.init_weights()
    model.train()

    imgs = torch.randn(1, 3, 224, 224)
    feat = model(imgs)
    assert len(feat) == 6
    assert feat[0].shape == (1, 64, 112, 112)
    assert feat[1].shape == (1, 128, 56, 56)
    assert feat[2].shape == (1, 256, 28, 28)
    assert feat[3].shape == (1, 512, 14, 14)
    assert feat[4].shape == (1, 512, 7, 7)
    assert feat[5].shape == (1, 10)

    # Test VGG11BN forward
    model = VGG(11, norm_cfg=dict(type='BN'), 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, 128, 56, 56)
    assert feat[2].shape == (1, 256, 28, 28)
    assert feat[3].shape == (1, 512, 14, 14)
    assert feat[4].shape == (1, 512, 7, 7)

    # Test VGG11BN forward with classifiers
    model = VGG(
        11,
        num_classes=10,
        norm_cfg=dict(type='BN'),
        out_indices=(0, 1, 2, 3, 4, 5))
    model.init_weights()
    model.train()

    imgs = torch.randn(1, 3, 224, 224)
    feat = model(imgs)
    assert len(feat) == 6
    assert feat[0].shape == (1, 64, 112, 112)
    assert feat[1].shape == (1, 128, 56, 56)
    assert feat[2].shape == (1, 256, 28, 28)
    assert feat[3].shape == (1, 512, 14, 14)
    assert feat[4].shape == (1, 512, 7, 7)
    assert feat[5].shape == (1, 10)

    # Test VGG13 with layers 1, 2, 3 out forward
    model = VGG(13, out_indices=(0, 1, 2))
    model.init_weights()
    model.train()

    imgs = torch.randn(1, 3, 224, 224)
    feat = model(imgs)
    assert len(feat) == 3
    assert feat[0].shape == (1, 64, 112, 112)
    assert feat[1].shape == (1, 128, 56, 56)
    assert feat[2].shape == (1, 256, 28, 28)

    # Test VGG16 with top feature maps out forward
    model = VGG(16)
    model.init_weights()
    model.train()

    imgs = torch.randn(1, 3, 224, 224)
    feat = model(imgs)
    assert feat.shape == (1, 512, 7, 7)

    # Test VGG19 with classification score out forward
    model = VGG(19, num_classes=10)
    model.init_weights()
    model.train()

    imgs = torch.randn(1, 3, 224, 224)
    feat = model(imgs)
    assert feat.shape == (1, 10)