forked from facebookresearch/ClassyVision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resnext3d_block.py
506 lines (465 loc) · 17.6 KB
/
resnext3d_block.py
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import torch.nn as nn
from .r2plus1_util import r2plus1_unit
class BasicTransformation(nn.Module):
"""
Basic transformation: 3x3x3 group conv, 3x3x3 group conv
"""
def __init__(
self,
dim_in,
dim_out,
temporal_stride,
spatial_stride,
groups,
inplace_relu=True,
bn_eps=1e-5,
bn_mmt=0.1,
use_r2plus1=False,
**kwargs
):
"""
Args:
dim_in (int): the channel dimensions of the input.
dim_out (int): the channel dimension of the output.
temporal_stride (int): the temporal stride of the bottleneck.
spatial_stride (int): the spatial_stride of the bottleneck.
groups (int): number of groups for the convolution.
inplace_relu (bool): calculate the relu on the original input
without allocating new memory.
bn_eps (float): epsilon for batch norm.
bn_mmt (float): momentum for batch norm. Noted that BN momentum in
PyTorch = 1 - BN momentum in Caffe2.
use_r2plus1 (bool): If true, decompose the original 3D conv into one 2D
spatial conv and one 1D temporal conv
"""
super(BasicTransformation, self).__init__()
if not use_r2plus1:
# 3x3x3 group conv, BN, ReLU.
branch2a = nn.Conv3d(
dim_in,
dim_out,
[3, 3, 3], # kernel
stride=[temporal_stride, spatial_stride, spatial_stride],
padding=[1, 1, 1],
groups=groups,
bias=False,
)
else:
# Implementation of R(2+1)D operation <https://arxiv.org/abs/1711.11248>.
# decompose the original 3D conv into one 2D spatial conv and one
# 1D temporal conv
branch2a = r2plus1_unit(
dim_in,
dim_out,
temporal_stride,
spatial_stride,
groups,
inplace_relu,
bn_eps,
bn_mmt,
)
branch2a_bn = nn.BatchNorm3d(dim_out, eps=bn_eps, momentum=bn_mmt)
branch2a_relu = nn.ReLU(inplace=inplace_relu)
if not use_r2plus1:
# 3x3x3 group conv, BN, ReLU.
branch2b = nn.Conv3d(
dim_out,
dim_out,
[3, 3, 3], # kernel
stride=[1, 1, 1],
padding=[1, 1, 1],
groups=groups,
bias=False,
)
else:
# Implementation of R(2+1)D operation <https://arxiv.org/abs/1711.11248>.
# decompose the original 3D conv into one 1x3x3 group conv and one
# 3x1x1 group conv
branch2b = r2plus1_unit(
dim_out,
dim_out,
1, # temporal_stride
1, # spatial_stride
groups,
inplace_relu,
bn_eps,
bn_mmt,
)
branch2b_bn = nn.BatchNorm3d(dim_out, eps=bn_eps, momentum=bn_mmt)
branch2b_bn.final_transform_op = True
self.basic_transform = nn.Sequential(
branch2a, branch2a_bn, branch2a_relu, branch2b, branch2b_bn
)
def forward(self, x):
return self.basic_transform(x)
class PostactivatedBottleneckTransformation(nn.Module):
"""
Bottleneck transformation: Tx1x1, 1x3x3, 1x1x1, where T is the size of
temporal kernel.
"""
def __init__(
self,
dim_in,
dim_out,
temporal_stride,
spatial_stride,
num_groups,
dim_inner,
temporal_kernel_size=3,
temporal_conv_1x1=True,
spatial_stride_1x1=False,
inplace_relu=True,
bn_eps=1e-5,
bn_mmt=0.1,
**kwargs
):
"""
Args:
dim_in (int): the channel dimensions of the input.
dim_out (int): the channel dimension of the output.
temporal_kernel_size (int): the temporal kernel sizes of the middle
convolution in the bottleneck.
temporal_conv_1x1 (bool): if True, do temporal convolution in the fist
1x1 Conv3d. Otherwise, do it in the second 3x3 Conv3d
temporal_stride (int): the temporal stride of the bottleneck.
spatial_stride (int): the spatial_stride of the bottleneck.
num_groups (int): number of groups for the convolution.
dim_inner (int): the inner dimension of the block.
is for standard ResNet like networks, and num_groups>1 is for
ResNeXt like networks.
spatial_stride_1x1 (bool): if True, apply spatial_stride to 1x1 conv.
inplace_relu (bool): calculate the relu on the original input
without allocating new memory.
bn_eps (float): epsilon for batch norm.
bn_mmt (float): momentum for batch norm. Noted that BN momentum in
PyTorch = 1 - BN momentum in Caffe2.
"""
super(PostactivatedBottleneckTransformation, self).__init__()
(temporal_kernel_size_1x1, temporal_kernel_size_3x3) = (
(temporal_kernel_size, 1)
if temporal_conv_1x1
else (1, temporal_kernel_size)
)
# MSRA -> stride=2 is on 1x1; TH/C2 -> stride=2 is on 3x3.
(str1x1, str3x3) = (
(spatial_stride, 1) if spatial_stride_1x1 else (1, spatial_stride)
)
# Tx1x1 conv, BN, ReLU.
self.branch2a = nn.Conv3d(
dim_in,
dim_inner,
kernel_size=[temporal_kernel_size_1x1, 1, 1],
stride=[1, str1x1, str1x1],
padding=[temporal_kernel_size_1x1 // 2, 0, 0],
bias=False,
)
self.branch2a_bn = nn.BatchNorm3d(dim_inner, eps=bn_eps, momentum=bn_mmt)
self.branch2a_relu = nn.ReLU(inplace=inplace_relu)
# Tx3x3 group conv, BN, ReLU.
self.branch2b = nn.Conv3d(
dim_inner,
dim_inner,
[temporal_kernel_size_3x3, 3, 3],
stride=[temporal_stride, str3x3, str3x3],
padding=[temporal_kernel_size_3x3 // 2, 1, 1],
groups=num_groups,
bias=False,
)
self.branch2b_bn = nn.BatchNorm3d(dim_inner, eps=bn_eps, momentum=bn_mmt)
self.branch2b_relu = nn.ReLU(inplace=inplace_relu)
# 1x1x1 conv, BN.
self.branch2c = nn.Conv3d(
dim_inner,
dim_out,
kernel_size=[1, 1, 1],
stride=[1, 1, 1],
padding=[0, 0, 0],
bias=False,
)
self.branch2c_bn = nn.BatchNorm3d(dim_out, eps=bn_eps, momentum=bn_mmt)
self.branch2c_bn.final_transform_op = True
def forward(self, x):
# Explicitly forward every layer.
# Branch2a.
x = self.branch2a(x)
x = self.branch2a_bn(x)
x = self.branch2a_relu(x)
# Branch2b.
x = self.branch2b(x)
x = self.branch2b_bn(x)
x = self.branch2b_relu(x)
# Branch2c
x = self.branch2c(x)
x = self.branch2c_bn(x)
return x
class PreactivatedBottleneckTransformation(nn.Module):
"""
Bottleneck transformation with pre-activation, which includes BatchNorm3D
and ReLu. Conv3D kernsl are Tx1x1, 1x3x3, 1x1x1, where T is the size of
temporal kernel (https://arxiv.org/abs/1603.05027).
"""
def __init__(
self,
dim_in,
dim_out,
temporal_stride,
spatial_stride,
num_groups,
dim_inner,
temporal_kernel_size=3,
temporal_conv_1x1=True,
spatial_stride_1x1=False,
inplace_relu=True,
bn_eps=1e-5,
bn_mmt=0.1,
disable_pre_activation=False,
**kwargs
):
"""
Args:
dim_in (int): the channel dimensions of the input.
dim_out (int): the channel dimension of the output.
temporal_kernel_size (int): the temporal kernel sizes of the middle
convolution in the bottleneck.
temporal_conv_1x1 (bool): if True, do temporal convolution in the fist
1x1 Conv3d. Otherwise, do it in the second 3x3 Conv3d
temporal_stride (int): the temporal stride of the bottleneck.
spatial_stride (int): the spatial_stride of the bottleneck.
num_groups (int): number of groups for the convolution.
dim_inner (int): the inner dimension of the block.
is for standard ResNet like networks, and num_groups>1 is for
ResNeXt like networks.
spatial_stride_1x1 (bool): if True, apply spatial_stride to 1x1 conv.
inplace_relu (bool): calculate the relu on the original input
without allocating new memory.
bn_eps (float): epsilon for batch norm.
bn_mmt (float): momentum for batch norm. Noted that BN momentum in
PyTorch = 1 - BN momentum in Caffe2.
disable_pre_activation (bool): If true, disable pre activation,
including BatchNorm3D and ReLU.
"""
super(PreactivatedBottleneckTransformation, self).__init__()
(temporal_kernel_size_1x1, temporal_kernel_size_3x3) = (
(temporal_kernel_size, 1)
if temporal_conv_1x1
else (1, temporal_kernel_size)
)
(str1x1, str3x3) = (
(spatial_stride, 1) if spatial_stride_1x1 else (1, spatial_stride)
)
self.disable_pre_activation = disable_pre_activation
if not disable_pre_activation:
self.branch2a_bn = nn.BatchNorm3d(dim_in, eps=bn_eps, momentum=bn_mmt)
self.branch2a_relu = nn.ReLU(inplace=inplace_relu)
self.branch2a = nn.Conv3d(
dim_in,
dim_inner,
kernel_size=[temporal_kernel_size_1x1, 1, 1],
stride=[1, str1x1, str1x1],
padding=[temporal_kernel_size_1x1 // 2, 0, 0],
bias=False,
)
# Tx3x3 group conv, BN, ReLU.
self.branch2b_bn = nn.BatchNorm3d(dim_inner, eps=bn_eps, momentum=bn_mmt)
self.branch2b_relu = nn.ReLU(inplace=inplace_relu)
self.branch2b = nn.Conv3d(
dim_inner,
dim_inner,
[temporal_kernel_size_3x3, 3, 3],
stride=[temporal_stride, str3x3, str3x3],
padding=[temporal_kernel_size_3x3 // 2, 1, 1],
groups=num_groups,
bias=False,
)
# 1x1x1 conv, BN.
self.branch2c_bn = nn.BatchNorm3d(dim_inner, eps=bn_eps, momentum=bn_mmt)
self.branch2c_relu = nn.ReLU(inplace=inplace_relu)
self.branch2c = nn.Conv3d(
dim_inner,
dim_out,
kernel_size=[1, 1, 1],
stride=[1, 1, 1],
padding=[0, 0, 0],
bias=False,
)
self.branch2c.final_transform_op = True
def forward(self, x):
# Branch2a
if not self.disable_pre_activation:
x = self.branch2a_bn(x)
x = self.branch2a_relu(x)
x = self.branch2a(x)
# Branch2b
x = self.branch2b_bn(x)
x = self.branch2b_relu(x)
x = self.branch2b(x)
# Branch2c
x = self.branch2c_bn(x)
x = self.branch2c_relu(x)
x = self.branch2c(x)
return x
residual_transformations = {
"basic_transformation": BasicTransformation,
"postactivated_bottleneck_transformation": PostactivatedBottleneckTransformation,
"preactivated_bottleneck_transformation": PreactivatedBottleneckTransformation,
# For more types of residual transformations, add them below
}
class PostactivatedShortcutTransformation(nn.Module):
"""
Skip connection used in ResNet3D model.
"""
def __init__(
self,
dim_in,
dim_out,
temporal_stride,
spatial_stride,
bn_eps=1e-5,
bn_mmt=0.1,
**kwargs
):
super(PostactivatedShortcutTransformation, self).__init__()
# Use skip connection with projection if dim or spatial/temporal res change.
assert (dim_in != dim_out) or (spatial_stride != 1) or (temporal_stride != 1)
self.branch1 = nn.Conv3d(
dim_in,
dim_out,
kernel_size=1,
stride=[temporal_stride, spatial_stride, spatial_stride],
padding=0,
bias=False,
)
self.branch1_bn = nn.BatchNorm3d(dim_out, eps=bn_eps, momentum=bn_mmt)
def forward(self, x):
return self.branch1_bn(self.branch1(x))
class PreactivatedShortcutTransformation(nn.Module):
"""
Skip connection with pre-activation, which includes BatchNorm3D and ReLU,
in ResNet3D model (https://arxiv.org/abs/1603.05027).
"""
def __init__(
self,
dim_in,
dim_out,
temporal_stride,
spatial_stride,
inplace_relu=True,
bn_eps=1e-5,
bn_mmt=0.1,
disable_pre_activation=False,
**kwargs
):
super(PreactivatedShortcutTransformation, self).__init__()
# Use skip connection with projection if dim or spatial/temporal res change.
assert (dim_in != dim_out) or (spatial_stride != 1) or (temporal_stride != 1)
if not disable_pre_activation:
self.branch1_bn = nn.BatchNorm3d(dim_in, eps=bn_eps, momentum=bn_mmt)
self.branch1_relu = nn.ReLU(inplace=inplace_relu)
self.branch1 = nn.Conv3d(
dim_in,
dim_out,
kernel_size=1,
stride=[temporal_stride, spatial_stride, spatial_stride],
padding=0,
bias=False,
)
def forward(self, x):
if hasattr(self, "branch1_bn") and hasattr(self, "branch1_relu"):
x = self.branch1_relu(self.branch1_bn(x))
x = self.branch1(x)
return x
skip_transformations = {
"postactivated_shortcut": PostactivatedShortcutTransformation,
"preactivated_shortcut": PreactivatedShortcutTransformation,
# For more types of skip transformations, add them below
}
class ResBlock(nn.Module):
"""
Residual block with skip connection.
"""
def __init__(
self,
dim_in,
dim_out,
dim_inner,
temporal_kernel_size,
temporal_conv_1x1,
temporal_stride,
spatial_stride,
skip_transformation_type,
residual_transformation_type,
num_groups=1,
inplace_relu=True,
bn_eps=1e-5,
bn_mmt=0.1,
disable_pre_activation=False,
use_r2plus1=False,
):
"""
ResBlock class constructs redisual blocks. More details can be found in:
"Deep residual learning for image recognition."
https://arxiv.org/abs/1512.03385
Args:
dim_in (int): the channel dimensions of the input.
dim_out (int): the channel dimension of the output.
dim_inner (int): the inner dimension of the block.
temporal_kernel_size (int): the temporal kernel sizes of the middle
convolution in the bottleneck.
temporal_conv_1x1 (bool): Only useful for PostactivatedBottleneckTransformation.
if True, do temporal convolution in the fist 1x1 Conv3d.
Otherwise, do it in the second 3x3 Conv3d
temporal_stride (int): the temporal stride of the bottleneck.
spatial_stride (int): the spatial_stride of the bottleneck.
stride (int): the stride of the bottleneck.
skip_transformation_type (str): the type of skip transformation
residual_transformation_type (str): the type of residual transformation
num_groups (int): number of groups for the convolution. num_groups=1
is for standard ResNet like networks, and num_groups>1 is for
ResNeXt like networks.
disable_pre_activation (bool): If true, disable the preactivation,
which includes BatchNorm3D and ReLU.
use_r2plus1 (bool): If true, decompose the original 3D conv into one 2D
spatial conv and one 1D temporal conv
"""
super(ResBlock, self).__init__()
assert skip_transformation_type in skip_transformations, (
"unknown skip transformation: %s" % skip_transformation_type
)
if (dim_in != dim_out) or (spatial_stride != 1) or (temporal_stride != 1):
self.skip = skip_transformations[skip_transformation_type](
dim_in,
dim_out,
temporal_stride,
spatial_stride,
bn_eps=bn_eps,
bn_mmt=bn_mmt,
disable_pre_activation=disable_pre_activation,
)
assert residual_transformation_type in residual_transformations, (
"unknown residual transformation: %s" % residual_transformation_type
)
self.residual = residual_transformations[residual_transformation_type](
dim_in,
dim_out,
temporal_stride,
spatial_stride,
num_groups,
dim_inner,
temporal_kernel_size=temporal_kernel_size,
temporal_conv_1x1=temporal_conv_1x1,
disable_pre_activation=disable_pre_activation,
use_r2plus1=use_r2plus1,
)
self.relu = nn.ReLU(inplace_relu)
def forward(self, x):
if hasattr(self, "skip"):
x = self.skip(x) + self.residual(x)
else:
x = x + self.residual(x)
x = self.relu(x)
return x