示例#1
0
    def __init__(self,
                 w_decay,
                 keep_p,
                 optimizer,
                 input_shape=[None, 28, 28],
                 input_reshape=[-1, 28, 28, 1]):
        self.img_size = inception.inception_v1.default_image_size
        self.features = tf.placeholder(tf.float32, input_shape)
        self.images = tf.reshape(self.features, input_reshape)
        self.images = tf.image.resize_images(self.images,
                                             [self.img_size, self.img_size])
        self.labels = tf.placeholder(tf.float32, [None, 10])
        self.labels = tf.cast(self.labels, tf.int32)

        with tf.variable_scope("Inception") as scope:
            self.train_digits, _ = inception.inception_v1(self.images,
                                                          num_classes=10,
                                                          is_training=True)
            scope.reuse_variables()
            self.pred_digits, _ = inception.inception_v1(self.images,
                                                         num_classes=10,
                                                         is_training=False)

        self.logits = self.pred_digits
        self.correct_prediction = tf.equal(tf.argmax(self.pred_digits, 1),
                                           tf.argmax(self.labels, 1))
        self.accuracy = tf.reduce_mean(
            tf.cast(self.correct_prediction, "float"))

        self.loss = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits_v2(
                labels=self.labels, logits=self.train_digits))
        self.train_op = optimizer.minimize(self.loss)
    def testNoBatchNormScaleByDefault(self):
        height, width = 224, 224
        num_classes = 1000
        inputs = tf.placeholder(tf.float32, (1, height, width, 3))
        with slim.arg_scope(inception.inception_v1_arg_scope()):
            inception.inception_v1(inputs, num_classes, is_training=False)

        self.assertEqual(tf.global_variables('.*/BatchNorm/gamma:0$'), [])
示例#3
0
  def testNoBatchNormScaleByDefault(self):
    height, width = 224, 224
    num_classes = 1000
    inputs = tf.placeholder(tf.float32, (1, height, width, 3))
    with slim.arg_scope(inception.inception_v1_arg_scope()):
      inception.inception_v1(inputs, num_classes, is_training=False)

    self.assertEqual(tf.global_variables('.*/BatchNorm/gamma:0$'), [])
示例#4
0
  def testBatchNormScale(self):
    height, width = 224, 224
    num_classes = 1000
    inputs = tf.placeholder(tf.float32, (1, height, width, 3))
    with slim.arg_scope(
        inception.inception_v1_arg_scope(batch_norm_scale=True)):
      inception.inception_v1(inputs, num_classes, is_training=False)

    gamma_names = set(
        v.op.name for v in tf.global_variables('.*/BatchNorm/gamma:0$'))
    self.assertGreater(len(gamma_names), 0)
    for v in tf.global_variables('.*/BatchNorm/moving_mean:0$'):
      self.assertIn(v.op.name[:-len('moving_mean')] + 'gamma', gamma_names)
示例#5
0
  def testBatchNormScale(self):
    height, width = 224, 224
    num_classes = 1000
    inputs = tf.placeholder(tf.float32, (1, height, width, 3))
    with slim.arg_scope(
        inception.inception_v1_arg_scope(batch_norm_scale=True)):
      inception.inception_v1(inputs, num_classes, is_training=False)

    gamma_names = set(
        v.op.name for v in tf.global_variables('.*/BatchNorm/gamma:0$'))
    self.assertGreater(len(gamma_names), 0)
    for v in tf.global_variables('.*/BatchNorm/moving_mean:0$'):
      self.assertIn(v.op.name[:-len('moving_mean')] + 'gamma', gamma_names)
示例#6
0
  def testTrainEvalWithReuse(self):
    train_batch_size = 5
    eval_batch_size = 2
    height, width = 224, 224
    num_classes = 1000

    train_inputs = tf.random_uniform((train_batch_size, height, width, 3))
    inception.inception_v1(train_inputs, num_classes)
    eval_inputs = tf.random_uniform((eval_batch_size, height, width, 3))
    logits, _ = inception.inception_v1(eval_inputs, num_classes, reuse=True)
    predictions = tf.argmax(logits, 1)

    with self.test_session() as sess:
      sess.run(tf.global_variables_initializer())
      output = sess.run(predictions)
      self.assertEquals(output.shape, (eval_batch_size,))
示例#7
0
 def __set_up_inception(self):
     self.num_clss = self.config["Model"]["InceptionV1"]["num_clss"]
     self.map_path = self.config["Model"]["InceptionV1"]["map_path"]
     self.X = tf.placeholder(tf.float32,
                             shape=[
                                 None, self.config["image"]["height"],
                                 self.config["image"]["width"],
                                 self.config["image"]["num_channels"]
                             ])
     with slim.arg_scope(inception.inception_v1_arg_scope()):
         self.logits, self.end_points = inception.inception_v1(
             self.X,
             num_classes=self.num_clss,
             is_training=False,
             m_w=self.config["mantissa_width"],
             e_w=self.config["exponent_width"],
             s_w=self.config["shared_exponent_width"],
             alter=self.config["quantize"],
             debug=self.config["debug_layer"])
     self.predictions = self.end_points["Predictions"]
     self.variables_to_restore = slim.get_variables_to_restore()
     self.init_assign_fn = slim.assign_from_checkpoint_fn(
         self.config["to_ckpt"] +
         self.config["Model"]["InceptionV1"]["ckpt_path"],
         self.variables_to_restore)
示例#8
0
  def testTrainEvalWithReuse(self):
    train_batch_size = 5
    eval_batch_size = 2
    height, width = 224, 224
    num_classes = 1000

    train_inputs = tf.random_uniform((train_batch_size, height, width, 3))
    inception.inception_v1(train_inputs, num_classes)
    eval_inputs = tf.random_uniform((eval_batch_size, height, width, 3))
    logits, _ = inception.inception_v1(eval_inputs, num_classes, reuse=True)
    predictions = tf.argmax(logits, 1)

    with self.test_session() as sess:
      sess.run(tf.global_variables_initializer())
      output = sess.run(predictions)
      self.assertEquals(output.shape, (eval_batch_size,))
