-
Notifications
You must be signed in to change notification settings - Fork 10
/
i3d.py
313 lines (297 loc) · 14.7 KB
/
i3d.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
""" This implementation based on naive tensorflow framework
Inception-v1 Inflated 3D ConvNet used for Kinetics CVPR paper.
The model is introduced in:
Quo Vadis, Action Recognition? A New Model and the Kinetics Dataset
Joao Carreira, Andrew Zisserman
https://arxiv.org/pdf/1705.07750v1.pdf.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
from snets.net_utils import unit3D
def I3D(inputs,
num_classes=101,
is_training=True,
final_endpoint='Predictions',
data_format='NHWC',
dropout_keep_prob=0.5,
min_depth=16,
depth_multiplier=1.0,
scope=None):
end_points = {}
if depth_multiplier <= 0:
raise ValueError('depth_multiplier is not greater than zero.')
depth = lambda d: max(int(d * depth_multiplier), min_depth)
concat_axis = 2 if data_format == 'NCHW' else -1
with tf.variable_scope(scope, 'I3D', [inputs]):
end_point = 'Conv3d_1a_7x7x7'
net = unit3D(inputs, depth(64), [7,7,7], 2, is_training=is_training, name=end_point)
end_points[end_point] = net
if end_point == final_endpoint: return net, end_points
end_point = 'MaxPool3d_2a_1x3x3'
net = tf.nn.max_pool3d(net, [1, 1, 3, 3, 1], [1, 1, 2, 2, 1], padding='SAME', name=end_point)
end_points[end_point] = net
if end_point == final_endpoint: return net, end_points
end_point = 'Conv3d_2b_1x1x1'
net = unit3D(net, depth(64), [1, 1, 1], is_training=is_training, name=end_point)
end_points[end_point] = net
if end_point == final_endpoint: return net, end_points
end_point = 'Conv3d_2c_3x3x3'
net = unit3D(net, depth(192), [3, 3, 3], is_training=is_training, name=end_point)
end_points[end_point] = net
if end_point == final_endpoint: return net, end_points
end_point = 'MaxPool3d_3a_1x3x3'
net = tf.nn.max_pool3d(net, [1, 1, 3, 3, 1], [1, 1, 2, 2, 1], padding='SAME', name=end_point)
end_points[end_point] = net
if end_point == final_endpoint: return net, end_points
end_point = 'Mixed_3b'
with tf.variable_scope(end_point):
with tf.variable_scope('Branch_0'):
branch_0 = unit3D(net, depth(64), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0a_1x1x1')
with tf.variable_scope('Branch_1'):
branch_1 = unit3D(net, depth(96), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0a_1x1x1')
branch_1 = unit3D(branch_1, depth(128), kernel_shape=[3, 3, 3],
is_training=is_training, name='Conv3d_0b_3x3x3')
with tf.variable_scope('Branch_2'):
branch_2 = unit3D(net, depth(16), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0a_1x1x1')
branch_2 = unit3D(branch_2, depth(32), kernel_shape=[3, 3, 3],
is_training=is_training, name='Conv3d_0b_3x3x3')
with tf.variable_scope('Branch_3'):
branch_3 = tf.nn.max_pool3d(net, [1, 3, 3, 3, 1], strides=[1, 1, 1, 1, 1],
padding='SAME', name='MaxPool3d_0a_3x3x3')
branch_3 = unit3D(branch_3, depth(32), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0b_1x1x1')
net = tf.concat([branch_0, branch_1, branch_2, branch_3], concat_axis)
end_points[end_point] = net
if end_point == final_endpoint: return net, end_points
end_point = 'Mixed_3c'
with tf.variable_scope(end_point):
with tf.variable_scope('Branch_0'):
branch_0 = unit3D(net, depth(128), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0a_1x1x1')
with tf.variable_scope('Branch_1'):
branch_1 = unit3D(net, depth(128), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0a_1x1x1')
branch_1 = unit3D(branch_1, depth(192), kernel_shape=[3, 3, 3],
is_training=is_training, name='Conv3d_0b_3x3x3')
with tf.variable_scope('Branch_2'):
branch_2 = unit3D(net, depth(32), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0a_1x1x1')
branch_2 = unit3D(branch_2, depth(96), kernel_shape=[3, 3, 3],
is_training=is_training, name='Conv3d_0b_3x3x3')
with tf.variable_scope('Branch_3'):
branch_3 = tf.nn.max_pool3d(net, ksize=[1, 3, 3, 3, 1],
strides=[1, 1, 1, 1, 1], padding='SAME',
name='MaxPool3d_0a_3x3x3')
branch_3 = unit3D(branch_3, depth(64), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0b_1x1x1')
net = tf.concat([branch_0, branch_1, branch_2, branch_3], concat_axis)
end_points[end_point] = net
if end_point == final_endpoint: return net, end_points
end_point = 'MaxPool3d_4a_3x3x3'
net = tf.nn.max_pool3d(net, ksize=[1, 3, 3, 3, 1], strides=[1, 2, 2, 2, 1],
padding='SAME', name=end_point)
end_points[end_point] = net
if end_point == final_endpoint: return net, end_points
end_point = 'Mixed_4b'
with tf.variable_scope(end_point):
with tf.variable_scope('Branch_0'):
branch_0 = unit3D(net, depth(192), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0a_1x1x1')
with tf.variable_scope('Branch_1'):
branch_1 = unit3D(net, depth(96), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0a_1x1x1')
branch_1 = unit3D(branch_1, depth(208), kernel_shape=[3, 3, 3],
is_training=is_training, name='Conv3d_0b_3x3x3')
with tf.variable_scope('Branch_2'):
branch_2 = unit3D(net, depth(16), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0a_1x1x1')
branch_2 = unit3D(branch_2, depth(48), kernel_shape=[3, 3, 3],
is_training=is_training, name='Conv3d_0b_3x3x3')
with tf.variable_scope('Branch_3'):
branch_3 = tf.nn.max_pool3d(net, ksize=[1, 3, 3, 3, 1],
strides=[1, 1, 1, 1, 1], padding='SAME',
name='MaxPool3d_0a_3x3x3')
branch_3 = unit3D(branch_3, depth(64), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0b_1x1x1')
net = tf.concat([branch_0, branch_1, branch_2, branch_3], concat_axis)
end_points[end_point] = net
if end_point == final_endpoint: return net, end_points
end_point = 'Mixed_4c'
with tf.variable_scope(end_point):
with tf.variable_scope('Branch_0'):
branch_0 = unit3D(net, depth(160), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0a_1x1x1')
with tf.variable_scope('Branch_1'):
branch_1 = unit3D(net, depth(112), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0a_1x1x1')
branch_1 = unit3D(branch_1, depth(224), kernel_shape=[3, 3, 3],
is_training=is_training, name='Conv3d_0b_3x3x3')
with tf.variable_scope('Branch_2'):
branch_2 = unit3D(net, depth(24), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0a_1x1x1')
branch_2 = unit3D(branch_2, depth(64), kernel_shape=[3, 3, 3],
is_training=is_training, name='Conv3d_0b_3x3x3')
with tf.variable_scope('Branch_3'):
branch_3 = tf.nn.max_pool3d(net, ksize=[1, 3, 3, 3, 1],
strides=[1, 1, 1, 1, 1], padding='SAME',
name='MaxPool3d_0a_3x3x3')
branch_3 = unit3D(branch_3, depth(64), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0b_1x1x1')
net = tf.concat([branch_0, branch_1, branch_2, branch_3], concat_axis)
end_points[end_point] = net
if end_point == final_endpoint: return net, end_points
end_point = 'Mixed_4d'
with tf.variable_scope(end_point):
with tf.variable_scope('Branch_0'):
branch_0 = unit3D(net, depth(128), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0a_1x1x1')
with tf.variable_scope('Branch_1'):
branch_1 = unit3D(net, depth(128), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0a_1x1x1')
branch_1 = unit3D(branch_1, depth(256), kernel_shape=[3, 3, 3],
is_training=is_training, name='Conv3d_0b_3x3x3')
with tf.variable_scope('Branch_2'):
branch_2 = unit3D(net, depth(24), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0a_1x1x1')
branch_2 = unit3D(branch_2, depth(64), kernel_shape=[3, 3, 3],
is_training=is_training, name='Conv3d_0b_3x3x3')
with tf.variable_scope('Branch_3'):
branch_3 = tf.nn.max_pool3d(net, ksize=[1, 3, 3, 3, 1],
strides=[1, 1, 1, 1, 1], padding='SAME',
name='MaxPool3d_0a_3x3x3')
branch_3 = unit3D(branch_3, depth(64), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0b_1x1x1')
net = tf.concat([branch_0, branch_1, branch_2, branch_3], concat_axis)
end_points[end_point] = net
if end_point == final_endpoint: return net, end_points
end_point = 'Mixed_4e'
with tf.variable_scope(end_point):
with tf.variable_scope('Branch_0'):
branch_0 = unit3D(net, depth(112), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0a_1x1x1')
with tf.variable_scope('Branch_1'):
branch_1 = unit3D(net, depth(144), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0a_1x1x1')
branch_1 = unit3D(branch_1, depth(288), kernel_shape=[3, 3, 3],
is_training=is_training, name='Conv3d_0b_3x3x3')
with tf.variable_scope('Branch_2'):
branch_2 = unit3D(net, depth(32), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0a_1x1x1')
branch_2 = unit3D(branch_2, depth(64), kernel_shape=[3, 3, 3],
is_training=is_training, name='Conv3d_0b_3x3x3')
with tf.variable_scope('Branch_3'):
branch_3 = tf.nn.max_pool3d(net, ksize=[1, 3, 3, 3, 1],
strides=[1, 1, 1, 1, 1], padding='SAME',
name='MaxPool3d_0a_3x3x3')
branch_3 = unit3D(branch_3, depth(64), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0b_1x1x1')
net = tf.concat([branch_0, branch_1, branch_2, branch_3], concat_axis)
end_points[end_point] = net
if end_point == final_endpoint: return net, end_points
end_point = 'Mixed_4f'
with tf.variable_scope(end_point):
with tf.variable_scope('Branch_0'):
branch_0 = unit3D(net, depth(256), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0a_1x1x1')
with tf.variable_scope('Branch_1'):
branch_1 = unit3D(net, depth(160), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0a_1x1x1')
branch_1 = unit3D(branch_1, depth(320), kernel_shape=[3, 3, 3],
is_training=is_training, name='Conv3d_0b_3x3x3')
with tf.variable_scope('Branch_2'):
branch_2 = unit3D(net, depth(32), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0a_1x1x1')
branch_2 = unit3D(branch_2, depth(128), kernel_shape=[3, 3, 3],
is_training=is_training, name='Conv3d_0b_3x3x3')
with tf.variable_scope('Branch_3'):
branch_3 = tf.nn.max_pool3d(net, ksize=[1, 3, 3, 3, 1],
strides=[1, 1, 1, 1, 1], padding='SAME',
name='MaxPool3d_0a_3x3x3')
branch_3 = unit3D(branch_3, depth(128), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0b_1x1x1')
net = tf.concat([branch_0, branch_1, branch_2, branch_3], concat_axis)
end_points[end_point] = net
if end_point == final_endpoint: return net, end_points
end_point = 'MaxPool3d_5a_2x2x2'
net = tf.nn.max_pool3d(net, ksize=[1, 2, 2, 2, 1], strides=[1, 2, 2, 2, 1],
padding='SAME', name=end_point)
end_points[end_point] = net
if end_point == final_endpoint: return net, end_points
end_point = 'Mixed_5b'
with tf.variable_scope(end_point):
with tf.variable_scope('Branch_0'):
branch_0 = unit3D(net, depth(256), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0a_1x1x1')
with tf.variable_scope('Branch_1'):
branch_1 = unit3D(net, depth(160), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0a_1x1x1')
branch_1 = unit3D(branch_1, depth(320), kernel_shape=[3, 3, 3],
is_training=is_training, name='Conv3d_0b_3x3x3')
with tf.variable_scope('Branch_2'):
branch_2 = unit3D(net, depth(32), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0a_1x1x1')
branch_2 = unit3D(branch_2, depth(128), kernel_shape=[3, 3, 3],
is_training=is_training, name='Conv3d_0a_3x3x3')
with tf.variable_scope('Branch_3'):
branch_3 = tf.nn.max_pool3d(net, ksize=[1, 3, 3, 3, 1],
strides=[1, 1, 1, 1, 1], padding='SAME',
name='MaxPool3d_0a_3x3x3')
branch_3 = unit3D(branch_3, depth(128), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0b_1x1x1')
net = tf.concat([branch_0, branch_1, branch_2, branch_3], concat_axis)
end_points[end_point] = net
if end_point == final_endpoint: return net, end_points
end_point = 'Mixed_5c'
with tf.variable_scope(end_point):
with tf.variable_scope('Branch_0'):
branch_0 = unit3D(net, depth(384), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0a_1x1x1')
with tf.variable_scope('Branch_1'):
branch_1 = unit3D(net, depth(192), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0a_1x1x1')
branch_1 = unit3D(branch_1, depth(384), kernel_shape=[3, 3, 3],
is_training=is_training, name='Conv3d_0b_3x3x3')
with tf.variable_scope('Branch_2'):
branch_2 = unit3D(net, depth(48), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0a_1x1x1')
branch_2 = unit3D(branch_2, depth(128), kernel_shape=[3, 3, 3],
is_training=is_training, name='Conv3d_0b_3x3x3')
with tf.variable_scope('Branch_3'):
branch_3 = tf.nn.max_pool3d(net, ksize=[1, 3, 3, 3, 1],
strides=[1, 1, 1, 1, 1], padding='SAME',
name='MaxPool3d_0a_3x3x3')
branch_3 = unit3D(branch_3, depth(128), kernel_shape=[1, 1, 1],
is_training=is_training, name='Conv3d_0b_1x1x1')
net = tf.concat([branch_0, branch_1, branch_2, branch_3], concat_axis)
end_points[end_point] = net
if end_point == final_endpoint: return net, end_points
end_point = 'Logits'
with tf.variable_scope(end_point):
net = tf.nn.avg_pool3d(net, ksize=[1, 2, 7, 7, 1],
strides=[1, 1, 1, 1, 1], padding='VALID')
net = tf.nn.dropout(net, dropout_keep_prob)
logits = unit3D(net, num_classes,
kernel_shape=[1, 1, 1],
activation_fn=None,
is_training=is_training,
use_batch_norm=False,
use_bias=True,
name='Conv3d_0c_1x1x1')
logits = tf.squeeze(logits, [2, 3], name='SpatialSqueeze')
averaged_logits = tf.reduce_mean(logits, axis=1)
end_points[end_point] = averaged_logits
if end_point == final_endpoint: return logits, end_points
end_point = 'Predictions'
predictions = tf.nn.softmax(averaged_logits)
end_points[end_point] = predictions
if end_point == final_endpoint: return predictions, end_points
if __name__ == '__main__':
# inputs: [batch_size, num_frames, h, w, c], outputs: [batch_size, num_classes]
inps = tf.placeholder(dtype=tf.float32, shape=[4, 64, 224, 224, 3])
si3d, _ = I3D(inps, final_endpoint='Logits')
print(si3d)