-
Notifications
You must be signed in to change notification settings - Fork 0
/
cnn_vis_sem_rnn_model.py
234 lines (198 loc) · 12 KB
/
cnn_vis_sem_rnn_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
import tensorflow as tf
from nets import inception
class Model(object):
def __init__(self, config, is_training=True, batch_size=26):
self.config = config
self.is_training = is_training
self.batch_size = batch_size
self.images_frontal = tf.placeholder(dtype=tf.float32, shape=[self.batch_size, config.image_size, config.image_size, 3])
self.images_lateral = tf.placeholder(dtype=tf.float32, shape=[self.batch_size, config.image_size, config.image_size, 3])
self.sentences = tf.placeholder(dtype=tf.int32, shape=[self.batch_size, config.max_sentence_num * config.max_sentence_length])
self.masks = tf.placeholder(dtype=tf.float32, shape=[self.batch_size, config.max_sentence_num * config.max_sentence_length])
self.build_cnn()
self.build_rnn()
self.build_metrics()
if is_training:
self.build_optimizer()
self.build_summary()
def build_cnn(self):
net_f, _ = inception.inception_v3(self.images_frontal, trainable=True, is_training=True, add_summaries=False, scope='FrontalInceptionV3')
net_l, _ = inception.inception_v3(self.images_lateral, trainable=True, is_training=True, add_summaries=False, scope='LateralInceptionV3')
self.visual_feats = tf.concat([net_f, net_l], axis=1) # [batch_size, 4096]
print('cnn built.')
def build_rnn(self):
with tf.variable_scope("word_embedding"):
word_embedding_matrix = tf.get_variable(
name='weights',
shape=[self.config.vocabulary_size, self.config.word_embedding_size],
trainable=True)
# 1. build rnn
WordRNN = tf.nn.rnn_cell.LSTMCell(
name='word_rnn',
num_units=self.config.rnn_units)
if self.is_training:
WordRNN = tf.nn.rnn_cell.DropoutWrapper(
WordRNN,
input_keep_prob=1.0 - self.config.rnn_dropout_rate,
output_keep_prob=1.0 - self.config.rnn_dropout_rate,
state_keep_prob=1.0 - self.config.rnn_dropout_rate)
predicts = []
cross_entropies = []
corrects = []
global last_sentence
# 2. generate first sentence
for sent_id in range(1):
# 2.1 init Word RNN
with tf.variable_scope('word_rnn_initialize_0'):
context = self.visual_feats
init_c = tf.layers.dense(context, units=self.config.rnn_units, activation=tf.tanh, use_bias=True, name='fc_c')
init_h = tf.layers.dense(context, units=self.config.rnn_units, activation=tf.tanh, use_bias=True, name='fc_h')
WordRNN_last_state = init_c, init_h
WordRNN_last_word = tf.zeros([self.batch_size], tf.int32)
# 2.2 generate word one by one
last_sentence = []
for word_id in range(self.config.max_sentence_length):
with tf.variable_scope('word_embedding'):
word_embedding = tf.nn.embedding_lookup(word_embedding_matrix, WordRNN_last_word)
with tf.variable_scope('word_rnn'):
WordRNN_output, WordRNN_state = WordRNN(word_embedding, WordRNN_last_state)
WordRNN_last_state = WordRNN_state
with tf.variable_scope('decode'):
WordRNN_output = tf.layers.dropout(WordRNN_output, rate=self.config.dropout_rate, training=self.is_training, name='drop_d')
logits = tf.layers.dense(WordRNN_output, units=self.config.vocabulary_size, activation=None, use_bias=True, name='fc_d')
predict = tf.argmax(logits, 1)
predicts.append(predict)
last_sentence.append(predict)
tf.get_variable_scope().reuse_variables()
if self.is_training:
WordRNN_last_word = self.sentences[:, sent_id*self.config.max_sentence_length + word_id]
else:
WordRNN_last_word = predict
# compute cross entropy loss
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=self.sentences[:, sent_id*self.config.max_sentence_length + word_id], logits=logits)
masked_cross_entropy = cross_entropy * self.masks[:, sent_id*self.config.max_sentence_length + word_id]
cross_entropies.append(masked_cross_entropy)
# compute acc
ground_truth = tf.cast(self.sentences[:, sent_id*self.config.max_sentence_length + word_id], tf.int64)
correct = tf.where(
tf.equal(predict, ground_truth),
tf.cast(self.masks[:, sent_id*self.config.max_sentence_length + word_id], tf.float32),
tf.cast(tf.zeros_like(predict), tf.float32)
)
corrects.append(correct)
# 3. generate next ot last sentence
for sent_id in range(1, self.config.max_sentence_num):
# 3.1 get sentence feature
with tf.variable_scope('word_embedding'):
if self.is_training:
word_embeddings = tf.nn.embedding_lookup(word_embedding_matrix, self.sentences[:, (sent_id-1)*self.config.max_sentence_length : sent_id*self.config.max_sentence_length])
else:
batch_sentences = tf.stack(last_sentence, axis=0) # last_sentence shape = [max_sentence_length, batch_size]
batch_sentences_tran = tf.transpose(batch_sentences)
word_embeddings = tf.nn.embedding_lookup(word_embedding_matrix, batch_sentences_tran)
self.semantic_features = self.sentence_encode(word_embeddings)
# 3.2 init Word RNN
with tf.variable_scope('word_rnn_initialize_%s' % sent_id, reuse=tf.AUTO_REUSE):
# vis_context = tf.layers.dense(self.visual_feats, units=1024, activation=tf.tanh, use_bias=True, name='fc_v')
context = tf.concat([self.visual_feats, self.semantic_features], axis=1)
context = tf.layers.dropout(context, rate=self.config.dropout_rate, training=self.is_training, name='drop_s')
init_c = tf.layers.dense(context, units=self.config.rnn_units, activation=tf.tanh, use_bias=True, name='fc_c')
init_h = tf.layers.dense(context, units=self.config.rnn_units, activation=tf.tanh, use_bias=True, name='fc_h')
WordRNN_last_state = init_c, init_h
WordRNN_last_word = tf.zeros([self.batch_size], tf.int32)
# 3.3 generate word one by one
last_sentence = []
for word_id in range(self.config.max_sentence_length):
with tf.variable_scope("word_embedding"):
word_embedding = tf.nn.embedding_lookup(word_embedding_matrix, WordRNN_last_word)
with tf.variable_scope('word_rnn'):
WordRNN_output, WordRNN_state = WordRNN(word_embedding, WordRNN_last_state)
WordRNN_last_state = WordRNN_state
with tf.variable_scope('decode'):
WordRNN_output = tf.layers.dropout(WordRNN_output, rate=self.config.dropout_rate, training=self.is_training, name='drop_d')
logits = tf.layers.dense(WordRNN_output, units=self.config.vocabulary_size, activation=None, use_bias=True, name='fc_d')
predict = tf.argmax(logits, 1)
predicts.append(predict)
last_sentence.append(predict)
tf.get_variable_scope().reuse_variables()
if self.is_training:
WordRNN_last_word = self.sentences[:, sent_id * self.config.max_sentence_length + word_id]
else:
WordRNN_last_word = predict
# compute cross entropy loss
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
labels=self.sentences[:, sent_id * self.config.max_sentence_length + word_id],
logits=logits)
masked_cross_entropy = cross_entropy * self.masks[:,
sent_id * self.config.max_sentence_length + word_id]
cross_entropies.append(masked_cross_entropy)
# compute acc
ground_truth = tf.cast(self.sentences[:, sent_id * self.config.max_sentence_length + word_id],
tf.int64)
correct = tf.where(
tf.equal(predict, ground_truth),
tf.cast(self.masks[:, sent_id * self.config.max_sentence_length + word_id], tf.float32),
tf.cast(tf.zeros_like(predict), tf.float32)
)
corrects.append(correct)
self.predicts = predicts
self.cross_entropies = cross_entropies
self.corrects = corrects
print('rnn built.')
def build_metrics(self):
corrects = tf.stack(self.corrects, axis=1)
self.accuracy = tf.reduce_sum(corrects) / tf.reduce_sum(self.masks)
cross_entropies = tf.stack(self.cross_entropies, axis=1)
self.cross_entropy_loss = tf.reduce_sum(cross_entropies) / tf.reduce_sum(self.masks)
self.reg_loss = tf.losses.get_regularization_loss()
self.loss = self.cross_entropy_loss + self.reg_loss
print('metrics built.')
def build_optimizer(self):
self.global_step = tf.Variable(0, name='global_step', trainable=False)
learning_rate = tf.constant(self.config.learning_rate)
def _learning_rate_decay_fn(learning_rate, global_step):
return tf.train.exponential_decay(
learning_rate=learning_rate,
global_step=global_step,
decay_steps=self.config.decay_iters,
decay_rate=self.config.decay_rate,
staircase=True
)
learning_rate_decay_fn = _learning_rate_decay_fn
with tf.variable_scope('optimizer', reuse=tf.AUTO_REUSE):
optimizer = tf.train.AdamOptimizer(
learning_rate=learning_rate,
beta1=0.9,
beta2=0.999,
epsilon=1e-8
)
self.step_op = tf.contrib.layers.optimize_loss(
loss=self.loss,
global_step=self.global_step,
learning_rate=learning_rate,
optimizer=optimizer,
clip_gradients=5.0,
learning_rate_decay_fn=learning_rate_decay_fn,
# variables=other_var_list
)
print('optimizer built.')
def build_summary(self):
with tf.name_scope("metrics"):
tf.summary.scalar('cross entropy loss', self.cross_entropy_loss)
tf.summary.scalar('reg loss', self.reg_loss)
tf.summary.scalar('acc', self.accuracy)
self.summary = tf.summary.merge_all()
print('summary built.')
def sentence_encode(self, word_embeddings):
with tf.variable_scope('sentence_encode', reuse=tf.AUTO_REUSE):
net = tf.layers.conv1d(word_embeddings, filters=1024, kernel_size=3, strides=1)
sent_feature1 = tf.layers.max_pooling1d(net, pool_size=self.config.max_sentence_length - 2, strides=100)
net = tf.layers.conv1d(net, filters=1024, kernel_size=3, strides=1)
sent_feature2 = tf.layers.max_pooling1d(net, pool_size=self.config.max_sentence_length - 2 - 4, strides=100)
net = tf.layers.conv1d(net, filters=1024, kernel_size=3, strides=1)
sent_feature3 = tf.layers.max_pooling1d(net, pool_size=self.config.max_sentence_length - 2 - 6, strides=100)
sent_feature1 = tf.reshape(sent_feature1, shape=[self.batch_size, 1024])
sent_feature2 = tf.reshape(sent_feature2, shape=[self.batch_size, 1024])
sent_feature3 = tf.reshape(sent_feature3, shape=[self.batch_size, 1024])
semantic_features = tf.concat([sent_feature1, sent_feature2, sent_feature3], axis=1)
return semantic_features