def testNoBatchNormScaleByDefault(self):
    height, width = 299, 299
    num_classes = 1000
    inputs = tf.placeholder(tf.float32, (1, height, width, 3))
    with tf.contrib.slim.arg_scope(inception.inception_v4_arg_scope()):
      inception.inception_v4(inputs, num_classes, is_training=False)

    self.assertEqual(tf.global_variables('.*/BatchNorm/gamma:0$'), [])
  def testBatchNormScale(self):
    height, width = 299, 299
    num_classes = 1000
    inputs = tf.placeholder(tf.float32, (1, height, width, 3))
    with tf.contrib.slim.arg_scope(
        inception.inception_v4_arg_scope(batch_norm_scale=True)):
      inception.inception_v4(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)
 def testVariablesSetDevice(self):
   batch_size = 5
   height, width = 299, 299
   num_classes = 1000
   inputs = tf.random_uniform((batch_size, height, width, 3))
   # Force all Variables to reside on the device.
   with tf.variable_scope('on_cpu'), tf.device('/cpu:0'):
     inception.inception_v4(inputs, num_classes)
   with tf.variable_scope('on_gpu'), tf.device('/gpu:0'):
     inception.inception_v4(inputs, num_classes)
   for v in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='on_cpu'):
     self.assertDeviceEqual(v.device, '/cpu:0')
   for v in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='on_gpu'):
     self.assertDeviceEqual(v.device, '/gpu:0')
 def testVariablesSetDevice(self):
     batch_size = 5
     height, width = 299, 299
     num_classes = 1000
     inputs = tf.random.uniform((batch_size, height, width, 3))
     # Force all Variables to reside on the device.
     with tf.compat.v1.variable_scope('on_cpu'), tf.device('/cpu:0'):
         inception.inception_v4(inputs, num_classes)
     with tf.compat.v1.variable_scope('on_gpu'), tf.device('/gpu:0'):
         inception.inception_v4(inputs, num_classes)
     for v in tf.compat.v1.get_collection(
             tf.compat.v1.GraphKeys.GLOBAL_VARIABLES, scope='on_cpu'):
         self.assertDeviceEqual(v.device, '/cpu:0')
     for v in tf.compat.v1.get_collection(
             tf.compat.v1.GraphKeys.GLOBAL_VARIABLES, scope='on_gpu'):
         self.assertDeviceEqual(v.device, '/gpu:0')
 def testTrainEvalWithReuse(self):
   train_batch_size = 5
   eval_batch_size = 2
   height, width = 150, 150
   num_classes = 1000
   with self.test_session() as sess:
     train_inputs = tf.random_uniform((train_batch_size, height, width, 3))
     inception.inception_v4(train_inputs, num_classes)
     eval_inputs = tf.random_uniform((eval_batch_size, height, width, 3))
     logits, _ = inception.inception_v4(eval_inputs,
                                        num_classes,
                                        is_training=False,
                                        reuse=True)
     predictions = tf.argmax(logits, 1)
     sess.run(tf.global_variables_initializer())
     output = sess.run(predictions)
     self.assertEquals(output.shape, (eval_batch_size,))
Beispiel #6
0
 def testTrainEvalWithReuse(self):
   train_batch_size = 5
   eval_batch_size = 2
   height, width = 150, 150
   num_classes = 1000
   with self.test_session() as sess:
     train_inputs = tf.random_uniform((train_batch_size, height, width, 3))
     inception.inception_v4(train_inputs, num_classes)
     eval_inputs = tf.random_uniform((eval_batch_size, height, width, 3))
     logits, _ = inception.inception_v4(eval_inputs,
                                        num_classes,
                                        is_training=False,
                                        reuse=True)
     predictions = tf.argmax(logits, 1)
     sess.run(tf.global_variables_initializer())
     output = sess.run(predictions)
     self.assertEquals(output.shape, (eval_batch_size,))
Beispiel #7
0
 def testBuildPreLogitsNetwork(self):
   batch_size = 5
   height, width = 299, 299
   num_classes = None
   inputs = tf.random_uniform((batch_size, height, width, 3))
   net, end_points = inception.inception_v4(inputs, num_classes)
   self.assertTrue(net.op.name.startswith('InceptionV4/Logits/AvgPool'))
   self.assertListEqual(net.get_shape().as_list(), [batch_size, 1, 1, 1536])
   self.assertFalse('Logits' in end_points)
   self.assertFalse('Predictions' in end_points)
 def testBuildPreLogitsNetwork(self):
   batch_size = 5
   height, width = 299, 299
   num_classes = None
   inputs = tf.random_uniform((batch_size, height, width, 3))
   net, end_points = inception.inception_v4(inputs, num_classes)
   self.assertTrue(net.op.name.startswith('InceptionV4/Logits/AvgPool'))
   self.assertListEqual(net.get_shape().as_list(), [batch_size, 1, 1, 1536])
   self.assertFalse('Logits' in end_points)
   self.assertFalse('Predictions' in end_points)
Beispiel #9
0
def classify(checkpoints_dir, images,logo_names=[""], reuse=False):
    image_size = inception.inception_v4.default_image_size
    probabilities_list = []
    processed_image_list = []
    images_list = []
    output_probabilities = []
    for image in images:
        image = tf.image.decode_jpeg(image, channels=3)
        #image = tf.image.resize_image_with_crop_or_pad(image, 299, 299)
        processed_image = inception_preprocessing.preprocess_image(image,
                                                             image_size,
                                                             image_size,
                                                             is_training=False)
        #processed_images  = tf.expand_dims(processed_image, 0)
        images_list.append(image)
        processed_image_list.append(processed_image)
    with slim.arg_scope(inception_utils.inception_arg_scope()):
        logits, _ = inception.inception_v4(processed_image_list,
                               #num_classes=2,
                               reuse=reuse,
                               is_training=False,
                               logo_names= logo_names)
        probabilities = []
        output_probabilities = []
        for logo_name in logo_names:
            probabilities.append(tf.nn.softmax(logits[logo_name]))

        if tf.gfile.IsDirectory(checkpoints_dir):
          checkpoints_dir = tf.train.latest_checkpoint(checkpoints_dir)

        init_fn = slim.assign_from_checkpoint_fn(
        checkpoints_dir,
        slim.get_model_variables('InceptionV4'),
        ignore_missing_vars=True)
        with tf.Session() as sess:
            init_fn(sess)
            output_probabilities  = sess.run([images_list,
                                             processed_image_list]
                                             + probabilities)[2:]
        output_dict = {}
        print "range(len(output_probabilities): ", range(len(output_probabilities))
        for index in range(len(output_probabilities)):
            print 'logo_names[index]:[{}]'.format(logo_names[index])
            print('type logo_names[index]',type(logo_names[index]));
            if logo_names[index] == "":
                print "here???????????????????"
                output_dict [logo_names[index]] = output_probabilities[index]
            else:
                output_dict [logo_names[index]] = output_probabilities[index]
            print "output_dict [""]: ", output_dict [""].shape

        print "final 0,780 prob output_dict [""]: ", output_dict[""][0][780];
        output_dict[""] = np.argsort(output_dict[""], axis=1)[:, ::-1][:, :5]
        print "final output_dict [""]: ", output_dict [""]
        return output_dict
 def testBuildWithoutAuxLogits(self):
   batch_size = 5
   height, width = 299, 299
   num_classes = 1000
   inputs = tf.random_uniform((batch_size, height, width, 3))
   logits, endpoints = inception.inception_v4(inputs, num_classes,
                                              create_aux_logits=False)
   self.assertFalse('AuxLogits' in endpoints)
   self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
   self.assertListEqual(logits.get_shape().as_list(),
                        [batch_size, num_classes])
