-
Notifications
You must be signed in to change notification settings - Fork 3
/
train.py
44 lines (35 loc) · 1.3 KB
/
train.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
import numpy as np
import prepare_data
import models
import argparse
import sys
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-num_epochs', type=int, default=10)
parser.add_argument('-batch_size', type=int, default=100)
args = parser.parse_args()
print('Loading questions ...')
questions_train = prepare_data.get_questions_matrix('train')
questions_val = prepare_data.get_questions_matrix('val')
print('Loading answers ...')
answers_train = prepare_data.get_answers_matrix('train')
answers_val = prepare_data.get_answers_matrix('val')
print('Loading image features ...')
img_features_train = prepare_data.get_coco_features('train')
img_features_val = prepare_data.get_coco_features('val')
print('Creating model ...')
model = models.vis_lstm_2()
X_train = [img_features_train, questions_train, img_features_train]
X_val = [img_features_val, questions_val, img_features_val]
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(X_train,answers_train,
nb_epoch=args.num_epochs,
batch_size=args.batch_size,
validation_data=(X_val,answers_val),
verbose=1)
model.save_weights('weights/model_weights.h5')
with open('weights/model_architecture.json', 'w') as f:
f.write(model.to_json())
if __name__ == '__main__':main()