def testModelVariables(self): batch_size = 5 height, width = 224, 224 num_classes = 1000 with self.test_session(): inputs = random_ops.random_uniform((batch_size, height, width, 3)) vgg.vgg_19(inputs, num_classes) expected_names = [ 'vgg_19/conv1/conv1_1/weights', 'vgg_19/conv1/conv1_1/biases', 'vgg_19/conv1/conv1_2/weights', 'vgg_19/conv1/conv1_2/biases', 'vgg_19/conv2/conv2_1/weights', 'vgg_19/conv2/conv2_1/biases', 'vgg_19/conv2/conv2_2/weights', 'vgg_19/conv2/conv2_2/biases', 'vgg_19/conv3/conv3_1/weights', 'vgg_19/conv3/conv3_1/biases', 'vgg_19/conv3/conv3_2/weights', 'vgg_19/conv3/conv3_2/biases', 'vgg_19/conv3/conv3_3/weights', 'vgg_19/conv3/conv3_3/biases', 'vgg_19/conv3/conv3_4/weights', 'vgg_19/conv3/conv3_4/biases', 'vgg_19/conv4/conv4_1/weights', 'vgg_19/conv4/conv4_1/biases', 'vgg_19/conv4/conv4_2/weights', 'vgg_19/conv4/conv4_2/biases', 'vgg_19/conv4/conv4_3/weights', 'vgg_19/conv4/conv4_3/biases', 'vgg_19/conv4/conv4_4/weights', 'vgg_19/conv4/conv4_4/biases', 'vgg_19/conv5/conv5_1/weights', 'vgg_19/conv5/conv5_1/biases', 'vgg_19/conv5/conv5_2/weights', 'vgg_19/conv5/conv5_2/biases', 'vgg_19/conv5/conv5_3/weights', 'vgg_19/conv5/conv5_3/biases', 'vgg_19/conv5/conv5_4/weights', 'vgg_19/conv5/conv5_4/biases', 'vgg_19/fc6/weights', 'vgg_19/fc6/biases', 'vgg_19/fc7/weights', 'vgg_19/fc7/biases', 'vgg_19/fc8/weights', 'vgg_19/fc8/biases', ] model_variables = [ v.op.name for v in variables_lib.get_model_variables() ] self.assertSetEqual(set(model_variables), set(expected_names))
def testEndPoints(self): batch_size = 5 height, width = 224, 224 num_classes = 1000 for is_training in [True, False]: with ops.Graph().as_default(): inputs = random_ops.random_uniform( (batch_size, height, width, 3)) _, end_points = vgg.vgg_19(inputs, num_classes, is_training=is_training) expected_names = [ 'vgg_19/conv1/conv1_1', 'vgg_19/conv1/conv1_2', 'vgg_19/pool1', 'vgg_19/conv2/conv2_1', 'vgg_19/conv2/conv2_2', 'vgg_19/pool2', 'vgg_19/conv3/conv3_1', 'vgg_19/conv3/conv3_2', 'vgg_19/conv3/conv3_3', 'vgg_19/conv3/conv3_4', 'vgg_19/pool3', 'vgg_19/conv4/conv4_1', 'vgg_19/conv4/conv4_2', 'vgg_19/conv4/conv4_3', 'vgg_19/conv4/conv4_4', 'vgg_19/pool4', 'vgg_19/conv5/conv5_1', 'vgg_19/conv5/conv5_2', 'vgg_19/conv5/conv5_3', 'vgg_19/conv5/conv5_4', 'vgg_19/pool5', 'vgg_19/fc6', 'vgg_19/fc7', 'vgg_19/fc8' ] self.assertSetEqual(set(end_points.keys()), set(expected_names))
def testForward(self): batch_size = 1 height, width = 224, 224 with self.test_session() as sess: inputs = random_ops.random_uniform((batch_size, height, width, 3)) logits, _ = vgg.vgg_19(inputs) sess.run(variables.global_variables_initializer()) output = sess.run(logits) self.assertTrue(output.any())
def testFullyConvolutional(self): batch_size = 1 height, width = 256, 256 num_classes = 1000 with self.test_session(): inputs = random_ops.random_uniform((batch_size, height, width, 3)) logits, _ = vgg.vgg_19(inputs, num_classes, spatial_squeeze=False) self.assertEquals(logits.op.name, 'vgg_19/fc8/BiasAdd') self.assertListEqual(logits.get_shape().as_list(), [batch_size, 2, 2, num_classes])
def testBuild(self): batch_size = 5 height, width = 224, 224 num_classes = 1000 with self.test_session(): inputs = random_ops.random_uniform((batch_size, height, width, 3)) logits, _ = vgg.vgg_19(inputs, num_classes) self.assertEquals(logits.op.name, 'vgg_19/fc8/squeezed') self.assertListEqual(logits.get_shape().as_list(), [batch_size, num_classes])
def testEvaluation(self): batch_size = 2 height, width = 224, 224 num_classes = 1000 with self.test_session(): eval_inputs = tf.random_uniform((batch_size, height, width, 3)) logits, _ = vgg.vgg_19(eval_inputs, is_training=False) self.assertListEqual(logits.get_shape().as_list(), [batch_size, num_classes]) predictions = tf.argmax(logits, 1) self.assertListEqual(predictions.get_shape().as_list(), [batch_size])
def testTrainEvalWithReuse(self): train_batch_size = 2 eval_batch_size = 1 train_height, train_width = 224, 224 eval_height, eval_width = 256, 256 num_classes = 1000 with self.test_session(): train_inputs = random_ops.random_uniform( (train_batch_size, train_height, train_width, 3)) logits, _ = vgg.vgg_19(train_inputs) self.assertListEqual(logits.get_shape().as_list(), [train_batch_size, num_classes]) variable_scope.get_variable_scope().reuse_variables() eval_inputs = random_ops.random_uniform( (eval_batch_size, eval_height, eval_width, 3)) logits, _ = vgg.vgg_19(eval_inputs, is_training=False, spatial_squeeze=False) self.assertListEqual(logits.get_shape().as_list(), [eval_batch_size, 2, 2, num_classes]) logits = math_ops.reduce_mean(logits, [1, 2]) predictions = math_ops.argmax(logits, 1) self.assertEquals(predictions.get_shape().as_list(), [eval_batch_size])
def testEndPoints(self): batch_size = 5 height, width = 224, 224 num_classes = 1000 with self.test_session(): inputs = tf.random_uniform((batch_size, height, width, 3)) _, end_points = vgg.vgg_19(inputs, num_classes) expected_names = [ 'vgg_19/conv1/conv1_1', 'vgg_19/conv1/conv1_2', 'vgg_19/pool1', 'vgg_19/conv2/conv2_1', 'vgg_19/conv2/conv2_2', 'vgg_19/pool2', 'vgg_19/conv3/conv3_1', 'vgg_19/conv3/conv3_2', 'vgg_19/conv3/conv3_3', 'vgg_19/conv3/conv3_4', 'vgg_19/pool3', 'vgg_19/conv4/conv4_1', 'vgg_19/conv4/conv4_2', 'vgg_19/conv4/conv4_3', 'vgg_19/conv4/conv4_4', 'vgg_19/pool4', 'vgg_19/conv5/conv5_1', 'vgg_19/conv5/conv5_2', 'vgg_19/conv5/conv5_3', 'vgg_19/conv5/conv5_4', 'vgg_19/pool5', 'vgg_19/fc6', 'vgg_19/fc7', 'vgg_19/fc8' ] self.assertSetEqual(set(end_points.keys()), set(expected_names))
import tensorflow as tf import tensorflow.contrib.slim as slim from slim.nets.vgg import vgg_19, vgg_arg_scope from slim.nets.inception_resnet_v2 import inception_resnet_v2, inception_resnet_v2_arg_scope from slim.nets.resnet_v2 import resnet_v2_152, resnet_v2, resnet_arg_scope import numpy as np height = 224 width = 224 channels = 3 image_size = vgg_19.default_image_size X = tf.placeholder(tf.float32, shape=[None, image_size, image_size, channels]) with slim.arg_scope(vgg_arg_scope()): logits, end_points = vgg_19(X, num_classes=1000, is_training=False) print(end_points) # print(end_points.shape, end_points.type) predictions = end_points['vgg_19/fc8'] saver = tf.train.Saver() X_test = np.ones((1, image_size, image_size, channels)) # a fake image # Execute the graph with tf.Session() as sess: saver.restore(sess, '/home/duclong002/pretrained_model/vgg19/vgg_19.ckpt') predictions_val = predictions.eval(feed_dict={X: X_test}) tf.train.write_graph(sess.graph_def, '/home/duclong002/pretrained_model/vgg19/', 'vgg19.pbtxt', True)
import numpy as np import tensorflow as tf from tensorflow.contrib import slim from slim.nets import vgg import os checkpoints_dir = '/home/long/pretrained_model/vgg19/' summaries_dir = '/home/long/pretrained_model/summaries/vgg19' image_size = vgg.vgg_19.default_image_size with tf.Graph().as_default(): X = tf.placeholder(tf.float32, shape=[None, image_size, image_size, 3]) image = np.ones((1, image_size, image_size, 3), dtype=np.float32) logits, _ = vgg.vgg_19(image, 1000, is_training=False) init_fn = slim.assign_from_checkpoint_fn( os.path.join(checkpoints_dir, 'vgg_19.ckpt'), slim.get_model_variables('vgg_19')) probabilities = tf.nn.softmax(logits) with tf.Session() as sess: init_fn(sess) np_image, probabilities = sess.run([X, probabilities], feed_dict={X: image}) # with tf.name_scope('summaries'): # tf.summary.scalar(probabilities) # probabilities = probabilities[0, 0:] # sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x: x[1])] # names = imagenet.create_readable_names_for_imagenet_labels() # for i in range(5):