test_necks.py 5.09 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
176
177
178
179
# Copyright (c) OpenMMLab. All rights reserved.
import pytest
import torch

from mmpretrain.models.necks import (GeneralizedMeanPooling,
                                     GlobalAveragePooling, HRFuseScales,
                                     LinearNeck)


def test_gap_neck():

    # test 1d gap_neck
    neck = GlobalAveragePooling(dim=1)
    # batch_size, num_features, feature_size
    fake_input = torch.rand(1, 16, 24)

    output = neck(fake_input)
    # batch_size, num_features
    assert output.shape == (1, 16)

    # test 1d gap_neck
    neck = GlobalAveragePooling(dim=2)
    # batch_size, num_features, feature_size(2)
    fake_input = torch.rand(1, 16, 24, 24)

    output = neck(fake_input)
    # batch_size, num_features
    assert output.shape == (1, 16)

    # test 1d gap_neck
    neck = GlobalAveragePooling(dim=3)
    # batch_size, num_features, feature_size(3)
    fake_input = torch.rand(1, 16, 24, 24, 5)

    output = neck(fake_input)
    # batch_size, num_features
    assert output.shape == (1, 16)

    with pytest.raises(AssertionError):
        # dim must in [1, 2, 3]
        GlobalAveragePooling(dim='other')


def test_gem_neck():

    # test gem_neck
    neck = GeneralizedMeanPooling()

    # default p is trainable
    assert neck.p.requires_grad

    # batch_size, num_features, feature_size(2)
    fake_input = torch.rand(1, 16, 24, 24)

    output = neck(fake_input)
    # batch_size, num_features
    assert output.shape == (1, 16)

    # test tuple input gem_neck
    neck = GeneralizedMeanPooling()
    # batch_size, num_features, feature_size(2)
    fake_input = (torch.rand(1, 8, 24, 24), torch.rand(1, 16, 24, 24))

    output = neck(fake_input)
    # batch_size, num_features
    assert output[0].shape == (1, 8)
    assert output[1].shape == (1, 16)

    # test gem_neck with p_trainable=False
    neck = GeneralizedMeanPooling(p_trainable=False)

    # p is not trainable
    assert not neck.p.requires_grad

    # batch_size, num_features, feature_size(2)
    fake_input = torch.rand(1, 16, 24, 24)

    output = neck(fake_input)
    # batch_size, num_features
    assert output.shape == (1, 16)

    with pytest.raises(AssertionError):
        # p must be a value greater then 1
        GeneralizedMeanPooling(p=0.5)


def test_hr_fuse_scales():

    in_channels = (18, 32, 64, 128)
    neck = HRFuseScales(in_channels=in_channels, out_channels=1024)

    feat_size = 56
    inputs = []
    for in_channel in in_channels:
        input_tensor = torch.rand(3, in_channel, feat_size, feat_size)
        inputs.append(input_tensor)
        feat_size = feat_size // 2

    with pytest.raises(AssertionError):
        neck(inputs)

    outs = neck(tuple(inputs))
    assert isinstance(outs, tuple)
    assert len(outs) == 1
    assert outs[0].shape == (3, 1024, 7, 7)


def test_linear_reduction():
    # test linear_reduction without `act_cfg` and `norm_cfg`
    neck = LinearNeck(10, 5, 0, None, None)
    neck.eval()
    assert isinstance(neck.gap, torch.nn.Identity)
    assert isinstance(neck.act, torch.nn.Identity)
    assert isinstance(neck.norm, torch.nn.Identity)

    # batch_size, in_channels, out_channels
    fake_input = torch.rand(1, 10)
    output = neck(fake_input)
    # batch_size, out_features
    assert output[-1].shape == (1, 5)

    # batch_size, in_features, feature_size(2)
    fake_input = (torch.rand(1, 20), torch.rand(1, 10))

    output = neck(fake_input)
    # batch_size, out_features
    assert output[-1].shape == (1, 5)

    # batch_size, in_channels, out_channels, gap_dim
    neck = LinearNeck(10, 5, 1, None, None)
    fake_input = torch.rand(1, 10, 10)
    output = neck(fake_input)
    # batch_size, out_features
    assert output[-1].shape == (1, 5)

    # batch_size, in_channels, out_channels, gap_dim
    neck = LinearNeck(10, 5, 2, None, None)
    fake_input = torch.rand(1, 10, 10, 10)
    output = neck(fake_input)
    # batch_size, out_features
    assert output[-1].shape == (1, 5)

    # batch_size, in_channels, out_channels, gap_dim
    neck = LinearNeck(10, 5, 3, None, None)
    fake_input = torch.rand(1, 10, 10, 10, 10)
    output = neck(fake_input)
    # batch_size, out_features
    assert output[-1].shape == (1, 5)

    # batch_size, in_channels, out_channels, gap_dim
    with pytest.raises(AssertionError):
        neck = LinearNeck(10, 5, None, None, None)

    # test linear_reduction with `init_cfg`
    neck = LinearNeck(10, 5, init_cfg=dict(type='Xavier', layer=['Linear']))

    # test linear_reduction with `act_cfg` and `norm_cfg`
    neck = LinearNeck(
        10, 5, act_cfg=dict(type='ReLU'), norm_cfg=dict(type='BN1d'))
    neck.eval()

    assert isinstance(neck.act, torch.nn.ReLU)
    assert isinstance(neck.norm, torch.nn.BatchNorm1d)

    # batch_size, in_channels, out_channels
    fake_input = torch.rand(1, 10)
    output = neck(fake_input)
    # batch_size, out_features
    assert output[-1].shape == (1, 5)
    #
    # # batch_size, in_features, feature_size(2)
    fake_input = (torch.rand(1, 20), torch.rand(1, 10))

    output = neck(fake_input)
    # batch_size, out_features
    assert output[-1].shape == (1, 5)

    with pytest.raises(AssertionError):
        neck([])