-
Notifications
You must be signed in to change notification settings - Fork 18
/
04.evaluate_C3D.py
342 lines (303 loc) · 8.88 KB
/
04.evaluate_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
import numpy as np
import cv2
import time
import argparse
import pandas as pd
from keras.layers import Activation
from keras.regularizers import l2
from keras.models import Model
from keras.optimizers import SGD
from keras.utils import np_utils
from keras.layers.core import Dense, Dropout, Flatten
from keras.layers.convolutional import Conv3D, MaxPooling3D, ZeroPadding3D
from keras.layers import Input
# from schedules import onetenth_4_8_12
from sklearn.metrics import (
precision_score,
recall_score,
f1_score,
accuracy_score
)
import imageio.core.util
from facenet_pytorch import MTCNN
from PIL import Image
def ignore_warnings(*args, **kwargs):
pass
imageio.core.util._precision_warn = ignore_warnings
# Create face detector
mtcnn = MTCNN(
margin=40,
select_largest=False,
post_process=False,
device="cuda:0"
)
def conv3d_model(batch_size):
input_shape = (batch_size, 112, 112, 3)
weight_decay = 0.005
nb_classes = 2
inputs = Input(input_shape)
x = Conv3D(
64,
(3, 3, 3),
strides=(1, 1, 1),
padding="same",
activation="relu",
kernel_regularizer=l2(weight_decay),
)(inputs)
x = MaxPooling3D((2, 2, 1), strides=(2, 2, 1), padding="same")(x)
x = Conv3D(
128,
(3, 3, 3),
strides=(1, 1, 1),
padding="same",
activation="relu",
kernel_regularizer=l2(weight_decay),
)(x)
x = MaxPooling3D((2, 2, 2), strides=(2, 2, 2), padding="same")(x)
x = Conv3D(
128,
(3, 3, 3),
strides=(1, 1, 1),
padding="same",
activation="relu",
kernel_regularizer=l2(weight_decay),
)(x)
x = MaxPooling3D((2, 2, 2), strides=(2, 2, 2), padding="same")(x)
x = Conv3D(
256,
(3, 3, 3),
strides=(1, 1, 1),
padding="same",
activation="relu",
kernel_regularizer=l2(weight_decay),
)(x)
x = MaxPooling3D((2, 2, 2), strides=(2, 2, 2), padding="same")(x)
x = Conv3D(
256,
(3, 3, 3),
strides=(1, 1, 1),
padding="same",
activation="relu",
kernel_regularizer=l2(weight_decay),
)(x)
x = MaxPooling3D((2, 2, 2), strides=(2, 2, 2), padding="same")(x)
x = Flatten()(x)
x = Dense(2048, activation="relu", kernel_regularizer=l2(weight_decay))(x)
x = Dropout(0.5)(x)
x = Dense(2048, activation="relu", kernel_regularizer=l2(weight_decay))(x)
x = Dropout(0.5)(x)
x = Dense(nb_classes, kernel_regularizer=l2(weight_decay))(x)
x = Activation("softmax")(x)
model = Model(inputs, x)
return model
def c3d_model(batch_size):
""" Return the Keras model of the network
"""
main_input = Input(shape=(batch_size, 112, 112, 3), name="main_input")
# 1st layer group
x = Conv3D(
64,
kernel_size=(3, 3, 3),
activation="relu",
padding="same",
name="conv1",
strides=(1, 1, 1),
)(main_input)
x = MaxPooling3D(
pool_size=(1, 2, 2), strides=(1, 2, 2), padding="valid", name="pool1"
)(x)
# 2nd layer group
x = Conv3D(
128,
kernel_size=(3, 3, 3),
activation="relu",
padding="same",
name="conv2",
strides=(1, 1, 1),
)(x)
x = MaxPooling3D(
pool_size=(2, 2, 2), strides=(2, 2, 2), padding="valid", name="pool2"
)(x)
# 3rd layer group
x = Conv3D(
256,
kernel_size=(3, 3, 3),
activation="relu",
padding="same",
name="conv3a",
strides=(1, 1, 1),
)(x)
x = Conv3D(
256,
kernel_size=(3, 3, 3),
activation="relu",
padding="same",
name="conv3b",
strides=(1, 1, 1),
)(x)
x = MaxPooling3D(
pool_size=(2, 2, 2), strides=(2, 2, 2), padding="valid", name="pool3"
)(x)
# 4th layer group
x = Conv3D(
512,
kernel_size=(3, 3, 3),
activation="relu",
padding="same",
name="conv4a",
strides=(1, 1, 1),
)(x)
x = Conv3D(
512,
kernel_size=(3, 3, 3),
activation="relu",
padding="same",
name="conv4b",
strides=(1, 1, 1),
)(x)
x = MaxPooling3D(
pool_size=(2, 2, 2), strides=(2, 2, 2), padding="valid", name="pool4"
)(x)
# 5th layer group
x = Conv3D(
512,
kernel_size=(3, 3, 3),
activation="relu",
padding="same",
name="conv5a",
strides=(1, 1, 1),
)(x)
x = Conv3D(
512,
kernel_size=(3, 3, 3),
activation="relu",
padding="same",
name="conv5b",
strides=(1, 1, 1),
)(x)
x = ZeroPadding3D(padding=(0, 1, 1))(x)
x = MaxPooling3D(
pool_size=(2, 2, 2), strides=(2, 2, 2), padding="valid", name="pool5"
)(x)
x = Flatten()(x)
# FC layers group
x = Dense(2048, activation="relu", name="fc6")(x)
x = Dropout(0.5)(x)
x = Dense(2048, activation="relu", name="fc7")(x)
x = Dropout(0.5)(x)
predictions = Dense(2, activation="softmax", name="fc8")(x)
model = Model(inputs=main_input, outputs=predictions)
return model
def process_batch(video_paths, batch_size, num_frames=16):
num = len(video_paths)
batch = np.zeros((num, batch_size, 112, 112, 3), dtype="float32")
labels = np.zeros(num, dtype="int")
for i in range(num):
cap = cv2.VideoCapture(video_paths[i])
batches = []
counter = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame = Image.fromarray(frame)
face = mtcnn(frame)
try:
face = face.permute(1, 2, 0).float().numpy()
face = cv2.resize(face, (171, 128))
batch[i][counter][:][:][:] = face[8:120, 30:142, :]
batches.append(face)
except AttributeError:
print("Image Skipping")
if counter == batch_size-1:
break
counter += 1
cap.release()
# path = video_paths[i]
label = video_paths[i].split("/")[1]
label = int(label)
labels[i] = label
return batch, labels
def preprocess(inputs):
# inputs[..., 0] -= 99.9
# inputs[..., 1] -= 92.1
# inputs[..., 2] -= 82.6
# inputs[..., 0] /= 65.8
# inputs[..., 1] /= 62.3
# inputs[..., 2] /= 60.3
inputs /= 255.0
return inputs
def generator_test_batch(test_vid_list, batch_size, num_classes):
num = len(test_vid_list)
while True:
for i in range(int(num / batch_size)):
a = i * batch_size
b = (i + 1) * batch_size
y_test, y_labels = process_batch(test_vid_list[a:b], batch_size)
x = preprocess(y_test)
y = np_utils.to_categorical(np.array(y_labels), num_classes)
yield x, y
def main():
test_data = pd.read_csv("test_vids_c40.csv")
test_vids_list = test_data["vids_list"]
test_vids_list = np.array(test_vids_list)
true_labels = test_data["label"]
start = time.time()
ap = argparse.ArgumentParser()
ap.add_argument(
"-m", "--model_name", required=True, type=str,
help="Imagenet model to train", default="c3d"
)
ap.add_argument(
"-w",
"--load_weights_name",
required=True,
type=str,
help="Model wieghts name"
)
ap.add_argument(
"-b", "--batch_size", required=True, type=int,
help="Batch size", default=32
)
args = ap.parse_args()
# Model choice can be added more
if args.model_name == "c3d":
model = c3d_model(batch_size=args.batch_size)
else:
model = conv3d_model(batch_size=args.batch_size)
lr = 0.005
sgd = SGD(lr=lr, momentum=0.9, nesterov=True)
model.compile(
loss="categorical_crossentropy",
optimizer=sgd,
metrics=["accuracy"]
)
model.load_weights("results/weights_c3d.h5")
print("Weights loaded...")
num_classes = 2
batch_size = args.batch_size
probabs = model.predict_generator(
generator_test_batch(test_vids_list, batch_size, num_classes),
steps=len(test_vids_list) // batch_size,
verbose=1
)
np.save("C3D_probabs.npy", probabs)
y_pred = probabs.argmax(1)
np.save("C3D_preds.npy", y_pred)
print("Accuracy Score:", accuracy_score(true_labels, y_pred))
print("Precision Score", precision_score(true_labels, y_pred))
print("Recall Score:", recall_score(true_labels, y_pred))
print("F1 Score:", f1_score(true_labels, y_pred))
end = time.time()
dur = end - start
if dur < 60:
print("Execution Time:", dur, "seconds")
elif dur > 60 and dur < 3600:
dur = dur / 60
print("Execution Time:", dur, "minutes")
else:
dur = dur / (60 * 60)
print("Execution Time:", dur, "hours")
if __name__ == "__main__":
main()