forked from halwai/cnn-rnn-siamese-video-similarity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fintune.py
315 lines (257 loc) · 15.2 KB
/
fintune.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
#! /usr/bin/env python
import tensorflow as tf
import numpy as np
import re
import os
import time
import datetime
import gc
from helper import InputHelper, save_plot, compute_distance
from siamese_network import SiameseLSTM
import gzip
from random import random
from amos import Conv
# Parameters
# ==================================================
tf.flags.DEFINE_integer("embedding_dim", 1000, "Dimensionality of character embedding (default: 300)")
tf.flags.DEFINE_float("dropout_keep_prob", 0.5, "Dropout keep probability (default: 0.5)")
tf.flags.DEFINE_float("l2_reg_lambda", 0.01, "L2 regularizaion lambda (default: 0.0)")
tf.flags.DEFINE_string("training_file_path", "/home/tushar/Heavy_dataset/gta_data/final/", "training folder (default: /home/halwai/gta_data/final)")
tf.flags.DEFINE_string("training_files_path", "./annotation_files/", "training folder...")
tf.flags.DEFINE_integer("max_frames", 20, "Maximum Number of frame (default: 20)")
tf.flags.DEFINE_string("name", "result", "prefix names of the output files(default: result)")
# Training parameters
tf.flags.DEFINE_integer("batch_size", 4, "Batch Size (default: 10)")
tf.flags.DEFINE_integer("num_epochs", 15, "Number of training epochs (default: 200)")
tf.flags.DEFINE_integer("checkpoint_every", 1, "Save model after this many epochs (default: 100)")
tf.flags.DEFINE_integer("num_lstm_layers", 3, "Number of LSTM layers(default: 1)")
tf.flags.DEFINE_integer("hidden_dim", 80, "Number of LSTM layers(default: 2)")
tf.flags.DEFINE_string("loss", "contrastive", "Type of Loss functions:: contrastive/AAAI(default: contrastive)")
tf.flags.DEFINE_boolean("projection", False, "Project Conv Layers Output to a Lower Dimensional Embedding (Default: True)")
tf.flags.DEFINE_boolean("conv_net_training", False, "Training ConvNet (Default: False)")
tf.flags.DEFINE_float("lr", 0.000001, "learning-rate(default: 0.00001)")
# Misc Parameters
tf.flags.DEFINE_boolean("allow_soft_placement", False, "Allow device soft device placement")
tf.flags.DEFINE_boolean("log_device_placement", False, "Log placement of ops on devices")
tf.flags.DEFINE_integer("return_outputs", 1, "Outpust from LSTM, 0=>Last LSMT output, 2=> Cell-State Output. 1=> Hidden-State Output (default: 2)")
tf.flags.DEFINE_string("summaries_dir", "/home/tushar/codes/rnn-cnn/summaries/", "Summary storage")
#Conv Net Parameters
tf.flags.DEFINE_string("conv_layer", "pool6", "CNN features from AMOSNet(default: pool6)")
tf.flags.DEFINE_string("conv_layer_weight_pretrained_path", "/home/tushar/Heavy_dataset/hybrid/data.npy", "AMOSNet pre-trained weights path")
#tf.flags.DEFINE_string("train_file_positive", "./annotation_files2/positives-nospills-old+new-inters-train+val.txt", "Positive_training_file")
#tf.flags.DEFINE_string("train_file_negative", "./annotation_files2/negs-nospills.txt", "Negative_training_file")
tf.flags.DEFINE_string("train_file_positive", "./annotation_files2/positives-nospills-old+new-inters-train+val-sametraj.txt", "Positive_training_file")
tf.flags.DEFINE_string("train_file_negative", "./annotation_files2/negs-nospills-lessforsame.txt", "Negative_training_file")
FLAGS = tf.flags.FLAGS
FLAGS._parse_flags()
print("\nParameters:")
for attr, value in sorted(FLAGS.__flags.items()):
print("{}={}".format(attr.upper(), value))
print("")
if FLAGS.training_files_path==None:
print("Input Files List is empty. use --training_files_path argument.")
exit()
inpH = InputHelper()
train_set, dev_set, sum_no_of_batches,num_pos,num_neg = inpH.getDataSets(FLAGS.training_file_path,FLAGS.training_files_path, FLAGS.max_frames,64 ,50 , FLAGS.batch_size, FLAGS.train_file_positive,FLAGS.train_file_negative)
# Training
# ==================================================
print("starting graph def")
with tf.Graph().as_default():
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.7)
session_conf = tf.ConfigProto(
allow_soft_placement=FLAGS.allow_soft_placement,
log_device_placement=FLAGS.log_device_placement,
gpu_options=gpu_options,
)
sess = tf.Session(config=session_conf)
print("started session")
with sess.as_default():
convModel = Conv(
FLAGS.conv_layer,
FLAGS.conv_layer_weight_pretrained_path,
FLAGS.batch_size,
FLAGS.max_frames,
FLAGS.conv_net_training)
siameseModel = SiameseLSTM(
sequence_length=FLAGS.max_frames,
input_size=9216,
embedding_size=FLAGS.embedding_dim,
l2_reg_lambda=FLAGS.l2_reg_lambda,
batch_size=FLAGS.batch_size,
num_lstm_layers=FLAGS.num_lstm_layers,
hidden_unit_dim=FLAGS.hidden_dim,
loss=FLAGS.loss,
projection=FLAGS.projection,
return_outputs=FLAGS.return_outputs,
num_pos=num_pos,
num_neg=num_neg)
# Define Training procedure
global_step = tf.Variable(0, name="global_step", trainable=False)
learning_rate=tf.train.exponential_decay(1e-5, global_step, sum_no_of_batches*5, 0.95, staircase=False, name=None)
optimizer = tf.train.AdamOptimizer(FLAGS.lr)
optimizer_finetuning = tf.train.AdamOptimizer(FLAGS.lr, name="my_Adam")
print("initialized convmodel and siamesemodel object")
tv = tf.trainable_variables()
#regularization_cost = tf.reduce_sum([ tf.nn.l2_loss(v) for v in tv ])
regularization_cost = tf.reduce_sum([ tf.nn.l2_loss(v) for v in tv if 'bias' not in v.name ])
total_loss=siameseModel.loss+float(0.01)*regularization_cost
grads_and_vars=optimizer.compute_gradients(total_loss)
tr_op_set = optimizer.apply_gradients(grads_and_vars, global_step=global_step)
# Output directory for models and summaries
timestamp = str(int(time.time()))
out_dir = os.path.abspath(os.path.join("/home/tushar/codes/rnn-cnn/", "runs", FLAGS.name))
print("Writing to {}\n".format(out_dir))
# Checkpoint directory. Tensorflow assumes this directory already exists so we need to create it
checkpoint_dir = os.path.abspath(os.path.join(out_dir, "checkpoints"))
checkpoint_prefix = os.path.join(checkpoint_dir, "model")
#lstm_checkpoint_prefix = os.path.join(checkpoint_dir, "lstm_model")
if not os.path.exists(checkpoint_dir):
os.makedirs(checkpoint_dir)
lstm_savepath="/home/tushar/codes/rnn-cnn/data/lstm_outputs"
if not os.path.exists(lstm_savepath):
os.makedirs(lstm_savepath)
load_var_list = [v for v in tf.global_variables() if 'my_Adam' not in v.name]
tv_finetune = [v for v in tf.trainable_variables() if 'my_Adam' in v.name]
regularization_cost_finetune = tf.reduce_sum([ tf.nn.l2_loss(v) for v in tv_finetune if 'bias' not in v.name ])
total_loss_finetune=siameseModel.loss+float(0.01)*regularization_cost_finetune
grads_and_vars_finetune=optimizer.compute_gradients(total_loss_finetune)
tr_op_set_finetune = optimizer.apply_gradients(grads_and_vars_finetune, global_step=global_step)
saver = tf.train.Saver(tf.global_variables(), max_to_keep=2)
loader = tf.train.Saver(var_list=load_var_list)
loader_file = "/home/tushar/codes/rnn-cnn/runs/second_3_80_amos_inters_sametraj_noconvnettrain_reg0.1/checkpoints/model-6748"
#lstm_saver = tf.train.Saver([out1,out2], max_to_keep=2)
# Initialize all variables
sess.run(tf.global_variables_initializer())
loader.restore(sess, loader_file)
#print all trainable parameters
tvar = tv_finetune
for i, var in enumerate(tvar):
print("{}".format(var.name))
print("init all variables")
graph_def = tf.get_default_graph().as_graph_def()
graphpb_txt = str(graph_def)
with open(os.path.join(checkpoint_dir, "graphpb.txt"), 'w') as f:
f.write(graphpb_txt)
def train_step(x1_batch, x2_batch, y_batch, video_lengths):
#A single training step
[x1_batch] = sess.run([convModel.features], feed_dict={convModel.input_imgs: x1_batch})
[x2_batch] = sess.run([convModel.features], feed_dict={convModel.input_imgs: x2_batch})
feed_dict = {
siameseModel.input_x1: x1_batch,
siameseModel.input_x2: x2_batch,
siameseModel.input_y: y_batch,
siameseModel.dropout_keep_prob: FLAGS.dropout_keep_prob,
siameseModel.video_lengths: video_lengths,
}
out1, out2, _, step, loss, dist = sess.run([siameseModel.out1, siameseModel.out2, tr_op_set_finetune, global_step, siameseModel.loss, siameseModel.distance], feed_dict)
time_str = datetime.datetime.now().isoformat()
d=compute_distance(dist, FLAGS.loss)
correct = y_batch==d
#print(out1, out2)
#print(video_lengths)
#print("TRAIN {}: step {}, loss {:g}, acc {:g}".format(time_str, step, loss, correct))
print(y_batch, dist, d)
return np.sum(correct), loss
def dev_step(x1_batch, x2_batch, y_batch, video_lengths, dev_iter, epoch):
#A single training step
[x1_batch] = sess.run([convModel.features], feed_dict={convModel.input_imgs: x1_batch})
[x2_batch] = sess.run([convModel.features], feed_dict={convModel.input_imgs: x2_batch})
feed_dict = {
siameseModel.input_x1: x1_batch,
siameseModel.input_x2: x2_batch,
siameseModel.input_y: y_batch,
siameseModel.dropout_keep_prob: FLAGS.dropout_keep_prob,
siameseModel.video_lengths: video_lengths,
}
step, loss, dist, out1, out2 = sess.run([global_step, siameseModel.loss, siameseModel.distance, siameseModel.out1,siameseModel.out2], feed_dict)
#np.save(lstm_savepath+'/out1_'+str(dev_iter)+'_'+str(epoch),out1)
#np.save(lstm_savepath+'/out2_'+str(dev_iter)+'_'+str(epoch),out2)
#np.save(lstm_savepath+'/y_'+str(dev_iter)+'_'+str(epoch),y_batch)
time_str = datetime.datetime.now().isoformat()
d=compute_distance(dist, FLAGS.loss)
correct = y_batch==d
#print("DEV {}: step {}, loss {:g}, acc {:g}".format(time_str, step, loss, correct))
#print(y_batch, dist, d)
return np.sum(correct), loss, correct
# Generate batches
batches=inpH.batch_iter(
train_set[0], train_set[1], train_set[2], train_set[3], FLAGS.batch_size, FLAGS.num_epochs, convModel.spec, shuffle=True, is_train=False)
ptr=0
max_validation_correct=0.0
start_time = time.time()
train_accuracy, val_accuracy, pos_val_accuracy, neg_val_accuracy = [] , [], [], []
train_loss, val_loss = [], []
train_batch_loss_arr, val_batch_loss_arr = [], []
for nn in xrange(FLAGS.num_epochs):
current_step = tf.train.global_step(sess, global_step)
print("Epoch Number: {}".format(nn))
epoch_start_time = time.time()
sum_train_correct=0.0
train_epoch_loss=0.0
for kk in xrange(sum_no_of_batches):
x1_batch, x2_batch, y_batch, video_lengths = batches.next()
if len(y_batch)<1:
continue
train_batch_correct, train_batch_loss =train_step(x1_batch, x2_batch, y_batch, video_lengths)
#train_writer.add_summary(summary, current_step)
sum_train_correct = sum_train_correct + train_batch_correct
train_epoch_loss = train_epoch_loss + train_batch_loss* len(y_batch)
train_batch_loss_arr.append(train_batch_loss*len(y_batch))
print("train_loss ={}".format(train_epoch_loss/len(train_set[2])))
print("total_train_correct={}/total_train={}".format(sum_train_correct, len(train_set[2])))
train_accuracy.append(sum_train_correct*1.0/len(train_set[2]))
train_loss.append(train_epoch_loss/len(train_set[2]))
# Evaluate on Validataion Data for every epoch
sum_val_correct=0.0
sum_pos_correct=0.0
sum_neg_correct=0.0
sum_pos_samples=0.0
sum_neg_samples=0.0
val_epoch_loss=0.0
val_results = []
print("\nEvaluation:")
dev_batches = inpH.batch_iter(dev_set[0],dev_set[1],dev_set[2],dev_set[3], FLAGS.batch_size, 1, convModel.spec, shuffle=False , is_train=False)
dev_iter=0
for (x1_dev_b,x2_dev_b,y_dev_b, dev_video_lengths) in dev_batches:
if len(y_dev_b)<1:
continue
dev_iter += 1
batch_val_correct , val_batch_loss, batch_results = dev_step(x1_dev_b, x2_dev_b, y_dev_b, dev_video_lengths, dev_iter,nn)
pos_samples = np.sum(y_dev_b)
sum_pos_samples = sum_pos_samples + pos_samples
sum_neg_samples= sum_neg_samples + len(y_dev_b)-pos_samples
pos_correct_array = np.multiply(y_dev_b,batch_results)
pos_correct=np.sum(pos_correct_array)
neg_correct=batch_val_correct-pos_correct
sum_pos_correct = sum_pos_correct + pos_correct
sum_neg_correct = sum_neg_correct + neg_correct
val_results = np.concatenate([val_results, batch_results])
sum_val_correct = sum_val_correct + batch_val_correct
#val_writer.add_summary(summary, current_step)
val_epoch_loss = val_epoch_loss + val_batch_loss*len(y_dev_b)
val_batch_loss_arr.append(val_batch_loss*len(y_dev_b))
print("val_loss ={}".format(val_epoch_loss/len(dev_set[2])))
print("total_val_correct={}/total_val={}".format(sum_val_correct, len(dev_set[2])))
print("total_pos_correct={}/total_pos={}".format(sum_pos_correct,sum_pos_samples))
print("total_neg_correct={}/total_neg={}".format(sum_neg_correct,sum_neg_samples))
val_accuracy.append(sum_val_correct*1.0/len(dev_set[2]))
val_loss.append(val_epoch_loss/len(dev_set[2]))
pos_val_accuracy.append(sum_pos_correct*1.0/sum_pos_samples)
neg_val_accuracy.append(sum_neg_correct*1.0/sum_neg_samples)
# Update stored model
if current_step % (FLAGS.checkpoint_every) == 0:
#if sum_val_correct >= max_validation_correct:
max_validation_correct = sum_val_correct
saver.save(sess, checkpoint_prefix, global_step=current_step)
#lstm_saver.save(sess, lstm_checkpoint_prefix, global_step=current_step)
tf.train.write_graph(sess.graph.as_graph_def(), checkpoint_prefix, "graph"+str(nn)+".pb", as_text=False)
print("Saved model {} with checkpoint to {}".format(nn, checkpoint_prefix))
epoch_end_time = time.time()
empty=[]
print("Total time for {} th-epoch is {}\n".format(nn, epoch_end_time-epoch_start_time))
save_plot(train_accuracy, val_accuracy, pos_val_accuracy, neg_val_accuracy, 'epochs', 'accuracy', 'Accuracy vs epochs', [-0.1, nn+0.1, 0, 1.01], ['train','val','pos_val','neg_val' ],'./accuracy_'+str(FLAGS.name))
save_plot(train_loss, val_loss,empty,empty, 'epochs', 'loss', 'Loss vs epochs', [-0.1, nn+0.1, 0, np.max(train_loss)+0.2], ['train','val' ],'./loss_'+str(FLAGS.name))
save_plot(train_batch_loss_arr, val_batch_loss_arr,empty,empty, 'steps', 'loss', 'Loss vs steps', [-0.1, (nn+1)*sum_no_of_batches+0.1, 0, np.max(train_batch_loss_arr)+0.2], ['train','val' ],'./loss_batch_'+str(FLAGS.name))
end_time = time.time()
print("Total time for {} epochs is {}".format(FLAGS.num_epochs, end_time-start_time))
#"""