-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAtt.py
More file actions
executable file
·315 lines (279 loc) · 16.4 KB
/
Att.py
File metadata and controls
executable file
·315 lines (279 loc) · 16.4 KB
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
#-*- coding: utf-8 -*-
import tensorflow as tf
import pandas as pd
import numpy as np
import os, h5py, sys, argparse
import pdb
import time
import json
from collections import defaultdict
#from tensorflow.models.rnn import rnn, rnn_cell
#from keras.preprocessing import sequence
from cocoeval import COCOScorer
import unicodedata
from tensorflow.python.tools.inspect_checkpoint import print_tensors_in_checkpoint_file
from utils.model_ops import *
from utils.record_helper import read_and_decode
###### custom parameters #######
model_path = '/home/shenxu/V2S-tensorflow/models/att/'
###### custom parameters #######
class Video_Caption_Generator():
def __init__(self, dim_image, n_words, dim_hidden, batch_size, n_caption_steps, n_video_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_caption_steps = n_caption_steps
self.n_video_steps = n_video_steps
self.drop_out_rate = drop_out_rate
with tf.device("/cpu:0"):
self.Wemb = tf.Variable(tf.random_uniform([n_words, dim_hidden], -0.1, 0.1), name='Wemb')
self.lstm3 = tf.contrib.rnn.LSTMCell(self.dim_hidden, use_peepholes = True, state_is_tuple = True)
self.lstm3_dropout = tf.contrib.rnn.DropoutWrapper(self.lstm3,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_att_w = tf.Variable(tf.random_uniform([dim_hidden, 1], -0.1,0.1), name='embed_att_w')
self.embed_att_Wa = tf.Variable(tf.random_uniform([dim_hidden, dim_hidden], -0.1,0.1), name='embed_att_Wa')
self.embed_att_Ua = tf.Variable(tf.random_uniform([dim_hidden, dim_hidden],-0.1,0.1), name='embed_att_Ua')
self.embed_att_ba = tf.Variable( tf.zeros([dim_hidden]), name='embed_att_ba')
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')
self.embed_nn_Wp = tf.Variable(tf.random_uniform([3*dim_hidden, dim_hidden], -0.1,0.1), name='embed_nn_Wp')
self.embed_nn_bp = tf.Variable(tf.zeros([dim_hidden]), name='embed_nn_bp')
def build_model(self, video, video_mask, caption, caption_mask):
caption_mask = tf.cast(caption_mask, tf.float32)
video_mask = tf.cast(video_mask, tf.float32)
video_flat = tf.reshape(video, [-1, self.dim_image]) # (b x n) x d
image_emb = tf.nn.xw_plus_b( video_flat, self.encode_image_W, self.encode_image_b) # (b x n) x h
image_emb = tf.reshape(image_emb, [self.batch_size, self.n_video_steps, self.dim_hidden]) # b x n x h
image_emb = tf.transpose(image_emb, [1,0,2]) # n x b x h
c_init = tf.zeros([self.batch_size, self.dim_hidden])
m_init = tf.zeros([self.batch_size, self.dim_hidden])
state1 = (c_init, m_init) # b x s
h_prev = tf.zeros([self.batch_size, self.dim_hidden]) # b x h
loss_caption = 0.0
current_embed = tf.zeros([self.batch_size, self.dim_hidden]) # b x h
image_part = tf.reshape(image_emb, [-1, self.dim_hidden])
image_part = tf.matmul(image_part, self.embed_att_Ua) + self.embed_att_ba
image_part = tf.reshape(image_part, [self.n_video_steps, self.batch_size, self.dim_hidden])
with tf.variable_scope("model") as scope:
for i in range(self.n_caption_steps):
e = tf.tanh(tf.matmul(h_prev, self.embed_att_Wa) + image_part) # n x b x h
# e = tf.batch_matmul(e, brcst_w) # unnormalized relevance score
e = tf.reshape(e, [-1, self.dim_hidden])
e = tf.matmul(e, self.embed_att_w) # n x b
e = tf.reshape(e, [self.n_video_steps, self.batch_size])
# e = tf.reduce_sum(e,2) # n x b
e_hat_exp = tf.multiply(tf.transpose(video_mask), tf.exp(e)) # n x b
denomin = tf.reduce_sum(e_hat_exp,0) # b
denomin = denomin + tf.to_float(tf.equal(denomin, 0)) # regularize denominator
alphas = tf.tile(tf.expand_dims(tf.div(e_hat_exp,denomin),2),[1,1,self.dim_hidden]) # n x b x h # normalize to obtain alpha
attention_list = tf.multiply(alphas, image_emb) # n x b x h
atten = tf.reduce_sum(attention_list,0) # b x h # soft-attention weighted sum
# if i > 0: tf.get_variable_scope().reuse_variables()
if i > 0: scope.reuse_variables()
with tf.variable_scope("LSTM3"):
output1, state1 = self.lstm3_dropout(tf.concat([atten, current_embed], 1), state1 ) # b x h
output2 = tf.tanh(tf.nn.xw_plus_b(tf.concat([output1,atten,current_embed], 1), self.embed_nn_Wp, self.embed_nn_bp)) # b x h
h_prev = output1 # b x h
labels = tf.expand_dims(caption[:,i], 1) # b x 1
indices = tf.expand_dims(tf.range(0, self.batch_size, 1), 1) # b x 1
concated = tf.concat([indices, labels], 1) # b x 2
onehot_labels = tf.sparse_to_dense(concated, tf.stack([self.batch_size, self.n_words]), 1.0, 0.0) # b x w
with tf.device("/cpu:0"):
current_embed = tf.nn.embedding_lookup(self.Wemb, caption[:,i])
logit_words = tf.nn.xw_plus_b(output2, self.embed_word_W, self.embed_word_b) # b x w
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits = logit_words, labels = onehot_labels) # b x 1
cross_entropy = cross_entropy * caption_mask[:,i] # b x 1
loss_caption += tf.reduce_sum(cross_entropy) # 1
loss_caption = loss_caption / tf.reduce_sum(caption_mask)
loss = loss_caption
return loss
def build_generator(self, video, video_mask):
video_mask = tf.cast(video_mask, tf.float32)
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, [self.batch_size, self.n_video_steps, self.dim_hidden])
image_emb = tf.transpose(image_emb, [1,0,2])
c_init = tf.zeros([self.batch_size, self.dim_hidden])
m_init = tf.zeros([self.batch_size, self.dim_hidden])
state1 = (c_init, m_init) # b x s
h_prev = tf.zeros([self.batch_size, self.dim_hidden])
generated_words = []
current_embed = tf.zeros([self.batch_size, self.dim_hidden])
brcst_w = tf.tile(tf.expand_dims(self.embed_att_w, 0), [self.n_video_steps,1,1]) # n x h x 1
image_part = tf.reshape(image_emb, [-1, self.dim_hidden])
image_part = tf.matmul(image_part, self.embed_att_Ua) + self.embed_att_ba
image_part = tf.reshape(image_part, [self.n_video_steps, self.batch_size, self.dim_hidden])
with tf.variable_scope("model") as scope:
scope.reuse_variables()
for i in range(self.n_caption_steps):
e = tf.tanh(tf.matmul(h_prev, self.embed_att_Wa) + image_part) # n x b x h
# e = tf.batch_matmul(e, brcst_w)
e = tf.reshape(e, [-1, self.dim_hidden])
e = tf.matmul(e, self.embed_att_w) # n x b
e = tf.reshape(e, [self.n_video_steps, self.batch_size])
# e = tf.reduce_sum(e,2) # n x b
e_hat_exp = tf.multiply(tf.transpose(video_mask), tf.exp(e)) # n x b
denomin = tf.reduce_sum(e_hat_exp,0) # b
denomin = denomin + tf.to_float(tf.equal(denomin, 0))
alphas = tf.tile(tf.expand_dims(tf.div(e_hat_exp,denomin),2),[1,1,self.dim_hidden]) # n x b x h
attention_list = tf.multiply(alphas, image_emb) # n x b x h
atten = tf.reduce_sum(attention_list,0) # b x h
# if i > 0: tf.get_variable_scope().reuse_variables()
if i > 0: scope.reuse_variables()
with tf.variable_scope("LSTM3") as vs:
output1, state1 = self.lstm3( tf.concat([atten, current_embed], 1), state1 ) # b x h
lstm3_variables = [v for v in tf.global_variables() if v.name.startswith(vs.name)]
output2 = tf.tanh(tf.nn.xw_plus_b(tf.concat([output1,atten,current_embed], 1), self.embed_nn_Wp, self.embed_nn_bp)) # b x h
h_prev = output1
logit_words = tf.nn.xw_plus_b( output2, self.embed_word_W, self.embed_word_b) # b x w
max_prob_index = tf.argmax(logit_words, 1) # b
generated_words.append(max_prob_index) # b
with tf.device("/cpu:0"):
current_embed = tf.nn.embedding_lookup(self.Wemb, max_prob_index)
generated_words = tf.transpose(tf.stack(generated_words))
return generated_words, lstm3_variables
def train():
assert os.path.isdir(home_folder)
assert os.path.isfile(video_data_path_train)
assert os.path.isfile(video_data_path_val)
assert os.path.isdir(model_path)
print 'load meta data...'
wordtoix = np.load(home_folder + 'data0/wordtoix.npy').tolist()
print 'build model and session...'
# place shared parameters on the GPU
with tf.device("/gpu:0"):
model = Video_Caption_Generator(
dim_image=dim_image,
n_words=len(wordtoix),
dim_hidden=dim_hidden,
batch_size=batch_size,
n_caption_steps=n_caption_steps,
n_video_steps=n_video_steps,
drop_out_rate = 0.5,
bias_init_vector=None)
tStart_total = time.time()
n_epoch_steps = int(n_train_samples / batch_size)
n_steps = n_epochs * n_epoch_steps
# preprocessing on the CPU
with tf.device('/cpu:0'):
train_data, train_encode_data, _, _, train_video_label, train_caption_label, train_caption_id, train_caption_id_1, \
_, _, _, _ = read_and_decode(video_data_path_train)
val_data, val_encode_data, val_fname, val_title, val_video_label, val_caption_label, val_caption_id, val_caption_id_1, \
_, _, _, _ = read_and_decode(video_data_path_val)
# random batches
train_data, train_encode_data, train_video_label, train_caption_label, train_caption_id, train_caption_id_1 = \
tf.train.shuffle_batch([train_data, train_encode_data, train_video_label, train_caption_label, train_caption_id, train_caption_id_1],
batch_size=batch_size, num_threads=num_threads, capacity=prefetch, min_after_dequeue=min_queue_examples)
val_data, val_encode_data, val_video_label, val_fname, val_caption_id = \
tf.train.batch([val_data, val_encode_data, val_video_label, val_fname, val_caption_id], batch_size=batch_size, num_threads=1, capacity=2*batch_size)
# operation on the GPU
with tf.device("/gpu:0"):
tf_loss= model.build_model(train_data, train_video_label, train_caption_id, train_caption_label)
val_caption_tf, val_lstm3_variables_tf = model.build_generator(val_data, val_video_label)
sess = tf.InteractiveSession(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=False))
# check for model file
with tf.device("/cpu:0"):
saver = tf.train.Saver(max_to_keep=100)
ckpt = tf.train.get_checkpoint_state(model_path)
if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path):
print("Reading model parameters from %s" % ckpt.model_checkpoint_path)
saver.restore(sess, ckpt.model_checkpoint_path)
print_tensors_in_checkpoint_file(ckpt.model_checkpoint_path, "", True)
else:
print("Created model with fresh parameters.")
sess.run(tf.global_variables_initializer())
temp = set(tf.global_variables())
# train on the GPU
with tf.device("/gpu:0"):
# train_op = tf.train.AdamOptimizer(learning_rate).minimize(tf_loss)
optimizer = tf.train.AdamOptimizer(learning_rate)
gvs = optimizer.compute_gradients(tf_loss)
# when variable is not related to the loss, grad returned as None
clip_gvs = [(tf.clip_by_norm(grad, clip_norm), var) for grad, var in gvs if grad is not None]
train_op = optimizer.apply_gradients(gvs)
## initialize variables added for optimizer
sess.run(tf.variables_initializer(set(tf.global_variables()) - temp))
# initialize epoch variable in queue reader
sess.run(tf.local_variables_initializer())
loss_epoch = 0
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
# write graph architecture to file
summary_writer = tf.summary.FileWriter(model_path + 'summary', sess.graph)
for step in xrange(1, n_steps+1):
tStart = time.time()
_, loss_val = sess.run([train_op, tf_loss])
tStop = time.time()
print "step:", step, " Loss:", loss_val,
print "Time Cost:", round(tStop - tStart, 2), "s"
loss_epoch += loss_val
if step % n_epoch_steps == 0:
epoch = step / n_epoch_steps
loss_epoch /= n_epoch_steps
with tf.device("/cpu:0"):
saver.save(sess, os.path.join(model_path, 'model'), global_step=epoch)
print 'epoch:', epoch, 'loss:', loss_epoch
loss_epoch = 0
######### test sentence generation ##########
ixtoword = pd.Series(np.load(home_folder + 'data0/ixtoword.npy').tolist())
n_val_steps = int(n_val_samples / batch_size)
[pred_sent, gt_sent, id_list, gt_dict, pred_dict] = testing_all(sess, 1, ixtoword, val_caption_tf, val_fname)
for key in pred_dict.keys():
for ele in gt_dict[key]:
print "GT: " + ele['caption']
print "PD: " + pred_dict[key][0]['caption']
print '-------'
[pred_sent, gt_sent, id_list, gt_dict, pred_dict] = testing_all(sess, n_val_steps, ixtoword, val_caption_tf, val_fname)
scorer = COCOScorer()
total_score = scorer.score(gt_dict, pred_dict, id_list)
######### test video generation #############
mse = test_all_videos(sess, n_val_steps, val_data, val_video_tf)
sys.stdout.flush()
sys.stdout.flush()
coord.request_stop()
coord.join(threads)
print "Finally, saving the model ..."
with tf.device("/cpu:0"):
saver.save(sess, os.path.join(model_path, 'model'), global_step=n_epochs)
tStop_total = time.time()
print "Total Time Cost:", round(tStop_total - tStart_total,2), "s"
sess.close()
def test(model_path='models/model-900', video_feat_path=video_feat_path):
meta_data, train_data, val_data, test_data = get_video_data_jukin(video_data_path_train, video_data_path_val, video_data_path_test)
# test_data = val_data # to evaluate on testing data or validation data
ixtoword = pd.Series(np.load('./data0/ixtoword.npy').tolist())
model = Video_Caption_Generator(
dim_image=dim_image,
n_words=len(ixtoword),
dim_hidden=dim_hidden,
batch_size=batch_size,
n_video_steps=n_frame_step,
n_caption_steps=n_caption_steps,
drop_out_rate = 0,
bias_init_vector=None)
video_tf, video_mask_tf, caption_tf, lstm3_variables_tf = model.build_generator()
sess = tf.InteractiveSession(config=tf.ConfigProto(allow_soft_placement=True))
with tf.device("/cpu:0"):
saver = tf.train.Saver()
saver.restore(sess, model_path)
for ind, row in enumerate(lstm3_variables_tf):
if ind % 4 == 0:
assign_op = row.assign(tf.multiply(row,1-0.5))
sess.run(assign_op)
[pred_sent, gt_sent, id_list, gt_dict, pred_dict] = testing_all(sess, test_data, ixtoword,video_tf, video_mask_tf, caption_tf)
#np.savez('Att_result/'+model_path.split('/')[1],gt = gt_sent,pred=pred_sent)
scorer = COCOScorer()
total_score = scorer.score(gt_dict, pred_dict, id_list)
return total_score
if __name__ == '__main__':
args = parse_args()
if args.task == 'train':
train()
elif args.task == 'test':
with tf.device('/gpu:'+str(args.gpu_id)):
total_score = test(model_path = args.model)