-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathm_c3d.py
358 lines (275 loc) · 17 KB
/
m_c3d.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
from m_video import clipread, randcrop
from sklearn.utils import shuffle
from time import ctime
import tensorflow as tf
import scipy.io as sio
import numpy as np
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
HEIGHT = 128
WIDTH = 171
FRAMES = 16
CROP_SIZE = 112
CHANNELS = 3
BATCH_SIZE = 32
def c3d(inputs, num_class, training):
"""
C3D network for video classification
:param inputs: Tensor inputs (batch, depth=16, height=112, width=112, channels=3), should be means subtracted
:param num_class: A scalar for number of class
:param training: A boolean tensor for training mode (True) or testing mode (False)
:return: Output tensor
"""
net = tf.layers.conv3d(inputs=inputs, filters=64, kernel_size=3, padding='SAME', activation=tf.nn.relu)
net = tf.layers.max_pooling3d(inputs=net, pool_size=(1, 2, 2), strides=(1, 2, 2), padding='SAME')
net = tf.layers.conv3d(inputs=net, filters=128, kernel_size=3, padding='SAME', activation=tf.nn.relu)
net = tf.layers.max_pooling3d(inputs=net, pool_size=2, strides=2, padding='SAME')
net = tf.layers.conv3d(inputs=net, filters=256, kernel_size=3, padding='SAME', activation=tf.nn.relu)
net = tf.layers.conv3d(inputs=net, filters=256, kernel_size=3, padding='SAME', activation=tf.nn.relu)
net = tf.layers.max_pooling3d(inputs=net, pool_size=2, strides=2, padding='SAME')
net = tf.layers.conv3d(inputs=net, filters=512, kernel_size=3, padding='SAME', activation=tf.nn.relu)
net = tf.layers.conv3d(inputs=net, filters=512, kernel_size=3, padding='SAME', activation=tf.nn.relu)
net = tf.layers.max_pooling3d(inputs=net, pool_size=2, strides=2, padding='SAME')
net = tf.pad(net, [[0, 0], [1, 1], [1, 1], [1, 1], [0, 0]])
net = tf.layers.conv3d(inputs=net, filters=512, kernel_size=3, activation=tf.nn.relu, padding='VALID')
net = tf.pad(net, [[0, 0], [1, 1], [1, 1], [1, 1], [0, 0]])
net = tf.layers.conv3d(inputs=net, filters=512, kernel_size=3, activation=tf.nn.relu, padding='VALID')
net = tf.layers.max_pooling3d(inputs=net, pool_size=2, strides=2, padding='SAME')
net = tf.layers.flatten(net)
net = tf.layers.dense(inputs=net, units=4096, activation=tf.nn.relu)
net = tf.identity(net, name='fc1')
net = tf.layers.dropout(inputs=net, rate=0.5, training=training)
net = tf.layers.dense(inputs=net, units=4096, activation=tf.nn.relu)
net = tf.identity(net, name='fc2')
net = tf.layers.dropout(inputs=net, rate=0.5, training=training)
net = tf.layers.dense(inputs=net, units=num_class, activation=None)
net = tf.identity(net, name='logits')
return net
def c3d_ucf101(inputs, training, weights=None):
"""
C3D network for ucf101 dataset, use pretrained weights on UCF101 for testing
:param inputs: Tensor inputs (batch, depth=16, height=112, width=112, channels=3), should be means subtracted
:param training: A boolean tensor for training mode (True) or testing mode (False)
:param weights: pretrained weights, if None, return network with random initialization
:return: Output tensor for 101 classes
"""
# create c3d network with pretrained ucf101 weights
net = tf.layers.conv3d(inputs=inputs, filters=64, kernel_size=3, padding='SAME', activation=tf.nn.relu,
kernel_initializer=tf.constant_initializer(weights[0]),
bias_initializer=tf.constant_initializer(weights[1]))
net = tf.layers.max_pooling3d(inputs=net, pool_size=(1, 2, 2), strides=(1, 2, 2), padding='SAME')
net = tf.layers.conv3d(inputs=net, filters=128, kernel_size=3, padding='SAME', activation=tf.nn.relu,
kernel_initializer=tf.constant_initializer(weights[2]),
bias_initializer=tf.constant_initializer(weights[3]))
net = tf.layers.max_pooling3d(inputs=net, pool_size=2, strides=2, padding='SAME')
net = tf.layers.conv3d(inputs=net, filters=256, kernel_size=3, padding='SAME', activation=tf.nn.relu,
kernel_initializer=tf.constant_initializer(weights[4]),
bias_initializer=tf.constant_initializer(weights[5]))
net = tf.layers.conv3d(inputs=net, filters=256, kernel_size=3, padding='SAME', activation=tf.nn.relu,
kernel_initializer=tf.constant_initializer(weights[6]),
bias_initializer=tf.constant_initializer(weights[7]))
net = tf.layers.max_pooling3d(inputs=net, pool_size=2, strides=2, padding='SAME')
net = tf.layers.conv3d(inputs=net, filters=512, kernel_size=3, padding='SAME', activation=tf.nn.relu,
kernel_initializer=tf.constant_initializer(weights[8]),
bias_initializer=tf.constant_initializer(weights[9]))
net = tf.layers.conv3d(inputs=net, filters=512, kernel_size=3, padding='SAME', activation=tf.nn.relu,
kernel_initializer=tf.constant_initializer(weights[10]),
bias_initializer=tf.constant_initializer(weights[11]))
net = tf.layers.max_pooling3d(inputs=net, pool_size=2, strides=2, padding='SAME')
net = tf.pad(net, [[0, 0], [1, 1], [1, 1], [1, 1], [0, 0]])
net = tf.layers.conv3d(inputs=net, filters=512, kernel_size=3, activation=tf.nn.relu, padding='VALID',
kernel_initializer=tf.constant_initializer(weights[12]),
bias_initializer=tf.constant_initializer(weights[13]))
net = tf.pad(net, [[0, 0], [1, 1], [1, 1], [1, 1], [0, 0]])
net = tf.layers.conv3d(inputs=net, filters=512, kernel_size=3, activation=tf.nn.relu, padding='VALID',
kernel_initializer=tf.constant_initializer(weights[14]),
bias_initializer=tf.constant_initializer(weights[15]))
net = tf.layers.max_pooling3d(inputs=net, pool_size=2, strides=2, padding='SAME')
net = tf.layers.flatten(net)
net = tf.layers.dense(inputs=net, units=4096, activation=tf.nn.relu,
kernel_initializer=tf.constant_initializer(weights[16]),
bias_initializer=tf.constant_initializer(weights[17]))
net = tf.identity(net, name='fc1')
net = tf.layers.dropout(inputs=net, rate=0.5, training=training)
net = tf.layers.dense(inputs=net, units=4096, activation=tf.nn.relu,
kernel_initializer=tf.constant_initializer(weights[18]),
bias_initializer=tf.constant_initializer(weights[19]))
net = tf.identity(net, name='fc2')
net = tf.layers.dropout(inputs=net, rate=0.5, training=training)
net = tf.layers.dense(inputs=net, units=101, activation=None,
kernel_initializer=tf.constant_initializer(weights[20]),
bias_initializer=tf.constant_initializer(weights[21]))
net = tf.identity(net, name='logits')
return net
def c3d_ucf101_finetune(inputs, training, weights=None):
"""
C3D network for ucf101 dataset fine-tuned from weights pretrained on Sports1M
:param inputs: Tensor inputs (batch, depth=16, height=112, width=112, channels=3), should be means subtracted
:param training: A boolean tensor for training mode (True) or testing mode (False)
:param weights: pretrained weights, if None, return network with random initialization
:return: Output tensor for 101 classes
"""
# create c3d network with pretrained Sports1M weights
net = tf.layers.conv3d(inputs=inputs, filters=64, kernel_size=3, padding='SAME', activation=tf.nn.relu,
kernel_initializer=tf.constant_initializer(weights[0]),
bias_initializer=tf.constant_initializer(weights[1]))
net = tf.layers.max_pooling3d(inputs=net, pool_size=(1, 2, 2), strides=(1, 2, 2), padding='SAME')
net = tf.layers.conv3d(inputs=net, filters=128, kernel_size=3, padding='SAME', activation=tf.nn.relu,
kernel_initializer=tf.constant_initializer(weights[2]),
bias_initializer=tf.constant_initializer(weights[3]))
net = tf.layers.max_pooling3d(inputs=net, pool_size=2, strides=2, padding='SAME')
net = tf.layers.conv3d(inputs=net, filters=256, kernel_size=3, padding='SAME', activation=tf.nn.relu,
kernel_initializer=tf.constant_initializer(weights[4]),
bias_initializer=tf.constant_initializer(weights[5]))
net = tf.layers.conv3d(inputs=net, filters=256, kernel_size=3, padding='SAME', activation=tf.nn.relu,
kernel_initializer=tf.constant_initializer(weights[6]),
bias_initializer=tf.constant_initializer(weights[7]))
net = tf.layers.max_pooling3d(inputs=net, pool_size=2, strides=2, padding='SAME')
net = tf.layers.conv3d(inputs=net, filters=512, kernel_size=3, padding='SAME', activation=tf.nn.relu,
kernel_initializer=tf.constant_initializer(weights[8]),
bias_initializer=tf.constant_initializer(weights[9]))
net = tf.layers.conv3d(inputs=net, filters=512, kernel_size=3, padding='SAME', activation=tf.nn.relu,
kernel_initializer=tf.constant_initializer(weights[10]),
bias_initializer=tf.constant_initializer(weights[11]))
net = tf.layers.max_pooling3d(inputs=net, pool_size=2, strides=2, padding='SAME')
net = tf.pad(net, [[0, 0], [1, 1], [1, 1], [1, 1], [0, 0]])
net = tf.layers.conv3d(inputs=net, filters=512, kernel_size=3, activation=tf.nn.relu, padding='VALID',
kernel_initializer=tf.constant_initializer(weights[12]),
bias_initializer=tf.constant_initializer(weights[13]))
net = tf.pad(net, [[0, 0], [1, 1], [1, 1], [1, 1], [0, 0]])
net = tf.layers.conv3d(inputs=net, filters=512, kernel_size=3, activation=tf.nn.relu, padding='VALID',
kernel_initializer=tf.constant_initializer(weights[14]),
bias_initializer=tf.constant_initializer(weights[15]))
net = tf.layers.max_pooling3d(inputs=net, pool_size=2, strides=2, padding='SAME')
net = tf.layers.flatten(net)
net = tf.layers.dense(inputs=net, units=4096, activation=tf.nn.relu,
kernel_initializer=tf.constant_initializer(weights[16]),
bias_initializer=tf.constant_initializer(weights[17]))
net = tf.identity(net, name='fc1')
net = tf.layers.dropout(inputs=net, rate=0.5, training=training)
net = tf.layers.dense(inputs=net, units=4096, activation=tf.nn.relu,
kernel_initializer=tf.constant_initializer(weights[18]),
bias_initializer=tf.constant_initializer(weights[19]))
net = tf.identity(net, name='fc2')
net = tf.layers.dropout(inputs=net, rate=0.5, training=training)
net = tf.layers.dense(inputs=net, units=101, activation=None,
kernel_initializer=tf.random_normal_initializer(mean=0.0, stddev=0.001),
bias_initializer=tf.zeros_initializer())
net = tf.identity(net, name='logits')
return net
def read_train(tr_file):
path, frm, cls = tr_file.split(' ')
start = np.random.randint(int(frm) - FRAMES)
frame_dir = './frames/'
v_paths = [frame_dir + path + 'frm_%06d.jpg' % (f + 1) for f in range(start, start + FRAMES)]
offsets = randcrop(scales=[128, 112, 96, 84], size=(128, 171))
voxel = clipread(v_paths, offsets, size=(128, 171), crop_size=(112, 112), mode='RGB')
is_flip = np.random.rand(1, 1).squeeze() > 0.5
if is_flip:
voxel = np.flip(voxel, axis=2)
return voxel, np.float32(cls)
def read_test(tst_file):
path, start, cls, vid = tst_file.split(' ')
start = int(start)
frame_dir = './frames/'
v_paths = [frame_dir + path + 'frm_%06d.jpg' % (f + 1) for f in range(start, start + FRAMES)]
offsets = [8, 8 + 112, 30, 30 + 112] # center crop
voxel = clipread(v_paths, offsets, size=(128, 171), crop_size=(112, 112), mode='RGB')
return voxel, np.float32(cls), np.float32(vid)
def demo_test():
# Demo of testing on UCF101
# Each line in test/val file is in form: path start_frame class video
with open('./list/c3d_demo01.txt', 'r') as f:
lines = f.read().split('\n')
tst_files = [line for line in lines if len(line) > 0]
weights = sio.loadmat('pretrained/c3d_ucf101_tf.mat', squeeze_me=True)['weights']
# Define placeholders
x = tf.placeholder(tf.float32, shape=(None, FRAMES, CROP_SIZE, CROP_SIZE, CHANNELS), name='input_x')
y = tf.placeholder(tf.int64, None, name='input_y')
training = tf.placeholder(tf.bool, name='training')
# Define the C3D model for UCF101
# inputs = x - tf.constant([90.2, 97.6, 101.4], dtype=tf.float32, shape=[1, 1, 1, 1, 3])
inputs = x - tf.constant([96.6], dtype=tf.float32, shape=[1, 1, 1, 1, 1])
logits = c3d_ucf101(inputs=inputs, training=training, weights=weights)
correct_opt = tf.equal(tf.argmax(logits, 1), y, name='correct')
acc_opt = tf.reduce_mean(tf.cast(correct_opt, tf.float32), name='accuracy')
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
with tf.Session(config=config) as sess:
sess.run(tf.global_variables_initializer())
n_sample = len(tst_files)
for epoch in range(1):
batch_x = np.zeros(shape=(BATCH_SIZE, FRAMES, CROP_SIZE, CROP_SIZE, CHANNELS), dtype=np.float32)
batch_y = np.zeros(shape=BATCH_SIZE, dtype=np.float32)
accuracy = []
scores = []
clss = []
vids = []
bidx = 0
for idx, tst_file in enumerate(tst_files):
voxel, cls, vid = read_test(tst_file)
batch_x[bidx] = voxel
batch_y[bidx] = cls
clss.append(cls)
vids.append(vid)
bidx += 1
if (idx + 1) % BATCH_SIZE == 0 or (idx + 1) == n_sample:
feeds = {x: batch_x[:bidx], y: batch_y[:bidx], training: False}
acc, score = sess.run([acc_opt, logits], feed_dict=feeds)
scores.append(score)
accuracy.append(acc * bidx)
print '%05d/%05d' % (idx, n_sample), 'acc: %.2f' % acc, ctime()
# reset batch
bidx = 0
print 'Acc: ', np.sum(accuracy) / n_sample
mat = dict()
mat['scores'] = np.vstack(scores).transpose()
mat['cls'] = np.array(clss)
mat['vid'] = np.array(vids)
sio.savemat('./c3d_demo_01.mat', mat)
def demo_finetune():
# Demo of training on UCF101
with open('./list/c3d_train01.txt', 'r') as f:
lines = f.read().split('\n')
tr_files = [line for line in lines if len(line) > 0]
weights = sio.loadmat('pretrained/c3d_sports1m_tf.mat', squeeze_me=True)['weights']
# Define placeholders
x = tf.placeholder(tf.float32, shape=(None, FRAMES, CROP_SIZE, CROP_SIZE, CHANNELS), name='input_x')
y = tf.placeholder(tf.int64, None, name='input_y')
training = tf.placeholder(tf.bool, name='training')
# Define the C3D model for UCF101
inputs = x - tf.constant([96.6], dtype=tf.float32, shape=[1, 1, 1, 1, 1])
logits = c3d_ucf101_finetune(inputs=inputs, training=training, weights=weights)
labels = tf.one_hot(y, 101, name='labels')
# Some operations
correct_opt = tf.equal(tf.argmax(logits, 1), y, name='correct')
acc_opt = tf.reduce_mean(tf.cast(correct_opt, tf.float32), name='accuracy')
# Define training opt
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=labels, logits=logits), name='loss')
# Learning rate
global_step = tf.Variable(0, trainable=False)
learning_rate = tf.train.exponential_decay(learning_rate=0.001, global_step=global_step, decay_steps=1000,
decay_rate=0.96, staircase=True)
train_opt = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(loss)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
n_train = len(tr_files)
for epoch in range(30):
tr_files = shuffle(tr_files)
batch_x = np.zeros(shape=(BATCH_SIZE, FRAMES, CROP_SIZE, CROP_SIZE, CHANNELS), dtype=np.float32)
batch_y = np.zeros(shape=BATCH_SIZE, dtype=np.float32)
bidx = 0
for idx, tr_file in enumerate(tr_files):
voxel, cls = read_train(tr_file)
batch_x[bidx] = voxel
batch_y[bidx] = cls
bidx += 1
if (idx + 1) % BATCH_SIZE == 0 or (idx + 1) == n_train:
feeds = {x: batch_x[:bidx], y: batch_y[:bidx], training: True}
_, lss, acc = sess.run([train_opt, loss, acc_opt], feed_dict=feeds)
print '%04d/%04d/%04d, loss: %.3f, acc: %.2f' % (idx / BATCH_SIZE, idx, n_train, lss, acc), ctime()
# reset batch
bidx = 0
if __name__ == '__main__':
demo_test()
# demo_finetune()