示例#9
0
    def build(self, dataset, image_height, image_width, num_classes, is_training):

        images, labels = self._load_batch(dataset, height=image_height, width=image_width)

        # Create the model, use the default arg scope to configure the batch norm parameters.
        with slim.arg_scope(inception_v1_arg_scope()):
            logits, end_points = inception_v1(images, num_classes=num_classes, is_training=is_training)

        # Specify the loss function:
        one_hot_labels = slim.one_hot_encoding(labels, num_classes)
        loss = slim.losses.softmax_cross_entropy(logits, one_hot_labels)

        # Create some summaries to visualize the training process:
        tf.summary.scalar('losses/Total Loss', loss)

        if is_training:
            # Specify the optimizer and create the train op:
            optimizer = tf.train.RMSPropOptimizer(learning_rate=0.01)
            variables_to_train = []
            for scope in self.trainable_scopes:
                variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope)
                variables_to_train.extend(variables)

            variables = slim.get_model_variables("InceptionV1/Logits")
            exec_op = slim.learning.create_train_op(loss, optimizer, variables_to_train=variables)

        else:
            exec_op = end_points['Predictions']

        if self.checkpoints_file is not None:
            self.init_fn = self._get_init_fn()

        return exec_op, tf.get_default_graph()
def evaluate_inceptionV1_bin(batch_size):
    with tf.Graph().as_default():
        tf.logging.set_verbosity(tf.logging.INFO)
        global_step = slim.get_or_create_global_step()

        # Summary


        dataset = plantVSnoplant.get_split('validation', plantVSnoplant_data_dir)
        images,labels = load_batch(dataset, batch_size = batch_size,  k=0, r=[], is_training =False)

        with slim.arg_scope(inception.inception_v1_arg_scope(weight_decay=weight_decay)):
                logits, _ = inception.inception_v1(images, num_classes=2, is_training=False)


        total_output = []
        total_labels = []
        total_images = []

        with tf.Session() as sess:
            coord = tf.train.Coordinator()
            saver = tf.train.Saver()
            saver.restore(sess, tf.train.latest_checkpoint(train_inceptionV1_bin_dir))
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)
            for i in range(batch_size):
                print('step: %d/%d' % (i, batch_size))
                o, l , image= sess.run([logits, labels, images[0]])
                o = tf.reduce_sum(o, 0)/float(5)
                total_output.append(o)
                total_labels.append(l[0])
                total_images.append(image)
            coord.request_stop()
            coord.join(threads)


            total_output = tf.stack(total_output,0)
            total_output = tf.nn.softmax(total_output)
            labels = tf.constant(total_labels)
            total_images = sess.run(tf.stack(total_images,0))

            top1_op = tf.nn.in_top_k(total_output, labels, 1)
            top1_acc = sess.run(tf.reduce_mean(tf.cast(top1_op, tf.float32)))
            print(top1_acc)


            top5_op = tf.nn.in_top_k(total_output, labels, 5)
            top5_acc = sess.run(tf.reduce_mean(tf.cast(top5_op, tf.float32)))
            print(top5_acc)

            accuracy1_sum = tf.summary.scalar('top1_accuracy', top1_acc)
            accuracy5_sum = tf.summary.scalar('top5_accuracy', top5_acc)
            images_sum = tf.summary.image("input_images", total_images, batch_size)

            accuracy1, accuracy5, image_batch, step = sess.run([accuracy1_sum,accuracy5_sum,images_sum, global_step])
            writer = tf.summary.FileWriter(eval_inceptionV1_bin_dir)
            writer.add_summary(accuracy1, step)
            writer.add_summary(accuracy5, step)
            writer.add_summary(image_batch)
示例#11
0
    def testLogitsNotSqueezed(self):
        num_classes = 25
        images = tf.random_uniform([1, 224, 224, 3])
        logits, _ = inception.inception_v1(images, num_classes=num_classes, spatial_squeeze=False)

        with self.test_session() as sess:
            tf.global_variables_initializer().run()
            logits_out = sess.run(logits)
            self.assertListEqual(list(logits_out.shape), [1, 1, 1, num_classes])
示例#12
0
    def testLogitsNotSqueezed(self):
        num_classes = 25
        images = tf.random_uniform([1, 224, 224, 3])
        logits, _ = inception.inception_v1(images, num_classes=num_classes, spatial_squeeze=False)

        with self.test_session() as sess:
            tf.initialize_all_variables().run()
            logits_out = sess.run(logits)
            self.assertListEqual(list(logits_out.shape), [1, 1, 1, num_classes])
def train_inceptionV1(log_steps,save_summaries_sec,save_interval_secs,num_iterations = num_iterations_inception):
    with tf.Graph().as_default():
        tf.logging.set_verbosity(tf.logging.INFO)

        summaries = set(tf.get_collection(tf.GraphKeys.SUMMARIES))

        global_step = slim.get_or_create_global_step()

        dataset = plantclef2015.get_split('train', plant_data_dir)
        images,labels = load_batch(dataset, batch_size = batch_size, k=num_patches_inception, r=r_rotations_inception)

        # Add Images to summaries
        summaries.add(tf.summary.image("input_images", images, batch_size))

        # Create the models
        with slim.arg_scope(inception.inception_v1_arg_scope(weight_decay=weight_decay)):
            logits, _ = inception.inception_v1(images, num_classes=1000, is_training=True)


        # Specify the loss function:
        one_hot_labels = slim.one_hot_encoding(labels, dataset.num_classes)

        loss(logits, one_hot_labels)
        #slim.losses.softmax_cross_entropy(logits, one_hot_labels)
        total_loss = slim.losses.get_total_loss()

        # Create some summaries to visualize the training process:
        for variable in slim.get_model_variables():
            summaries.add(tf.summary.histogram(variable.op.name, variable))
        summaries.add(tf.summary.scalar('losses/Total_Loss', total_loss))

        # Specify the optimizer and create the train op:
        learning_rate = tf.train.exponential_decay(start_learning_rate, global_step,updating_iteration_for_learning_rate, updating_gamma, staircase=True)

        optimizer = tf.train.MomentumOptimizer(learning_rate= learning_rate, momentum=momentum)

        train_op = slim.learning.create_train_op(total_loss, optimizer)

        summaries.add(tf.summary.scalar('training/Learning_Rate', learning_rate))

        summary_op = tf.summary.merge(list(summaries), name='summary_op')

        # Run the training:
        final_loss = slim.learning.train(
            train_op,
            logdir=train_inceptionV1_dir,
            log_every_n_steps=log_steps,
            global_step=global_step,
            number_of_steps= num_iterations,
            summary_op=summary_op,
            init_fn=get_init_fn_V1(),
            save_summaries_secs=save_summaries_sec,
            save_interval_secs=save_interval_secs)

    print('Finished training. Last batch loss %f' % final_loss)