Beispiel #11
0
def predict(image, version='V3'):
    tf.reset_default_graph()
    # Process the image
    raw_image, processed_image = process_image(image)
    class_names = imagenet.create_readable_names_for_imagenet_labels()
    # Create a placeholder for the images
    X = tf.placeholder(tf.float32, [None, 299, 299, 3], name="X")
    #inception_v3 function returns logits and end_points dictionary
    #logits are output of the network before applying softmax activation

    if version.upper() == 'V3':
        model_ckpt_path = INCEPTION_V3_CKPT_PATH
        with slim.arg_scope(inception.inception_v3_arg_scope()):
            # Set the number of classes and is_training parameter
            logits, end_points = inception.inception_v3(
                X, num_classes=1001, is_training=False)

    elif version.upper() == 'V4':
        model_ckpt_path = INCEPTION_V4_CKPT_PATH
        with slim.arg_scope(inception.inception_v3_arg_scope()):
            # Set the number of classes and is_training parameter
            # Logits
            logits, end_points = inception.inception_v4(
                X, num_classes=1001, is_training=False)

    predictions = end_points.get('Predictions', 'No key named predictions')
    saver = tf.train.Saver()

    with tf.Session() as sess:
        saver.restore(sess, model_ckpt_path)
        prediction_values = predictions.eval({X: processed_image})

    try:
        # Add an index to predictions and then sort by probability
        prediction_values = [
            (i, prediction)
            for i, prediction in enumerate(prediction_values[0, :])
        ]
        prediction_values = sorted(
            prediction_values, key=lambda x: x[1], reverse=True)
        # Plot the image
        # plot_color_image(raw_image)
        plot_color_image(image)
        print("Using Inception_{} CNN\nPrediction: Probability\n".format(
            version))
        # Display the image and predictions
        for i in range(5):
            predicted_class = class_names[prediction_values[i][0]]
            probability = prediction_values[i][1]
            print("{}: {:.2f}%".format(predicted_class, probability * 100))

    # If the predictions do not come out right
    except:
        print(predictions)
Beispiel #12
0
 def testBuildWithoutAuxLogits(self):
   batch_size = 5
   height, width = 299, 299
   num_classes = 1000
   inputs = tf.random_uniform((batch_size, height, width, 3))
   logits, endpoints = inception.inception_v4(inputs, num_classes,
                                              create_aux_logits=False)
   self.assertFalse('AuxLogits' in endpoints)
   self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
   self.assertListEqual(logits.get_shape().as_list(),
                        [batch_size, num_classes])
 def testHalfSizeImages(self):
   batch_size = 5
   height, width = 150, 150
   num_classes = 1000
   inputs = tf.random_uniform((batch_size, height, width, 3))
   logits, end_points = inception.inception_v4(inputs, num_classes)
   self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
   self.assertListEqual(logits.get_shape().as_list(),
                        [batch_size, num_classes])
   pre_pool = end_points['Mixed_7d']
   self.assertListEqual(pre_pool.get_shape().as_list(),
                        [batch_size, 3, 3, 1536])
 def testGlobalPool(self):
     batch_size = 2
     height, width = 400, 600
     num_classes = 1000
     inputs = tf.random.uniform((batch_size, height, width, 3))
     logits, end_points = inception.inception_v4(inputs, num_classes)
     self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
     self.assertListEqual(logits.get_shape().as_list(),
                          [batch_size, num_classes])
     pre_pool = end_points['Mixed_7d']
     self.assertListEqual(pre_pool.get_shape().as_list(),
                          [batch_size, 11, 17, 1536])
 def testHalfSizeImages(self):
     batch_size = 5
     height, width = 150, 150
     num_classes = 1000
     inputs = tf.random_uniform((batch_size, height, width, 3))
     logits, end_points = inception.inception_v4(inputs, num_classes)
     self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
     self.assertListEqual(logits.get_shape().as_list(),
                          [batch_size, num_classes])
     pre_pool = end_points['Mixed_7d']
     self.assertListEqual(pre_pool.get_shape().as_list(),
                          [batch_size, 3, 3, 1536])
 def testEvaluation(self):
   batch_size = 2
   height, width = 299, 299
   num_classes = 1000
   with self.test_session() as sess:
     eval_inputs = tf.random_uniform((batch_size, height, width, 3))
     logits, _ = inception.inception_v4(eval_inputs,
                                        num_classes,
                                        is_training=False)
     predictions = tf.argmax(logits, 1)
     sess.run(tf.global_variables_initializer())
     output = sess.run(predictions)
     self.assertEquals(output.shape, (batch_size,))
 def testEvaluation(self):
     batch_size = 2
     height, width = 299, 299
     num_classes = 1000
     with self.test_session() as sess:
         eval_inputs = tf.random_uniform((batch_size, height, width, 3))
         logits, _ = inception.inception_v4(eval_inputs,
                                            num_classes,
                                            is_training=False)
         predictions = tf.argmax(logits, 1)
         sess.run(tf.global_variables_initializer())
         output = sess.run(predictions)
         self.assertEquals(output.shape, (batch_size, ))
 def testUnknownBatchSize(self):
   batch_size = 1
   height, width = 299, 299
   num_classes = 1000
   with self.test_session() as sess:
     inputs = tf.placeholder(tf.float32, (None, height, width, 3))
     logits, _ = inception.inception_v4(inputs, num_classes)
     self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
     self.assertListEqual(logits.get_shape().as_list(),
                          [None, num_classes])
     images = tf.random_uniform((batch_size, height, width, 3))
     sess.run(tf.global_variables_initializer())
     output = sess.run(logits, {inputs: images.eval()})
     self.assertEquals(output.shape, (batch_size, num_classes))
 def testUnknownBatchSize(self):
     batch_size = 1
     height, width = 299, 299
     num_classes = 1000
     with self.test_session() as sess:
         inputs = tf.placeholder(tf.float32, (None, height, width, 3))
         logits, _ = inception.inception_v4(inputs, num_classes)
         self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
         self.assertListEqual(logits.get_shape().as_list(),
                              [None, num_classes])
         images = tf.random_uniform((batch_size, height, width, 3))
         sess.run(tf.global_variables_initializer())
         output = sess.run(logits, {inputs: images.eval()})
         self.assertEquals(output.shape, (batch_size, num_classes))
Beispiel #20
0
  def __call__(self, x_input, batch_size=None, is_training=False):
    """Constructs model and return probabilities for given input."""
    reuse = True if self.built else None
    with slim.arg_scope(inception.inception_v4_arg_scope()):
      with tf.variable_scope(self.ckpt):
        logits, end_points = inception.inception_v4(
            x_input, num_classes=self.num_classes, is_training=is_training,
            reuse=reuse)

      preds = tf.argmax(logits, axis=1)
    self.built = True
    self.logits = logits
    self.preds = preds
    return logits
Beispiel #21
0
 def testAllEndPointsShapes(self):
     batch_size = 5
     height, width = 299, 299
     num_classes = 1000
     inputs = tf.random_uniform((batch_size, height, width, 3))
     _, end_points = inception.inception_v4(inputs, num_classes)
     endpoints_shapes = {
         'Conv2d_1a_3x3': [batch_size, 149, 149, 32],
         'Conv2d_2a_3x3': [batch_size, 147, 147, 32],
         'Conv2d_2b_3x3': [batch_size, 147, 147, 64],
         'Mixed_3a': [batch_size, 73, 73, 160],
         'Mixed_4a': [batch_size, 71, 71, 192],
         'Mixed_5a': [batch_size, 35, 35, 384],
         # 4 x Inception-A blocks
         'Mixed_5b': [batch_size, 35, 35, 384],
         'Mixed_5c': [batch_size, 35, 35, 384],
         'Mixed_5d': [batch_size, 35, 35, 384],
         'Mixed_5e': [batch_size, 35, 35, 384],
         # Reduction-A block
         'Mixed_6a': [batch_size, 17, 17, 1024],
         # 7 x Inception-B blocks
         'Mixed_6b': [batch_size, 17, 17, 1024],
         'Mixed_6c': [batch_size, 17, 17, 1024],
         'Mixed_6d': [batch_size, 17, 17, 1024],
         'Mixed_6e': [batch_size, 17, 17, 1024],
         'Mixed_6f': [batch_size, 17, 17, 1024],
         'Mixed_6g': [batch_size, 17, 17, 1024],
         'Mixed_6h': [batch_size, 17, 17, 1024],
         # Reduction-A block
         'Mixed_7a': [batch_size, 8, 8, 1536],
         # 3 x Inception-C blocks
         'Mixed_7b': [batch_size, 8, 8, 1536],
         'Mixed_7c': [batch_size, 8, 8, 1536],
         'Mixed_7d': [batch_size, 8, 8, 1536],
         # Logits and predictions
         'AuxLogits': [batch_size, num_classes],
         'global_pool': [batch_size, 1, 1, 1536],
         'PreLogitsFlatten': [batch_size, 1536],
         'Logits': [batch_size, num_classes],
         'Predictions': [batch_size, num_classes]
     }
     self.assertItemsEqual(list(endpoints_shapes.keys()),
                           list(end_points.keys()))
     for endpoint_name in endpoints_shapes:
         expected_shape = endpoints_shapes[endpoint_name]
         self.assertTrue(endpoint_name in end_points)
         self.assertListEqual(
             end_points[endpoint_name].get_shape().as_list(),
             expected_shape)
