-
Notifications
You must be signed in to change notification settings - Fork 9
/
ms_lstm.py
225 lines (170 loc) · 6.2 KB
/
ms_lstm.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
from models import MS_LSTM
import numpy as np
from theano.tensor import basic as tensor
from keras import backend as K
from keras.optimizers import SGD
import argparse
from keras.callbacks import ModelCheckpoint
from from feature_generator import CustomDataGenerator
parser = argparse.ArgumentParser(description='Train multi-stage LSTM (MS-LSTM)')
parser.add_argument(
"--action-aware",
metavar="<path>",
required=True,
type=str,
help="path to action-aware features")
parser.add_argument(
"--context-aware",
metavar="<path>",
required=True,
type=str,
help="path to context-aware features")
parser.add_argument(
"--classes",
type=int,
default=21,
help="number of classes in target dataset")
parser.add_argument(
"--loss-type",
choices=['crossentropy', 'hinge', 'totally_linear', 'partially_linear', 'exponential'],
default='crossentropy',
help="The loss function to train MS-LSTM")
parser.add_argument(
"--epochs",
default=128,
type=int,
elp="number of epochs")
parser.add_argument(
"--samples-per-epoch",
default=None,
type=int,
help="samples per epoch, default=all")
parser.add_argument(
"--save-model",
metavar="<prefix>",
default=None,
type=str,
help="save model at the end of each epoch")
parser.add_argument(
"--save-best-only",
default=False,
action='store_true',
help="only save model if it is the best so far")
parser.add_argument(
"--num-val-samples",
default=None,
type=int,
help="number of validation samples to use (default=all)")
parser.add_argument(
"--seed",
default=10,
type=int,
help="random seed")
parser.add_argument(
"--workers",
default=1,
type=int,
help="number of data preprocessing worker threads to launch")
parser.add_argument(
"--learning-rate",
default=0.001,
type=float,
help="initial/fixed learning rate")
parser.add_argument(
"--batch-size",
default=32,
type=int,
help="batch size")
parser.add_argument(
"--temporal-length",
default=50,
type=int,
help="number of frames representing each video")
parser.add_argument(
"--cell",
default=2048,
type=int,
help="number of hidden units in LSTM cells")
args = parser.parse_args()
def totally_linear(y_true, y_pred):
exp_loss = 0
T = 18
for t in range(1,21):
exp_loss = exp_loss + ((np.double(t)/(T)) * (K.categorical_crossentropy(y_pred, y_true)))
return exp_loss
def totally_expontial(y_true, y_pred):
exp_loss = 0
T = 18
for t in range(0, 21):
exp_loss = exp_loss + (np.exp((-1) * (T - t)) * K.categorical_crossentropy(y_pred, y_true))
return exp_loss
def partially_linear(true_dist, coding_dist):
loss = 0
TIME = 150
N_C = 21
batch = 32
for t in range (TIME):
term1 = true_dist[:,t] * tensor.log(coding_dist[:,t]+0.0000001)
term2 = (1-true_dist[:,t]) * tensor.log(1-coding_dist[:,t]+0.0000001)
loss = loss + np.double(1)/N_C * tensor.sum(term1+term2*np.double(t)/TIME, axis=1)
return -loss/batch
def categorical_hinge(y_true, y_pred):
pos = K.sum(y_true * y_pred, axis=-1)
neg = K.max((1. - y_true) * y_pred, axis=-1)
return K.maximum(0., neg - pos + 1.)
def categorical_crossentropy(y_true, y_pred):
return K.categorical_crossentropy(y_true, y_pred)
model = MS_LSTM(INPUT_LEN=args.temporal_length, INPUT_DIM=4096, OUTPUT_LEN=args.classes, cells=args.cell)
sgd = SGD(lr=args.learning_rate, momentum=0.9, nesterov=True)
if args.loss == "crossentropy": model.compile(
loss={'stage1':'categorical_crossentropy', 'stage2':'categorical_crossentropy'},
optimizer=sgd, metrics=['accuracy'])
elif args.loss == "hinge": model.compile(
loss={'stage1': categorical_hinge, 'stage2': categorical_hinge},
optimizer=sgd, metrics=['accuracy'])
elif args.loss == "totally_linear": model.compile(
loss={'stage1': totally_linear, 'stage2': totally_linear},
optimizer=sgd, metrics=['accuracy'])
elif args.loss == "partially_linear": model.compile(
loss={'stage1': partially_linear, 'stage2': partially_linear},
optimizer=sgd, metrics=['accuracy'])
elif args.loss == "exponential": model.compile(
loss={'stage1': totally_expontial, 'stage2': totally_expontial},
optimizer=sgd, metrics=['accuracy'])
else: model.compile(
loss={'stage1':'categorical_crossentropy', 'stage2':'categorical_crossentropy'},
optimizer=sgd, metrics=['accuracy'])
callbacks = []
if args.save_model:
callbacks.append(ModelCheckpoint(args.save_model,
verbose=0,
monitor='val_stage1_acc',
save_best_only=args.save_best_only))
train_generator_obj = CustomDataGenerator(
data_path_context=args.context_aware + '/train/',
data_path_action=args.action_aware + '/train/',
batch_size=args.batch_size,
temporal_length=args.temporal_length,
N_C=args.classes)
train_generator = train_generator_obj.generator()
validation_generator_obj = CustomDataGenerator(
data_path_context=args.context_aware + '/val/',
data_path_action=args.action_aware + '/val/',
batch_size=args.batch_size,
temporal_length=args.temporal_length,
N_C=args.classes)
validation_generator = validation_generator_obj.generator()
print("Assuming %d output classes" % train_generator.nb_class)
samples_per_epoch = args.samples_per_epoch or train_generator_obj.data_size // args.batch_size
samples_per_epoch -= (samples_per_epoch % args.batch_size)
num_val_samples = args.num_val_samples or validation_generator_obj.data_size // args.batch_size
print("Starting to train...")
model.fit_generator(train_generator,
samples_per_epoch=samples_per_epoch,
verbose=1,
callbacks=callbacks,
nb_epoch=args.epochs,
nb_worker=args.workers,
pickle_safe=False,
validation_data=validation_generator, nb_val_samples=num_val_samples)
model.save_weights('data/model_weights/ms_lstm_final.h5')