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_resnet_v2_arg_scope()):
      inception.inception_resnet_v2(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_resnet_v2_arg_scope(batch_norm_scale=True)):
      inception.inception_resnet_v2(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)
Ejemplo n.º 3
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_resnet_v2(train_inputs, num_classes)
         eval_inputs = tf.random_uniform((eval_batch_size, height, width, 3))
         logits, _ = inception.inception_resnet_v2(eval_inputs, num_classes, is_training=False, reuse=True)
         predictions = tf.argmax(logits, 1)
         sess.run(tf.initialize_all_variables())
         output = sess.run(predictions)
         self.assertEquals(output.shape, (eval_batch_size,))
Ejemplo n.º 4
0
 def testVariablesSetDevice(self):
     batch_size = 5
     height, width = 299, 299
     num_classes = 1000
     with self.test_session():
         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_resnet_v2(inputs, num_classes)
         with tf.variable_scope("on_gpu"), tf.device("/gpu:0"):
             inception.inception_resnet_v2(inputs, num_classes)
         for v in tf.get_collection(tf.GraphKeys.VARIABLES, scope="on_cpu"):
             self.assertDeviceEqual(v.device, "/cpu:0")
         for v in tf.get_collection(tf.GraphKeys.VARIABLES, scope="on_gpu"):
             self.assertDeviceEqual(v.device, "/gpu:0")
 def testVariablesSetDevice(self):
   batch_size = 5
   height, width = 299, 299
   num_classes = 1000
   with self.test_session():
     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_resnet_v2(inputs, num_classes)
     with tf.variable_scope('on_gpu'), tf.device('/gpu:0'):
       inception.inception_resnet_v2(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')
Ejemplo n.º 6
0
def evaluate():
    """Eval KAGGLE-128 for a number of steps."""
    with tf.Graph().as_default() as g:
        # Get images and labels for KAGGLE-128.
        eval_data = FLAGS.eval_data
        images, labels = kaggle128.inputs(eval_data=eval_data)

        # Build a Graph that computes the logits predictions from the
        # inference model.
        logits, end_points = inception.inception_resnet_v2(
            images,
            num_classes=kaggle128.NUM_CLASSES,
            create_aux_logits=False,
            is_training=False)
        # Calculate predictions.
        top_k_op = tf.nn.in_top_k(logits, labels, 1)

        # Restore the moving average version of the learned variables for eval.
        variable_averages = tf.train.ExponentialMovingAverage(
            kaggle128.MOVING_AVERAGE_DECAY)
        variables_to_restore = variable_averages.variables_to_restore()
        saver = tf.train.Saver(variables_to_restore)

        # Build the summary operation based on the TF collection of Summaries.
        summary_op = tf.summary.merge_all()

        summary_writer = tf.summary.FileWriter(FLAGS.eval_dir, g)

        #while True:
        eval_once(saver, summary_writer, top_k_op, summary_op)
Ejemplo n.º 7
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_resnet_v2_arg_scope()):
            with tf.variable_scope(self.name):
                min_pooled = -tf.nn.max_pool(-x_input,
                                             ksize=[1, 10, 10, 1],
                                             strides=[1, 1, 1, 1],
                                             padding='SAME')
                max_pooled = tf.nn.max_pool(x_input,
                                            ksize=[1, 10, 10, 1],
                                            strides=[1, 1, 1, 1],
                                            padding='SAME')
                avg_pooled = (min_pooled + max_pooled) / 2
                compress_pooled = avg_pooled + (x_input - avg_pooled) * (
                    tf.sign(max_pooled - min_pooled - 0.3) + 1) / 2
                logits, end_points = inception.inception_resnet_v2(
                    compress_pooled,
                    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
def freeze_model(name):
    #checkpoints_dir = '/data/huang/behaviour/data/tfmodel'
    OUTPUT_PB_FILENAME = 'inception_resnet_v2_behaviour_371_9_25_{}k.pb'.format(name[0:3])
    NUM_CLASSES = 371  
    tensorName_v4='InceptionResnetV2/Logits/Predictions'
    
    
    
    with tf.Graph().as_default():
        
        image_placeholder = tf.placeholder(tf.float32, shape=(1,512,512,3), name='input_image')
        with slim.arg_scope(inception.inception_resnet_v2_arg_scope()):
            _, probabilities = inception.inception_resnet_v2(image_placeholder,
                                                             num_classes = NUM_CLASSES,
                                                             is_training=False)
    
        #model_path = tf.train.latest_checkpoint(checkpoints_dir)
        model_path = '/data/huang/behaviour/data/tfmodel/model_backup/model.ckpt-{}'.format(name)
    
        init_fn = slim.assign_from_checkpoint_fn(
            model_path,
            slim.get_model_variables())
    
        with tf.Session() as sess:
            # Now call the initialization function within the session
            init_fn(sess)
            constant_graph = convert_variables_to_constants(sess, sess.graph_def, ["input_image",tensorName_v4])
            tf.train.write_graph(constant_graph, '.', OUTPUT_PB_FILENAME, as_text=False)
Ejemplo n.º 9
0
def classify_from_url(url):
    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_resnet_v2_arg_scope()):
        logits, _ = inception.inception_resnet_v2(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, checkpoints_filename),
        slim.get_model_variables(model_name))
    
    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], names[index]))
 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_resnet_v2(train_inputs, num_classes)
     eval_inputs = tf.random_uniform((eval_batch_size, height, width, 3))
     logits, _ = inception.inception_resnet_v2(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,))