Beispiel #22
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[3])
     with slim.arg_scope(inception.inception_v4_arg_scope()):
         _, end_points = inception.inception_v4(
             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 output
Beispiel #23
0
def inception_v4(inputs, is_training, opts):
    with slim.arg_scope(inception.inception_v4_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_v4(
            inputs,
            num_classes=opts.num_classes,
            is_training=is_training,
            dropout_keep_prob=opts.dropout_keep_prob,
            reuse=None,
            create_aux_logits=opts.create_aux_logits,
            global_pool=opts.global_pool)
    def use_fined_model(self):
        image_size = inception.inception_v4.default_image_size
        batch_size = 3
        flowers_data_dir = "../../data/flower"
        train_dir = '/tmp/inception_finetuned/'

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

            dataset = flowers.get_split('train', flowers_data_dir)
            images, images_raw, labels = self.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_v4_arg_scope()):
                logits, _ = inception.inception_v4(
                    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 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.astype(np.uint8))
                        plt.title('Ground Truth: [%s], Prediction [%s]' %
                                  (true_name, predicted_name))
                        plt.axis('off')
                        plt.show()
                return
Beispiel #25
0
def run_inference_on_image(image):
    """Runs inference on an image.

    Args:
      image: Image file name.

    Returns:
      Nothing
    """
    dest_directory = FLAGS.model_dir

    image_size = inception.inception_v4.default_image_size

    if not tf.gfile.Exists(image):
        tf.logging.fatal('File does not exist %s', image)
    image_string = tf.gfile.FastGFile(image, 'rb').read()

    with tf.Graph().as_default():
        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_v4_arg_scope()):
            logits, _ = inception.inception_v4(
                processed_images, num_classes=1001, is_training=False)
        probabilities = tf.nn.softmax(logits)

        init_fn = slim.assign_from_checkpoint_fn(
            os.path.join(dest_directory, 'inception_v4.ckpt'),
            slim.get_model_variables('InceptionV4'))

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

        names = create_readable_names_for_imagenet_labels()
        top_k = FLAGS.num_top_predictions
        for i in range(top_k):
            index = sorted_inds[i]
            print('%s (score = %.5f)' % (names[index], probabilities[index]))
 def testAllEndPointsShapes(self):
   batch_size = 5
   height, width = 299, 299
   num_classes = 1000
   inputs = tf.random_uniform((batch_size, height, width, 3))
   _, end_points = inception.inception_v4(inputs, num_classes)
   endpoints_shapes = {'Conv2d_1a_3x3': [batch_size, 149, 149, 32],
                       'Conv2d_2a_3x3': [batch_size, 147, 147, 32],
                       'Conv2d_2b_3x3': [batch_size, 147, 147, 64],
                       'Mixed_3a': [batch_size, 73, 73, 160],
                       'Mixed_4a': [batch_size, 71, 71, 192],
                       'Mixed_5a': [batch_size, 35, 35, 384],
                       # 4 x Inception-A blocks
                       'Mixed_5b': [batch_size, 35, 35, 384],
                       'Mixed_5c': [batch_size, 35, 35, 384],
                       'Mixed_5d': [batch_size, 35, 35, 384],
                       'Mixed_5e': [batch_size, 35, 35, 384],
                       # Reduction-A block
                       'Mixed_6a': [batch_size, 17, 17, 1024],
                       # 7 x Inception-B blocks
                       'Mixed_6b': [batch_size, 17, 17, 1024],
                       'Mixed_6c': [batch_size, 17, 17, 1024],
                       'Mixed_6d': [batch_size, 17, 17, 1024],
                       'Mixed_6e': [batch_size, 17, 17, 1024],
                       'Mixed_6f': [batch_size, 17, 17, 1024],
                       'Mixed_6g': [batch_size, 17, 17, 1024],
                       'Mixed_6h': [batch_size, 17, 17, 1024],
                       # Reduction-A block
                       'Mixed_7a': [batch_size, 8, 8, 1536],
                       # 3 x Inception-C blocks
                       'Mixed_7b': [batch_size, 8, 8, 1536],
                       'Mixed_7c': [batch_size, 8, 8, 1536],
                       'Mixed_7d': [batch_size, 8, 8, 1536],
                       # Logits and predictions
                       'AuxLogits': [batch_size, num_classes],
                       'global_pool': [batch_size, 1, 1, 1536],
                       'PreLogitsFlatten': [batch_size, 1536],
                       'Logits': [batch_size, num_classes],
                       'Predictions': [batch_size, num_classes]}
   self.assertItemsEqual(endpoints_shapes.keys(), end_points.keys())
   for endpoint_name in endpoints_shapes:
     expected_shape = endpoints_shapes[endpoint_name]
     self.assertTrue(endpoint_name in end_points)
     self.assertListEqual(end_points[endpoint_name].get_shape().as_list(),
                          expected_shape)
