resnet.py 11.3 KB
Newer Older
1
from typing import Tuple, Optional, Callable, List, Sequence, Type, Any, Union
2

3
4
5
import torch.nn as nn
from torch import Tensor

6
from ..._internally_replaced_utils import load_state_dict_from_url
7
from ...utils import _log_api_usage_once
8

9
__all__ = ["r3d_18", "mc3_18", "r2plus1d_18"]
10
11

model_urls = {
12
13
14
    "r3d_18": "https://download.pytorch.org/models/r3d_18-b3b3357e.pth",
    "mc3_18": "https://download.pytorch.org/models/mc3_18-a90a0ba3.pth",
    "r2plus1d_18": "https://download.pytorch.org/models/r2plus1d_18-91a641e6.pth",
15
16
17
18
}


class Conv3DSimple(nn.Conv3d):
19
    def __init__(
20
        self, in_planes: int, out_planes: int, midplanes: Optional[int] = None, stride: int = 1, padding: int = 1
21
    ) -> None:
22

23
        super().__init__(
24
25
26
27
28
            in_channels=in_planes,
            out_channels=out_planes,
            kernel_size=(3, 3, 3),
            stride=stride,
            padding=padding,
29
30
            bias=False,
        )
31
32

    @staticmethod
33
    def get_downsample_stride(stride: int) -> Tuple[int, int, int]:
34
        return stride, stride, stride
35
36
37


class Conv2Plus1D(nn.Sequential):
38
    def __init__(self, in_planes: int, out_planes: int, midplanes: int, stride: int = 1, padding: int = 1) -> None:
39
        super().__init__(
40
41
42
43
44
45
46
47
            nn.Conv3d(
                in_planes,
                midplanes,
                kernel_size=(1, 3, 3),
                stride=(1, stride, stride),
                padding=(0, padding, padding),
                bias=False,
            ),
48
49
            nn.BatchNorm3d(midplanes),
            nn.ReLU(inplace=True),
50
51
52
53
            nn.Conv3d(
                midplanes, out_planes, kernel_size=(3, 1, 1), stride=(stride, 1, 1), padding=(padding, 0, 0), bias=False
            ),
        )
54
55

    @staticmethod
56
    def get_downsample_stride(stride: int) -> Tuple[int, int, int]:
57
        return stride, stride, stride
58
59
60


class Conv3DNoTemporal(nn.Conv3d):
61
    def __init__(
62
        self, in_planes: int, out_planes: int, midplanes: Optional[int] = None, stride: int = 1, padding: int = 1
63
    ) -> None:
64

65
        super().__init__(
66
67
68
69
70
            in_channels=in_planes,
            out_channels=out_planes,
            kernel_size=(1, 3, 3),
            stride=(1, stride, stride),
            padding=(0, padding, padding),
71
72
            bias=False,
        )
73
74

    @staticmethod
75
    def get_downsample_stride(stride: int) -> Tuple[int, int, int]:
76
        return 1, stride, stride
77
78
79
80
81
82


class BasicBlock(nn.Module):

    expansion = 1

83
84
85
86
87
88
89
90
    def __init__(
        self,
        inplanes: int,
        planes: int,
        conv_builder: Callable[..., nn.Module],
        stride: int = 1,
        downsample: Optional[nn.Module] = None,
    ) -> None:
91
92
        midplanes = (inplanes * planes * 3 * 3 * 3) // (inplanes * 3 * 3 + 3 * planes)

93
        super().__init__()
94
        self.conv1 = nn.Sequential(
95
            conv_builder(inplanes, planes, midplanes, stride), nn.BatchNorm3d(planes), nn.ReLU(inplace=True)
96
        )
97
        self.conv2 = nn.Sequential(conv_builder(planes, planes, midplanes), nn.BatchNorm3d(planes))
98
99
100
101
        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
        self.stride = stride

102
    def forward(self, x: Tensor) -> Tensor:
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
        residual = x

        out = self.conv1(x)
        out = self.conv2(out)
        if self.downsample is not None:
            residual = self.downsample(x)

        out += residual
        out = self.relu(out)

        return out


class Bottleneck(nn.Module):
    expansion = 4

119
120
121
122
123
124
125
126
    def __init__(
        self,
        inplanes: int,
        planes: int,
        conv_builder: Callable[..., nn.Module],
        stride: int = 1,
        downsample: Optional[nn.Module] = None,
    ) -> None:
127

128
        super().__init__()
