-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathi3dpt.py
434 lines (378 loc) · 15.3 KB
/
i3dpt.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
import math
import os
import numpy as np
import torch
import torch.nn
from torch.nn import ReplicationPad3d
from torch.nn.modules.padding import ConstantPad3d
def get_padding_shape(filter_shape, stride):
def _pad_top_bottom(filter_dim, stride_val):
pad_along = max(filter_dim - stride_val, 0)
pad_top = pad_along // 2
pad_bottom = pad_along - pad_top
return pad_top, pad_bottom
padding_shape = []
for filter_dim, stride_val in zip(filter_shape, stride):
pad_top, pad_bottom = _pad_top_bottom(filter_dim, stride_val)
padding_shape.append(pad_top)
padding_shape.append(pad_bottom)
depth_top = padding_shape.pop(0)
depth_bottom = padding_shape.pop(0)
padding_shape.append(depth_top)
padding_shape.append(depth_bottom)
return tuple(padding_shape)
def simplify_padding(padding_shapes):
all_same = True
padding_init = padding_shapes[0]
for pad in padding_shapes[1:]:
if pad != padding_init:
all_same = False
return all_same, padding_init
class Unit3Dpy(torch.nn.Module):
def __init__(self,
in_channels,
out_channels,
kernel_size=(1, 1, 1),
stride=(1, 1, 1),
activation='relu',
padding='SAME',
use_bias=False,
use_bn=True):
super(Unit3Dpy, self).__init__()
self.padding = padding
self.activation = activation
self.use_bn = use_bn
if padding == 'SAME':
padding_shape = get_padding_shape(kernel_size, stride)
simplify_pad, pad_size = simplify_padding(padding_shape)
self.simplify_pad = simplify_pad
elif padding == 'VALID':
padding_shape = 0
else:
raise ValueError(
'padding should be in [VALID|SAME] but got {}'.format(padding))
if padding == 'SAME':
if not simplify_pad:
self.pad = torch.nn.ConstantPad3d(padding_shape, 0)
self.conv3d = torch.nn.Conv3d(
in_channels,
out_channels,
kernel_size,
stride=stride,
bias=use_bias)
else:
self.conv3d = torch.nn.Conv3d(
in_channels,
out_channels,
kernel_size,
stride=stride,
padding=pad_size,
bias=use_bias)
elif padding == 'VALID':
self.conv3d = torch.nn.Conv3d(
in_channels,
out_channels,
kernel_size,
padding=padding_shape,
stride=stride,
bias=use_bias)
else:
raise ValueError(
'padding should be in [VALID|SAME] but got {}'.format(padding))
if self.use_bn:
self.batch3d = torch.nn.BatchNorm3d(out_channels)
if activation == 'relu':
self.activation = torch.nn.functional.relu
def forward(self, inp):
if self.padding == 'SAME' and self.simplify_pad is False:
inp = self.pad(inp)
out = self.conv3d(inp)
if self.use_bn:
out = self.batch3d(out)
if self.activation is not None:
out = torch.nn.functional.relu(out)
return out
class MaxPool3dTFPadding(torch.nn.Module):
def __init__(self, kernel_size, stride=None, padding='SAME'):
super(MaxPool3dTFPadding, self).__init__()
if padding == 'SAME':
padding_shape = get_padding_shape(kernel_size, stride)
self.padding_shape = padding_shape
self.pad = torch.nn.ConstantPad3d(padding_shape, 0)
self.pool = torch.nn.MaxPool3d(kernel_size, stride)
def forward(self, inp):
inp = self.pad(inp)
out = self.pool(inp)
return out
class Mixed(torch.nn.Module):
def __init__(self, in_channels, out_channels):
super(Mixed, self).__init__()
# Branch 0
self.branch_0 = Unit3Dpy(
in_channels, out_channels[0], kernel_size=(1, 1, 1))
# Branch 1
branch_1_conv1 = Unit3Dpy(
in_channels, out_channels[1], kernel_size=(1, 1, 1))
branch_1_conv2 = Unit3Dpy(
out_channels[1], out_channels[2], kernel_size=(3, 3, 3))
self.branch_1 = torch.nn.Sequential(branch_1_conv1, branch_1_conv2)
# Branch 2
branch_2_conv1 = Unit3Dpy(
in_channels, out_channels[3], kernel_size=(1, 1, 1))
branch_2_conv2 = Unit3Dpy(
out_channels[3], out_channels[4], kernel_size=(3, 3, 3))
self.branch_2 = torch.nn.Sequential(branch_2_conv1, branch_2_conv2)
# Branch3
branch_3_pool = MaxPool3dTFPadding(
kernel_size=(3, 3, 3), stride=(1, 1, 1), padding='SAME')
branch_3_conv2 = Unit3Dpy(
in_channels, out_channels[5], kernel_size=(1, 1, 1))
self.branch_3 = torch.nn.Sequential(branch_3_pool, branch_3_conv2)
def forward(self, inp):
out_0 = self.branch_0(inp)
out_1 = self.branch_1(inp)
out_2 = self.branch_2(inp)
out_3 = self.branch_3(inp)
out = torch.cat((out_0, out_1, out_2, out_3), 1)
return out
class I3D(torch.nn.Module):
def __init__(self,
num_classes,
modality='rgb',
dropout_keep_prob=1,
name='inception'):
super(I3D, self).__init__()
self.name = name
self.num_classes = num_classes
if modality == 'rgb':
in_channels = 3
elif modality == 'flow':
in_channels = 2
else:
raise ValueError(
'{} not among known modalities [rgb|flow]'.format(modality))
self.modality = modality
conv3d_1a_7x7 = Unit3Dpy(
out_channels=64,
in_channels=in_channels,
kernel_size=(7, 7, 7),
stride=(2, 2, 2),
padding='SAME')
# 1st conv-pool
self.conv3d_1a_7x7 = conv3d_1a_7x7
self.maxPool3d_2a_3x3 = MaxPool3dTFPadding(
kernel_size=(1, 3, 3), stride=(1, 2, 2), padding='SAME')
# conv conv
conv3d_2b_1x1 = Unit3Dpy(
out_channels=64,
in_channels=64,
kernel_size=(1, 1, 1),
padding='SAME')
self.conv3d_2b_1x1 = conv3d_2b_1x1
conv3d_2c_3x3 = Unit3Dpy(
out_channels=192,
in_channels=64,
kernel_size=(3, 3, 3),
padding='SAME')
self.conv3d_2c_3x3 = conv3d_2c_3x3
self.maxPool3d_3a_3x3 = MaxPool3dTFPadding(
kernel_size=(1, 3, 3), stride=(1, 2, 2), padding='SAME')
# Mixed_3b
self.mixed_3b = Mixed(192, [64, 96, 128, 16, 32, 32])
self.mixed_3c = Mixed(256, [128, 128, 192, 32, 96, 64])
self.maxPool3d_4a_3x3 = MaxPool3dTFPadding(
kernel_size=(3, 3, 3), stride=(2, 2, 2), padding='SAME')
# Mixed 4
self.mixed_4b = Mixed(480, [192, 96, 208, 16, 48, 64])
self.mixed_4c = Mixed(512, [160, 112, 224, 24, 64, 64])
self.mixed_4d = Mixed(512, [128, 128, 256, 24, 64, 64])
self.mixed_4e = Mixed(512, [112, 144, 288, 32, 64, 64])
self.mixed_4f = Mixed(528, [256, 160, 320, 32, 128, 128])
# Ugly hack because I didn't use tensorflow's exact padding function
self.pad_5a = torch.nn.ConstantPad3d((0, 0, 0, 0, 0, 1), 0)
self.maxPool3d_5a_2x2 = MaxPool3dTFPadding(
kernel_size=(2, 2, 2), stride=(2, 2, 2), padding='SAME')
# Mixed 5
self.mixed_5b = Mixed(832, [256, 160, 320, 32, 128, 128])
self.mixed_5c = Mixed(832, [384, 192, 384, 48, 128, 128])
self.avg_pool = torch.nn.AvgPool3d((2, 7, 7), (1, 1, 1))
self.dropout = torch.nn.Dropout(dropout_keep_prob)
self.conv3d_0c_1x1 = Unit3Dpy(
in_channels=1024,
out_channels=self.num_classes,
kernel_size=(1, 1, 1),
activation=None,
use_bias=True,
use_bn=False)
self.softmax = torch.nn.Softmax(1)
def forward(self, inp):
# Preprocessing
out = self.conv3d_1a_7x7(inp)
out = self.maxPool3d_2a_3x3(out)
out = self.conv3d_2b_1x1(out)
out = self.conv3d_2c_3x3(out)
out = self.maxPool3d_3a_3x3(out)
out = self.mixed_3b(out)
out = self.mixed_3c(out)
out = self.maxPool3d_4a_3x3(out)
out = self.mixed_4b(out)
out = self.mixed_4c(out)
out = self.mixed_4d(out)
out = self.mixed_4e(out)
out = self.mixed_4f(out)
out = self.pad_5a(out)
out = self.maxPool3d_5a_2x2(out)
out = self.mixed_5b(out)
out = self.mixed_5c(out)
out = self.avg_pool(out)
out = self.dropout(out)
out = self.conv3d_0c_1x1(out)
out = out.squeeze(3)
out = out.squeeze(3)
out = out.mean(2)
out_logits = out
out = self.softmax(out_logits)
return out, out_logits
def load_tf_weights(self, sess):
state_dict = {}
if self.modality == 'rgb':
prefix = 'RGB/inception_i3d'
elif self.modality == 'flow':
prefix = 'Flow/inception_i3d'
load_conv3d(state_dict, 'conv3d_1a_7x7', sess,
os.path.join(prefix, 'Conv3d_1a_7x7'))
load_conv3d(state_dict, 'conv3d_2b_1x1', sess,
os.path.join(prefix, 'Conv3d_2b_1x1'))
load_conv3d(state_dict, 'conv3d_2c_3x3', sess,
os.path.join(prefix, 'Conv3d_2c_3x3'))
load_mixed(state_dict, 'mixed_3b', sess,
os.path.join(prefix, 'Mixed_3b'))
load_mixed(state_dict, 'mixed_3c', sess,
os.path.join(prefix, 'Mixed_3c'))
load_mixed(state_dict, 'mixed_4b', sess,
os.path.join(prefix, 'Mixed_4b'))
load_mixed(state_dict, 'mixed_4c', sess,
os.path.join(prefix, 'Mixed_4c'))
load_mixed(state_dict, 'mixed_4d', sess,
os.path.join(prefix, 'Mixed_4d'))
load_mixed(state_dict, 'mixed_4e', sess,
os.path.join(prefix, 'Mixed_4e'))
# Here goest to 0.1 max error with tf
load_mixed(state_dict, 'mixed_4f', sess,
os.path.join(prefix, 'Mixed_4f'))
load_mixed(
state_dict,
'mixed_5b',
sess,
os.path.join(prefix, 'Mixed_5b'),
fix_typo=True)
load_mixed(state_dict, 'mixed_5c', sess,
os.path.join(prefix, 'Mixed_5c'))
load_conv3d(
state_dict,
'conv3d_0c_1x1',
sess,
os.path.join(prefix, 'Logits', 'Conv3d_0c_1x1'),
bias=True,
bn=False)
self.load_state_dict(state_dict)
def get_conv_params(sess, name, bias=False):
# Get conv weights
conv_weights_tensor = sess.graph.get_tensor_by_name(
os.path.join(name, 'w:0'))
if bias:
conv_bias_tensor = sess.graph.get_tensor_by_name(
os.path.join(name, 'b:0'))
conv_bias = sess.run(conv_bias_tensor)
conv_weights = sess.run(conv_weights_tensor)
conv_shape = conv_weights.shape
kernel_shape = conv_shape[0:3]
in_channels = conv_shape[3]
out_channels = conv_shape[4]
conv_op = sess.graph.get_operation_by_name(
os.path.join(name, 'convolution'))
padding_name = conv_op.get_attr('padding')
padding = _get_padding(padding_name, kernel_shape)
all_strides = conv_op.get_attr('strides')
strides = all_strides[1:4]
conv_params = [
conv_weights, kernel_shape, in_channels, out_channels, strides, padding
]
if bias:
conv_params.append(conv_bias)
return conv_params
def get_bn_params(sess, name):
moving_mean_tensor = sess.graph.get_tensor_by_name(
os.path.join(name, 'moving_mean:0'))
moving_var_tensor = sess.graph.get_tensor_by_name(
os.path.join(name, 'moving_variance:0'))
beta_tensor = sess.graph.get_tensor_by_name(os.path.join(name, 'beta:0'))
moving_mean = sess.run(moving_mean_tensor)
moving_var = sess.run(moving_var_tensor)
beta = sess.run(beta_tensor)
return moving_mean, moving_var, beta
def _get_padding(padding_name, conv_shape):
padding_name = padding_name.decode("utf-8")
if padding_name == "VALID":
return [0, 0]
elif padding_name == "SAME":
#return [math.ceil(int(conv_shape[0])/2), math.ceil(int(conv_shape[1])/2)]
return [
math.floor(int(conv_shape[0]) / 2),
math.floor(int(conv_shape[1]) / 2),
math.floor(int(conv_shape[2]) / 2)
]
else:
raise ValueError('Invalid padding name ' + padding_name)
def load_conv3d(state_dict, name_pt, sess, name_tf, bias=False, bn=True):
# Transfer convolution params
conv_name_tf = os.path.join(name_tf, 'conv_3d')
conv_params = get_conv_params(sess, conv_name_tf, bias=bias)
if bias:
conv_weights, kernel_shape, in_channels, out_channels, strides, padding, conv_bias = conv_params
else:
conv_weights, kernel_shape, in_channels, out_channels, strides, padding = conv_params
conv_weights_rs = np.transpose(
conv_weights, (4, 3, 0, 1,
2)) # to pt format (out_c, in_c, depth, height, width)
state_dict[name_pt + '.conv3d.weight'] = torch.from_numpy(conv_weights_rs)
if bias:
state_dict[name_pt + '.conv3d.bias'] = torch.from_numpy(conv_bias)
# Transfer batch norm params
if bn:
conv_tf_name = os.path.join(name_tf, 'batch_norm')
moving_mean, moving_var, beta = get_bn_params(sess, conv_tf_name)
out_planes = conv_weights_rs.shape[0]
state_dict[name_pt + '.batch3d.weight'] = torch.ones(out_planes)
state_dict[name_pt + '.batch3d.bias'] = torch.from_numpy(beta)
state_dict[name_pt
+ '.batch3d.running_mean'] = torch.from_numpy(moving_mean)
state_dict[name_pt
+ '.batch3d.running_var'] = torch.from_numpy(moving_var)
def load_mixed(state_dict, name_pt, sess, name_tf, fix_typo=False):
# Branch 0
load_conv3d(state_dict, name_pt + '.branch_0', sess,
os.path.join(name_tf, 'Branch_0/Conv3d_0a_1x1'))
# Branch .1
load_conv3d(state_dict, name_pt + '.branch_1.0', sess,
os.path.join(name_tf, 'Branch_1/Conv3d_0a_1x1'))
load_conv3d(state_dict, name_pt + '.branch_1.1', sess,
os.path.join(name_tf, 'Branch_1/Conv3d_0b_3x3'))
# Branch 2
load_conv3d(state_dict, name_pt + '.branch_2.0', sess,
os.path.join(name_tf, 'Branch_2/Conv3d_0a_1x1'))
if fix_typo:
load_conv3d(state_dict, name_pt + '.branch_2.1', sess,
os.path.join(name_tf, 'Branch_2/Conv3d_0a_3x3'))
else:
load_conv3d(state_dict, name_pt + '.branch_2.1', sess,
os.path.join(name_tf, 'Branch_2/Conv3d_0b_3x3'))
# Branch 3
load_conv3d(state_dict, name_pt + '.branch_3.1', sess,
os.path.join(name_tf, 'Branch_3/Conv3d_0b_1x1'))
if __name__ == '__main__':
model = I3D(num_classes=400, modality='rgb')
data=torch.autograd.Variable(torch.rand(10,3,16,224,224)) # if modality=='Flow', please change the 2nd dimension 3==>2
out=model(data)
print (model)
print (out[0].size())