Beispiel #27
0
 def testGlobalPoolUnknownImageShape(self):
   batch_size = 1
   height, width = 350, 400
   num_classes = 1000
   with self.test_session() as sess:
     inputs = tf.placeholder(tf.float32, (batch_size, None, None, 3))
     logits, end_points = inception.inception_v4(
         inputs, num_classes, create_aux_logits=False)
     self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
     self.assertListEqual(logits.get_shape().as_list(),
                          [batch_size, num_classes])
     pre_pool = end_points['Mixed_7d']
     images = tf.random_uniform((batch_size, height, width, 3))
     sess.run(tf.global_variables_initializer())
     logits_out, pre_pool_out = sess.run([logits, pre_pool],
                                         {inputs: images.eval()})
     self.assertTupleEqual(logits_out.shape, (batch_size, num_classes))
     self.assertTupleEqual(pre_pool_out.shape, (batch_size, 9, 11, 1536))
 def testGlobalPoolUnknownImageShape(self):
   batch_size = 2
   height, width = 400, 600
   num_classes = 1000
   with self.test_session() as sess:
     inputs = tf.placeholder(tf.float32, (batch_size, None, None, 3))
     logits, end_points = inception.inception_v4(
         inputs, num_classes, create_aux_logits=False)
     self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
     self.assertListEqual(logits.get_shape().as_list(),
                          [batch_size, num_classes])
     pre_pool = end_points['Mixed_7d']
     images = tf.random_uniform((batch_size, height, width, 3))
     sess.run(tf.global_variables_initializer())
     logits_out, pre_pool_out = sess.run([logits, pre_pool],
                                         {inputs: images.eval()})
     self.assertTupleEqual(logits_out.shape, (batch_size, num_classes))
     self.assertTupleEqual(pre_pool_out.shape, (batch_size, 11, 17, 1536))
 def testBuildLogits(self):
   batch_size = 5
   height, width = 299, 299
   num_classes = 1000
   inputs = tf.random_uniform((batch_size, height, width, 3))
   logits, end_points = inception.inception_v4(inputs, num_classes)
   auxlogits = end_points['AuxLogits']
   predictions = end_points['Predictions']
   self.assertTrue(auxlogits.op.name.startswith('InceptionV4/AuxLogits'))
   self.assertListEqual(auxlogits.get_shape().as_list(),
                        [batch_size, num_classes])
   self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
   self.assertListEqual(logits.get_shape().as_list(),
                        [batch_size, num_classes])
   self.assertTrue(predictions.op.name.startswith(
       'InceptionV4/Logits/Predictions'))
   self.assertListEqual(predictions.get_shape().as_list(),
                        [batch_size, num_classes])
 def testBuildLogits(self):
     batch_size = 5
     height, width = 299, 299
     num_classes = 1000
     inputs = tf.random_uniform((batch_size, height, width, 3))
     logits, end_points = inception.inception_v4(inputs, num_classes)
     auxlogits = end_points['AuxLogits']
     predictions = end_points['Predictions']
     self.assertTrue(auxlogits.op.name.startswith('InceptionV4/AuxLogits'))
     self.assertListEqual(auxlogits.get_shape().as_list(),
                          [batch_size, num_classes])
     self.assertTrue(logits.op.name.startswith('InceptionV4/Logits'))
     self.assertListEqual(logits.get_shape().as_list(),
                          [batch_size, num_classes])
     self.assertTrue(
         predictions.op.name.startswith('InceptionV4/Logits/Predictions'))
     self.assertListEqual(predictions.get_shape().as_list(),
                          [batch_size, num_classes])
    def fine_tune_inception(self):
        train_dir = '/tmp/inception_finetuned/'
        image_size = inception.inception_v4.default_image_size
        checkpoint_path = "../../data/trained_models/inception_v4/inception_v4.ckpt"
        flowers_data_dir = "../../data/flower"

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

            dataset = flowers.get_split('train', flowers_data_dir)
            images, _, labels = self.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_v4_arg_scope()):
                logits, _ = inception.inception_v4(
                    images, num_classes=dataset.num_classes, is_training=True)

            # Specify the loss function:
            one_hot_labels = slim.one_hot_encoding(labels, dataset.num_classes)
            total_loss = slim.losses.softmax_cross_entropy(
                logits, one_hot_labels)
            #             total_loss = slim.losses.get_total_loss(add_regularization_losses=False)
            #             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.001)
            train_op = slim.learning.create_train_op(total_loss, optimizer)

            # Run the training:
            number_of_steps = math.ceil(dataset.num_samples / 32) * 1
            final_loss = slim.learning.train(
                train_op,
                logdir=train_dir,
                init_fn=self.get_init_fn(checkpoint_path),
                number_of_steps=number_of_steps)

            print('Finished training. Last batch loss %f' % final_loss)
        return
Beispiel #32
0
def getResult(photo_filenames):
    labels = []
    maps = getMaps(LABELS_TXT)
    with tf.Graph().as_default():
        tf.logging.set_verbosity(tf.logging.INFO)
        input_x = tf.placeholder(dtype=tf.string)

        image_raw = tf.image.decode_jpeg(input_x, channels=3)
        processed_image = inception_preprocessing.preprocess_image(
            image_raw, 320, 320, is_training=False)
        processed_images = tf.expand_dims(processed_image, 0)

        with slim.arg_scope(inception.inception_v4_arg_scope()):
            logits, _ = inception.inception_v4(processed_images,
                                               num_classes=100,
                                               is_training=False)

        probabilities = tf.nn.softmax(logits)

        init_fn = slim.assign_from_checkpoint_fn(
            checkpoint_path, slim.get_model_variables('InceptionV4'))

        with tf.Session() as sess:
            init_fn(sess)
            with open(photo_filenames, 'rb') as g:
                content = g.read()
            probabilities_val = sess.run([probabilities],
                                         feed_dict={input_x: content})
            p_val = np.squeeze(probabilities_val)
            #top_k = p_val.argsort()[-5:][::-1]
            #print(top_k)
            sorted_inds = [
                i[0] for i in sorted(
                    enumerate(p_val), key=lambda x: x[1], reverse=True)
            ][:5]

            label = ''
            for indx in sorted_inds:
                label += maps[str(indx)]

            labels.append(label)
    return labels, labels[0]
    def use_inceptionv4(self):
        image_size = inception.inception_v4.default_image_size
        img_path = "../../data/misec_images/EnglishCockerSpaniel_simon.jpg"
        checkpoint_path = "../../data/trained_models/inception_v4/inception_v4.ckpt"

        with tf.Graph().as_default():

            image_string = tf.read_file(img_path)
            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_v4_arg_scope()):
                logits, _ = inception.inception_v4(processed_images,
                                                   num_classes=1001,
                                                   is_training=False)
            probabilities = tf.nn.softmax(logits)

            init_fn = slim.assign_from_checkpoint_fn(
                checkpoint_path, slim.get_model_variables('InceptionV4'))

            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])
                ]
                self.disp_names(sorted_inds, probabilities)

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

        return
Beispiel #34
0
    def __init__(self, name):
        super(Classifier, self).__init__(name)
        maybe_download_and_extract()
        import logging
        from logging.handlers import RotatingFileHandler
        file_handler = RotatingFileHandler(FLAGS.log,
                                           maxBytes=1024 * 1024 * 100,
                                           backupCount=20)
        file_handler.setLevel(logging.INFO)
        formatter = logging.Formatter(
            "%(asctime)s - %(name)s - %(levelname)s - %(message)s")
        file_handler.setFormatter(formatter)
        self.logger.addHandler(file_handler)
        self.names = imagenet.create_readable_names_for_imagenet_labels()
        self.image_size = inception.inception_v4.default_image_size

        self.image_str_placeholder = tf.placeholder(tf.string)
        image = tf.image.decode_jpeg(self.image_str_placeholder, channels=3)
        processed_image = inception_preprocessing.preprocess_image(
            image, self.image_size, self.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_v4_arg_scope()):
            logits, _ = inception.inception_v4(processed_images,
                                               num_classes=1001,
                                               is_training=False)
        self.probabilities = tf.nn.softmax(logits)

        dest_directory = FLAGS.model_dir
        init_fn = slim.assign_from_checkpoint_fn(
            os.path.join(dest_directory, 'inception_v4.ckpt'),
            slim.get_model_variables('InceptionV4'))

        self.sess = tf.Session()
        init_fn(self.sess)
Beispiel #35
0
def export():

	with tf.Graph().as_default():

		# build inference model

		# imagenet labels
		names = imagenet.create_readable_names_for_imagenet_labels()

		names_tensor = tf.constant(list(names.values()))

		names_lookup_table = tf.contrib.lookup.index_to_string_table_from_tensor(names_tensor)

		# input transformation
		serialized_tf_example = tf.placeholder(tf.string, name="tf_example")
		feature_configs = {
			"image/encoded": tf.FixedLenFeature(shape=[], dtype=tf.string),
		}
		tf_example = tf.parse_example(serialized_tf_example, feature_configs)
		jpegs      = tf_example["image/encoded"]
		images     = tf.map_fn(preprocess_image, jpegs, dtype=tf.float32)

		# run inference
		with slim.arg_scope(inception.inception_v4_arg_scope()):
			# inception v4 models
			logits, end_points = inception.inception_v4(images, num_classes=NUM_CLASSES, is_training=False)
			# logits = tf.Print(logits, [logits])

		probs = tf.nn.softmax(logits)

		# transform output to topk result
		topk_probs, topk_indices = tf.nn.top_k(probs, NUM_TOP_CLASSES)

		topk_names = names_lookup_table.lookup(tf.to_int64(topk_indices))

		init_fn = slim.assign_from_checkpoint_fn(
			tf.train.latest_checkpoint(FLAGS.checkpoint_dir),
			slim.get_model_variables(),
		)

		# sess config
		config = tf.ConfigProto(
			gpu_options={ "allow_growth": 1, },
			allow_soft_placement=True,
			log_device_placement=False,
		)

		with tf.Session(config=config) as sess:

			init_fn(sess)

			# # to print out all the tensornames in the attached layers to inception V3
			# for node_tensor in tf.get_default_graph().as_graph_def().node:
			#   if str(node_tensor.name).startswith('InceptionV4/Logits'):
			#     print str(node_tensor.name)

			prelogits = sess.graph.get_tensor_by_name("InceptionV4/Logits/Predictions:0")

			# an optional alternative
			# prelogits = end_points['PreLogitsFlatten']

			# export inference model.
			output_path = os.path.join(
				tf.compat.as_bytes(FLAGS.output_dir),
				tf.compat.as_bytes(str(FLAGS.model_version))
			)
			print("Exporting trained model to", output_path)
			builder = tf.saved_model.builder.SavedModelBuilder(output_path)

			# build the signature_def_map.
			predict_inputs_tensor_info   = tf.saved_model.utils.build_tensor_info(jpegs)
			classes_output_tensor_info   = tf.saved_model.utils.build_tensor_info(topk_names)
			scores_output_tensor_info    = tf.saved_model.utils.build_tensor_info(topk_probs)
			prelogits_output_tensor_info = tf.saved_model.utils.build_tensor_info(prelogits)

			prediction_signature = (
				tf.saved_model.signature_def_utils.build_signature_def(
					inputs={
						"images":      predict_inputs_tensor_info
					},
					outputs={
						"classes":     classes_output_tensor_info,
						"scores":       scores_output_tensor_info,
						"prelogits": prelogits_output_tensor_info,
					},
					method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME
				)
			)

			legacy_init_op = tf.group(tf.tables_initializer(), name="legacy_init_op")

			builder.add_meta_graph_and_variables(
				sess, [tf.saved_model.tag_constants.SERVING],
				signature_def_map={ "predict_images": prediction_signature, },
				legacy_init_op=legacy_init_op
			)

			builder.save()

			print("Successfully exported model to %s" % FLAGS.output_dir)
