-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtemporalnetfdd.py
More file actions
406 lines (357 loc) · 17.6 KB
/
temporalnetfdd.py
File metadata and controls
406 lines (357 loc) · 17.6 KB
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
from __future__ import print_function
from numpy.random import seed
seed(1)
import numpy as np
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot as plt
import os
from keras.models import Model, Sequential
from keras.layers import Input, Convolution2D, MaxPooling2D, Flatten, Activation, Dense, Dropout, ZeroPadding2D
from keras.optimizers import Adam
from keras.layers.normalization import BatchNormalization
from keras import backend as K
K.set_image_dim_ordering('th')
from sklearn.metrics import confusion_matrix, accuracy_score
import h5py
import scipy.io as sio
import cv2
import glob
import gc
from sklearn.model_selection import KFold
from keras.layers.advanced_activations import ELU
# CHANGE THESE VARIABLES
data_folder = '/ssd_drive/FDD_Fall_OF_D100/'
mean_file = '/ssd_drive/flow_mean.mat'
vgg_16_weights = 'weights.h5'
model_file = 'models/exp_'
weights_file = 'weights/exp_'
features_file = 'features_fdd.h5'
labels_file = 'labels_fdd.h5'
features_key = 'features'
labels_key = 'labels'
L = 10
num_features = 4096
batch_norm = True
learning_rate = 0.001
mini_batch_size = 0
weight_0 = 2
epochs = 3000
save_plots = True
save_features = False
# Name of the experiment
exp = 'lr{}_batchs{}_batchnorm{}_w0_{}'.format(learning_rate, mini_batch_size, batch_norm, weight_0)
def plot_training_info(case, metrics, save, history):
'''
Function to create plots for train and validation loss and accuracy
Input:
* case: name for the plot, an 'accuracy.png' or 'loss.png' will be concatenated after the name.
* metrics: list of metrics to store: 'loss' and/or 'accuracy'
* save: boolean to store the plots or only show them.
* history: History object returned by the Keras fit function.
'''
plt.ioff()
if 'accuracy' in metrics:
fig = plt.figure()
plt.plot(history['acc'])
plt.plot(history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper left')
if save == True:
plt.savefig(case + 'accuracy.png')
plt.gcf().clear()
else:
plt.show()
plt.close(fig)
# summarize history for loss
if 'loss' in metrics:
fig = plt.figure()
plt.plot(history['loss'])
plt.plot(history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
#plt.ylim(1e-3, 1e-2)
plt.yscale("log")
plt.legend(['train', 'val'], loc='upper left')
if save == True:
plt.savefig(case + 'loss.png')
plt.gcf().clear()
else:
plt.show()
plt.close(fig)
def generator(list1, lits2):
'''
Auxiliar generator: returns the ith element of both given list with each call to next()
'''
for x,y in zip(list1,lits2):
yield x, y
def saveFeatures(feature_extractor, features_file, labels_file, features_key, labels_key):
'''
Function to load the optical flow stacks, do a feed-forward through the feature extractor (VGG16) and
store the output feature vectors in the file 'features_file' and the labels in 'labels_file'.
Input:
* feature_extractor: model VGG16 until the fc6 layer.
* features_file: path to the hdf5 file where the extracted features are going to be stored
* labels_file: path to the hdf5 file where the labels of the features are going to be stored
* features_key: name of the key for the hdf5 file to store the features
* labels_key: name of the key for the hdf5 file to store the labels
'''
class0 = 'Falls'
class1 = 'NotFalls'
# Load the mean file to subtract to the images
d = sio.loadmat(mean_file)
flow_mean = d['image_mean']
# Fill the folders and classes arrays with all the paths to the data
folders, classes = [], []
fall_videos = [f for f in os.listdir(data_folder + class0) if os.path.isdir(os.path.join(data_folder + class0, f))]
fall_videos.sort()
for fall_video in fall_videos:
x_images = glob.glob(data_folder + class0 + '/' + fall_video + '/flow_x*.jpg')
if int(len(x_images)) >= 10:
folders.append(data_folder + class0 + '/' + fall_video)
classes.append(0)
not_fall_videos = [f for f in os.listdir(data_folder + class1) if os.path.isdir(os.path.join(data_folder + class1, f))]
not_fall_videos.sort()
for not_fall_video in not_fall_videos:
x_images = glob.glob(data_folder + class1 + '/' + not_fall_video + '/flow_x*.jpg')
if int(len(x_images)) >= 10:
folders.append(data_folder + class1 + '/' + not_fall_video)
classes.append(1)
# Total amount of stacks, with sliding window = num_images-L+1
nb_total_stacks = 0
for folder in folders:
x_images = glob.glob(folder + '/flow_x*.jpg')
nb_total_stacks += int(len(x_images))-L+1
# File to store the extracted features and datasets to store them
# IMPORTANT NOTE: 'w' mode totally erases previous data
h5features = h5py.File(features_file,'w')
h5labels = h5py.File(labels_file,'w')
dataset_features = h5features.create_dataset(features_key, shape=(nb_total_stacks, num_features), dtype='float64')
dataset_labels = h5labels.create_dataset(labels_key, shape=(nb_total_stacks, 1), dtype='float64')
cont = 0
for folder, label in zip(folders, classes):
x_images = glob.glob(folder + '/flow_x*.jpg')
x_images.sort()
y_images = glob.glob(folder + '/flow_y*.jpg')
y_images.sort()
nb_stacks = int(len(x_images))-L+1
# Here nb_stacks optical flow stacks will be stored
flow = np.zeros(shape=(224,224,2*L,nb_stacks), dtype=np.float64)
gen = generator(x_images,y_images)
for i in range(len(x_images)):
flow_x_file, flow_y_file = gen.next()
img_x = cv2.imread(flow_x_file, cv2.IMREAD_GRAYSCALE)
img_y = cv2.imread(flow_y_file, cv2.IMREAD_GRAYSCALE)
# Assign an image i to the jth stack in the kth position, but also in the j+1th stack in the k+1th position and so on (for sliding window)
for s in list(reversed(range(min(10,i+1)))):
if i-s < nb_stacks:
flow[:,:,2*s, i-s] = img_x
flow[:,:,2*s+1,i-s] = img_y
del img_x,img_y
gc.collect()
# Subtract mean
flow = flow - np.tile(flow_mean[...,np.newaxis], (1, 1, 1, flow.shape[3]))
# Transpose for channel ordering (Theano in this case)
flow = np.transpose(flow, (3, 2, 0, 1))
predictions = np.zeros((flow.shape[0], num_features), dtype=np.float64)
truth = np.zeros((flow.shape[0], 1), dtype=np.float64)
# Process each stack: do the feed-forward pass and store in the hdf5 file the output
for i in range(flow.shape[0]):
prediction = feature_extractor.predict(np.expand_dims(flow[i, ...],0))
predictions[i, ...] = prediction
truth[i] = label
dataset_features[cont:cont+flow.shape[0],:] = predictions
dataset_labels[cont:cont+flow.shape[0],:] = truth
cont += flow.shape[0]
h5features.close()
h5labels.close()
def main():
# =============================================================================================================
# VGG-16 ARCHITECTURE
# =============================================================================================================
model = Sequential()
model.add(ZeroPadding2D((1, 1), input_shape=(20, 224, 224)))
model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_2'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_2'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(Flatten())
model.add(Dense(4096, name='fc6', init='glorot_uniform'))
# =============================================================================================================
# WEIGHT INITIALIZATION
# =============================================================================================================
layerscaffe = ['conv1_1', 'conv1_2', 'conv2_1', 'conv2_2', 'conv3_1', 'conv3_2', 'conv3_3', 'conv4_1', 'conv4_2', 'conv4_3', 'conv5_1', 'conv5_2', 'conv5_3', 'fc6', 'fc7', 'fc8']
h5 = h5py.File(vgg_16_weights)
layer_dict = dict([(layer.name, layer) for layer in model.layers])
# Copy the weights stored in the 'vgg_16_weights' file to the feature extractor part of the VGG16
for layer in layerscaffe[:-3]:
w2, b2 = h5['data'][layer]['0'], h5['data'][layer]['1']
w2 = np.transpose(np.asarray(w2), (0,1,2,3))
w2 = w2[:, :, ::-1, ::-1]
b2 = np.asarray(b2)
layer_dict[layer].W.set_value(w2)
layer_dict[layer].b.set_value(b2)
# Copy the weights of the first fully-connected layer (fc6)
layer = layerscaffe[-3]
w2, b2 = h5['data'][layer]['0'], h5['data'][layer]['1']
w2 = np.transpose(np.asarray(w2), (1,0))
b2 = np.asarray(b2)
layer_dict[layer].W.set_value(w2)
layer_dict[layer].b.set_value(b2)
adam = Adam(lr=learning_rate, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0005)
model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['accuracy'])
# =============================================================================================================
# FEATURE EXTRACTION
# =============================================================================================================
if save_features:
saveFeatures(model, features_file, labels_file)
# =============================================================================================================
# TRAINING
# =============================================================================================================
do_training = True
compute_metrics = True
threshold = 0.5
if do_training:
h5features = h5py.File(features_file, 'r')
h5labels = h5py.File(labels_file, 'r')
# X_full will contain all the feature vectors extracted from optical flow images
X_full = h5features[features_key]
_y_full = np.asarray(h5labels[labels_key])
zeroes = np.asarray(np.where(_y_full==0)[0])
ones = np.asarray(np.where(_y_full==1)[0])
zeroes.sort()
ones.sort()
kf_falls = KFold(n_splits=5)
kf_falls.get_n_splits(X_full[zeroes, ...])
kf_nofalls = KFold(n_splits=5)
kf_nofalls.get_n_splits(X_full[ones, ...])
sensitivities = []
specificities = []
accuracies = []
for (train_index_falls, test_index_falls), (train_index_nofalls, test_index_nofalls) in zip(kf_falls.split(X_full[zeroes, ...]), kf_nofalls.split(X_full[ones, ...])):
train_index_falls = np.asarray(train_index_falls)
test_index_falls = np.asarray(test_index_falls)
train_index_nofalls = np.asarray(train_index_nofalls)
test_index_nofalls = np.asarray(test_index_nofalls)
train_index = np.concatenate((train_index_falls, train_index_nofalls), axis=0)
test_index = np.concatenate((test_index_falls, test_index_nofalls), axis=0)
train_index.sort()
test_index.sort()
X = np.concatenate((X_full[train_index_falls, ...], X_full[train_index_nofalls, ...]))
_y = np.concatenate((_y_full[train_index_falls, ...], _y_full[train_index_nofalls, ...]))
X2 = np.concatenate((X_full[test_index_falls, ...], X_full[test_index_nofalls, ...]))
_y2 = np.concatenate((_y_full[test_index_falls, ...], _y_full[test_index_nofalls, ...]))
# Balance the number of positive and negative samples so that there is the same amount of each of them
all0 = np.asarray(np.where(_y==0)[0])
all1 = np.asarray(np.where(_y==1)[0])
if len(all0) < len(all1):
all1 = np.random.choice(all1, len(all0), replace=False)
else:
all0 = np.random.choice(all0, len(all1), replace=False)
allin = np.concatenate((all0.flatten(),all1.flatten()))
allin.sort()
X = X[allin,...]
_y = _y[allin]
# ==================== CLASSIFIER ========================
extracted_features = Input(shape=(4096,), dtype='float32', name='input')
if batch_norm:
x = BatchNormalization(axis=-1, momentum=0.99, epsilon=0.001)(extracted_features)
x = ELU(alpha=1.0)(x)
else:
x = ELU(alpha=1.0)(extracted_features)
x = Dropout(0.9)(x)
x = Dense(4096, name='fc2', init='glorot_uniform')(x)
if batch_norm:
x = BatchNormalization(axis=-1, momentum=0.99, epsilon=0.001)(x)
x = Activation('relu')(x)
else:
x = ELU(alpha=1.0)(x)
x = Dropout(0.8)(x)
x = Dense(1, name='predictions', init='glorot_uniform')(x)
x = Activation('sigmoid')(x)
classifier = Model(input=extracted_features, output=x, name='classifier')
classifier.compile(optimizer=adam, loss='binary_crossentropy', metrics=['accuracy'])
# ==================== TRAINING =======================
# weighting of each class: only the fall class gets a different weight
class_weight = {0: weight_0, 1: 1}
# Batch training
if mini_batch_size == 0:
history = classifier.fit(X,_y, validation_data=(X2,_y2), batch_size=X.shape[0], nb_epoch=epochs, shuffle='batch', class_weight=class_weight)
else:
history = classifier.fit(X,_y, validation_data=(X2,_y2), batch_size=mini_batch_size, nb_epoch=epochs, shuffle='batch', class_weight=class_weight)
plot_training_info(exp, ['accuracy', 'loss'], save_plots, history.history)
# ==================== EVALUATION ========================
if compute_metrics:
predicted = classifier.predict(np.asarray(X2))
for i in range(len(predicted)):
if predicted[i] < threshold:
predicted[i] = 0
else:
predicted[i] = 1
# Array of predictions 0/1
predicted = np.asarray(predicted)
# Compute metrics and print them
cm = confusion_matrix(_y2, predicted,labels=[0,1])
tp = cm[0][0]
fn = cm[0][1]
fp = cm[1][0]
tn = cm[1][1]
tpr = tp/float(tp+fn)
fpr = fp/float(fp+tn)
fnr = fn/float(fn+tp)
tnr = tn/float(tn+fp)
precision = tp/float(tp+fp)
recall = tp/float(tp+fn)
specificity = tn/float(tn+fp)
f1 = 2*float(precision*recall)/float(precision+recall)
accuracy = accuracy_score(_y2, predicted)
print('TP: {}, TN: {}, FP: {}, FN: {}'.format(tp,tn,fp,fn))
print('TPR: {}, TNR: {}, FPR: {}, FNR: {}'.format(tpr,tnr,fpr,fnr))
print('Sensitivity/Recall: {}'.format(recall))
print('Specificity: {}'.format(specificity))
print('Precision: {}'.format(precision))
print('F1-measure: {}'.format(f1))
print('Accuracy: {}'.format(accuracy))
# Store the metrics for this epoch
sensitivities.append(tp/float(tp+fn))
specificities.append(tn/float(tn+fp))
accuracies.append(accuracy)
print('5-FOLD CROSS-VALIDATION RESULTS ===================')
print("Sensitivity: %.2f%% (+/- %.2f%%)" % (np.mean(sensitivities), np.std(sensitivities)))
print("Specificity: %.2f%% (+/- %.2f%%)" % (np.mean(specificities), np.std(specificities)))
print("Accuracy: %.2f%% (+/- %.2f%%)" % (np.mean(accuracies), np.std(accuracies)))
if __name__ == '__main__':
if not os.path.exists('models'):
os.makedirs('models')
if not os.path.exists('weights'):
os.makedirs('weights')
main()