24
24
from __future__ import print_function
25
25
26
26
import tensorflow as tf
27
+ import tf_slim as slim
27
28
28
29
from core .ops import image_embedding
29
30
from core .ops import image_processing
@@ -52,7 +53,7 @@ def __init__(self, config, mode, train_inception=False):
52
53
self .train_inception = train_inception
53
54
54
55
# Reader for the input data.
55
- self .reader = tf .TFRecordReader ()
56
+ self .reader = tf .compat . v1 . TFRecordReader ()
56
57
57
58
# To match the "Show and Tell" paper we initialize all variables with a
58
59
# random uniform initializer.
@@ -129,10 +130,10 @@ def build_inputs(self):
129
130
"""
130
131
if self .mode == "inference" :
131
132
# In inference mode, images and inputs are fed via placeholders.
132
- image_feed = tf .placeholder (dtype = tf .string , shape = [], name = "image_feed" )
133
- input_feed = tf .placeholder (dtype = tf .int64 ,
134
- shape = [None ], # batch_size
135
- name = "input_feed" )
133
+ image_feed = tf .compat . v1 . placeholder (dtype = tf .string , shape = [], name = "image_feed" )
134
+ input_feed = tf .compat . v1 . placeholder (dtype = tf .int64 ,
135
+ shape = [None ], # batch_size
136
+ name = "input_feed" )
136
137
137
138
# Process image and insert batch dimensions.
138
139
images = tf .expand_dims (self .process_image (image_feed ), 0 )
@@ -192,12 +193,12 @@ def build_image_embeddings(self):
192
193
self .images ,
193
194
trainable = self .train_inception ,
194
195
is_training = self .is_training ())
195
- self .inception_variables = tf .get_collection (
196
- tf .GraphKeys .GLOBAL_VARIABLES , scope = "InceptionV3" )
196
+ self .inception_variables = tf .compat . v1 . get_collection (
197
+ tf .compat . v1 . GraphKeys .GLOBAL_VARIABLES , scope = "InceptionV3" )
197
198
198
199
# Map inception output into embedding space.
199
- with tf .variable_scope ("image_embedding" ) as scope :
200
- image_embeddings = tf . contrib .layers .fully_connected (
200
+ with tf .compat . v1 . variable_scope ("image_embedding" ) as scope :
201
+ image_embeddings = slim .layers .fully_connected (
201
202
inputs = inception_output ,
202
203
num_outputs = self .config .embedding_size ,
203
204
activation_fn = None ,
@@ -219,8 +220,8 @@ def build_seq_embeddings(self):
219
220
Outputs:
220
221
self.seq_embeddings
221
222
"""
222
- with tf .variable_scope ("seq_embedding" ), tf .device ("/cpu:0" ):
223
- embedding_map = tf .get_variable (
223
+ with tf .compat . v1 . variable_scope ("seq_embedding" ), tf .device ("/cpu:0" ):
224
+ embedding_map = tf .compat . v1 . get_variable (
224
225
name = "map" ,
225
226
shape = [self .config .vocab_size , self .config .embedding_size ],
226
227
initializer = self .initializer )
@@ -245,15 +246,15 @@ def build_model(self):
245
246
# This LSTM cell has biases and outputs tanh(new_c) * sigmoid(o), but the
246
247
# modified LSTM in the "Show and Tell" paper has no biases and outputs
247
248
# new_c * sigmoid(o).
248
- lstm_cell = tf .contrib . rnn .BasicLSTMCell (
249
+ lstm_cell = tf .compat . v1 . nn . rnn_cell .BasicLSTMCell (
249
250
num_units = self .config .num_lstm_units , state_is_tuple = True )
250
251
if self .mode == "train" :
251
252
lstm_cell = tf .contrib .rnn .DropoutWrapper (
252
253
lstm_cell ,
253
254
input_keep_prob = self .config .lstm_dropout_keep_prob ,
254
255
output_keep_prob = self .config .lstm_dropout_keep_prob )
255
256
256
- with tf .variable_scope ("lstm" , initializer = self .initializer ) as lstm_scope :
257
+ with tf .compat . v1 . variable_scope ("lstm" , initializer = self .initializer ) as lstm_scope :
257
258
# Feed the image embeddings to set the initial LSTM state.
258
259
zero_state = lstm_cell .zero_state (
259
260
batch_size = self .image_embeddings .get_shape ()[0 ], dtype = tf .float32 )
@@ -268,9 +269,9 @@ def build_model(self):
268
269
tf .concat (axis = 1 , values = initial_state , name = "initial_state" )
269
270
270
271
# Placeholder for feeding a batch of concatenated states.
271
- state_feed = tf .placeholder (dtype = tf .float32 ,
272
- shape = [None , sum (lstm_cell .state_size )],
273
- name = "state_feed" )
272
+ state_feed = tf .compat . v1 . placeholder (dtype = tf .float32 ,
273
+ shape = [None , sum (lstm_cell .state_size )],
274
+ name = "state_feed" )
274
275
state_tuple = tf .split (value = state_feed , num_or_size_splits = 2 , axis = 1 )
275
276
276
277
# Run a single LSTM step.
@@ -293,8 +294,8 @@ def build_model(self):
293
294
# Stack batches vertically.
294
295
lstm_outputs = tf .reshape (lstm_outputs , [- 1 , lstm_cell .output_size ])
295
296
296
- with tf .variable_scope ("logits" ) as logits_scope :
297
- logits = tf . contrib .layers .fully_connected (
297
+ with tf .compat . v1 . variable_scope ("logits" ) as logits_scope :
298
+ logits = slim .layers .fully_connected (
298
299
inputs = lstm_outputs ,
299
300
num_outputs = self .config .vocab_size ,
300
301
activation_fn = None ,
@@ -341,11 +342,11 @@ def restore_fn(sess):
341
342
342
343
def setup_global_step (self ):
343
344
"""Sets up the global step Tensor."""
344
- global_step = tf .Variable (
345
+ global_step = tf .compat . v1 . Variable (
345
346
initial_value = 0 ,
346
347
name = "global_step" ,
347
348
trainable = False ,
348
- collections = [tf .GraphKeys .GLOBAL_STEP , tf .GraphKeys .GLOBAL_VARIABLES ])
349
+ collections = [tf .compat . v1 . GraphKeys .GLOBAL_STEP , tf . compat . v1 .GraphKeys .GLOBAL_VARIABLES ])
349
350
350
351
self .global_step = global_step
351
352
0 commit comments