# checkpoints_dir = '/Users/zhangxin/data_public/goolenet/v4' # inception_v4.ckpt
checkpoints_dir = '/Users/zhangxin/gitlab/CarRecognition/_tmp/Train/zhangkan/inception_v4_recognition/'
img_file = '/Users/zhangxin/pic/car.jpg'
image_string = open(img_file, 'r').read()


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_v4_arg_scope()):
        logits, _ = inception.inception_v4(processed_images, num_classes=1001, is_training=False)
    probabilities = tf.nn.softmax(logits)
    
    # init_fn = slim.assign_from_checkpoint_fn(
    #     os.path.join(checkpoints_dir, 'inception_v4.ckpt'),
    #     slim.get_model_variables('InceptionV4'))
    
    
    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))
Beispiel #37
0
if os.path.isdir(images_conv_feats_save_path) is False:
    os.mkdir(images_conv_feats_save_path)
if os.path.isdir(images_fc_feats_save_path) is False:
    os.mkdir(images_fc_feats_save_path)

tf_image = tf.placeholder(tf.string, None)
image = tf.image.decode_jpeg(tf_image, 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_v4_arg_scope()):
    tf_feats_att, tf_feats_fc = inception.inception_v4(processed_images,
                                                       num_classes=1001,
                                                       is_training=False,
                                                       create_aux_logits=False)
init_fn = slim.assign_from_checkpoint_fn(
    os.path.join(checkpoints_dir, 'inception_v4.ckpt'),
    slim.get_model_variables('InceptionV4'))

with tf.Session() as sess:
    init_fn(sess)

    for idx, image_path in enumerate(images_lists):
        start_time = time.time()

        image_name = os.path.basename(image_path)

        url = 'file://' + image_path
        image_string = urlopen(url).read()
Beispiel #38
0
    # the network.
    processed_image = inception_preprocessing.preprocess_image(
        image, image_size, image_size, is_training=False)
    #print processed_image
    # Networks accept images in batches.
    # The first dimension usually represents the batch size.
    # In our case the batch size is one.
    processed_images = tf.expand_dims(processed_image, 0)

    # Create the model, use the default arg scope to configure
    # the batch norm parameters. arg_scope is a very conveniet
    # feature of slim library -- you can define default
    # parameters for layers -- like stride, padding etc.
    with slim.arg_scope(inception.inception_v4_arg_scope()):
        logits, _ = inception.inception_v4(processed_images,
                                           num_classes=1001,
                                           is_training=False)

    # In order to get probabilities we apply softmax on the output.
    probabilities = tf.nn.softmax(logits)

    # Create a function that reads the network weights
    # from the checkpoint file that you downloaded.
    # We will run it in session later.
    #print slim.get_model_variables()
    init_fn = slim.assign_from_checkpoint_fn(
        os.path.join(checkpoints_dir, 'inception_v4.ckpt'),
        slim.get_model_variables('InceptionV4'))

    with tf.Session() as sess:
