resnet.py 10 KB
Newer Older
1
2
import logging

Kai Chen's avatar
Kai Chen committed
3
4
import torch.nn as nn
import torch.utils.checkpoint as cp
Kai Chen's avatar
Kai Chen committed
5
6

from mmcv.cnn import constant_init, kaiming_init
Kai Chen's avatar
Kai Chen committed
7
from mmcv.runner import load_checkpoint
Kai Chen's avatar
Kai Chen committed
8

Kai Chen's avatar
Kai Chen committed
9
10
from ..registry import BACKBONES

Kai Chen's avatar
Kai Chen committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

def conv3x3(in_planes, out_planes, stride=1, dilation=1):
    "3x3 convolution with padding"
    return nn.Conv2d(
        in_planes,
        out_planes,
        kernel_size=3,
        stride=stride,
        padding=dilation,
        dilation=dilation,
        bias=False)


class BasicBlock(nn.Module):
    expansion = 1

    def __init__(self,
                 inplanes,
                 planes,
                 stride=1,
                 dilation=1,
                 downsample=None,
Kai Chen's avatar
Kai Chen committed
33
34
                 style='pytorch',
                 with_cp=False):
Kai Chen's avatar
Kai Chen committed
35
36
37
38
39
40
41
42
43
        super(BasicBlock, self).__init__()
        self.conv1 = conv3x3(inplanes, planes, stride, dilation)
        self.bn1 = nn.BatchNorm2d(planes)
        self.relu = nn.ReLU(inplace=True)
        self.conv2 = conv3x3(planes, planes)
        self.bn2 = nn.BatchNorm2d(planes)
        self.downsample = downsample
        self.stride = stride
        self.dilation = dilation
Kai Chen's avatar
Kai Chen committed
44
        assert not with_cp
Kai Chen's avatar
Kai Chen committed
45
46

    def forward(self, x):
pangjm's avatar
pangjm committed
47
        identity = x
Kai Chen's avatar
Kai Chen committed
48
49
50
51
52
53
54
55
56

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)

        if self.downsample is not None:
pangjm's avatar
pangjm committed
57
            identity = self.downsample(x)
Kai Chen's avatar
Kai Chen committed
58

pangjm's avatar
pangjm committed
59
        out += identity
Kai Chen's avatar
Kai Chen committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
        out = self.relu(out)

        return out


class Bottleneck(nn.Module):
    expansion = 4

    def __init__(self,
                 inplanes,
                 planes,
                 stride=1,
                 dilation=1,
                 downsample=None,
74
                 style='pytorch',
Kai Chen's avatar
Kai Chen committed
75
                 with_cp=False):
pangjm's avatar
pangjm committed
76
        """Bottleneck block for ResNet.
77
78
        If style is "pytorch", the stride-two layer is the 3x3 conv layer,
        if it is "caffe", the stride-two layer is the first 1x1 conv layer.
Kai Chen's avatar
Kai Chen committed
79
80
        """
        super(Bottleneck, self).__init__()
81
        assert style in ['pytorch', 'caffe']
pangjm's avatar
pangjm committed
82
83
        self.inplanes = inplanes
        self.planes = planes
84
        if style == 'pytorch':
pangjm's avatar
pangjm committed
85
86
            self.conv1_stride = 1
            self.conv2_stride = stride
Kai Chen's avatar
Kai Chen committed
87
        else:
pangjm's avatar
pangjm committed
88
89
            self.conv1_stride = stride
            self.conv2_stride = 1
Kai Chen's avatar
Kai Chen committed
90
        self.conv1 = nn.Conv2d(
pangjm's avatar
pangjm committed
91
92
93
94
95
            inplanes,
            planes,
            kernel_size=1,
            stride=self.conv1_stride,
            bias=False)
Kai Chen's avatar
Kai Chen committed
96
97
98
99
        self.conv2 = nn.Conv2d(
            planes,
            planes,
            kernel_size=3,
pangjm's avatar
pangjm committed
100
            stride=self.conv2_stride,
Kai Chen's avatar
Kai Chen committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
            padding=dilation,
            dilation=dilation,
            bias=False)

        self.bn1 = nn.BatchNorm2d(planes)
        self.bn2 = nn.BatchNorm2d(planes)
        self.conv3 = nn.Conv2d(
            planes, planes * self.expansion, kernel_size=1, bias=False)
        self.bn3 = nn.BatchNorm2d(planes * self.expansion)
        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
        self.stride = stride
        self.dilation = dilation
        self.with_cp = with_cp

    def forward(self, x):

        def _inner_forward(x):
pangjm's avatar
pangjm committed
119
            identity = x
