forked from jazzsaxmafia/video_to_sequence
-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
390 lines (295 loc) · 16.4 KB
/
model.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
#-*- coding: utf-8 -*-
import tensorflow as tf
import pandas as pd
from pandas import Series, DataFrame
import numpy as np
import os
import ipdb
import cv2
from tensorflow.python.ops import rnn_cell
from keras.preprocessing import sequence
class Video_Caption_Generator():
def __init__(self, dim_image, n_words, dim_hidden, batch_size, n_lstm_steps, drop_out_rate, bias_init_vector=None):
self.dim_image = dim_image
self.n_words = n_words
self.dim_hidden = dim_hidden
self.batch_size = batch_size
self.n_lstm_steps = n_lstm_steps
self.drop_out_rate = drop_out_rate
with tf.device("/gpu:2"):
self.Wemb = tf.Variable(tf.random_uniform([n_words, dim_hidden], -0.1, 0.1), name='Wemb')
# self.lstm1 = rnn_cell.BasicLSTMCell(dim_hidden)
# self.lstm2 = rnn_cell.BasicLSTMCell(dim_hidden)
self.lstm1 = rnn_cell.LSTMCell(self.dim_hidden,self.dim_hidden,use_peepholes = True)
self.lstm1_dropout = rnn_cell.DropoutWrapper(self.lstm1,output_keep_prob=1 - self.drop_out_rate)
self.lstm2 = rnn_cell.LSTMCell(self.dim_hidden,self.dim_hidden,use_peepholes = True)
self.lstm2_dropout = rnn_cell.DropoutWrapper(self.lstm2,output_keep_prob=1 - self.drop_out_rate)
self.encode_image_W = tf.Variable( tf.random_uniform([dim_image, dim_hidden], -0.1, 0.1), name='encode_image_W')
self.encode_image_b = tf.Variable( tf.zeros([dim_hidden]), name='encode_image_b')
self.embed_word_W = tf.Variable(tf.random_uniform([dim_hidden, n_words], -0.1,0.1), name='embed_word_W')
if bias_init_vector is not None:
self.embed_word_b = tf.Variable(bias_init_vector.astype(np.float32), name='embed_word_b')
else:
self.embed_word_b = tf.Variable(tf.zeros([n_words]), name='embed_word_b')
def build_model(self):
video = tf.placeholder(tf.float32, [self.batch_size, self.n_lstm_steps, self.dim_image])
video_mask = tf.placeholder(tf.float32, [self.batch_size, self.n_lstm_steps])
caption = tf.placeholder(tf.int32, [self.batch_size, self.n_lstm_steps])
caption_mask = tf.placeholder(tf.float32, [self.batch_size, self.n_lstm_steps])
video_flat = tf.reshape(video, [-1, self.dim_image])
image_emb = tf.nn.xw_plus_b( video_flat, self.encode_image_W, self.encode_image_b) # (batch_size*n_lstm_steps, dim_hidden)
image_emb = tf.reshape(image_emb, [self.batch_size, self.n_lstm_steps, self.dim_hidden])
state1 = tf.zeros([self.batch_size, self.lstm1.state_size])
state2 = tf.zeros([self.batch_size, self.lstm2.state_size])
padding = tf.zeros([self.batch_size, self.dim_hidden])
probs = []
loss = 0.0
for i in range(self.n_lstm_steps): ## Phase 1 => only read frames
if i > 0:
tf.get_variable_scope().reuse_variables()
with tf.variable_scope("LSTM1"):
#output1, state1 = self.lstm1( image_emb[:,i,:], state1 )
output1, state1 = self.lstm1_dropout( image_emb[:,i,:], state1 )
with tf.variable_scope("LSTM2"):
#output2, state2 = self.lstm2( tf.concat(1,[padding,output1]), state2 )
output2, state2 = self.lstm2_dropout( tf.concat(1,[padding,output1]), state2 )
# Each video might have different length. Need to mask those.
# But how? Padding with 0 would be enough?
# Therefore... TODO: for those short videos, keep the last LSTM hidden and output til the end.
for i in range(self.n_lstm_steps): ## Phase 2 => only generate captions
if i == 0:
current_embed = tf.zeros([self.batch_size, self.dim_hidden])
else:
with tf.device("/gpu:2"):
current_embed = tf.nn.embedding_lookup(self.Wemb, caption[:,i-1])
tf.get_variable_scope().reuse_variables()
with tf.variable_scope("LSTM1"):
#output1, state1 = self.lstm1( padding, state1 )
output1, state1 = self.lstm1_dropout( padding, state1 )
with tf.variable_scope("LSTM2"):
#output2, state2 = self.lstm2( tf.concat(1,[current_embed,output1]), state2 )
output2, state2 = self.lstm2_dropout( tf.concat(1,[current_embed,output1]), state2 )
labels = tf.expand_dims(caption[:,i], 1)
indices = tf.expand_dims(tf.range(0, self.batch_size, 1), 1)
concated = tf.concat(1, [indices, labels])
onehot_labels = tf.sparse_to_dense(concated, tf.pack([self.batch_size, self.n_words]), 1.0, 0.0)
logit_words = tf.nn.xw_plus_b(output2, self.embed_word_W, self.embed_word_b)
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logit_words, onehot_labels)
cross_entropy = cross_entropy * caption_mask[:,i]
probs.append(logit_words)
current_loss = tf.reduce_sum(cross_entropy)
loss += current_loss
loss = loss / tf.reduce_sum(caption_mask)
return loss, video, video_mask, caption, caption_mask, probs
def build_generator(self):
video = tf.placeholder(tf.float32, [1, self.n_lstm_steps, self.dim_image])
video_mask = tf.placeholder(tf.float32, [1, self.n_lstm_steps])
video_flat = tf.reshape(video, [-1, self.dim_image])
image_emb = tf.nn.xw_plus_b( video_flat, self.encode_image_W, self.encode_image_b)
image_emb = tf.reshape(image_emb, [1, self.n_lstm_steps, self.dim_hidden])
state1 = tf.zeros([1, self.lstm1.state_size])
state2 = tf.zeros([1, self.lstm2.state_size])
padding = tf.zeros([1, self.dim_hidden])
generated_words = []
probs = []
embeds = []
for i in range(self.n_lstm_steps):
if i > 0: tf.get_variable_scope().reuse_variables()
with tf.variable_scope("LSTM1"):
output1, state1 = self.lstm1( image_emb[:,i,:], state1 )
with tf.variable_scope("LSTM2"):
output2, state2 = self.lstm2( tf.concat(1,[padding,output1]), state2 )
for i in range(self.n_lstm_steps):
tf.get_variable_scope().reuse_variables()
if i == 0:
current_embed = tf.zeros([1, self.dim_hidden])
with tf.variable_scope("LSTM1"):
output1, state1 = self.lstm1( padding, state1 )
with tf.variable_scope("LSTM2"):
output2, state2 = self.lstm2( tf.concat(1,[current_embed,output1]), state2 )
logit_words = tf.nn.xw_plus_b( output2, self.embed_word_W, self.embed_word_b)
max_prob_index = tf.argmax(logit_words, 1)[0]
generated_words.append(max_prob_index)
probs.append(logit_words)
with tf.device("/gpu:2"):
current_embed = tf.nn.embedding_lookup(self.Wemb, max_prob_index)
current_embed = tf.expand_dims(current_embed, 0)
embeds.append(current_embed)
return video, video_mask, generated_words, probs, embeds
############### Global Parameters ###############
video_path = './youtube_videos'
video_data_path='./video_corpus.csv'
video_feat_path = './youtube_feats'
train_video_feat_path = '/home2/dataset/MSVD/MSVD_train_feats'
val_video_feat_path = '/home2/dataset/MSVD/MSVD_val_feats'
train_sents_gt_path = '/home2/dataset/MSVD/train_sents_gt.txt'
test_sents_gt_path = '/home2/dataset/MSVD/test_sents_gt.txt'
val_sents_gt_path = '/home2/dataset/MSVD/val_sents_gt.txt'
vgg16_path = './tfmodel/vgg16.tfmodel'
model_path = './MSVD_models1/'
############## Train Parameters #################
dim_image = 4096
dim_hidden= 256
n_frame_step = 80
n_epochs = 10000
batch_size = 100
learning_rate = 0.001
##################################################
def get_video_data(video_data_path, video_feat_path, train_ratio=0.9):
video_data = pd.read_csv(video_data_path, sep=',')
video_data = video_data[video_data['Language'] == 'English']
video_data['video_path'] = video_data.apply(lambda row: row['VideoID']+'_'+str(row['Start'])+'_'+str(row['End'])+'.avi.npy', axis=1)
video_data['video_path'] = video_data['video_path'].map(lambda x: os.path.join(video_feat_path, x))
video_data = video_data[video_data['video_path'].map(lambda x: os.path.exists( x ))]
video_data = video_data[video_data['Description'].map(lambda x: isinstance(x, str))]
unique_filenames = video_data['video_path'].unique()
train_len = int(len(unique_filenames)*train_ratio)
train_vids = unique_filenames[:train_len]
test_vids = unique_filenames[train_len:]
train_data = video_data[video_data['video_path'].map(lambda x: x in train_vids)]
test_data = video_data[video_data['video_path'].map(lambda x: x in test_vids)]
return train_data, test_data
def MSVD_get_video_data( sents_gt_path, video_feat_path ):
video_path = []
description = []
videoID = []
with open(train_sents_gt_path) as file :
for line in file :
id_sent = line.strip().split('\t')
description.append( ''.join(id_sent[-1:]) ) #list to str
videoID.append( id_sent[0] )
video_path.append( os.path.join( video_feat_path, id_sent[0]+'.avi.npy' ) )
video_data = DataFrame({'VideoID':videoID, 'Description':description, 'video_path':video_path})
return video_data
def preProBuildWordVocab(sentence_iterator, word_count_threshold=5): # borrowed this function from NeuralTalk
print 'preprocessing word counts and creating vocab based on word count threshold %d' % (word_count_threshold, )
word_counts = {}
nsents = 0
for sent in sentence_iterator:
nsents += 1
for w in sent.lower().split(' '):
word_counts[w] = word_counts.get(w, 0) + 1
vocab = [w for w in word_counts if word_counts[w] >= word_count_threshold]
print 'filtered words from %d to %d' % (len(word_counts), len(vocab))
ixtoword = {}
ixtoword[0] = '.' # period at the end of the sentence. make first dimension be end token
wordtoix = {}
wordtoix['#START#'] = 0 # make first vector be the start token
ix = 1
for w in vocab:
wordtoix[w] = ix
ixtoword[ix] = w
ix += 1
word_counts['.'] = nsents
bias_init_vector = np.array([1.0*word_counts[ixtoword[i]] for i in ixtoword])
bias_init_vector /= np.sum(bias_init_vector) # normalize to frequencies
bias_init_vector = np.log(bias_init_vector)
bias_init_vector -= np.max(bias_init_vector) # shift to nice numeric range
return wordtoix, ixtoword, bias_init_vector
def train():
# data w/o split
#train_data, _ = get_video_data(video_data_path, video_feat_path, train_ratio=0.9)
#print(train_data)
#print(type(train_data))
loss_vector = []
train_data = MSVD_get_video_data( train_sents_gt_path, train_video_feat_path )
captions = train_data['Description'].values
captions = map(lambda x: x.replace('.', ''), captions)
captions = map(lambda x: x.replace(',', ''), captions)
wordtoix, ixtoword, bias_init_vector = preProBuildWordVocab(captions, word_count_threshold=10)
np.save('./data/MSVD/ixtoword', ixtoword)
model = Video_Caption_Generator(
dim_image=dim_image,
n_words=len(wordtoix),
dim_hidden=dim_hidden,
batch_size=batch_size,
n_lstm_steps=n_frame_step,
drop_out_rate = 0.5,
bias_init_vector=bias_init_vector)
tf_loss, tf_video, tf_video_mask, tf_caption, tf_caption_mask, tf_probs = model.build_model()
sess = tf.InteractiveSession()
train_op = tf.train.AdamOptimizer(learning_rate).minimize(tf_loss)
saver = tf.train.Saver(max_to_keep=10)
tf.initialize_all_variables().run()
for epoch in range(n_epochs+1):
index = list(train_data.index)
np.random.shuffle(index)
train_data = train_data.ix[index]
current_train_data = train_data.groupby('video_path').apply(lambda x: x.irow(np.random.choice(len(x))))
current_train_data = current_train_data.reset_index(drop=True)
for start,end in zip(
range(0, len(current_train_data), batch_size),
range(batch_size, len(current_train_data), batch_size)):
current_batch = current_train_data[start:end]
current_videos = current_batch['video_path'].values
current_feats = np.zeros((batch_size, n_frame_step, dim_image))
current_feats_vals = map(lambda vid: np.load(vid), current_videos)
current_video_masks = np.zeros((batch_size, n_frame_step))
for ind,feat in enumerate(current_feats_vals):
current_feats[ind][:len(current_feats_vals[ind])] = feat
current_video_masks[ind][:len(current_feats_vals[ind])] = 1
current_captions = current_batch['Description'].values
current_captions = map(lambda x: x.replace('.', ''), current_captions)
current_captions = map(lambda x: x.replace(',', ''), current_captions)
current_caption_ind = map(lambda cap: [wordtoix[word] for word in cap.lower().split(' ') if word in wordtoix], current_captions)
current_captions_ = map( lambda sent: [ixtoword[ix] for ix in sent], current_caption_ind )
current_caption_matrix = sequence.pad_sequences(current_caption_ind, padding='post', maxlen=n_frame_step-1)
current_caption_matrix = np.hstack( [current_caption_matrix, np.zeros( [len(current_caption_matrix),1]) ] ).astype(int)
current_caption_masks = np.zeros((current_caption_matrix.shape[0], current_caption_matrix.shape[1]))
nonzeros = np.array( map(lambda x: (x != 0).sum()+1, current_caption_matrix ))
for ind, row in enumerate(current_caption_masks):
row[:nonzeros[ind]] = 1
probs_val = sess.run(tf_probs, feed_dict={
tf_video:current_feats,
tf_caption: current_caption_matrix
})
_, loss_val = sess.run(
[train_op, tf_loss],
feed_dict={
tf_video: current_feats,
tf_video_mask : current_video_masks,
tf_caption: current_caption_matrix,
tf_caption_mask: current_caption_masks
})
epoch_loss = loss_val
print loss_val
loss_vector.append( epoch_loss )
if np.mod(epoch, 100) == 0:
print "Epoch ", epoch, " is done. Saving the model ..."
saver.save(sess, os.path.join(model_path, 'model'), global_step=epoch)
np.save( os.path.join(model_path, 'loss-'+str(epoch)),loss_vector )
def test(model_path='models/model-900', video_feat_path=video_feat_path):
train_data, test_data = get_video_data(video_data_path, video_feat_path, train_ratio=0.9)
test_videos = test_data['video_path'].unique()
ixtoword = pd.Series(np.load('./data/ixtoword.npy').tolist())
model = Video_Caption_Generator(
dim_image=dim_image,
n_words=len(ixtoword),
dim_hidden=dim_hidden,
batch_size=batch_size,
n_lstm_steps=n_frame_step,
bias_init_vector=None)
video_tf, video_mask_tf, caption_tf, probs_tf, last_embed_tf = model.build_generator()
sess = tf.InteractiveSession()
saver = tf.train.Saver()
saver.restore(sess, model_path)
for video_feat_path in test_videos:
print video_feat_path
if video_feat.shape[1] == n_frame_step:
video_mask = np.ones((video_feat.shape[0], video_feat.shape[1]))
else:
shape_templete = np.zeros(shape=(1, n_frame_step, 4096), dtype=float )
shape_templete[:video_feat.shape[0],:video_feat.shape[1],:video_feat.shape[2]] = video_feat
video_feat = shape_templete
video_mask = np.ones((video_feat.shape[0], n_frame_step))
generated_word_index = sess.run(caption_tf, feed_dict={video_tf:video_feat, video_mask_tf:video_mask})
probs_val = sess.run(probs_tf, feed_dict={video_tf:video_feat})
embed_val = sess.run(last_embed_tf, feed_dict={video_tf:video_feat})
generated_words = ixtoword[generated_word_index]
punctuation = np.argmax(np.array(generated_words) == '.')+1
generated_words = generated_words[:punctuation]
generated_sentence = ' '.join(generated_words)
print generated_sentence
ipdb.set_trace()
ipdb.set_trace()
train()