def extract_feat(frames_name):
    """
    Reference:
        1. Vasili's codes
        2. https://github.com/tensorflow/models/issues/429#issuecomment-277885861
    """

    slim_dir = "/home/xyang/workspace/models/research/slim"
    checkpoints_dir = slim_dir + "/pretrain"
    checkpoints_file = checkpoints_dir + '/inception_v1.ckpt'
    batch_size = 256

    sys.path.append(slim_dir)
    from nets import inception
    import tensorflow as tf
    slim = tf.contrib.slim
    image_size = inception.inception_v1.default_image_size

    feat = []
    with tf.Graph().as_default():
        input_batch = tf.placeholder(dtype=tf.uint8,
                                     shape=(batch_size, 300, 300, 3))
        resized_images = tf.image.resize_images(
            tf.image.convert_image_dtype(input_batch, dtype=tf.float32),
            [image_size, image_size]
            )
        preprocessed_images = tf.multiply(tf.subtract(resized_images, 0.5), 2.0)
        
        # Create the model, use the default arg scope to configure
        # the batch norm parameters.
        with slim.arg_scope(inception.inception_v1_arg_scope()):
            logits, endpoints = inception.inception_v1(preprocessed_images,
                                                        num_classes=1001,
                                                        is_training=False)
        pool5 = endpoints['AvgPool_0a_7x7']

        with tf.Session() as sess:
            saver = tf.train.Saver()
            saver.restore(sess, checkpoints_file)

            for i in range(0, len(frames_name), batch_size):
                print (i, '/', len(frames_name))
                current_batch = np.zeros((batch_size, 300, 300, 3), dtype=np.uint8)
                for j in range(batch_size):
                    if i+j == len(frames_name):
                        j -= 1
                        break
                    img = Image.open(frames_name[i+j]).convert('RGB').resize((300,300))
                    current_batch[j] = np.array(img)

                feat_batch = sess.run(pool5, feed_dict={input_batch: current_batch})
                feat_batch = np.squeeze(feat_batch)
                feat.append(feat_batch[:j+1].astype('float32'))

    return np.concatenate(feat, axis=0)
示例#15
0
文件: predict.py 项目: jgeller1/IVF
def predict(train_dir, test_dir, output_file, num_classes):
	""" 
	Loads weights from a TF model and makes predictions.

	Arguments:

	train_dir: directory of trained model

	test_dir: directory of test images (split into folders by class)

	output_file: output file to store predictions

	num_classes: number of classes of prediction

	Returns:

	Outpiuts a file output_file with predictions  
	"""

	train_dir = train_dir
	test_path = test_dir
	output = output_file
	nb_classes = num_classes

	print('Start to read images!')
	image_list = get_test_images(test_path)
	processed_images = tf.placeholder(tf.float32, shape=(None, image_size, image_size, 3))

	with slim.arg_scope(inception.inception_v1_arg_scope()):
		logits, _ = inception.inception_v1(processed_images, num_classes=nb_classes, is_training=False)

	def predict_fn(images):
	    return session.run(probabilities, feed_dict={processed_images: images})

	probabilities = tf.nn.softmax(logits)

	#Loads in latest training checkpoint
	checkpoint_path = tf.train.latest_checkpoint(train_dir)
	init_fn = slim.assign_from_checkpoint_fn(checkpoint_path,slim.get_variables_to_restore())
	init_fn(session)
	print('Start to transform images!')
	images = transform_img_fn(image_list)

	fto = open(output, 'w')
	print('Start doing predictions!')
	preds = predict_fn(images)
	print (len(preds))
	for p in range(len(preds)):
		print (image_list[p], preds[p,:], np.argmax(preds[p,:]))
		fto.write(image_list[p])
		for j in range(len(preds[p,:])):
			fto.write('\t' + str(preds[p, j]))
		fto.write('\n')

	fto.close()
示例#16
0
    def adapt_pretrain_imagenet_to_flower():
        import os
        from datasets import flowers
        from nets import inception
        from preprocessing import inception_preprocessing

        slim = tf.contrib.slim
        image_size = inception.inception_v1.default_image_size

        def get_init_fn():
            checkpoint_exclude_scopes = [
                'InceptionV1/Logits', 'InceptionV1/AuxLogits'
            ]
            exclusions = [scope.strip() for scope in checkpoint_exclude_scopes]
            variables_to_restore = []
            for var in slim.get_model_variables():
                excluded = False
                for exclusion in exclusions:
                    if var.op.name.startswith(exclusion):
                        excluded = True
                        break
                if not excluded:
                    variables_to_restore.append(var)

            return slim.assign_from_checkpoint_fn(
                os.path.join(checkpoint_dir, 'inception_v1.ckpt'),
                variables_to_restore)

        with tf.Graph().as_default():
            tf.logging.set_verbosity(tf.logging.INFO)
            dataset = flowers.get_split('train', flower_data_dir)
            images, _, labels = load_batch(dataset,
                                           height=image_size,
                                           width=image_size)

            with slim.arg_scope(inception.inception_v1_arg_scope()):
                logits, _ = inception.inception_v1(
                    images, num_classes=dataset.num_classes, is_training=True)

                one_hot_labels = slim.one_hot_encoding(labels,
                                                       dataset.num_classes)
                slim.losses.softmax_cross_entropy(logits, one_hot_labels)
                total_loss = slim.losses.get_total_loss()

                tf.scalar_summary('losses/Total Loss', total_loss)

                optimizer = tf.train.AdamOptimizer(learning_rate=0.01)
                train_op = slim.learning.create_train_op(total_loss, optimizer)

                final_loss = slim.learning.train(train_op,
                                                 logdir=train_dir,
                                                 init_fn=get_init_fn(),
                                                 number_of_steps=2)

        print("Finished training. Las batch loss %f" % final_loss)
