-
Notifications
You must be signed in to change notification settings - Fork 0
/
R3D.py
243 lines (184 loc) · 9.31 KB
/
R3D.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
# -*- coding: utf-8 -*-
#modified done
#2019.06.03
#haidong
import math
import torch.nn as nn
from torch.nn.modules.utils import _triple
class SpatioTemporalConv(nn.Module):
r"""Applies a factored 3D convolution over an input signal composed of several input
planes with distinct spatial and time axes, by performing a 2D convolution over the
spatial axes to an intermediate subspace, followed by a 1D convolution over the time
axis to produce the final output.
Args:
in_channels (int): Number of channels in the input tensor
out_channels (int): Number of channels produced by the convolution
kernel_size (int or tuple): Size of the convolving kernel
stride (int or tuple, optional): Stride of the convolution. Default: 1
padding (int or tuple, optional): Zero-padding added to the sides of the input during their respective convolutions. Default: 0
bias (bool, optional): If ``True``, adds a learnable bias to the output. Default: ``True``
"""
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, bias=False):
super(SpatioTemporalConv, self).__init__()
# if ints are entered, convert them to iterables, 1 -> [1, 1, 1]
kernel_size = _triple(kernel_size)
stride = _triple(stride)
padding = _triple(padding)
self.temporal_spatial_conv = nn.Conv3d(in_channels, out_channels, kernel_size,
stride=stride, padding=padding, bias=bias)
self.bn = nn.BatchNorm3d(out_channels)
self.relu = nn.ReLU()
def forward(self, x):
x = self.bn(self.temporal_spatial_conv(x))
x = self.relu(x)
return x
class SpatioTemporalResBlock(nn.Module):
r"""Single block for the ResNet network. Uses SpatioTemporalConv in
the standard ResNet block layout (conv->batchnorm->ReLU->conv->batchnorm->sum->ReLU)
Args:
in_channels (int): Number of channels in the input tensor.
out_channels (int): Number of channels in the output produced by the block.
kernel_size (int or tuple): Size of the convolving kernels.
downsample (bool, optional): If ``True``, the output size is to be smaller than the input. Default: ``False``
"""
def __init__(self, in_channels, out_channels, kernel_size, downsample=False):
super(SpatioTemporalResBlock, self).__init__()
# If downsample == True, the first conv of the layer has stride = 2
# to halve the residual output size, and the input x is passed
# through a seperate 1x1x1 conv with stride = 2 to also halve it.
# no pooling layers are used inside ResNet
self.downsample = downsample
# to allow for SAME padding
padding = kernel_size // 2
if self.downsample:
# downsample with stride =2 the input x
self.downsampleconv = SpatioTemporalConv(in_channels, out_channels, 1, stride=2)
self.downsamplebn = nn.BatchNorm3d(out_channels)
# downsample with stride = 2when producing the residual
self.conv1 = SpatioTemporalConv(in_channels, out_channels, kernel_size, padding=padding, stride=2)
else:
self.conv1 = SpatioTemporalConv(in_channels, out_channels, kernel_size, padding=padding)
self.bn1 = nn.BatchNorm3d(out_channels)
self.relu1 = nn.ReLU()
# standard conv->batchnorm->ReLU
self.conv2 = SpatioTemporalConv(out_channels, out_channels, kernel_size, padding=padding)
self.bn2 = nn.BatchNorm3d(out_channels)
self.outrelu = nn.ReLU()
def forward(self, x):
res = self.relu1(self.bn1(self.conv1(x)))
res = self.bn2(self.conv2(res))
if self.downsample:
x = self.downsamplebn(self.downsampleconv(x))
return self.outrelu(x + res)
class SpatioTemporalResLayer(nn.Module):
r"""Forms a single layer of the ResNet network, with a number of repeating
blocks of same output size stacked on top of each other
Args:
in_channels (int): Number of channels in the input tensor.
out_channels (int): Number of channels in the output produced by the layer.
kernel_size (int or tuple): Size of the convolving kernels.
layer_size (int): Number of blocks to be stacked to form the layer
block_type (Module, optional): Type of block that is to be used to form the layer. Default: SpatioTemporalResBlock.
downsample (bool, optional): If ``True``, the first block in layer will implement downsampling. Default: ``False``
"""
def __init__(self, in_channels, out_channels, kernel_size, layer_size, block_type=SpatioTemporalResBlock,
downsample=False):
super(SpatioTemporalResLayer, self).__init__()
# implement the first block
self.block1 = block_type(in_channels, out_channels, kernel_size, downsample)
# prepare module list to hold all (layer_size - 1) blocks
self.blocks = nn.ModuleList([])
for i in range(layer_size - 1):
# all these blocks are identical, and have downsample = False by default
self.blocks += [block_type(out_channels, out_channels, kernel_size)]
def forward(self, x):
x = self.block1(x)
for block in self.blocks:
x = block(x)
return x
class R3DNet(nn.Module):
r"""Forms the overall ResNet feature extractor by initializng 5 layers, with the number of blocks in
each layer set by layer_sizes, and by performing a global average pool at the end producing a
512-dimensional vector for each element in the batch.
Args:
layer_sizes (tuple): An iterable containing the number of blocks in each layer
block_type (Module, optional): Type of block that is to be used to form the layers. Default: SpatioTemporalResBlock.
"""
def __init__(self, layer_sizes, block_type=SpatioTemporalResBlock):
super(R3DNet, self).__init__()
# first conv, with stride 1x2x2 and kernel size 3x7x7
self.conv1 = SpatioTemporalConv(3, 64, [3, 7, 7], stride=[1, 2, 2], padding=[1, 3, 3])
# output of conv2 is same size as of conv1, no downsampling needed. kernel_size 3x3x3
self.conv2 = SpatioTemporalResLayer(64, 64, 3, layer_sizes[0], block_type=block_type)
# each of the final three layers doubles num_channels, while performing downsampling
# inside the first block
self.conv3 = SpatioTemporalResLayer(64, 128, 3, layer_sizes[1], block_type=block_type, downsample=True)
self.conv4 = SpatioTemporalResLayer(128, 256, 3, layer_sizes[2], block_type=block_type, downsample=True)
self.conv5 = SpatioTemporalResLayer(256, 512, 3, layer_sizes[3], block_type=block_type, downsample=True)
# global average pooling of the output
self.pool = nn.AdaptiveAvgPool3d(1)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
x = self.conv4(x)
x = self.conv5(x)
x = self.pool(x)
return x.view(-1, 512)
class R3DClassifier(nn.Module):
r"""Forms a complete ResNet classifier producing vectors of size num_classes, by initializng 5 layers,
with the number of blocks in each layer set by layer_sizes, and by performing a global average pool
at the end producing a 512-dimensional vector for each element in the batch,
and passing them through a Linear layer.
Args:
num_classes(int): Number of classes in the data
layer_sizes (tuple): An iterable containing the number of blocks in each layer
block_type (Module, optional): Type of block that is to be used to form the layers. Default: SpatioTemporalResBlock.
"""
def __init__(self, num_classes, layer_sizes, block_type=SpatioTemporalResBlock, pretrained=False):
super(R3DClassifier, self).__init__()
self.res3d = R3DNet(layer_sizes, block_type)
self.linear = nn.Linear(512, num_classes)
self.__init_weight()
if pretrained:
self.__load_pretrained_weights()
def forward(self, x):
x = self.res3d(x)
logits = self.linear(x)
return logits
def __load_pretrained_weights(self):
s_dict = self.state_dict()
for name in s_dict:
print(name)
print(s_dict[name].size())
def __init_weight(self):
for m in self.modules():
if isinstance(m, nn.Conv3d):
nn.init.kaiming_normal_(m.weight)
elif isinstance(m, nn.BatchNorm3d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def get_1x_lr_params(model):
"""
This generator returns all the parameters for the conv layer of the net.
"""
b = [model.res3d]
for i in range(len(b)):
for k in b[i].parameters():
if k.requires_grad:
yield k
def get_10x_lr_params(model):
"""
This generator returns all the parameters for the fc layer of the net.
"""
b = [model.linear]
for j in range(len(b)):
for k in b[j].parameters():
if k.requires_grad:
yield k
if __name__ == "__main__":
import torch
inputs = torch.rand(1, 3, 16, 112, 112)
net = R3DClassifier(101, (2, 2, 2, 2), pretrained=True)
outputs = net.forward(inputs)
print(outputs.size())