Beispiel #39
0
def main(_):
  batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3]
  num_classes = 1001

  # max_epsilon over checking
  # get original images
  origin_img_list=np.sort(glob.glob(FLAGS.origin_img_dir+"*.png"));
  origin_imgs=np.zeros((len(origin_img_list),FLAGS.image_height,FLAGS.image_width,3),dtype=float);
  for i in range(len(origin_img_list)):
    origin_imgs[i]=imread(origin_img_list[i],mode='RGB').astype(np.float);
  # get adv images
  adv_img_list=np.sort(glob.glob(FLAGS.input_dir+"*.png"));
  adv_imgs=np.zeros((len(adv_img_list),FLAGS.image_height,FLAGS.image_width,3),dtype=float);
  for i in range(len(adv_img_list)):
    adv_imgs[i]=imread(adv_img_list[i],mode='RGB').astype(np.float);
  epsilon_list=np.linalg.norm(np.reshape(abs(origin_imgs-adv_imgs),[-1,FLAGS.image_height*FLAGS.image_width*3]),ord=np.inf,axis=1);
  #print(epsilon_list);exit(1);
  over_epsilon_list=np.zeros((len(origin_img_list),2),dtype=object);
  cnt=0;
  for i in range(len(origin_img_list)):
    file_name=origin_img_list[i].split("/")[-1];
    file_name=file_name.split(".")[0];
    over_epsilon_list[i,0]=file_name;
    if(epsilon_list[i]>FLAGS.max_epsilon):
      over_epsilon_list[i,1]="1";
      cnt+=1;
  tf.logging.set_verbosity(tf.logging.INFO)

  with tf.Graph().as_default():
    # Prepare graph
    x_input = tf.placeholder(tf.float32, shape=batch_shape)

    if(FLAGS.checkpoint_file_name=="inception_v3.ckpt"):
      with slim.arg_scope(inception.inception_v3_arg_scope()):
        _, end_points = inception.inception_v3(
            x_input, num_classes=num_classes, is_training=False)
      predicted_labels = tf.argmax(end_points['Predictions'], 1)
    elif(FLAGS.checkpoint_file_name=="inception_v4.ckpt"):
      with slim.arg_scope(inception.inception_v4_arg_scope()):
        _, end_points = inception.inception_v4(
            x_input, num_classes=num_classes, is_training=False)
      predicted_labels = tf.argmax(end_points['Predictions'], 1)
    elif(FLAGS.checkpoint_file_name=="inception_resnet_v2_2016_08_30.ckpt"):
      with slim.arg_scope(inception.inception_resnet_v2_arg_scope()):
        _, end_points = inception.inception_resnet_v2(
            x_input, num_classes=num_classes, is_training=False)
      predicted_labels = tf.argmax(end_points['Predictions'], 1)
    elif(FLAGS.checkpoint_file_name=="resnet_v2_101.ckpt"):
      x_input2 = tf.image.resize_bilinear(x_input,[224,224],align_corners=False);
      with slim.arg_scope(resnet_utils.resnet_arg_scope()):
        _, end_points = resnet_v2.resnet_v2_101(
            x_input2, num_classes=num_classes, is_training=False)
      predicted_labels = tf.argmax(end_points['predictions'], 1)
    elif(FLAGS.checkpoint_file_name=="resnet_v2_50.ckpt"):
      x_input2 = tf.image.resize_bilinear(x_input,[224,224],align_corners=False);
      with slim.arg_scope(resnet_utils.resnet_arg_scope()):
        _, end_points = resnet_v2.resnet_v2_50(
            x_input2, num_classes=num_classes, is_training=False)
      predicted_labels = tf.argmax(end_points['predictions'], 1)
    elif(FLAGS.checkpoint_file_name=="resnet_v2_152.ckpt"):
      x_input2 = tf.image.resize_bilinear(x_input,[224,224],align_corners=False);
      with slim.arg_scope(resnet_utils.resnet_arg_scope()):
        _, end_points = resnet_v2.resnet_v2_152(
            x_input2, num_classes=num_classes, is_training=False)
      predicted_labels = tf.argmax(end_points['predictions'], 1)
    elif(FLAGS.checkpoint_file_name=="inception_v1.ckpt"):
      x_input2 = tf.image.resize_bilinear(x_input,[224,224],align_corners=False);
      with slim.arg_scope(inception.inception_v1_arg_scope()):
        _, end_points = inception.inception_v1(
            x_input2, num_classes=num_classes, is_training=False)
      predicted_labels = tf.argmax(end_points['Predictions'], 1)
    elif(FLAGS.checkpoint_file_name=="inception_v2.ckpt"):
      x_input2 = tf.image.resize_bilinear(x_input,[224,224],align_corners=False);
      with slim.arg_scope(inception.inception_v2_arg_scope()):
        _, end_points = inception.inception_v2(
            x_input2, num_classes=num_classes, is_training=False)
      predicted_labels = tf.argmax(end_points['Predictions'], 1)

    # Resnet v1 and vgg are not working now
    elif(FLAGS.checkpoint_file_name=="vgg_16.ckpt"):
      x_input_list=tf.unstack(x_input,FLAGS.batch_size,0);
      for i in range(FLAGS.batch_size):
        x_input_list[i]=vgg_preprocessing.preprocess_image(x_input_list[i],224,224);
      x_input2=tf.stack(x_input_list,0);
      with slim.arg_scope(vgg.vgg_arg_scope()):
        _, end_points = vgg.vgg_16(
            x_input2, num_classes=num_classes-1, is_training=False)
      predicted_labels = tf.argmax(end_points['vgg_16/fc8'], 1)+1
    elif(FLAGS.checkpoint_file_name=="vgg_19.ckpt"):
      x_input_list=tf.unstack(x_input,FLAGS.batch_size,0);
      for i in range(FLAGS.batch_size):
        x_input_list[i]=vgg_preprocessing.preprocess_image(x_input_list[i],224,224);
      x_input2=tf.stack(x_input_list,0);
      with slim.arg_scope(vgg.vgg_arg_scope()):
        _, end_points = vgg.vgg_19(
            x_input2, num_classes=num_classes-1, is_training=False)
      predicted_labels = tf.argmax(end_points['vgg_19/fc8'], 1)+1
    elif(FLAGS.checkpoint_file_name=="resnet_v1_50.ckpt"):
      x_input_list=tf.unstack(x_input,FLAGS.batch_size,0);
      for i in range(FLAGS.batch_size):
        x_input_list[i]=vgg_preprocessing.preprocess_image(x_input_list[i],224,224);
      x_input2=tf.stack(x_input_list,0);
      with slim.arg_scope(resnet_utils.resnet_arg_scope()):
        _, end_points = resnet_v1.resnet_v1_50(
            x_input, num_classes=num_classes-1, is_training=False)
      predicted_labels = tf.argmax(end_points['predictions'], 1)+1
    elif(FLAGS.checkpoint_file_name=="resnet_v1_101.ckpt"):
      x_input_list=tf.unstack(x_input,FLAGS.batch_size,0);
      for i in range(FLAGS.batch_size):
        x_input_list[i]=vgg_preprocessing.preprocess_image(x_input_list[i],224,224);
      x_input2=tf.stack(x_input_list,0);
      with slim.arg_scope(resnet_utils.resnet_arg_scope()):
        _, end_points = resnet_v1.resnet_v1_101(
            x_input2, num_classes=num_classes-1, is_training=False)
      predicted_labels = tf.argmax(end_points['predictions'], 1)+1
    elif(FLAGS.checkpoint_file_name=="resnet_v1_152.ckpt"):
      x_input_list=tf.unstack(x_input,FLAGS.batch_size,0);
      for i in range(FLAGS.batch_size):
        x_input_list[i]=vgg_preprocessing.preprocess_image(x_input_list[i],224,224);
      x_input2=tf.stack(x_input_list,0);
      with slim.arg_scope(resnet_utils.resnet_arg_scope()):
        _, end_points = resnet_v1.resnet_v1_152(
            x_input2, num_classes=num_classes-1, is_training=False)
      predicted_labels = tf.argmax(end_points['predictions'], 1)+1
    
    # Run computation
    saver = tf.train.Saver(slim.get_model_variables())
    session_creator = tf.train.ChiefSessionCreator(
        scaffold=tf.train.Scaffold(saver=saver),
        checkpoint_filename_with_path=FLAGS.checkpoint_path+FLAGS.checkpoint_file_name,
        master=FLAGS.master)

    f=open(FLAGS.true_label,"r");
    t_label_list=np.array([i[:-1].split(",") for i in f.readlines()]);
    
    score=0;
    with tf.train.MonitoredSession(session_creator=session_creator) as sess:
      with tf.gfile.Open(FLAGS.output_file, 'w') as out_file:
        for filenames, images in load_images(FLAGS.input_dir, batch_shape):
          labels = sess.run(predicted_labels, feed_dict={x_input: images})
          for filename, label in zip(filenames, labels):
            f_name=filename.split(".")[0];
            t_label=int(t_label_list[t_label_list[:,0]==f_name,1][0]);
            if(t_label!=label):
              if(over_epsilon_list[over_epsilon_list[:,0]==f_name,1]!="1"):
                score+=1;
            #out_file.write('{0},{1}\n'.format(filename, label))
  print("Over max epsilon#: "+str(cnt));
  print(str(FLAGS.max_epsilon)+" max epsilon Score: "+str(score));
Beispiel #40
0
    # Open specified url and load image as a string
    #image_string = urllib2.urlopen(url).read()

    # Decode string into matrix with intensity values
    #image = tf.image.decode_jpeg(image_string, channels=3)
    X = tf.placeholder(tf.float32,
                       shape=[None, image_size, image_size, image_channel])
    images, processed_images = get_data()
    # Create the model, use the default arg scope to configure
    # the batch norm parameters. arg_scope is a very conveniet
    # feature of slim library -- you can define default
    # parameters for layers -- like stride, padding etc.
    with slim.arg_scope(inception.inception_v4_arg_scope()):
        logits, _ = inception.inception_v4(X,
                                           num_classes=1001,
                                           is_training=False)
    # In order to get probabilities we apply softmax on the output.
    probabilities = tf.nn.softmax(logits)

    # Create a function that reads the network weights
    # from the checkpoint file that you downloaded.
    # We will run it in session later.
    #print slim.get_model_variables()
    init_fn = slim.assign_from_checkpoint_fn(
        os.path.join(checkpoints_dir, 'inception_v4.ckpt'),
        slim.get_model_variables('InceptionV4'))
    names = imagenet.create_readable_names_for_imagenet_labels()
    with tf.Session() as sess:

        # Load weights