Kai Chen's avatar
Kai Chen committed
120
121
122
123
124
125
126
127
128
129
130
131
132

            out = self.conv1(x)
            out = self.bn1(out)
            out = self.relu(out)

            out = self.conv2(out)
            out = self.bn2(out)
            out = self.relu(out)

            out = self.conv3(out)
            out = self.bn3(out)

            if self.downsample is not None:
pangjm's avatar
pangjm committed
133
                identity = self.downsample(x)
Kai Chen's avatar
Kai Chen committed
134

pangjm's avatar
pangjm committed
135
            out += identity
Kai Chen's avatar
Kai Chen committed
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154

            return out

        if self.with_cp and x.requires_grad:
            out = cp.checkpoint(_inner_forward, x)
        else:
            out = _inner_forward(x)

        out = self.relu(out)

        return out


def make_res_layer(block,
                   inplanes,
                   planes,
                   blocks,
                   stride=1,
                   dilation=1,
155
                   style='pytorch',
Kai Chen's avatar
Kai Chen committed
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
                   with_cp=False):
    downsample = None
    if stride != 1 or inplanes != planes * block.expansion:
        downsample = nn.Sequential(
            nn.Conv2d(
                inplanes,
                planes * block.expansion,
                kernel_size=1,
                stride=stride,
                bias=False),
            nn.BatchNorm2d(planes * block.expansion),
        )

    layers = []
    layers.append(
        block(
            inplanes,
            planes,
            stride,
            dilation,
            downsample,
            style=style,
            with_cp=with_cp))
    inplanes = planes * block.expansion
    for i in range(1, blocks):
        layers.append(
            block(inplanes, planes, 1, dilation, style=style, with_cp=with_cp))

    return nn.Sequential(*layers)


Kai Chen's avatar
Kai Chen committed
187
@BACKBONES.register_module
Kai Chen's avatar
Kai Chen committed
188
189
class ResNet(nn.Module):
    """ResNet backbone.
Kai Chen's avatar
Kai Chen committed
190

Kai Chen's avatar
Kai Chen committed
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
    Args:
        depth (int): Depth of resnet, from {18, 34, 50, 101, 152}.
        num_stages (int): Resnet stages, normally 4.
        strides (Sequence[int]): Strides of the first block of each stage.
        dilations (Sequence[int]): Dilation of each stage.
        out_indices (Sequence[int]): Output from which stages.
        style (str): `pytorch` or `caffe`. If set to "pytorch", the stride-two
            layer is the 3x3 conv layer, otherwise the stride-two layer is
            the first 1x1 conv layer.
        frozen_stages (int): Stages to be frozen (all param fixed). -1 means
            not freezing any parameters.
        bn_eval (bool): Whether to set BN layers to eval mode, namely, freeze
            running stats (mean and var).
        bn_frozen (bool): Whether to freeze weight and bias of BN layers.
        with_cp (bool): Use checkpoint or not. Using checkpoint will save some
            memory while slowing down the training speed.
    """
Kai Chen's avatar
Kai Chen committed
208

Kai Chen's avatar
Kai Chen committed
209
210
211
212
213
214
215
    arch_settings = {
        18: (BasicBlock, (2, 2, 2, 2)),
        34: (BasicBlock, (3, 4, 6, 3)),
        50: (Bottleneck, (3, 4, 6, 3)),
        101: (Bottleneck, (3, 4, 23, 3)),
        152: (Bottleneck, (3, 8, 36, 3))
    }
Kai Chen's avatar
Kai Chen committed
216
217

    def __init__(self,
Kai Chen's avatar
Kai Chen committed
218
219
                 depth,
                 num_stages=4,
Kai Chen's avatar
Kai Chen committed
220
221
222
                 strides=(1, 2, 2, 2),
                 dilations=(1, 1, 1, 1),
                 out_indices=(0, 1, 2, 3),
223
                 style='pytorch',
Kai Chen's avatar
Kai Chen committed
224
225
226
227
                 frozen_stages=-1,
                 bn_eval=True,
                 bn_frozen=False,
                 with_cp=False):
Kai Chen's avatar
Kai Chen committed
228
        super(ResNet, self).__init__()
Kai Chen's avatar
Kai Chen committed
229
230
        if depth not in self.arch_settings:
            raise KeyError('invalid depth {} for resnet'.format(depth))
pangjm's avatar
pangjm committed
231
232
        self.depth = depth
        self.num_stages = num_stages
Kai Chen's avatar
Kai Chen committed
233
        assert num_stages >= 1 and num_stages <= 4
pangjm's avatar
pangjm committed
234
235
        self.strides = strides
        self.dilations = dilations