示例#17
0
  def testBuildPreLogitsNetwork(self):
    batch_size = 5
    height, width = 224, 224
    num_classes = None

    inputs = tf.random_uniform((batch_size, height, width, 3))
    net, end_points = inception.inception_v1(inputs, num_classes)
    self.assertTrue(net.op.name.startswith('InceptionV1/Logits/AvgPool'))
    self.assertListEqual(net.get_shape().as_list(), [batch_size, 1, 1, 1024])
    self.assertFalse('Logits' in end_points)
    self.assertFalse('Predictions' in end_points)
示例#18
0
    def testBuildClassificationNetwork(self):
        batch_size = 5
        height, width = 224, 224
        num_classes = 1000

        inputs = tf.random_uniform((batch_size, height, width, 3))
        logits, end_points = inception.inception_v1(inputs, num_classes)
        self.assertTrue(logits.op.name.startswith("InceptionV1/Logits"))
        self.assertListEqual(logits.get_shape().as_list(), [batch_size, num_classes])
        self.assertTrue("Predictions" in end_points)
        self.assertListEqual(end_points["Predictions"].get_shape().as_list(), [batch_size, num_classes])
示例#19
0
    def testBuildClassificationNetwork(self):
        batch_size = 5
        height, width = 224, 224
        num_classes = 1000

        inputs = tf.random_uniform((batch_size, height, width, 3))
        logits, end_points = inception.inception_v1(inputs, num_classes)
        self.assertTrue(logits.op.name.startswith('InceptionV1/Logits'))
        self.assertListEqual(logits.get_shape().as_list(), [batch_size, num_classes])
        self.assertTrue('Predictions' in end_points)
        self.assertListEqual(end_points['Predictions'].get_shape().as_list(), [batch_size, num_classes])
示例#20
0
def get_network_logits_and_endpoints(network, images):
  if(network == 'inceptionV1'):
    with slim.arg_scope(inception.inception_v1_arg_scope(weight_decay=weight_decay)):
      logits, endpoints = inception.inception_v1(images, num_classes=1000, is_training=False)

  elif(network == 'vgg16'):

    with slim.arg_scope(vgg.vgg_arg_scope(weight_decay=weight_decay)):
      logits, endpoints = vgg.vgg_16(images, num_classes=1000, is_training=False)

  return logits,endpoints
示例#21
0
  def testBuildPreLogitsNetwork(self):
    batch_size = 5
    height, width = 224, 224
    num_classes = None

    inputs = tf.random_uniform((batch_size, height, width, 3))
    net, end_points = inception.inception_v1(inputs, num_classes)
    self.assertTrue(net.op.name.startswith('InceptionV1/Logits/AvgPool'))
    self.assertListEqual(net.get_shape().as_list(), [batch_size, 1, 1, 1024])
    self.assertFalse('Logits' in end_points)
    self.assertFalse('Predictions' in end_points)
示例#22
0
    def testEvaluation(self):
        batch_size = 2
        height, width = 224, 224
        num_classes = 1000

        eval_inputs = tf.random_uniform((batch_size, height, width, 3))
        logits, _ = inception.inception_v1(eval_inputs, num_classes, is_training=False)
        predictions = tf.argmax(logits, 1)

        with self.test_session() as sess:
            sess.run(tf.global_variables_initializer())
            output = sess.run(predictions)
            self.assertEquals(output.shape, (batch_size,))
示例#23
0
    def testEvaluation(self):
        batch_size = 2
        height, width = 224, 224
        num_classes = 1000

        eval_inputs = tf.random_uniform((batch_size, height, width, 3))
        logits, _ = inception.inception_v1(eval_inputs, num_classes, is_training=False)
        predictions = tf.argmax(logits, 1)

        with self.test_session() as sess:
            sess.run(tf.initialize_all_variables())
            output = sess.run(predictions)
            self.assertEquals(output.shape, (batch_size,))
示例#24
0
    def apply_pretrained_model():
        import numpy as np
        import os
        import tensorflow as tf
        import urllib2
        from datasets import imagenet
        from nets import inception
        from preprocessing import inception_preprocessing

        slim = tf.contrib.slim

        batch_size = 3
        image_size = inception.inception_v1.default_image_size

        with tf.Graph().as_default():
            url = 'https://upload.wikimedia.org/wikipedia/commons/7/70/EnglishCockerSpaniel_simon.jpg'
            image_string = urllib2.urlopen(url).read()
            image = tf.image.decode_jpeg(image_string, channels=3)
            processed_image = inception_preprocessing.preprocess_image(
                image, image_size, image_size, is_training=False)
            processed_images = tf.expand_dims(processed_image, 0)

            with slim.arg_scope(inception.inception_v1_arg_scope()):
                logits, _ = inception.inception_v1(processed_images,
                                                   num_classes=1001,
                                                   is_training=False)
            probabilities = tf.nn.softmax(logits)

            init_fn = slim.assign_from_checkpoint_fn(
                os.path.join(checkpoint_dir, 'inception_v1.ckpt'),
                slim.get_model_variables('InceptionV1'))

            with tf.Session() as sess:
                init_fn(sess)
                np_image, probabilities = sess.run([image, probabilities])
                probabilities = probabilities[0, 0:]
                sorted_inds = [
                    i[0] for i in sorted(enumerate(-probabilities),
                                         key=lambda x: x[1])
                ]

            plt.figure()
            plt.imshow(np_image.astype(np.uint8))
            plt.axis('off')
            plt.show()

            names = imagenet.create_readable_names_for_imagenet_labels()
            for i in range(5):
                index = sorted_inds[i]
                print('Probabity %0.2f%% => [%s]' %
                      (probabilities[index], names[index]))