def train():
    eps = 2.0 * float(FLAGS.max_epsilon) / 256.0
    tf.logging.set_verbosity(tf.logging.INFO)
    with tf.Graph().as_default():
        # Design architecture
        # input
        x_data = tf.placeholder(tf.float32,
                                [None, FLAGS.img_height, FLAGS.img_width, 3],
                                name="x_data")
        y_label = tf.placeholder(tf.float32, [None, FLAGS.num_classes],
                                 name="y_label")

        # generator
        x_generated, g_params = build_generator(x_data, FLAGS)
        x_generated = x_generated * eps

        x_generated = x_data + x_generated

        # discriminator(inception v3)
        with slim.arg_scope(inception.inception_v3_arg_scope()):
            _, end_points = inception.inception_v3(
                x_generated, num_classes=FLAGS.num_classes, is_training=False)
        predicted_labels = tf.argmax(end_points['Predictions'], 1)
        predicted_logits = end_points['Logits']
        disc_var_list = slim.get_model_variables()
        # discriminator(resnet v2 50)
        x_generated2 = tf.image.resize_bilinear(x_generated, [224, 224],
                                                align_corners=False)
        with slim.arg_scope(resnet_utils.resnet_arg_scope()):
            _, end_points2 = resnet_v2.resnet_v2_50(
                x_generated2, num_classes=FLAGS.num_classes, is_training=False)
        predicted_labels2 = tf.argmax(end_points2['predictions'], 1)
        predicted_logits2 = end_points2['predictions']
        disc_var_list2 = slim.get_model_variables()[len(disc_var_list):]
        # discriminator(resnet v2 152)
        with slim.arg_scope(resnet_utils.resnet_arg_scope()):
            _, end_points3 = resnet_v2.resnet_v2_152(
                x_generated2, num_classes=FLAGS.num_classes, is_training=False)
        predicted_labels3 = tf.argmax(end_points3['predictions'], 1)
        predicted_logits3 = end_points3['predictions']
        disc_var_list3 = slim.get_model_variables()[(len(disc_var_list) +
                                                     len(disc_var_list2)):]
        # discriminator(resnet v2 101)
        with slim.arg_scope(resnet_utils.resnet_arg_scope()):
            _, end_points4 = resnet_v2.resnet_v2_101(
                x_generated2, num_classes=FLAGS.num_classes, is_training=False)
        predicted_labels4 = tf.argmax(end_points4['predictions'], 1)
        predicted_logits4 = end_points4['predictions']
        disc_var_list4 = slim.get_model_variables()[(len(disc_var_list) +
                                                     len(disc_var_list2) +
                                                     len(disc_var_list3)):]
        # discriminator(inception v4)
        with slim.arg_scope(inception.inception_v4_arg_scope()):
            _, end_points5 = inception.inception_v4(
                x_generated, num_classes=FLAGS.num_classes, is_training=False)
        predicted_labels5 = tf.argmax(end_points5['Predictions'], 1)
        predicted_logits5 = end_points['Logits']
        disc_var_list5 = slim.get_model_variables()[(len(disc_var_list) +
                                                     len(disc_var_list2) +
                                                     len(disc_var_list3) +
                                                     len(disc_var_list4)):]
        """
    # discriminator(vgg 19)
    with slim.arg_scope(vgg.vgg_arg_scope()):
      _, end_points3 = vgg.vgg_19(
          x_generated2, num_classes=FLAGS.num_classes, is_training=False)
    predicted_labels3 = tf.argmax(end_points3['vgg_19/fc8'], 1);
    predicted_logits3 = end_points3['vgg_19/fc8'];
    disc_var_list3=slim.get_model_variables()[(len(disc_var_list)+len(disc_var_list2)):];
    """

        # loss and optimizer
        gen_acc = tf.reduce_mean(
            tf.cast(tf.equal(predicted_labels, tf.argmax(y_label, 1)),
                    tf.float32))
        cross_entropy = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(logits=predicted_logits,
                                                    labels=y_label))
        gen_acc2 = tf.reduce_mean(
            tf.cast(tf.equal(predicted_labels2, tf.argmax(y_label, 1)),
                    tf.float32))
        cross_entropy2 = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(logits=predicted_logits2,
                                                    labels=y_label))
        gen_acc3 = tf.reduce_mean(
            tf.cast(tf.equal(predicted_labels3, tf.argmax(y_label, 1)),
                    tf.float32))
        cross_entropy3 = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(logits=predicted_logits3,
                                                    labels=y_label))
        gen_acc4 = tf.reduce_mean(
            tf.cast(tf.equal(predicted_labels4, tf.argmax(y_label, 1)),
                    tf.float32))
        cross_entropy4 = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(logits=predicted_logits4,
                                                    labels=y_label))
        gen_acc5 = tf.reduce_mean(
            tf.cast(tf.equal(predicted_labels5, tf.argmax(y_label, 1)),
                    tf.float32))
        cross_entropy5 = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(logits=predicted_logits5,
                                                    labels=y_label))
        infi_norm = tf.reduce_mean(
            tf.norm(tf.reshape(abs(x_data - x_generated),
                               [-1, FLAGS.img_size]),
                    ord=np.inf,
                    axis=1))

        g_loss = -1 * cross_entropy - 1 * cross_entropy2 - 1 * cross_entropy3 - 1 * cross_entropy4 - 1 * cross_entropy5

        optimizer = tf.train.AdamOptimizer(0.0001)

        g_trainer = optimizer.minimize(g_loss, var_list=g_params)

        # get the data and label
        img_list = np.sort(glob.glob(FLAGS.input_folder + "*.png"))
        total_data = np.zeros(
            (len(img_list), FLAGS.img_height, FLAGS.img_width, 3), dtype=float)
        for i in range(len(img_list)):
            total_data[i] = imread(img_list[i], mode='RGB').astype(
                np.float) / 255.0
            total_data[i] = total_data[i] * 2.0 - 1.0
            # 0~1 -> -1~1
        val_data = np.copy(total_data[0])
        f = open(FLAGS.label_folder + "true_label", "r")
        total_label2 = np.array([i[:-1].split(",")[1] for i in f.readlines()],
                                dtype=int)
        total_label = np.zeros((len(total_data), FLAGS.num_classes), dtype=int)
        for i in range(len(total_data)):
            total_label[i, total_label2[i]] = 1
        val_label = np.copy(total_label[0])

        # shuffle
        total_idx = range(len(total_data))
        np.random.shuffle(total_idx)
        total_data = total_data[total_idx]
        total_label = total_label[total_idx]

        # Run computation
        saver = tf.train.Saver(disc_var_list)
        saver2 = tf.train.Saver(disc_var_list2)
        saver3 = tf.train.Saver(disc_var_list3)
        saver_gen = tf.train.Saver(g_params)

        # initialization
        init = tf.global_variables_initializer()
        with tf.Session() as sess:
            sess.run(init)
            saver.restore(sess,
                          FLAGS.checkpoint_path + FLAGS.checkpoint_file_name)
            saver2.restore(sess,
                           FLAGS.checkpoint_path + FLAGS.checkpoint_file_name2)
            saver3.restore(sess,
                           FLAGS.checkpoint_path + FLAGS.checkpoint_file_name3)
            # training
            for i in range(FLAGS.max_epoch):
                tr_infi = 0
                tr_ce = 0
                tr_gen_acc = 0
                tr_ce2 = 0
                tr_gen_acc2 = 0
                tr_ce3 = 0
                tr_gen_acc3 = 0
                tr_ce4 = 0
                tr_gen_acc4 = 0
                tr_ce5 = 0
                tr_gen_acc5 = 0
                for j in range(len(total_data) / FLAGS.batch_size):
                    batch_data = total_data[j * FLAGS.batch_size:(j + 1) *
                                            FLAGS.batch_size]
                    batch_label = total_label[j * FLAGS.batch_size:(j + 1) *
                                              FLAGS.batch_size]
                    acc_p3, ce_p3, acc_p2, ce_p2, acc_p, ce_p, infi_p, _ = sess.run(
                        [
                            gen_acc3, cross_entropy3, gen_acc2, cross_entropy2,
                            gen_acc, cross_entropy, infi_norm, g_trainer
                        ],
                        feed_dict={
                            x_data: batch_data,
                            y_label: batch_label
                        })
                    tr_infi += infi_p
                    tr_ce += ce_p
                    tr_gen_acc += acc_p
                    tr_ce2 += ce_p2
                    tr_gen_acc2 += acc_p2
                    tr_ce3 += ce_p3
                    tr_gen_acc3 += acc_p3
                print(
                    str(i + 1) + " Epoch InfiNorm:" + str(tr_infi / (j + 1)) +
                    ",CE: " + str(tr_ce / (j + 1)) + ",Acc: " +
                    str(tr_gen_acc / (j + 1)) + ",CE2: " + str(tr_ce2 /
                                                               (j + 1)) +
                    ",Acc2: " + str(tr_gen_acc2 / (j + 1)) + ",CE3: " +
                    str(tr_ce3 / (j + 1)) + ",Acc3: " + str(tr_gen_acc3 /
                                                            (j + 1)))
                total_idx = range(len(total_data))
                np.random.shuffle(total_idx)
                total_data = total_data[total_idx]
                total_label = total_label[total_idx]
            saver_gen.save(
                sess, "my-models_iv3_rv250_rv2152/my-model_" +
                str(FLAGS.max_epsilon) + ".ckpt")