Ejemplo n.º 11
0
def inference(images, keep_prob, is_training):
    images = tf.reshape(images,
                        [-1, IMAGE_HEIGHT, IMAGE_WIDTH, 3])  # 256,256,3

    softmax_linear, end_points = inception.inception_resnet_v2(
        images, CLASSES_NUM)
    #  return softmax_linear
    return tf.reshape(softmax_linear, [-1, CLASSES_NUM])
Ejemplo n.º 12
0
 def testBuildLogits(self):
     batch_size = 5
     height, width = 299, 299
     num_classes = 1000
     with self.test_session():
         inputs = tf.random_uniform((batch_size, height, width, 3))
         logits, _ = inception.inception_resnet_v2(inputs, num_classes)
         self.assertTrue(logits.op.name.startswith("InceptionResnetV2/Logits"))
         self.assertListEqual(logits.get_shape().as_list(), [batch_size, num_classes])
Ejemplo n.º 13
0
 def testBuildLogits(self):
   batch_size = 5
   height, width = 299, 299
   num_classes = 1000
   with self.test_session():
     inputs = tf.random_uniform((batch_size, height, width, 3))
     logits, _ = inception.inception_resnet_v2(inputs, num_classes)
     self.assertTrue(logits.op.name.startswith('InceptionResnetV2/Logits'))
     self.assertListEqual(logits.get_shape().as_list(),
                          [batch_size, num_classes])
Ejemplo n.º 14
0
 def testHalfSizeImages(self):
     batch_size = 5
     height, width = 150, 150
     num_classes = 1000
     with self.test_session():
         inputs = tf.random_uniform((batch_size, height, width, 3))
         logits, end_points = inception.inception_resnet_v2(inputs, num_classes)
         self.assertTrue(logits.op.name.startswith("InceptionResnetV2/Logits"))
         self.assertListEqual(logits.get_shape().as_list(), [batch_size, num_classes])
         pre_pool = end_points["PrePool"]
         self.assertListEqual(pre_pool.get_shape().as_list(), [batch_size, 3, 3, 1536])
Ejemplo n.º 15
0
 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_resnet_v2(eval_inputs, num_classes, is_training=False)
         predictions = tf.argmax(logits, 1)
         sess.run(tf.initialize_all_variables())
         output = sess.run(predictions)
         self.assertEquals(output.shape, (batch_size,))
 def testBuildNoClasses(self):
   batch_size = 5
   height, width = 299, 299
   num_classes = None
   with self.test_session():
     inputs = tf.random_uniform((batch_size, height, width, 3))
     net, endpoints = inception.inception_resnet_v2(inputs, num_classes)
     self.assertTrue('AuxLogits' not in endpoints)
     self.assertTrue('Logits' not in endpoints)
     self.assertTrue(
         net.op.name.startswith('InceptionResnetV2/Logits/AvgPool'))
     self.assertListEqual(net.get_shape().as_list(), [batch_size, 1, 1, 1536])
 def testBuildWithoutAuxLogits(self):
   batch_size = 5
   height, width = 299, 299
   num_classes = 1000
   with self.test_session():
     inputs = tf.random_uniform((batch_size, height, width, 3))
     logits, endpoints = inception.inception_resnet_v2(inputs, num_classes,
                                                       create_aux_logits=False)
     self.assertTrue('AuxLogits' not in endpoints)
     self.assertTrue(logits.op.name.startswith('InceptionResnetV2/Logits'))
     self.assertListEqual(logits.get_shape().as_list(),
                          [batch_size, num_classes])
