def build_image_embeddings(self, images): """Builds the image model subgraph and generates image embeddings. Inputs: images Outputs: self.image_embeddings """ images = self.distort_images(images, tf.compat.v1.train.get_or_create_global_step()) inception_output = image_embedding.inception_v3( images, trainable=self.train_inception, is_training=self.is_training(), add_summaries=False) self.inception_variables = tf.get_collection( tf.GraphKeys.GLOBAL_VARIABLES, scope="InceptionV3") # Map inception output into embedding space. with tf.variable_scope("image_embedding") as scope: image_embeddings = tf.contrib.layers.fully_connected( inputs=inception_output, num_outputs=self.config.embedding_size, activation_fn=None, weights_initializer=self.initializer, biases_initializer=None, scope=scope) # Save the embedding size in the graph. tf.constant(self.config.embedding_size, name="embedding_size") return image_embeddings
def build_image_embeddings(self): """Builds the image model subgraph and generates image embeddings. Inputs: self.images Outputs: self.image_embeddings """ inception_output = image_embedding.inception_v3( self.images, trainable=self.train_inception, is_training=self.is_training()) self.inception_variables = tf.get_collection( tf.GraphKeys.GLOBAL_VARIABLES, scope="InceptionV3") combined_vector = tf.concat([inception_output,self.objects_vector],1) # Map inception output into embedding space. with tf.variable_scope("image_embedding") as scope: image_embeddings = tf.contrib.layers.fully_connected( inputs=combined_vector, num_outputs=self.config.embedding_size, activation_fn=None, weights_initializer=self.initializer, biases_initializer=None, scope=scope) # Save the embedding size in the graph. tf.constant(self.config.embedding_size, name="embedding_size") self.image_embeddings = image_embeddings
def build_image_embeddings(self): """Builds the image model subgraph and generates image embeddings. Inputs: self.images Outputs: self.image_embeddings """ inception_output = image_embedding.inception_v3( self.images, trainable=self.train_inception, is_training=self.is_training()) self.inception_variables = tf.get_collection( tf.GraphKeys.GLOBAL_VARIABLES, scope="InceptionV3") # Map inception output into embedding space. with tf.variable_scope("image_embedding") as scope: image_embeddings = tf.contrib.layers.fully_connected( inputs=inception_output, num_outputs=self.config.embedding_size, activation_fn=None, weights_initializer=self.initializer, biases_initializer=None, scope=scope) # Save the embedding size in the graph. tf.constant(self.config.embedding_size, name="embedding_size") self.image_embeddings = image_embeddings
def getVcap(imagePath): cat = misc.imread(imagePath) cat = misc.imresize(cat, (299, 299)) batch_size = 1 height = 299 width = 299 num_channels = 3 batch = cat.reshape((1, 299, 299, 3)) assert batch.shape == (1, 299, 299, 3) images = tf.placeholder(tf.float32, [batch_size, height, width, num_channels]) model = inception_v3(images) saver = tf.train.Saver() with tf.Session() as sess: init = tf.initialize_all_variables() sess.run(init) print "variables initialized" saver.restore(sess, "VCAP/inception_v3.ckpt") print("Model restored.") feed_dict = {images: batch} result = sess.run(model, feed_dict=feed_dict) return result[0, :]
def test_input_fn(): import image_processing import urllib.request import tensorflow.contrib.data as tfdata import image_embedding if args.test_urls: jpegs = [ urllib.request.urlopen(url).read() for url in args.test_urls.split(',') ] with tf.Graph().as_default() as g: jpeg = tf.placeholder(dtype=tf.string) image = image_processing.process_image(jpeg, False) features = image_embedding.inception_v3([image], False, False) saver = tf.train.Saver( tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope="InceptionV3")) with tf.Session(graph=g) as sess: saver.restore(sess, args.cnn_model) features_list = [ sess.run(features, feed_dict={jpeg: j}) for j in jpegs ] dataset = tfdata.Dataset.from_tensor_slices(np.array(features_list)) return {'features': dataset.make_one_shot_iterator().get_next()}, None else: raise Exception('pass test_urls')
def create_captions_records(records_path, image_records, captions, vocabulary, cnn_model): dataset = contrib.data.TFRecordDataset([image_records]) dataset = dataset.map(parse_image) imgage_record = dataset.make_one_shot_iterator().get_next() img_jpeg = imgage_record['jpeg'] img_id = imgage_record['id'] print('extract features', records_path, '\n') jpg_data = tf.placeholder(dtype=tf.string) img = image_processing.process_image(jpg_data, 'train' in image_records) features = image_embedding.inception_v3([img], False, 'train' in image_records) saver = tf.train.Saver( tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope="InceptionV3")) with tf.Session() as sess: saver.restore(sess, cnn_model) with tf.python_io.TFRecordWriter(records_path) as writer: i = 1 while True: try: img_id_a, img_jpeg_a = sess.run([img_id, img_jpeg]) except tf.errors.OutOfRangeError as e: print('\nProcessed\n') break for caption in captions[img_id_a]: caption_ids = [ vocabulary.word_to_id(word) for word in caption ] img_a, features_a = sess.run( [img, features], feed_dict={jpg_data: img_jpeg_a}) context = tf.train.Features( feature={ "image_id": _int64_feature(img_id_a), "features": _floats_feature(features_a.astype(np.float)[0]), }) feature_lists = tf.train.FeatureLists( feature_list={ "caption": _bytes_feature_list([w.encode() for w in caption]), "caption_ids": _int64_feature_list(caption_ids) }) sequence_example = tf.train.SequenceExample( context=context, feature_lists=feature_lists) writer.write(sequence_example.SerializeToString()) progress(i, len(captions)) i += 1 print('\n')
def testTrainableTrueIsTrainingFalse(self): embeddings = image_embedding.inception_v3( self._images, trainable=True, is_training=False) self.assertEqual([self._batch_size, 2048], embeddings.get_shape().as_list()) self._verifyParameterCounts() self._assertCollectionSize(376, tf.GraphKeys.GLOBAL_VARIABLES) self._assertCollectionSize(188, tf.GraphKeys.TRAINABLE_VARIABLES) self._assertCollectionSize(0, tf.GraphKeys.UPDATE_OPS) self._assertCollectionSize(94, tf.GraphKeys.REGULARIZATION_LOSSES) self._assertCollectionSize(0, tf.GraphKeys.LOSSES) self._assertCollectionSize(23, tf.GraphKeys.SUMMARIES)
def testTrainableTrueIsTrainingFalse(self): embeddings = image_embedding.inception_v3( self._images, trainable=True, is_training=False) self.assertEqual([self._batch_size, 2048], embeddings.get_shape().as_list()) self._verifyParameterCounts() self._assertCollectionSize(376, tf.GraphKeys.GLOBAL_VARIABLES) self._assertCollectionSize(188, tf.GraphKeys.TRAINABLE_VARIABLES) self._assertCollectionSize(0, tf.GraphKeys.UPDATE_OPS) self._assertCollectionSize(94, tf.GraphKeys.REGULARIZATION_LOSSES) self._assertCollectionSize(0, tf.GraphKeys.LOSSES) self._assertCollectionSize(23, tf.GraphKeys.SUMMARIES)
from scipy import misc import image_embedding from image_embedding import inception_v3 #cat = utils.load_image("cat.jpg") cat = misc.imread("Data/dog.jpg") cat = misc.imresize(cat, (299, 299)) batch_size = 1 height = 299 width = 299 num_channels = 3 batch = cat.reshape((1, 299, 299, 3)) assert batch.shape == (1, 299, 299, 3) images = tf.placeholder(tf.float32, [batch_size, height, width, num_channels]) model = inception_v3(images) saver = tf.train.Saver() with tf.Session() as sess: init = tf.initialize_all_variables() sess.run(init) print "variables initialized" saver.restore(sess, "inception_v3.ckpt") print("Model restored.") feed_dict = {images: batch} result = sess.run(model, feed_dict=feed_dict) print("*******************\n") print(result.shape)