-
Notifications
You must be signed in to change notification settings - Fork 92
/
model.py
316 lines (235 loc) · 11.9 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
#-*- coding: utf-8 -*-
import math
import os
import tensorflow as tf
import numpy as np
import pandas as pd
import cPickle
from tensorflow.models.rnn import rnn_cell
import tensorflow.python.platform
from keras.preprocessing import sequence
from collections import Counter
from cnn_util import *
class Caption_Generator():
def init_weight(self, dim_in, dim_out, name=None, stddev=1.0):
return tf.Variable(tf.truncated_normal([dim_in, dim_out], stddev=stddev/math.sqrt(float(dim_in))), name=name)
def init_bias(self, dim_out, name=None):
return tf.Variable(tf.zeros([dim_out]), name=name)
def __init__(self, dim_image, dim_embed, dim_hidden, batch_size, n_lstm_steps, n_words, bias_init_vector=None):
self.dim_image = np.int(dim_image)
self.dim_embed = np.int(dim_embed)
self.dim_hidden = np.int(dim_hidden)
self.batch_size = np.int(batch_size)
self.n_lstm_steps = np.int(n_lstm_steps)
self.n_words = np.int(n_words)
with tf.device("/cpu:0"):
self.Wemb = tf.Variable(tf.random_uniform([n_words, dim_embed], -0.1, 0.1), name='Wemb')
self.bemb = self.init_bias(dim_embed, name='bemb')
self.lstm = rnn_cell.BasicLSTMCell(dim_hidden)
#self.encode_img_W = self.init_weight(dim_image, dim_hidden, name='encode_img_W')
self.encode_img_W = tf.Variable(tf.random_uniform([dim_image, dim_hidden], -0.1, 0.1), name='encode_img_W')
self.encode_img_b = self.init_bias(dim_hidden, name='encode_img_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 = self.init_bias(n_words, name='embed_word_b')
def build_model(self):
image = tf.placeholder(tf.float32, [self.batch_size, self.dim_image])
sentence = tf.placeholder(tf.int32, [self.batch_size, self.n_lstm_steps])
mask = tf.placeholder(tf.float32, [self.batch_size, self.n_lstm_steps])
image_emb = tf.matmul(image, self.encode_img_W) + self.encode_img_b # (batch_size, dim_hidden)
state = tf.zeros([self.batch_size, self.lstm.state_size])
loss = 0.0
with tf.variable_scope("RNN"):
for i in range(self.n_lstm_steps): # maxlen + 1
if i == 0:
current_emb = image_emb
else:
with tf.device("/cpu:0"):
current_emb = tf.nn.embedding_lookup(self.Wemb, sentence[:,i-1]) + self.bemb
if i > 0 : tf.get_variable_scope().reuse_variables()
output, state = self.lstm(current_emb, state) # (batch_size, dim_hidden)
if i > 0: # 이미지 다음 바로 나오는건 #START# 임. 이건 무시.
labels = tf.expand_dims(sentence[:, i], 1) # (batch_size)
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) # (batch_size, n_words)
logit_words = tf.matmul(output, self.embed_word_W) + self.embed_word_b # (batch_size, n_words)
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logit_words, onehot_labels)
cross_entropy = cross_entropy * mask[:,i]#tf.expand_dims(mask, 1)
current_loss = tf.reduce_sum(cross_entropy)
loss = loss + current_loss
loss = loss / tf.reduce_sum(mask[:,1:])
return loss, image, sentence, mask
def build_generator(self, maxlen):
image = tf.placeholder(tf.float32, [1, self.dim_image])
image_emb = tf.matmul(image, self.encode_img_W) + self.encode_img_b
state = tf.zeros([1, self.lstm.state_size])
#last_word = image_emb # 첫 단어 대신 이미지
generated_words = []
with tf.variable_scope("RNN"):
output, state = self.lstm(image_emb, state)
last_word = tf.nn.embedding_lookup(self.Wemb, [0]) + self.bemb
for i in range(maxlen):
tf.get_variable_scope().reuse_variables()
output, state = self.lstm(last_word, state)
logit_words = tf.matmul(output, self.embed_word_W) + self.embed_word_b
max_prob_word = tf.argmax(logit_words, 1)
with tf.device("/cpu:0"):
last_word = tf.nn.embedding_lookup(self.Wemb, max_prob_word)
last_word += self.bemb
generated_words.append(max_prob_word)
return image, generated_words
def get_caption_data(annotation_path, feat_path):
feats = np.load(feat_path)
annotations = pd.read_table(annotation_path, sep='\t', header=None, names=['image', 'caption'])
captions = annotations['caption'].values
return feats, captions
def preProBuildWordVocab(sentence_iterator, word_count_threshold=30): # 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
################### 학습 관련 Parameters #####################
dim_embed = 256
dim_hidden = 256
dim_image = 4096
batch_size = 128
#learning_rate = 0.001
n_epochs = 1000
###############################################################
#################### 잡다한 Parameters ########################
model_path = './models/tensorflow'
vgg_path = './data/vgg16.tfmodel'
data_path = './data'
feat_path = './data/feats.npy'
annotation_path = os.path.join(data_path, 'results_20130124.token')
################################################################
def train():
learning_rate = 0.001
momentum = 0.9
feats, captions = get_caption_data(annotation_path, feat_path)
wordtoix, ixtoword, bias_init_vector = preProBuildWordVocab(captions)
np.save('data/ixtoword', ixtoword)
index = np.arange(len(feats))
np.random.shuffle(index)
feats = feats[index]
captions = captions[index]
sess = tf.InteractiveSession()
n_words = len(wordtoix)
maxlen = np.max( map(lambda x: len(x.split(' ')), captions) )
caption_generator = Caption_Generator(
dim_image=dim_image,
dim_hidden=dim_hidden,
dim_embed=dim_embed,
batch_size=batch_size,
n_lstm_steps=maxlen+2,
n_words=n_words,
bias_init_vector=bias_init_vector)
loss, image, sentence, mask = caption_generator.build_model()
saver = tf.train.Saver(max_to_keep=50)
train_op = tf.train.AdamOptimizer(learning_rate).minimize(loss)
tf.initialize_all_variables().run()
for epoch in range(n_epochs):
#train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
for start, end in zip( \
range(0, len(feats), batch_size),
range(batch_size, len(feats), batch_size)
):
current_feats = feats[start:end]
current_captions = captions[start:end]
current_caption_ind = map(lambda cap: [wordtoix[word] for word in cap.lower().split(' ')[:-1] if word in wordtoix], current_captions)
current_caption_matrix = sequence.pad_sequences(current_caption_ind, padding='post', maxlen=maxlen+1)
current_caption_matrix = np.hstack( [np.full( (len(current_caption_matrix),1), 0), current_caption_matrix] ).astype(int)
current_mask_matrix = np.zeros((current_caption_matrix.shape[0], current_caption_matrix.shape[1]))
nonzeros = np.array( map(lambda x: (x != 0).sum()+2, current_caption_matrix ))
# +2 -> #START# and '.'
for ind, row in enumerate(current_mask_matrix):
row[:nonzeros[ind]] = 1
_, loss_value = sess.run([train_op, loss], feed_dict={
image: current_feats,
sentence : current_caption_matrix,
mask : current_mask_matrix
})
print "Current Cost: ", loss_value
print "Epoch ", epoch, " is done. Saving the model ... "
saver.save(sess, os.path.join(model_path, 'model'), global_step=epoch)
learning_rate *= 0.95
def test(test_feat='./guitar_player.npy', model_path='./models/tensorflow/model-1', maxlen=30): # Naive greedy search
ixtoword = np.load('data/ixtoword.npy').tolist()
n_words = len(ixtoword)
feat = [np.load(test_feat)]
sess = tf.InteractiveSession()
caption_generator = Caption_Generator(
dim_image=dim_image,
dim_hidden=dim_hidden,
dim_embed=dim_embed,
batch_size=batch_size,
n_lstm_steps=maxlen,
n_words=n_words)
image, generated_words = caption_generator.build_generator(maxlen=maxlen)
# 이 부분이 존나 중요함. 계속 caption_generator를 가져온 뒤 바로 restore를 했었는데,
# TensorFlow의 LSTM은 call을 한 뒤에 weight가 만들어지기 때문에 build_generator보다 뒤쪽에서 restore를 해야 함.
saver = tf.train.Saver()
saver.restore(sess, model_path)
generated_word_index= sess.run(generated_words, feed_dict={image:feat})
generated_word_index = np.hstack(generated_word_index)
generated_sentence = [ixtoword[x] for x in generated_word_index]
def read_image(path):
img = crop_image(path, target_height=224, target_width=224)
if img.shape[2] == 4:
img = img[:,:,:3]
img = img[None, ...]
return img
def test_tf(test_image_path=None, model_path='./models/model-72', maxlen=30):
with open(vgg_path) as f:
fileContent = f.read()
graph_def = tf.GraphDef()
graph_def.ParseFromString(fileContent)
images = tf.placeholder("float32", [1, 224, 224, 3])
tf.import_graph_def(graph_def, input_map={"images":images})
ixtoword = np.load('./data/ixtoword.npy').tolist()
n_words = len(ixtoword)
image_val = read_image(test_image_path)
sess = tf.InteractiveSession()
caption_generator = Caption_Generator(
dim_image=dim_image,
dim_hidden=dim_hidden,
dim_embed=dim_embed,
batch_size=batch_size,
n_lstm_steps=maxlen,
n_words=n_words)
graph = tf.get_default_graph()
fc7 = sess.run(graph.get_tensor_by_name("import/fc7_relu:0"), feed_dict={images:image_val})
fc7_tf, generated_words = caption_generator.build_generator(maxlen=maxlen)
saver = tf.train.Saver()
saver.restore(sess, model_path)
generated_word_index= sess.run(generated_words, feed_dict={fc7_tf:fc7})
generated_word_index = np.hstack(generated_word_index)
generated_words = [ixtoword[x] for x in generated_word_index]
punctuation = np.argmax(np.array(generated_words) == '.')+1
generated_words = generated_words[:punctuation]
generated_sentence = ' '.join(generated_words)
print generated_sentence