Ejemplo n.º 18
0
 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_resnet_v2(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, ))
Ejemplo n.º 19
0
 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_resnet_v2(inputs, num_classes)
         self.assertTrue(logits.op.name.startswith("InceptionResnetV2/Logits"))
         self.assertListEqual(logits.get_shape().as_list(), [None, num_classes])
         images = tf.random_uniform((batch_size, height, width, 3))
         sess.run(tf.initialize_all_variables())
         output = sess.run(logits, {inputs: images.eval()})
         self.assertEquals(output.shape, (batch_size, num_classes))
Ejemplo n.º 20
0
 def testHalfSizeImages(self):
   batch_size = 5
   height, width = 150, 150
   num_classes = 1000
   with self.test_session():
     inputs = tf.random_uniform((batch_size, height, width, 3))
     logits, end_points = inception.inception_resnet_v2(inputs, num_classes)
     self.assertTrue(logits.op.name.startswith('InceptionResnetV2/Logits'))
     self.assertListEqual(logits.get_shape().as_list(),
                          [batch_size, num_classes])
     pre_pool = end_points['PrePool']
     self.assertListEqual(pre_pool.get_shape().as_list(),
                          [batch_size, 3, 3, 1536])
 def testGlobalPool(self):
   batch_size = 1
   height, width = 330, 400
   num_classes = 1000
   with self.test_session():
     inputs = tf.random_uniform((batch_size, height, width, 3))
     logits, end_points = inception.inception_resnet_v2(inputs, num_classes)
     self.assertTrue(logits.op.name.startswith('InceptionResnetV2/Logits'))
     self.assertListEqual(logits.get_shape().as_list(),
                          [batch_size, num_classes])
     pre_pool = end_points['Conv2d_7b_1x1']
     self.assertListEqual(pre_pool.get_shape().as_list(),
                          [batch_size, 8, 11, 1536])
 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_resnet_v2(inputs, num_classes)
     self.assertTrue(logits.op.name.startswith('InceptionResnetV2/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))
Ejemplo n.º 23
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_resnet_v2_arg_scope()):
      with tf.variable_scope(self.ckpt):
        logits, end_points = inception.inception_resnet_v2(
            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
Ejemplo n.º 24
0
def inception_resnet_v2(inputs, is_training, opts):
    with slim.arg_scope(inception.inception_resnet_v2_arg_scope(
            weight_decay=opts.weight_decay,
            batch_norm_decay=opts.batch_norm_decay,
            batch_norm_epsilon=opts.batch_norm_epsilon,
            activation_fn=tf.nn.relu)):
        return inception.inception_resnet_v2(
            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)
Ejemplo n.º 25
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[4])
     with slim.arg_scope(inception.inception_resnet_v2_arg_scope()):
         _, end_points = inception.inception_resnet_v2(
             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
Ejemplo n.º 26
0
 def testBuildEndPoints(self):
     batch_size = 5
     height, width = 299, 299
     num_classes = 1000
     with self.test_session():
         inputs = tf.random_uniform((batch_size, height, width, 3))
         _, end_points = inception.inception_resnet_v2(inputs, num_classes)
         self.assertTrue("Logits" in end_points)
         logits = end_points["Logits"]
         self.assertListEqual(logits.get_shape().as_list(), [batch_size, num_classes])
         self.assertTrue("AuxLogits" in end_points)
         aux_logits = end_points["AuxLogits"]
         self.assertListEqual(aux_logits.get_shape().as_list(), [batch_size, num_classes])
         pre_pool = end_points["PrePool"]
         self.assertListEqual(pre_pool.get_shape().as_list(), [batch_size, 8, 8, 1536])
Ejemplo n.º 27
0
 def testBuildEndPoints(self):
     batch_size = 5
     height, width = 299, 299
     num_classes = 1000
     with self.test_session():
         inputs = tf.random_uniform((batch_size, height, width, 3))
         _, end_points = inception.inception_resnet_v2(inputs, num_classes)
         self.assertTrue('Logits' in end_points)
         logits = end_points['Logits']
         self.assertListEqual(logits.get_shape().as_list(),
                              [batch_size, num_classes])
         self.assertTrue('AuxLogits' in end_points)
         aux_logits = end_points['AuxLogits']
         self.assertListEqual(aux_logits.get_shape().as_list(),
                              [batch_size, num_classes])
         pre_pool = end_points['Conv2d_7b_1x1']
         self.assertListEqual(pre_pool.get_shape().as_list(),
                              [batch_size, 8, 8, 1536])
 def testGlobalPoolUnknownImageShape(self):
   batch_size = 1
   height, width = 330, 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_resnet_v2(
         inputs, num_classes, create_aux_logits=False)
     self.assertTrue(logits.op.name.startswith('InceptionResnetV2/Logits'))
     self.assertListEqual(logits.get_shape().as_list(),
                          [batch_size, num_classes])
     pre_pool = end_points['Conv2d_7b_1x1']
     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, 8, 11, 1536))