129
130
131
132
        midplanes = (inplanes * planes * 3 * 3 * 3) // (inplanes * 3 * 3 + 3 * planes)

        # 1x1x1
        self.conv1 = nn.Sequential(
133
            nn.Conv3d(inplanes, planes, kernel_size=1, bias=False), nn.BatchNorm3d(planes), nn.ReLU(inplace=True)
134
135
136
        )
        # Second kernel
        self.conv2 = nn.Sequential(
137
            conv_builder(planes, planes, midplanes, stride), nn.BatchNorm3d(planes), nn.ReLU(inplace=True)
138
139
140
141
142
        )

        # 1x1x1
        self.conv3 = nn.Sequential(
            nn.Conv3d(planes, planes * self.expansion, kernel_size=1, bias=False),
143
            nn.BatchNorm3d(planes * self.expansion),
144
145
146
147
148
        )
        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
        self.stride = stride

149
    def forward(self, x: Tensor) -> Tensor:
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
        residual = x

        out = self.conv1(x)
        out = self.conv2(out)
        out = self.conv3(out)

        if self.downsample is not None:
            residual = self.downsample(x)

        out += residual
        out = self.relu(out)

        return out


class BasicStem(nn.Sequential):
166
167
    """The default conv-batchnorm-relu stem"""

168
    def __init__(self) -> None:
169
        super().__init__(
170
            nn.Conv3d(3, 64, kernel_size=(3, 7, 7), stride=(1, 2, 2), padding=(1, 3, 3), bias=False),
171
            nn.BatchNorm3d(64),
172
173
            nn.ReLU(inplace=True),
        )
174
175
176


class R2Plus1dStem(nn.Sequential):
177
178
    """R(2+1)D stem is different than the default one as it uses separated 3D convolution"""

179
    def __init__(self) -> None:
180
        super().__init__(
181
            nn.Conv3d(3, 45, kernel_size=(1, 7, 7), stride=(1, 2, 2), padding=(0, 3, 3), bias=False),
182
183
            nn.BatchNorm3d(45),
            nn.ReLU(inplace=True),
184
            nn.Conv3d(45, 64, kernel_size=(3, 1, 1), stride=(1, 1, 1), padding=(1, 0, 0), bias=False),
185
            nn.BatchNorm3d(64),
186
187
            nn.ReLU(inplace=True),
        )
188
189
190


class VideoResNet(nn.Module):
191
192
193
    def __init__(
        self,
        block: Type[Union[BasicBlock, Bottleneck]],
194
        conv_makers: Sequence[Type[Union[Conv3DSimple, Conv3DNoTemporal, Conv2Plus1D]]],
195
196
197
198
199
        layers: List[int],
        stem: Callable[..., nn.Module],
        num_classes: int = 400,
        zero_init_residual: bool = False,
    ) -> None:
200
201
202
        """Generic resnet video generator.

        Args:
203
204
205
            block (Type[Union[BasicBlock, Bottleneck]]): resnet building block
            conv_makers (List[Type[Union[Conv3DSimple, Conv3DNoTemporal, Conv2Plus1D]]]): generator
                function for each layer
206
            layers (List[int]): number of blocks per layer
207
            stem (Callable[..., nn.Module]): module specifying the ResNet stem.
208
209
210
            num_classes (int, optional): Dimension of the final FC layer. Defaults to 400.
            zero_init_residual (bool, optional): Zero init bottleneck residual BN. Defaults to False.
        """
211
        super().__init__()
Kai Zhang's avatar
Kai Zhang committed
212
        _log_api_usage_once(self)
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
        self.inplanes = 64

        self.stem = stem()

        self.layer1 = self._make_layer(block, conv_makers[0], 64, layers[0], stride=1)
        self.layer2 = self._make_layer(block, conv_makers[1], 128, layers[1], stride=2)
        self.layer3 = self._make_layer(block, conv_makers[2], 256, layers[2], stride=2)
        self.layer4 = self._make_layer(block, conv_makers[3], 512, layers[3], stride=2)

        self.avgpool = nn.AdaptiveAvgPool3d((1, 1, 1))
        self.fc = nn.Linear(512 * block.expansion, num_classes)

        # init weights
        self._initialize_weights()

        if zero_init_residual:
            for m in self.modules():
                if isinstance(m, Bottleneck):
231
                    nn.init.constant_(m.bn3.weight, 0)  # type: ignore[union-attr, arg-type]
232

233
    def forward(self, x: Tensor) -> Tensor:
234
235
236
237
238
239
240
241
242
243
244
245
246
247
        x = self.stem(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)

        x = self.avgpool(x)
        # Flatten the layer to fc
        x = x.flatten(1)
        x = self.fc(x)

        return x