Kai Chen's avatar
Kai Chen committed
236
        assert len(strides) == len(dilations) == num_stages
Kai Chen's avatar
Kai Chen committed
237
        self.out_indices = out_indices
pangjm's avatar
pangjm committed
238
        assert max(out_indices) < num_stages
Kai Chen's avatar
Kai Chen committed
239
        self.style = style
Kai Chen's avatar
Kai Chen committed
240
241
242
243
244
        self.frozen_stages = frozen_stages
        self.bn_eval = bn_eval
        self.bn_frozen = bn_frozen
        self.with_cp = with_cp

pangjm's avatar
pangjm committed
245
246
        self.block, stage_blocks = self.arch_settings[depth]
        self.stage_blocks = stage_blocks[:num_stages]
Kai Chen's avatar
Kai Chen committed
247
        self.inplanes = 64
pangjm's avatar
pangjm committed
248

Kai Chen's avatar
Kai Chen committed
249
250
251
252
253
254
        self.conv1 = nn.Conv2d(
            3, 64, kernel_size=7, stride=2, padding=3, bias=False)
        self.bn1 = nn.BatchNorm2d(64)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)

Kai Chen's avatar
Kai Chen committed
255
        self.res_layers = []
pangjm's avatar
pangjm committed
256
        for i, num_blocks in enumerate(self.stage_blocks):
Kai Chen's avatar
Kai Chen committed
257
258
259
260
            stride = strides[i]
            dilation = dilations[i]
            planes = 64 * 2**i
            res_layer = make_res_layer(
pangjm's avatar
pangjm committed
261
                self.block,
Kai Chen's avatar
Kai Chen committed
262
263
264
265
266
267
268
                self.inplanes,
                planes,
                num_blocks,
                stride=stride,
                dilation=dilation,
                style=self.style,
                with_cp=with_cp)
pangjm's avatar
pangjm committed
269
            self.inplanes = planes * self.block.expansion
Kai Chen's avatar
Kai Chen committed
270
            layer_name = 'layer{}'.format(i + 1)
271
            self.add_module(layer_name, res_layer)
Kai Chen's avatar
Kai Chen committed
272
273
            self.res_layers.append(layer_name)

pangjm's avatar
pangjm committed
274
275
        self.feat_dim = self.block.expansion * 64 * 2**(
            len(self.stage_blocks) - 1)
pangjm's avatar
pangjm committed
276

Kai Chen's avatar
Kai Chen committed
277
278
    def init_weights(self, pretrained=None):
        if isinstance(pretrained, str):
279
280
            logger = logging.getLogger()
            load_checkpoint(self, pretrained, strict=False, logger=logger)
Kai Chen's avatar
Kai Chen committed
281
282
283
        elif pretrained is None:
            for m in self.modules():
                if isinstance(m, nn.Conv2d):
Kai Chen's avatar
Kai Chen committed
284
                    kaiming_init(m)
Kai Chen's avatar
Kai Chen committed
285
                elif isinstance(m, nn.BatchNorm2d):
Kai Chen's avatar
Kai Chen committed
286
                    constant_init(m, 1)
Kai Chen's avatar
Kai Chen committed
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
        else:
            raise TypeError('pretrained must be a str or None')

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)
        outs = []
        for i, layer_name in enumerate(self.res_layers):
            res_layer = getattr(self, layer_name)
            x = res_layer(x)
            if i in self.out_indices:
                outs.append(x)
        if len(outs) == 1:
            return outs[0]
        else:
            return tuple(outs)

    def train(self, mode=True):
        super(ResNet, self).train(mode)
Kai Chen's avatar
Kai Chen committed
308
        if self.bn_eval:
Kai Chen's avatar
Kai Chen committed
309
310
311
            for m in self.modules():
                if isinstance(m, nn.BatchNorm2d):
                    m.eval()
Kai Chen's avatar
Kai Chen committed
312
                    if self.bn_frozen:
pangjm's avatar
pangjm committed
313
314
                        for params in m.parameters():
                            params.requires_grad = False
Kai Chen's avatar
Kai Chen committed
315
316
317
318
319
320
321
322
323
324
325
326
327
        if mode and self.frozen_stages >= 0:
            for param in self.conv1.parameters():
                param.requires_grad = False
            for param in self.bn1.parameters():
                param.requires_grad = False
            self.bn1.eval()
            self.bn1.weight.requires_grad = False
            self.bn1.bias.requires_grad = False
            for i in range(1, self.frozen_stages + 1):
                mod = getattr(self, 'layer{}'.format(i))
                mod.eval()
                for param in mod.parameters():
                    param.requires_grad = False