def freeze_model(name):
    checkpoints_dir = '/data/huang/behaviour/data/tfmodel'
    OUTPUT_PB_FILENAME = 'inception_resnet_v2_behaviour_309_4_10_{}k.pb'.format(
        name[0:3])
    NUM_CLASSES = 309
    tensorName_v4 = 'InceptionResnetV2/Logits/Predictions'

    image_size = inception.inception_resnet_v2.default_image_size

    with tf.Graph().as_default():

        input_image_t = tf.placeholder(tf.string, name='input_image')
        image = tf.image.decode_jpeg(input_image_t, channels=3)

        processed_image = inception_preprocessing.preprocess_for_eval(
            image, image_size, image_size, central_fraction=None)

        processed_images = tf.expand_dims(processed_image, 0)

        with slim.arg_scope(inception.inception_resnet_v2_arg_scope()):
            logits, _ = inception.inception_resnet_v2(processed_images,
                                                      num_classes=NUM_CLASSES,
                                                      is_training=False)
        # Apply softmax function to the logits (output of the last layer of the network)
        probabilities = tf.nn.softmax(logits)

        #model_path = tf.train.latest_checkpoint(checkpoints_dir)
        model_path = '/data/huang/behaviour/data/tfmodel/model_backup/model.ckpt-{}'.format(
            name)

        init_fn = slim.assign_from_checkpoint_fn(model_path,
                                                 slim.get_model_variables())

        with tf.Session() as sess:
            # Now call the initialization function within the session
            init_fn(sess)
            constant_graph = convert_variables_to_constants(
                sess, sess.graph_def,
                ["input_image", "DecodeJpeg", tensorName_v4])
            tf.train.write_graph(constant_graph,
                                 '.',
                                 OUTPUT_PB_FILENAME,
                                 as_text=False)
Ejemplo n.º 30
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_resnet_v2_arg_scope()):
            with tf.variable_scope(self.name):
                avg_pooled = tf.nn.avg_pool(x_input,
                                            ksize=[1, 6, 6, 1],
                                            strides=[1, 1, 1, 1],
                                            padding='SAME')
                logits, end_points = inception.inception_resnet_v2(
                    avg_pooled,
                    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
Ejemplo n.º 31
0
def evaluate():
    """Eval KAGGLE-128 for a number of steps."""
    with tf.Graph().as_default() as g:
        # Get images and labels for KAGGLE-128.
        eval_data = FLAGS.eval_data
        images, labels = kaggle128.inputs(eval_data=eval_data)

        # Build a Graph that computes the logits predictions from the
        # inference model.
        logits, end_points = inception.inception_resnet_v2(images, num_classes=kaggle128.NUM_CLASSES,
                                                           create_aux_logits=False,
                                                           is_training=False)
        # Calculate predictions.
        predictions = tf.argmax(logits, 1)

        # Restore the moving average version of the learned variables for eval.
        variable_averages = tf.train.ExponentialMovingAverage(
            kaggle128.MOVING_AVERAGE_DECAY)
        variables_to_restore = variable_averages.variables_to_restore()
        saver = tf.train.Saver(variables_to_restore)

        eval_once(saver, predictions, labels)
Ejemplo n.º 32
0
    def configure_inception_resnet(self):

        try:
            from nets import inception
            from datasets import dataset_utils
        except:
            import sys
            print(
                "Make sure you have installed tensorflow/models and it's accessible in the environment"
            )
            print("export PYTHONPATH=/home/ubuntu/models/slim")
            sys.exit()

        image_size = inception.inception_resnet_v2.default_image_size

        self.crop_generator = deepprofiler.imaging.cropping.SingleImageCropGenerator(
            self.config, self.dset)
        # Setup pretrained model
        network_input = crop_transform(self.raw_crops, image_size)
        url = self.config["profiling"]["url"]
        checkpoint = self.config["profiling"]["checkpoint"]
        if not os.path.isfile(checkpoint):
            dataset_utils.download_and_uncompress_tarball(
                url, os.path.dirname(checkpoint))
        with slim.arg_scope(inception.inception_resnet_v2_arg_scope()):
            _, self.endpoints = inception.inception_resnet_v2(
                network_input, num_classes=1001, is_training=False)
        init_fn = slim.assign_from_checkpoint_fn(checkpoint,
                                                 slim.get_model_variables())

        # Session configuration
        configuration = tf.ConfigProto()
        configuration.gpu_options.allow_growth = True
        configuration.gpu_options.visible_device_list = self.config[
            "profiling"]["gpu"]

        self.sess = tf.Session(config=configuration)
        init_fn(self.sess)
        self.crop_generator.start(self.sess)
Ejemplo n.º 33
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))
Ejemplo n.º 34
0
    print(img.shape)

    inputs = np.ones((1, 299, 299, 3), dtype=np.float32)
    inputs[0, 0, 0, 0] = -1
    #inputs[0] = img
    print(inputs.mean())
    print(inputs.std())
    inputs = tf.pack(inputs)
    # tensorflow normalization
    # https://github.com/tensorflow/models/blob/master/slim/preprocessing/inception_preprocessing.py#L273
    #inputs = tf.sub(inputs, 0.5)
    #inputs = tf.mul(inputs, 2.0)

    with slim.arg_scope(inception.inception_resnet_v2_arg_scope()):
        logits, _ = inception.inception_resnet_v2(inputs,
                                                  num_classes=1001,
                                                  is_training=False)

    with tf.Session() as sess:

        # Initialize model
        init_fn = slim.assign_from_checkpoint_fn(
            os.path.join(checkpoints_dir,
                         'inception_resnet_v2_2016_08_30.ckpt'),
            slim.get_model_variables('InceptionResnetV2'))

        init_fn(sess)

        # Display model variables
        for v in slim.get_model_variables():
            print('name = {}, shape = {}'.format(v.name, v.get_shape()))