示例#25
0
def eval(adv_imgs, labels, x_inputs, total_score, total_count):
    image = (((adv_imgs + 1.0) * 0.5) * 255.0)
    processed_imgs_inv1 = preprocess_for_model(image, 'inception_v1')
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits_inc_v1, end_points_inc_v1 = inception.inception_v1(
            processed_imgs_inv1, num_classes=FLAGS.num_classes, is_training=False, scope='InceptionV1', reuse=True)
    pred_inception = tf.argmax(end_points_inc_v1['Predictions'], 1)

    # rescale pixle range from [-1, 1] to [0, 255] for resnet_v1 and vgg's input
    processed_imgs_res_v1_50 = preprocess_for_model(image, 'resnet_v1_50')
    with slim.arg_scope(resnet_v1.resnet_arg_scope()):
        logits_res_v1_50, end_points_res_v1_50 = resnet_v1.resnet_v1_50(
            processed_imgs_res_v1_50, num_classes=FLAGS.num_classes, is_training=False, scope='resnet_v1_50', reuse=True)

    end_points_res_v1_50['logits'] = tf.squeeze(end_points_res_v1_50['resnet_v1_50/logits'], [1, 2])
    end_points_res_v1_50['probs'] = tf.nn.softmax(end_points_res_v1_50['logits'])
    pred_resnet = tf.argmax(end_points_res_v1_50['probs'], 1)

    processed_imgs_inv3 = preprocess_for_model(image, 'inception_v3')
    with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
        logits_res_inception_v3, end_points_inception_v3 = inception_v3.inception_v3(
            processed_imgs_inv3, num_classes=FLAGS.num_classes, is_training=False, scope='InceptionV3', reuse=True)
    pred_inception_v3 = tf.argmax(end_points_inception_v3['Predictions'], 1)

    processed_imgs_inv_res = preprocess_for_model(image, 'inception_resnet_v2')
    with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()):
        logits_res_inception_resnet, end_points_inception_resnet = inception_resnet_v2.inception_resnet_v2(
            processed_imgs_inv_res, num_classes=FLAGS.num_classes, is_training=False, scope='InceptionResnetV2')
    pred_ince_res = tf.argmax(end_points_inception_resnet['Predictions'], 1)


    for i in range(adv_imgs.shape[0]):
        def f1(total_score, total_count):
            total_score = tf.add(total_score, 64)
            return total_score, total_count

        def f2(total_score, total_count):
            adv = (((adv_imgs[i] + 1.0) * 0.5) * 255.0)
            ori = (((x_inputs[i] + 1.0) * 0.5) * 255.0)
            diff = tf.reshape(adv, [-1, 3]) - tf.reshape(ori, [-1, 3])
            distance = tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.square(diff), axis=1)))
            total_score = tf.add(total_score, distance)
            total_count = tf.add(total_count, 1)
            return total_score, total_count
        total_score, total_count = tf.cond(tf.equal(pred_inception[i], labels[i]), lambda: f2(total_score, total_count), lambda: f1(total_score, total_count))
        total_score, total_count = tf.cond(tf.equal(pred_resnet[i], labels[i]), lambda: f2(total_score, total_count), lambda: f1(total_score, total_count))
        # total_score, total_count = tf.cond(tf.equal(pred_inception_v3[i], labels[i]), lambda: f2(total_score, total_count), lambda: f1(total_score, total_count))
        total_score, total_count = tf.cond(tf.equal(pred_ince_res[i], labels[i]), lambda: f2(total_score, total_count), lambda: f1(total_score, total_count))

    return total_score, total_count
示例#26
0
    def testUnknowBatchSize(self):
        batch_size = 1
        height, width = 224, 224
        num_classes = 1000

        inputs = tf.placeholder(tf.float32, (None, height, width, 3))
        logits, _ = inception.inception_v1(inputs, num_classes)
        self.assertTrue(logits.op.name.startswith("InceptionV1/Logits"))
        self.assertListEqual(logits.get_shape().as_list(), [None, num_classes])
        images = tf.random_uniform((batch_size, height, width, 3))

        with self.test_session() as sess:
            sess.run(tf.initialize_all_variables())
            output = sess.run(logits, {inputs: images.eval()})
            self.assertEquals(output.shape, (batch_size, num_classes))
示例#27
0
    def testUnknowBatchSize(self):
        batch_size = 1
        height, width = 224, 224
        num_classes = 1000

        inputs = tf.placeholder(tf.float32, (None, height, width, 3))
        logits, _ = inception.inception_v1(inputs, num_classes)
        self.assertTrue(logits.op.name.startswith('InceptionV1/Logits'))
        self.assertListEqual(logits.get_shape().as_list(), [None, num_classes])
        images = tf.random_uniform((batch_size, height, width, 3))

        with self.test_session() as sess:
            sess.run(tf.global_variables_initializer())
            output = sess.run(logits, {inputs: images.eval()})
            self.assertEquals(output.shape, (batch_size, num_classes))
