-
Notifications
You must be signed in to change notification settings - Fork 2
/
recurrent_networks.py
393 lines (320 loc) · 15.9 KB
/
recurrent_networks.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
from keras.layers import Dense, Activation, Dropout, Bidirectional
from keras.layers.recurrent import LSTM
from keras.models import Sequential
from keras.applications.vgg16 import VGG16
from keras.optimizers import SGD
from keras.optimizers import Adam
from keras import backend as K
from keras.utils import np_utils
from sklearn.model_selection import train_test_split
from keras.callbacks import ModelCheckpoint
import os
import numpy as np
from ..library.utility.frame_extractors.vgg16_feature_extractor import extract_vgg16_features_live, \
scan_and_extract_vgg16_features
BATCH_SIZE = 4
NUM_EPOCHS = 50
VERBOSE = 1
HIDDEN_UNITS = 32
MAX_ALLOWED_FRAMES = 20
EMBEDDING_SIZE = 100
K.set_image_dim_ordering('tf')
def generate_batch(x_samples, y_samples):
num_batches = len(x_samples) // BATCH_SIZE
while True:
for batchIdx in range(0, num_batches):
start = batchIdx * BATCH_SIZE
end = (batchIdx + 1) * BATCH_SIZE
yield np.array(x_samples[start:end]), y_samples[start:end]
class VGG16BidirectionalLSTMVideoClassifier(object):
model_name = 'vgg16-bidirectional-lstm'
def __init__(self):
self.num_input_tokens = None
self.nb_classes = None
self.labels = None
self.labels_idx2word = None
self.model = None
self.vgg16_model = None
self.expected_frames = None
self.vgg16_include_top = True
self.config = None
def create_model(self):
model = Sequential()
model.add(Bidirectional(LSTM(units=HIDDEN_UNITS, return_sequences=True),
input_shape=(self.expected_frames, self.num_input_tokens)))
model.add(Bidirectional(LSTM(2)))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.7))
model.add(Dense(self.nb_classes))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
@staticmethod
def get_config_file_path(model_dir_path, vgg16_include_top=None):
if vgg16_include_top is None:
vgg16_include_top = True
if vgg16_include_top:
return model_dir_path + '/' + VGG16BidirectionalLSTMVideoClassifier.model_name + '-config.npy'
else:
return model_dir_path + '/' + VGG16BidirectionalLSTMVideoClassifier.model_name + '-hi-dim-config.npy'
@staticmethod
def get_weight_file_path(model_dir_path, vgg16_include_top=None):
if vgg16_include_top is None:
vgg16_include_top = True
if vgg16_include_top:
return model_dir_path + '/' + VGG16BidirectionalLSTMVideoClassifier.model_name + '-weights.h5'
else:
return model_dir_path + '/' + VGG16BidirectionalLSTMVideoClassifier.model_name + '-hi-dim-weights.h5'
@staticmethod
def get_architecture_file_path(model_dir_path, vgg16_include_top=None):
if vgg16_include_top is None:
vgg16_include_top = True
if vgg16_include_top:
return model_dir_path + '/' + VGG16BidirectionalLSTMVideoClassifier.model_name + '-architecture.json'
else:
return model_dir_path + '/' + VGG16BidirectionalLSTMVideoClassifier.model_name + '-hi-dim-architecture.json'
def load_model(self, config_file_path, weight_file_path):
if os.path.exists(config_file_path):
print('loading configuration from ', config_file_path)
else:
raise ValueError('cannot locate config file {}'.format(config_file_path))
config = np.load(config_file_path).item()
self.num_input_tokens = config['num_input_tokens']
self.nb_classes = config['nb_classes']
self.labels = config['labels']
self.expected_frames = config['expected_frames']
self.vgg16_include_top = config['vgg16_include_top']
self.labels_idx2word = dict([(idx, word) for word, idx in self.labels.items()])
self.config = config
self.model = self.create_model()
if os.path.exists(weight_file_path):
print('loading network weights from ', weight_file_path)
else:
raise ValueError('cannot local weight file {}'.format(weight_file_path))
self.model.load_weights(weight_file_path)
print('build vgg16 with pre-trained model')
vgg16_model = VGG16(include_top=self.vgg16_include_top, weights='imagenet')
vgg16_model.compile(optimizer=SGD(), loss='categorical_crossentropy', metrics=['accuracy'])
self.vgg16_model = vgg16_model
def predict(self, video_file_path):
x = extract_vgg16_features_live(self.vgg16_model, video_file_path)
frames = x.shape[0]
if frames > self.expected_frames:
x = x[0:self.expected_frames, :]
elif frames < self.expected_frames:
temp = np.zeros(shape=(self.expected_frames, x.shape[1]))
temp[0:frames, :] = x
x = temp
predicted_class = np.argmax(self.model.predict(np.array([x]))[0])
predicted_label = self.labels_idx2word[predicted_class]
return predicted_label
def fit(self, data_dir_path, model_dir_path, vgg16_include_top=True, data_set_name='UCF-101', test_size=0.3,
random_state=42):
self.vgg16_include_top = vgg16_include_top
config_file_path = self.get_config_file_path(model_dir_path, vgg16_include_top)
weight_file_path = self.get_weight_file_path(model_dir_path, vgg16_include_top)
architecture_file_path = self.get_architecture_file_path(model_dir_path, vgg16_include_top)
self.vgg16_model = VGG16(include_top=self.vgg16_include_top, weights='imagenet')
self.vgg16_model.compile(optimizer=Adam(lr=0.0001), loss='categorical_crossentropy', metrics=['accuracy'])
feature_dir_name = data_set_name + '-VGG16-Features'
if not vgg16_include_top:
feature_dir_name = data_set_name + '-VGG16-HiDimFeatures'
max_frames = 0
self.labels = dict()
x_samples, y_samples = scan_and_extract_vgg16_features(data_dir_path,
output_dir_path=feature_dir_name,
model=self.vgg16_model,
data_set_name=data_set_name)
self.num_input_tokens = x_samples[0].shape[1]
frames_list = []
for x in x_samples:
frames = x.shape[0]
frames_list.append(frames)
max_frames = max(frames, max_frames)
self.expected_frames = int(np.mean(frames_list))
print('max frames: ', max_frames)
print('expected frames: ', self.expected_frames)
for i in range(len(x_samples)):
x = x_samples[i]
frames = x.shape[0]
if frames > self.expected_frames:
x = x[0:self.expected_frames, :]
x_samples[i] = x
elif frames < self.expected_frames:
temp = np.zeros(shape=(self.expected_frames, x.shape[1]))
temp[0:frames, :] = x
x_samples[i] = temp
for y in y_samples:
if y not in self.labels:
self.labels[y] = len(self.labels)
print(self.labels)
for i in range(len(y_samples)):
y_samples[i] = self.labels[y_samples[i]]
self.nb_classes = len(self.labels)
y_samples = np_utils.to_categorical(y_samples, self.nb_classes)
config = dict()
config['labels'] = self.labels
config['nb_classes'] = self.nb_classes
config['num_input_tokens'] = self.num_input_tokens
config['expected_frames'] = self.expected_frames
config['vgg16_include_top'] = self.vgg16_include_top
self.config = config
np.save(config_file_path, config)
model = self.create_model()
open(architecture_file_path, 'w').write(model.to_json())
Xtrain, Xtest, Ytrain, Ytest = train_test_split(x_samples, y_samples, test_size=test_size,
random_state=random_state,shuffle=True)
train_gen = generate_batch(Xtrain, Ytrain)
test_gen = generate_batch(Xtest, Ytest)
train_num_batches = len(Xtrain) // BATCH_SIZE
test_num_batches = len(Xtest) // BATCH_SIZE
print("debug")
print(train_num_batches)
print(test_num_batches)
print("debug end")
checkpoint = ModelCheckpoint(filepath=weight_file_path, save_best_only=True)
history = model.fit_generator(generator=train_gen, steps_per_epoch=train_num_batches,
epochs=NUM_EPOCHS,
verbose=1, validation_data=test_gen, validation_steps=test_num_batches,
callbacks=[checkpoint])
model.save_weights(weight_file_path)
return history
class VGG16LSTMVideoClassifier(object):
model_name = 'vgg16-lstm'
def __init__(self):
self.num_input_tokens = None
self.nb_classes = None
self.labels = None
self.labels_idx2word = None
self.model = None
self.vgg16_model = None
self.expected_frames = None
self.vgg16_include_top = None
self.config = None
@staticmethod
def get_config_file_path(model_dir_path, vgg16_include_top=None):
if vgg16_include_top is None:
vgg16_include_top = True
if vgg16_include_top:
return model_dir_path + '/' + VGG16LSTMVideoClassifier.model_name + '-config.npy'
else:
return model_dir_path + '/' + VGG16LSTMVideoClassifier.model_name + '-hi-dim-config.npy'
@staticmethod
def get_weight_file_path(model_dir_path, vgg16_include_top=None):
if vgg16_include_top is None:
vgg16_include_top = True
if vgg16_include_top:
return model_dir_path + '/' + VGG16LSTMVideoClassifier.model_name + '-weights.h5'
else:
return model_dir_path + '/' + VGG16LSTMVideoClassifier.model_name + '-hi-dim-weights.h5'
@staticmethod
def get_architecture_file_path(model_dir_path, vgg16_include_top=None):
if vgg16_include_top is None:
vgg16_include_top = True
if vgg16_include_top:
return model_dir_path + '/' + VGG16LSTMVideoClassifier.model_name + '-architecture.json'
else:
return model_dir_path + '/' + VGG16LSTMVideoClassifier.model_name + '-hi-dim-architecture.json'
def create_model(self):
model = Sequential()
model.add(
LSTM(units=HIDDEN_UNITS, input_shape=(None, self.num_input_tokens), return_sequences=False, dropout=0.5))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(self.nb_classes))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
return model
def load_model(self, config_file_path, weight_file_path):
config = np.load(config_file_path).item()
self.num_input_tokens = config['num_input_tokens']
self.nb_classes = config['nb_classes']
self.labels = config['labels']
self.expected_frames = config['expected_frames']
self.vgg16_include_top = config['vgg16_include_top']
self.labels_idx2word = dict([(idx, word) for word, idx in self.labels.items()])
self.model = self.create_model()
self.model.load_weights(weight_file_path)
vgg16_model = VGG16(include_top=self.vgg16_include_top, weights='imagenet')
vgg16_model.compile(optimizer=Adam(), loss='categorical_crossentropy', metrics=['accuracy'])
self.vgg16_model = vgg16_model
def predict(self, video_file_path):
x = extract_vgg16_features_live(self.vgg16_model, video_file_path)
frames = x.shape[0]
if frames > self.expected_frames:
x = x[0:self.expected_frames, :]
elif frames < self.expected_frames:
temp = np.zeros(shape=(self.expected_frames, x.shape[1]))
temp[0:frames, :] = x
x = temp
predicted_class = np.argmax(self.model.predict(np.array([x]))[0])
predicted_label = self.labels_idx2word[predicted_class]
return predicted_label
def fit(self, data_dir_path, model_dir_path, vgg16_include_top=True, data_set_name='UCF-101', test_size=0.3, random_state=42):
self.vgg16_include_top = vgg16_include_top
config_file_path = self.get_config_file_path(model_dir_path, vgg16_include_top)
weight_file_path = self.get_weight_file_path(model_dir_path, vgg16_include_top)
architecture_file_path = self.get_architecture_file_path(model_dir_path, vgg16_include_top)
vgg16_model = VGG16(include_top=self.vgg16_include_top, weights='imagenet')
vgg16_model.compile(optimizer=SGD(), loss='categorical_crossentropy', metrics=['accuracy'])
self.vgg16_model = vgg16_model
feature_dir_name = data_set_name + '-VGG16-Features'
if not vgg16_include_top:
feature_dir_name = data_set_name + '-VGG16-HiDimFeatures'
max_frames = 0
self.labels = dict()
x_samples, y_samples = scan_and_extract_vgg16_features(data_dir_path,
output_dir_path=feature_dir_name,
model=self.vgg16_model,
data_set_name=data_set_name)
self.num_input_tokens = x_samples[0].shape[1]
frames_list = []
for x in x_samples:
frames = x.shape[0]
frames_list.append(frames)
max_frames = max(frames, max_frames)
self.expected_frames = int(np.mean(frames_list))
print('max frames: ', max_frames)
print('expected frames: ', self.expected_frames)
for i in range(len(x_samples)):
x = x_samples[i]
frames = x.shape[0]
print(x.shape)
if frames > self.expected_frames:
x = x[0:self.expected_frames, :]
x_samples[i] = x
elif frames < self.expected_frames:
temp = np.zeros(shape=(self.expected_frames, x.shape[1]))
temp[0:frames, :] = x
x_samples[i] = temp
for y in y_samples:
if y not in self.labels:
self.labels[y] = len(self.labels)
print(self.labels)
for i in range(len(y_samples)):
y_samples[i] = self.labels[y_samples[i]]
self.nb_classes = len(self.labels)
y_samples = np_utils.to_categorical(y_samples, self.nb_classes)
config = dict()
config['labels'] = self.labels
config['nb_classes'] = self.nb_classes
config['num_input_tokens'] = self.num_input_tokens
config['expected_frames'] = self.expected_frames
config['vgg16_include_top'] = self.vgg16_include_top
self.config = config
np.save(config_file_path, config)
model = self.create_model()
open(architecture_file_path, 'w').write(model.to_json())
Xtrain, Xtest, Ytrain, Ytest = train_test_split(x_samples, y_samples, test_size=test_size,
random_state=random_state)
train_gen = generate_batch(Xtrain, Ytrain)
test_gen = generate_batch(Xtest, Ytest)
train_num_batches = len(Xtrain) // BATCH_SIZE
test_num_batches = len(Xtest) // BATCH_SIZE
checkpoint = ModelCheckpoint(filepath=weight_file_path, save_best_only=True)
history = model.fit_generator(generator=train_gen, steps_per_epoch=train_num_batches,
epochs=NUM_EPOCHS,
verbose=1, validation_data=test_gen, validation_steps=test_num_batches,
callbacks=[checkpoint])
model.save_weights(weight_file_path)
return history