Ejemplo n.º 35
0
    # the network.
    # I use the "preprocess_for_eval()" method instead of "inception_preprocessing()"
    # because the latter crops all images to the center by 85% at
    # prediction time (training=False).
    processed_image = inception_preprocessing.preprocess_for_eval(
        image, image_size, image_size, central_fraction=None)

    # 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)

    # Load the inception network structure
    with slim.arg_scope(inception.inception_resnet_v2_arg_scope()):
        logits, _ = inception.inception_resnet_v2(processed_images,
                                                  num_classes=NUM_CLASSES,
                                                  is_training=False)
    # Apply softmax function to the logits (output of the last layer of the network)
    probabilities = tf.nn.softmax(logits)

    model_path = tf.train.latest_checkpoint(checkpoints_dir)

    # Get the function that initializes the network structure (its variables) with
    # the trained values contained in the checkpoint
    init_fn = slim.assign_from_checkpoint_fn(model_path,
                                             slim.get_model_variables())

    with tf.Session() as sess:
        # Now call the initialization function within the session
        init_fn(sess)
Ejemplo n.º 36
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));
Ejemplo n.º 37
0
    #    image_placeholder = tf.placeholder(tf.uint8, shape=(None,None,3), name='input_image')
    #    image_rgb = tf.reverse(image_placeholder, [-1])
    #    image = tf.image.convert_image_dtype(image_rgb, dtype=tf.float32)
    #    image = tf.expand_dims(image,0)
    #    image = tf.image.resize_bilinear(image, [image_size, image_size],
    #                                           align_corners=False)
    #    image = tf.squeeze(image, [0])
    #    image = tf.subtract(image, 0.5)
    #    image = tf.multiply(image, 2.0)
    #    image = tf.expand_dims(image,0)
    image_placeholder = tf.placeholder(tf.float32,
                                       shape=(1, 512, 512, 3),
                                       name='input_image')
    with slim.arg_scope(inception.inception_resnet_v2_arg_scope()):
        _, probabilities = inception.inception_resnet_v2(
            image_placeholder, num_classes=NUM_CLASSES, is_training=False)

    #model_path = tf.train.latest_checkpoint(checkpoints_dir)
    model_path = '/data1/model_backup/2018-9-21-model/ckpt-model/model.ckpt-518732'

    # Get the function that initializes the network structure (its variables) with
    # the trained values contained in the checkpoint
    init_fn = slim.assign_from_checkpoint_fn(model_path,
                                             slim.get_model_variables())

    with tf.Session() as sess:
        # Now call the initialization function within the session
        init_fn(sess)

        # Convert variables to constants and make sure the placeholder input_image is included
        # in the graph as well as the other neccesary tensors.