248
249
250
251
252
253
    def _make_layer(
        self,
        block: Type[Union[BasicBlock, Bottleneck]],
        conv_builder: Type[Union[Conv3DSimple, Conv3DNoTemporal, Conv2Plus1D]],
        planes: int,
        blocks: int,
254
        stride: int = 1,
255
    ) -> nn.Sequential:
256
257
258
259
260
        downsample = None

        if stride != 1 or self.inplanes != planes * block.expansion:
            ds_stride = conv_builder.get_downsample_stride(stride)
            downsample = nn.Sequential(
261
262
                nn.Conv3d(self.inplanes, planes * block.expansion, kernel_size=1, stride=ds_stride, bias=False),
                nn.BatchNorm3d(planes * block.expansion),
263
264
265
266
267
268
269
270
271
272
            )
        layers = []
        layers.append(block(self.inplanes, planes, conv_builder, stride, downsample))

        self.inplanes = planes * block.expansion
        for i in range(1, blocks):
            layers.append(block(self.inplanes, planes, conv_builder))

        return nn.Sequential(*layers)

273
    def _initialize_weights(self) -> None:
274
275
        for m in self.modules():
            if isinstance(m, nn.Conv3d):
276
                nn.init.kaiming_normal_(m.weight, mode="fan_out", nonlinearity="relu")
277
278
279
280
281
282
283
284
285
286
                if m.bias is not None:
                    nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.BatchNorm3d):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.Linear):
                nn.init.normal_(m.weight, 0, 0.01)
                nn.init.constant_(m.bias, 0)


287
def _video_resnet(arch: str, pretrained: bool = False, progress: bool = True, **kwargs: Any) -> VideoResNet:
288
289
290
    model = VideoResNet(**kwargs)

    if pretrained:
291
        state_dict = load_state_dict_from_url(model_urls[arch], progress=progress)
292
293
294
295
        model.load_state_dict(state_dict)
    return model


296
def r3d_18(pretrained: bool = False, progress: bool = True, **kwargs: Any) -> VideoResNet:
297
298
299
300
301
302
303
304
305
306
307
    """Construct 18 layer Resnet3D model as in
    https://arxiv.org/abs/1711.11248

    Args:
        pretrained (bool): If True, returns a model pre-trained on Kinetics-400
        progress (bool): If True, displays a progress bar of the download to stderr

    Returns:
        nn.Module: R3D-18 network
    """

308
309
310
311
312
313
314
315
316
317
    return _video_resnet(
        "r3d_18",
        pretrained,
        progress,
        block=BasicBlock,
        conv_makers=[Conv3DSimple] * 4,
        layers=[2, 2, 2, 2],
        stem=BasicStem,
        **kwargs,
    )
318
319


320
def mc3_18(pretrained: bool = False, progress: bool = True, **kwargs: Any) -> VideoResNet:
321
322
323
324
325
326
327
328
329
330
    """Constructor for 18 layer Mixed Convolution network as in
    https://arxiv.org/abs/1711.11248

    Args:
        pretrained (bool): If True, returns a model pre-trained on Kinetics-400
        progress (bool): If True, displays a progress bar of the download to stderr

    Returns:
        nn.Module: MC3 Network definition
    """
331
332
333
334
335
336
337
338
339
340
    return _video_resnet(
        "mc3_18",
        pretrained,
        progress,
        block=BasicBlock,
        conv_makers=[Conv3DSimple] + [Conv3DNoTemporal] * 3,  # type: ignore[list-item]
        layers=[2, 2, 2, 2],
        stem=BasicStem,
        **kwargs,
    )
341
342


343
def r2plus1d_18(pretrained: bool = False, progress: bool = True, **kwargs: Any) -> VideoResNet:
344
345
346
347
348
349
350
351
352
353
    """Constructor for the 18 layer deep R(2+1)D network as in
    https://arxiv.org/abs/1711.11248

    Args:
        pretrained (bool): If True, returns a model pre-trained on Kinetics-400
        progress (bool): If True, displays a progress bar of the download to stderr

    Returns:
        nn.Module: R(2+1)D-18 network
    """
354
355
356
357
358
359
360
361
362
363
    return _video_resnet(
        "r2plus1d_18",
        pretrained,
        progress,
        block=BasicBlock,
        conv_makers=[Conv2Plus1D] * 4,
        layers=[2, 2, 2, 2],
        stem=R2Plus1dStem,
        **kwargs,
    )