示例#28
0
def inception_v1(inputs, is_training, opts):
    with slim.arg_scope(inception.inception_v1_arg_scope(
            weight_decay=opts.weight_decay,
            use_batch_norm=opts.use_batch_norm,
            batch_norm_decay=opts.batch_norm_decay,
            batch_norm_epsilon=opts.batch_norm_epsilon,
            activation_fn=tf.nn.relu)):
        return inception.inception_v1(
            inputs,
            num_classes=opts.num_classes,
            is_training=is_training,
            dropout_keep_prob=opts.dropout_keep_prob,
            prediction_fn=slim.softmax,
            spatial_squeeze=opts.spatial_squeeze,
            reuse=None,
            global_pool=opts.global_pool)
示例#29
0
 def __call__(self, x_input):
     """Constructs model and return probabilities for given input."""
     reuse = True if self.built else None
     x_input = image_normalize(x_input, normalization_method[0])
     x_input = tf.image.resize_images(x_input, [224, 224])
     with slim.arg_scope(inception.inception_v1_arg_scope()):
         _, end_points = inception.inception_v1(
             x_input,
             num_classes=self.num_classes,
             is_training=False,
             reuse=reuse)
     self.built = True
     output = end_points['Predictions']
     # Strip off the extra reshape op at the output
     probs = output.op.inputs[0]
     return probs
示例#30
0
 def testUnknownImageShape(self):
     tf.reset_default_graph()
     batch_size = 2
     height, width = 224, 224
     num_classes = 1000
     input_np = np.random.uniform(0, 1, (batch_size, height, width, 3))
     with self.test_session() as sess:
         inputs = tf.placeholder(tf.float32, shape=(batch_size, None, None, 3))
         logits, end_points = inception.inception_v1(inputs, num_classes)
         self.assertTrue(logits.op.name.startswith("InceptionV1/Logits"))
         self.assertListEqual(logits.get_shape().as_list(), [batch_size, num_classes])
         pre_pool = end_points["Mixed_5c"]
         feed_dict = {inputs: input_np}
         tf.initialize_all_variables().run()
         pre_pool_out = sess.run(pre_pool, feed_dict=feed_dict)
         self.assertListEqual(list(pre_pool_out.shape), [batch_size, 7, 7, 1024])
    def model_fn(features, labels, mode):
        from nets import inception
        slim = tf.contrib.slim
        labels = tf.squeeze(labels, axis=1)
        with slim.arg_scope(inception.inception_v1_arg_scope()):
            logits, end_points = inception.inception_v1(features,
                                                        num_classes=2,
                                                        is_training=True)

        if mode == tf.estimator.ModeKeys.TRAIN:
            loss = tf.reduce_mean(
                tf.losses.sparse_softmax_cross_entropy(logits=logits,
                                                       labels=labels))
            return TFEstimatorSpec(mode, predictions=logits, loss=loss)
        else:
            raise NotImplementedError
示例#32
0
 def testUnknownImageShape(self):
     tf.reset_default_graph()
     batch_size = 2
     height, width = 224, 224
     num_classes = 1000
     input_np = np.random.uniform(0, 1, (batch_size, height, width, 3))
     with self.test_session() as sess:
         inputs = tf.placeholder(tf.float32, shape=(batch_size, None, None, 3))
         logits, end_points = inception.inception_v1(inputs, num_classes)
         self.assertTrue(logits.op.name.startswith('InceptionV1/Logits'))
         self.assertListEqual(logits.get_shape().as_list(), [batch_size, num_classes])
         pre_pool = end_points['Mixed_5c']
         feed_dict = {inputs: input_np}
         tf.global_variables_initializer().run()
         pre_pool_out = sess.run(pre_pool, feed_dict=feed_dict)
         self.assertListEqual(list(pre_pool_out.shape), [batch_size, 7, 7, 1024])
示例#33
0
    def eval_adapt_pretrained_to_flower():
        import numpy as np
        import tensorflow as tf
        from datasets import flowers
        from nets import inception

        slim = tf.contrib.slim

        image_size = inception.inception_v1.default_image_size
        batch_size = 8

        with tf.Graph().as_default():
            tf.logging.set_verbosity(tf.logging.INFO)
            dataset = flowers.get_split('train', flower_data_dir)
            images, images_raw, labels = load_batch(dataset,
                                                    height=image_size,
                                                    width=image_size)

            with slim.arg_scope(inception.inception_v1_arg_scope()):
                logits, _ = inception.inception_v1(
                    images, num_classes=dataset.num_classes, is_training=True)
                probabilities = tf.nn.softmax(logits)

                checkpoint_path = tf.train.latest_checkpoint(train_dir)
                init_fn = slim.assign_from_checkpoint_fn(
                    checkpoint_path, slim.get_variables_to_restore())
                with tf.Session() as sess:
                    with slim.queues.QueueRunners(sess):
                        sess.run(tf.initialize_local_variables())
                        init_fn(sess)
                        np_probabilities, np_images_raw, np_labels = sess.run(
                            [probabilities, images_raw, labels])

                        for i in xrange(batch_size):
                            image = np_images_raw[i, :, :, :]
                            true_label = np_labels[i]
                            predicted_label = np.argmax(np_probabilities[i, :])
                            predicted_name = dataset.labels_to_names[
                                predicted_label]
                            true_name = dataset.labels_to_names[true_label]

                            plt.figure()
                            plt.imshow(image.astype(np.uint8))
                            plt.title('Ground Truth: [%s], Prediction [%s]' %
                                      (true_name, predicted_name))
                            plt.axis('off')
                            plt.show()