Beispiel #42
0
def main(_):
    batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3]
    num_classes = 1001
    ensemble_type = FLAGS.ensemble_type

    tf.logging.set_verbosity(tf.logging.INFO)

    checkpoint_path_list = [
        FLAGS.checkpoint_path_inception_v1, FLAGS.checkpoint_path_inception_v2,
        FLAGS.checkpoint_path_inception_v3, FLAGS.checkpoint_path_inception_v4,
        FLAGS.checkpoint_path_inception_resnet_v2,
        FLAGS.checkpoint_path_resnet_v1_101,
        FLAGS.checkpoint_path_resnet_v1_152,
        FLAGS.checkpoint_path_resnet_v2_101,
        FLAGS.checkpoint_path_resnet_v2_152, FLAGS.checkpoint_path_vgg_16,
        FLAGS.checkpoint_path_vgg_19
    ]
    normalization_method = [
        'default', 'default', 'default', 'default', 'global', 'caffe_rgb',
        'caffe_rgb', 'default', 'default', 'caffe_rgb', 'caffe_rgb'
    ]
    pred_list = []
    for idx, checkpoint_path in enumerate(checkpoint_path_list, 1):
        with tf.Graph().as_default():
            if int(FLAGS.test_idx) == 20 and idx in [3]:
                continue
            if int(FLAGS.test_idx) in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
                                       ] and int(FLAGS.test_idx) != idx:
                continue
            # Prepare graph
            if idx in [1, 2, 6, 7, 10, 11]:
                _x_input = tf.placeholder(tf.float32, shape=batch_shape)
                x_input = tf.image.resize_images(_x_input, [224, 224])
            else:
                _x_input = tf.placeholder(tf.float32, shape=batch_shape)
                x_input = _x_input

            x_input = image_normalize(x_input, normalization_method[idx - 1])

            if idx == 1:
                with slim.arg_scope(inception.inception_v1_arg_scope()):
                    _, end_points = inception.inception_v1(
                        x_input, num_classes=num_classes, is_training=False)
            elif idx == 2:
                with slim.arg_scope(inception.inception_v2_arg_scope()):
                    _, end_points = inception.inception_v2(
                        x_input, num_classes=num_classes, is_training=False)
            elif idx == 3:
                with slim.arg_scope(inception.inception_v3_arg_scope()):
                    _, end_points = inception.inception_v3(
                        x_input, num_classes=num_classes, is_training=False)
            elif idx == 4:
                with slim.arg_scope(inception.inception_v4_arg_scope()):
                    _, end_points = inception.inception_v4(
                        x_input, num_classes=num_classes, is_training=False)
            elif idx == 5:
                with slim.arg_scope(inception.inception_resnet_v2_arg_scope()):
                    _, end_points = inception.inception_resnet_v2(
                        x_input, num_classes=num_classes, is_training=False)
            elif idx == 6:
                with slim.arg_scope(resnet_v1.resnet_arg_scope()):
                    _, end_points = resnet_v1.resnet_v1_101(x_input,
                                                            num_classes=1000,
                                                            is_training=False)
            elif idx == 7:
                with slim.arg_scope(resnet_v1.resnet_arg_scope()):
                    _, end_points = resnet_v1.resnet_v1_152(x_input,
                                                            num_classes=1000,
                                                            is_training=False)
            elif idx == 8:
                with slim.arg_scope(resnet_v2.resnet_arg_scope()):
                    _, end_points = resnet_v2.resnet_v2_101(
                        x_input, num_classes=num_classes, is_training=False)
            elif idx == 9:
                with slim.arg_scope(resnet_v2.resnet_arg_scope()):
                    _, end_points = resnet_v2.resnet_v2_152(
                        x_input, num_classes=num_classes, is_training=False)
            elif idx == 10:
                with slim.arg_scope(vgg.vgg_arg_scope()):
                    _, end_points = vgg.vgg_16(x_input,
                                               num_classes=1000,
                                               is_training=False)
                    end_points['predictions'] = tf.nn.softmax(
                        end_points['vgg_16/fc8'])
            elif idx == 11:
                with slim.arg_scope(vgg.vgg_arg_scope()):
                    _, end_points = vgg.vgg_19(x_input,
                                               num_classes=1000,
                                               is_training=False)
                    end_points['predictions'] = tf.nn.softmax(
                        end_points['vgg_19/fc8'])

            #end_points = tf.reduce_mean([end_points1['Predictions'], end_points2['Predictions'], end_points3['Predictions'], end_points4['Predictions']], axis=0)

            #predicted_labels = tf.argmax(end_points, 1)

            # Run computation
            saver = tf.train.Saver(slim.get_model_variables())
            session_creator = tf.train.ChiefSessionCreator(
                scaffold=tf.train.Scaffold(saver=saver),
                checkpoint_filename_with_path=checkpoint_path,
                master=FLAGS.master)

            pred_in = []
            filenames_list = []
            with tf.train.MonitoredSession(
                    session_creator=session_creator) as sess:
                for filenames, images in load_images(FLAGS.input_dir,
                                                     batch_shape):
                    #if idx in [1,2,6,7,10,11]:
                    #  # 16x299x299x3
                    #  images = zoom(images, (1, 0.7491638795986622, 0.7491638795986622, 1), order=2)
                    filenames_list.extend(filenames)
                    end_points_dict = sess.run(end_points,
                                               feed_dict={_x_input: images})
                    if idx in [6, 7, 10, 11]:
                        end_points_dict['predictions'] = \
                                      np.concatenate([np.zeros([FLAGS.batch_size, 1]),
                                                      np.array(end_points_dict['predictions'].reshape(-1, 1000))],
                                                      axis=1)
                    try:
                        pred_in.extend(end_points_dict['Predictions'].reshape(
                            -1, num_classes))
                    except KeyError:
                        pred_in.extend(end_points_dict['predictions'].reshape(
                            -1, num_classes))
            pred_list.append(pred_in)

    if ensemble_type == 'mean':
        pred = np.mean(pred_list, axis=0)
        labels = np.argmax(
            pred, axis=1
        )  # model_num X batch X class_num ==(np.mean)==> batch X class_num ==(np.argmax)==> batch
    elif ensemble_type == 'vote':
        pred = np.argmax(
            pred_list, axis=2
        )  # model_num X batch X class_num ==(np.mean)==> batch X class_num ==(np.argmax)==> batch
        labels = np.median(pred, axis=0)
    with tf.gfile.Open(FLAGS.output_file, 'w') as out_file:
        for filename, label in zip(filenames_list, labels):
            out_file.write('{0},{1}\n'.format(filename, label))
Beispiel #43
0
with tf.Graph().as_default():

    # Create model architecture

    from scipy import misc
    img = misc.imread('lena_299.png')
    print(img.shape)

    inputs = np.zeros((1, 299, 299, 3), dtype=np.float32)

    inputs[0] = img
    inputs = tf.stack(inputs)

    with slim.arg_scope(inception.inception_v4_arg_scope()):
        logits, _ = inception.inception_v4(inputs,
                                           num_classes=num_classes,
                                           is_training=False)

    with tf.Session() as sess:

        # Initialize model
        init_fn = slim.assign_from_checkpoint_fn(
            os.path.join(checkpoints_dir,
                         'china-drink-sku_20200214_model.ckpt-200000'),
            slim.get_model_variables('InceptionV4'))

        init_fn(sess)

        # Display model variables
        for v in slim.get_model_variables():
            print('name = {}, shape = {}'.format(v.name, v.get_shape()))