def inception_tst():
    with tf.Graph().as_default():
        url = 'https://upload.wikimedia.org/wikipedia/commons/7/70/EnglishCockerSpaniel_simon.jpg'
        image_string = urllib.urlopen(url).read()
        image = tf.image.decode_jpeg(image_string, channels=3)
        processed_image = inception_preprocessing.preprocess_image(
            image, image_size, image_size, is_training=False)
        processed_images = tf.expand_dims(processed_image, 0)

        # Create the model, use the default arg scope to configure the batch norm parameters.
        with slim.arg_scope(inception.inception_v1_arg_scope()):
            logits, _ = inception.inception_v1(processed_images,
                                               num_classes=1001,
                                               is_training=False)
        probabilities = tf.nn.softmax(logits)

        variables_to_restore = slim.get_variables()
        print(len(variables_to_restore))
        # pprint(variables_to_restore)
        variables_to_restore = slim.get_model_variables()
        print(len(variables_to_restore))
        return

        init_fn = slim.assign_from_checkpoint_fn(
            os.path.join(checkpoints_dir, 'inception_v1.ckpt'),
            slim.get_model_variables('InceptionV1'))

        with tf.Session() as sess:
            init_fn(sess)
            np_image, probabilities = sess.run([image, probabilities])
            probabilities = probabilities[0, 0:]
            sorted_inds = [
                i[0]
                for i in sorted(enumerate(-probabilities), key=lambda x: x[1])
            ]

        plt.figure()
        plt.imshow(np_image.astype(np.uint8))
        plt.axis('off')
        plt.show()

        names = imagenet.create_readable_names_for_imagenet_labels()
        for i in range(5):
            index = sorted_inds[i]
            print('Probability %0.2f%% => [%s]' %
                  (probabilities[index] * 100, names[index]))
示例#35
0
def predict(train_dir, test_dir, num_classes):
    """ 
	Loads weights from a TF model and makes predictions.

	Arguments:

	train_dir: directory of trained model

	test_dir: directory of test images (split into folders by class)

	num_classes: number of classes of prediction

	Returns:

	Returns logits in the order of test images given and nparray of predictions
	"""

    processed_images = tf.placeholder(tf.float32,
                                      shape=(None, image_size, image_size, 3))

    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits, _ = inception.inception_v1(processed_images,
                                           num_classes=num_classes,
                                           is_training=False)

    probabilities = tf.nn.softmax(logits)

    def predict_fn(images):
        return session.run(probabilities, feed_dict={processed_images: images})

    #Loads in latest training checkpoint
    checkpoint_path = tf.train.latest_checkpoint(train_dir)
    init_fn = slim.assign_from_checkpoint_fn(checkpoint_path,
                                             slim.get_variables_to_restore())
    init_fn(session)
    image_list = get_test_images(test_dir)
    images = transform_img_fn(image_list)
    predicted_probs = predict_fn(images)

    predictions = np.empty((len(predicted_probs), ))

    for example in range(len(predicted_probs)):
        predictions[example] = np.argmax(predicted_probs[example, :])

    return predicted_probs, predictions
示例#36
0
def label_test():
    image_size = inception.inception_v1.default_image_size
    batch_size = 12

    with tf.Graph().as_default():
        tf.logging.set_verbosity(tf.logging.INFO)

        dataset = flowers.get_split('validation', flowers_data_dir)
        images, images_raw, labels = load_batch(dataset,
                                                height=image_size,
                                                width=image_size)

        # Create the model, use the default arg scope to configure the batch norm parameters.
        with slim.arg_scope(inception.inception_v1_arg_scope()):
            logits, _ = inception.inception_v1(images,
                                               num_classes=dataset.num_classes,
                                               is_training=True)

        probabilities = tf.nn.softmax(logits)

        checkpoint_path = tf.train.latest_checkpoint(train_dir)
        init_fn = slim.assign_from_checkpoint_fn(
            checkpoint_path, slim.get_variables_to_restore())

        with tf.Session() as sess:
            with slim.queues.QueueRunners(sess):
                sess.run(tf.initialize_local_variables())
                init_fn(sess)
                np_probabilities, np_images_raw, np_labels = sess.run(
                    [probabilities, images, labels])

                for i in range(batch_size):
                    image = np_images_raw[i, :, :, :]
                    true_label = np_labels[i]
                    predicted_label = np.argmax(np_probabilities[i, :])
                    predicted_name = dataset.labels_to_names[predicted_label]
                    true_name = dataset.labels_to_names[true_label]

                    plt.figure()
                    plt.imshow(image / (image.max() - image.min()))
                    #                     plt.imshow(image.astype(np.uint8))
                    plt.title('Ground Truth: [%s], Prediction [%s]' %
                              (true_name, predicted_name))
                    plt.axis('off')
                    plt.show()
示例#37
0
def load_inception_model(network_config, net_input=None):

    with slim.arg_scope(inception.inception_v1_arg_scope()):

        if net_input is None:
            net_input = tf.placeholder("float",
                                       [None, image_size, image_size, 3],
                                       name='nn_input')

        logits, end_points = inception.inception_v1(net_input,
                                                    num_classes=1001,
                                                    is_training=True)

        init_fn = slim.assign_from_checkpoint_fn(
            os.path.join('./checkpoints', 'inception_v1.ckpt'),
            slim.get_model_variables('InceptionV1'))

        return logits, end_points, init_fn, net_input
示例#38
0
def eval():
    # This might take a few minutes.
    image_size = inception.inception_v1.default_image_size

    with tf.Graph().as_default():
        #tf.logging.set_verbosity(tf.logging.INFO)

        dataset = flowers.get_split('train', flowers_data_dir)
        images, images_raw, labels = load_batch(dataset,
                                                height=image_size,
                                                width=image_size)

        # Create the model, use the default arg scope to configure the batch norm parameters.
        with slim.arg_scope(inception.inception_v1_arg_scope()):
            logits, _ = inception.inception_v1(images,
                                               num_classes=dataset.num_classes,
                                               is_training=True)

        predictions = tf.argmax(logits, 1)

        checkpoint_path = tf.train.latest_checkpoint(train_dir)
        init_fn = slim.assign_from_checkpoint_fn(
            checkpoint_path, slim.get_variables_to_restore())

        # Define the metrics:
        names_to_values, names_to_updates = slim.metrics.aggregate_metric_map({
            'eval/Accuracy':
            slim.metrics.streaming_accuracy(predictions, labels),
            'eval/Recall@5':
            slim.metrics.streaming_sparse_recall_at_k(logits, labels, 5)
        })

        print('Running evaluation Loop...')
        checkpoint_path = tf.train.latest_checkpoint(train_dir)
        metric_values = slim.evaluation.evaluate_once(
            master='',
            checkpoint_path=checkpoint_path,
            logdir=train_dir,
            eval_op=list(names_to_updates.values()),
            final_op=list(names_to_values.values()))

        names_to_values = dict(zip(names_to_values.keys(), metric_values))
        for name in names_to_values:
            print('%s: %f' % (name, names_to_values[name]))
示例#39
0
    def build(self, dataset, image_height, image_width, num_classes,
              is_training):

        images, labels = self._load_batch(dataset,
                                          height=image_height,
                                          width=image_width)

        # Create the model, use the default arg scope to configure the batch norm parameters.
        with slim.arg_scope(inception_v1_arg_scope()):
            logits, end_points = inception_v1(images,
                                              num_classes=num_classes,
                                              is_training=is_training)

        # Specify the loss function:
        one_hot_labels = slim.one_hot_encoding(labels, num_classes)
        loss = slim.losses.softmax_cross_entropy(logits, one_hot_labels)

        # Create some summaries to visualize the training process:
        tf.summary.scalar('losses/Total Loss', loss)

        if is_training:
            # Specify the optimizer and create the train op:
            optimizer = tf.train.RMSPropOptimizer(learning_rate=0.01)
            variables_to_train = []
            for scope in self.trainable_scopes:
                variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                              scope)
                variables_to_train.extend(variables)

            variables = slim.get_model_variables("InceptionV1/Logits")
            exec_op = slim.learning.create_train_op(
                loss, optimizer, variables_to_train=variables)

        else:
            exec_op = end_points['Predictions']

        if self.checkpoints_file is not None:
            self.init_fn = self._get_init_fn()

        return exec_op, tf.get_default_graph()
示例#40
0
def fine_tune():
    with tf.Graph().as_default():
        tf.logging.set_verbosity(tf.logging.INFO)

        dataset = flowers.get_split('train', flowers_data_dir)
        images, _, labels = load_batch(dataset,
                                       height=image_size,
                                       width=image_size)

        # Create the model, use the default arg scope to configure the batch norm parameters.
        with slim.arg_scope(inception.inception_v1_arg_scope()):
            logits, _ = inception.inception_v1(images,
                                               num_classes=dataset.num_classes,
                                               is_training=True)

        # Specify the loss function:
        one_hot_labels = slim.one_hot_encoding(labels, dataset.num_classes)
        slim.losses.softmax_cross_entropy(logits, one_hot_labels)
        total_loss = slim.losses.get_total_loss()

        # Create some summaries to visualize the training process:
        tf.summary.scalar('losses/Total Loss', total_loss)

        # Specify the optimizer and create the train op:
        optimizer = tf.train.AdamOptimizer(learning_rate=0.01)
        train_op = slim.learning.create_train_op(total_loss, optimizer)

        # Run the training:
        final_loss = slim.learning.train(
            train_op,
            logdir=train_dir,
            init_fn=get_init_fn(),
            number_of_steps=1000,  # For speed, we just do 1 epoch
            save_interval_secs=600,
            save_summaries_secs=6000,
            log_every_n_steps=1,
        )

    print('Finished training. Last batch loss %f' % final_loss)
示例#41
0
EPOCHS = 1

classes = util.pkl_load(workspace.class_pkl)
csv_files = workspace.test_csvs


graph = tf.Graph()
with graph.as_default():
    tf.logging.set_verbosity(tf.logging.INFO)
    fp_placeholder = tf.placeholder(tf.string)
    image = read_one_image(fp_placeholder)
    images = tf.reshape(image, [1, 224, 224, 3])

    # Create the model, use the default arg scope to configure the batch norm parameters.
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits, _ = inception.inception_v1(images, num_classes=len(classes), is_training=False)
    probabilities = tf.nn.softmax(logits)

    checkpoint = tf.train.latest_checkpoint(os.path.join(workspace.inception_cpkt, 'log'))
    init_fn = slim.assign_from_checkpoint_fn(
        checkpoint,
        slim.get_model_variables())

with tf.Session(graph=graph) as sess:
    init_fn(sess)
    top1 = 0
    top5 = 0
    total = 0
    # there is prob a better way to do this will all TF but
    # this will do for calculating accuracy.
    for csv_fp in workspace.test_csvs:
slim = tf.contrib.slim

image_size = inception.inception_v1.default_image_size

with tf.Graph().as_default():
    #url = 'https://upload.wikimedia.org/wikipedia/commons/7/70/EnglishCockerSpaniel_simon.jpg'
    #url = 'https://pbs.twimg.com/media/B5B9ZsvIgAATiC6.jpg'
    url=sys.argv[1]
    image_string = urllib2.urlopen(url).read()
    image = tf.image.decode_jpeg(image_string, channels=3)
    processed_image = inception_preprocessing.preprocess_image(image, image_size, image_size, is_training=False)
    processed_images  = tf.expand_dims(processed_image, 0)
    
    # Create the model, use the default arg scope to configure the batch norm parameters.
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits, _ = inception.inception_v1(processed_images, num_classes=1001, is_training=False)
    probabilities = tf.nn.softmax(logits)
    checkpoints_dir='slim_pretrained' 
    init_fn = slim.assign_from_checkpoint_fn(
        os.path.join(checkpoints_dir, 'inception_v1.ckpt'),
        slim.get_variables_to_restore())
        #slim.get_model_variables())
        #slim.get_model_variables('InceptionV1'))
    #saver= tf.train.Saver(tf.all_variables()) 
    #saver= tf.train.Saver(list(set(slim.get_model_variables()) | set(tf.trainable_variables()))) 
    with tf.Session() as sess:
        init_fn(sess)
        np_image, probabilities = sess.run([image, probabilities])
        probabilities = probabilities[0, 0:]
        sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x:x[1])]
        #saver.save(sess,'/home/sankit/Documents/cv-tricks/models/slim/my-saved/inception_v1.ckpt')