def testTrainEvalWithReuse(self):
    train_batch_size = 5
    eval_batch_size = 2
    height, width = 224, 224
    num_classes = 1000

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

    with self.test_session() as sess:
      sess.run(tf.initialize_all_variables())
      output = sess.run(predictions)
      self.assertEquals(output.shape, (eval_batch_size,))
Example #2
0
def eval(x, num_classes=110):
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits_inc_v1, end_points_inc_v1 = inception.inception_v1(
            x, num_classes=num_classes, is_training=False, scope='InceptionV1')
    pred1 = tf.argmax(end_points_inc_v1['Predictions'], 1)
    # rescale pixle range from [-1, 1] to [0, 255] for resnet_v1 and vgg's input
    image = (((x + 1.0) * 0.5) * 255.0)
    processed_imgs_res_v1_50 = preprocess_for_model(image, 'resnet_v1_50')
    with slim.arg_scope(resnet_v1.resnet_arg_scope()):
        logits_res_v1_50, end_points_res_v1_50 = resnet_v1.resnet_v1_50(
            processed_imgs_res_v1_50,
            num_classes=num_classes,
            is_training=False,
            scope='resnet_v1_50')
    end_points_res_v1_50['logits'] = tf.squeeze(
        end_points_res_v1_50['resnet_v1_50/logits'], [1, 2])
    end_points_res_v1_50['probs'] = tf.nn.softmax(
        end_points_res_v1_50['logits'])
    pred2 = tf.argmax(end_points_res_v1_50['probs'], 1)
    # image = (((x + 1.0) * 0.5) * 255.0)#.astype(np.uint8)
    processed_imgs_vgg_16 = preprocess_for_model(image, 'vgg_16')
    with slim.arg_scope(vgg.vgg_arg_scope()):
        logits_vgg_16, end_points_vgg_16 = vgg.vgg_16(processed_imgs_vgg_16,
                                                      num_classes=num_classes,
                                                      is_training=False,
                                                      scope='vgg_16')
    end_points_vgg_16['logits'] = end_points_vgg_16['vgg_16/fc8']
    end_points_vgg_16['probs'] = tf.nn.softmax(end_points_vgg_16['logits'])
    pred3 = tf.argmax(end_points_vgg_16['probs'], 1)
    return [pred1, pred2, pred3]
Example #3
0
 def _build_graph(self, inputs):
   orig_image = inputs[0]
   with tp.symbolic_functions.guided_relu():
     with slim.arg_scope(inception.inception_v1_arg_scope()):
       image = tf.expand_dims(((orig_image / 255) - 0.5) * 2, 0)
       logits, _ = inception.inception_v1(image, 1001, False)
     tp.symbolic_functions.saliency_map(logits, orig_image, name="saliency")
Example #4
0
  def testTrainEvalWithReuse(self):
    train_batch_size = 5
    eval_batch_size = 2
    height, width = 224, 224
    num_classes = 1000

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

    with self.test_session() as sess:
      sess.run(tf.initialize_all_variables())
      output = sess.run(predictions)
      self.assertEquals(output.shape, (eval_batch_size,))
Example #5
0
def build_single_inceptionv1(train_tfdata, is_train, dropout_keep_prob):
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        identity, end_points = inception.inception_v1(train_tfdata, dropout_keep_prob = dropout_keep_prob, is_training=is_train)
        net = slim.avg_pool2d(end_points['Mixed_5c'], [7, 7], stride=1, scope='MaxPool_0a_7x7')
        net = slim.dropout(net, dropout_keep_prob, scope='Dropout_0b')
        feature = tf.squeeze(net, [1, 2])
    return identity, feature
Example #6
0
def main(_):
    batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3]
    nb_classes = FLAGS.num_classes

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

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

        with slim.arg_scope(inception.inception_v1_arg_scope()):
            _, end_points = inception.inception_v1(
                x_input, num_classes=nb_classes, is_training=False)
        predicted_labels = tf.argmax(end_points['Predictions'], 1)

        # Restore Model
        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)
        # Run computation
        with tf.train.MonitoredSession(session_creator=session_creator) as sess:
            with 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):
                        out_file.write('{0},{1}\n'.format(filename, label))
Example #7
0
def non_target_graph(x, y, i, x_max, x_min, grad):

    eps = 2.0 * max_epsilon / 255.0
    alpha = eps / num_iter
    num_classes = 110

    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits_inc_v1, end_points_inc_v1 = inception.inception_v1(
            x, num_classes=num_classes, is_training=False, scope='InceptionV1')

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

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

    # image = (((x + 1.0) * 0.5) * 255.0)#.astype(np.uint8)
    processed_imgs_vgg_16 = preprocess_for_model(image, 'vgg_16')
    with slim.arg_scope(vgg.vgg_arg_scope()):
        logits_vgg_16, end_points_vgg_16 = vgg.vgg_16(processed_imgs_vgg_16,
                                                      num_classes=num_classes,
                                                      is_training=False,
                                                      scope='vgg_16')

        end_points_vgg_16['logits'] = end_points_vgg_16['vgg_16/fc8']
        end_points_vgg_16['probs'] = tf.nn.softmax(end_points_vgg_16['logits'])

        ########################
        # Using model predictions as ground truth to avoid label leaking
        pred = tf.argmax(
            end_points_inc_v1['Predictions'] + end_points_res_v1_50['probs'] +
            end_points_vgg_16['probs'], 1)
        first_round = tf.cast(tf.equal(i, 0), tf.int64)
        y = first_round * pred + (1 - first_round) * y
        one_hot = tf.one_hot(y, num_classes)
        ########################
        logits = (end_points_inc_v1['Logits'] + end_points_res_v1_50['logits']
                  + end_points_vgg_16['logits']) / 3.0
        cross_entropy = tf.losses.softmax_cross_entropy(one_hot,
                                                        logits,
                                                        label_smoothing=0.0,
                                                        weights=1.0)
        noise = tf.gradients(cross_entropy, x)[0]
        noise = noise / tf.reduce_mean(tf.abs(noise), [1, 2, 3],
                                       keep_dims=True)
        noise = momentum * grad + noise
        x = x + alpha * tf.sign(noise)
        x = tf.clip_by_value(x, x_min, x_max)
        i = tf.add(i, 1)
        return x, y, i, x_max, x_min, noise
def target_graph(x, y, i, x_max, x_min, grad):

    eps = 2.0 * max_epsilon / 255.0
    alpha = eps / num_iter
    num_classes = 110
    #input image size[224,224,3]

    images3 = tf.image.resize_bilinear(input_diversity(x), [224, 224], align_corners=False)

    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits_inc_v1, end_points_inc_v1 = inception.inception_v1(
            images3, num_classes=num_classes, is_training=False, scope='InceptionV1')

    # rescale pixle range from [-1, 1] to [0, 255] for resnet_v1 and vgg's input
    image1 = (((input_diversity(x) + 1.0) * 0.5) * 255.0)
    processed_imgs_res_v1_50 = preprocess_for_model(image1, 'resnet_v1_50')
    with slim.arg_scope(resnet_v1.resnet_arg_scope()):
        logits_res_v1_50, end_points_res_v1_50 = resnet_v1.resnet_v1_50(
            processed_imgs_res_v1_50, num_classes=num_classes, is_training=False, scope='resnet_v1_50')

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

    # image = (((x + 1.0) * 0.5) * 255.0)#.astype(np.uint8)
    image2 = (((input_diversity(x) + 1.0) * 0.5) * 255.0)
    processed_imgs_vgg_16 = preprocess_for_model(image2, 'vgg_16')
    with slim.arg_scope(vgg.vgg_arg_scope()):
        logits_vgg_16, end_points_vgg_16 = vgg.vgg_16(
            processed_imgs_vgg_16, num_classes=num_classes, is_training=False, scope='vgg_16')

    end_points_vgg_16['logits'] = end_points_vgg_16['vgg_16/fc8']
    end_points_vgg_16['probs'] = tf.nn.softmax(end_points_vgg_16['logits'])


    one_hot = tf.one_hot(y, num_classes)

    logits = (end_points_inc_v1['Logits'] + end_points_res_v1_50['logits'] + end_points_vgg_16['logits']) / 3.0
    cross_entropy = tf.losses.softmax_cross_entropy(one_hot,
                                                    logits,
                                                    label_smoothing=0.0,
                                                    weights=1.0)
    noise = tf.gradients(cross_entropy, x)[0]

    noise = tf.nn.depthwise_conv2d(noise, stack_kernel, strides=[1, 1, 1, 1], padding='SAME')
    noise = noise / tf.reshape(tf.contrib.keras.backend.std(tf.reshape(noise, [batch_size, -1]), axis=1),
                               [batch_size, 1, 1, 1])
    noise = momentum * grad + noise
    noise = noise / tf.reshape(tf.contrib.keras.backend.std(tf.reshape(noise, [batch_size, -1]), axis=1),
                               [batch_size, 1, 1, 1])
    noise1 = tf.image.resize_images(noise, [140, 140], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
    print("noise shape:", noise.shape)
    noise1 = alpha * tf.clip_by_value(tf.round(noise1), -2, 2)
    noise_paded = tf.pad(noise1,[[0, 0], [42, 42], [42, 42], [0, 0]], constant_values=0.)
    x = x - noise_paded
    x = tf.clip_by_value(x, x_min, x_max)
    print("x.shape:", x.shape)
    i = tf.add(i, 1)
    return x, y, i, x_max, x_min, noise
Example #9
0
def target_graph(x, y, i, x_max, x_min, grad):
    eps = 2.0 * max_epsilon / 255.0
    alpha = eps / num_iter
    num_classes = 110

    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits_inc_v1, end_points_inc_v1 = inception.inception_v1(
            x, num_classes=num_classes, is_training=False, scope='InceptionV1')

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

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

    # image = (((x + 1.0) * 0.5) * 255.0)#.astype(np.uint8)
    processed_imgs_vgg_16 = preprocess_for_model(image, 'vgg_16')
    with slim.arg_scope(vgg.vgg_arg_scope()):
        logits_vgg_16, end_points_vgg_16 = vgg.vgg_16(processed_imgs_vgg_16,
                                                      num_classes=num_classes,
                                                      is_training=False,
                                                      scope='vgg_16')

    end_points_vgg_16['logits'] = end_points_vgg_16['vgg_16/fc8']
    end_points_vgg_16['probs'] = tf.nn.softmax(end_points_vgg_16['logits'])

    ########################
    one_hot = tf.one_hot(y, num_classes)
    ########################

    logits = (end_points_inc_v1['Logits'] + end_points_res_v1_50['logits'] +
              end_points_vgg_16['logits']) / 3.0
    cross_entropy = tf.losses.softmax_cross_entropy(one_hot,
                                                    logits,
                                                    label_smoothing=0.0,
                                                    weights=1.0)
    noise = tf.gradients(cross_entropy, x)[0]
    noise = noise / tf.reshape(
        tf.contrib.keras.backend.std(tf.reshape(noise, [batch_size, -1]),
                                     axis=1), [batch_size, 1, 1, 1])
    noise = momentum * grad + noise
    noise = noise / tf.reshape(
        tf.contrib.keras.backend.std(tf.reshape(noise, [batch_size, -1]),
                                     axis=1), [batch_size, 1, 1, 1])
    x = x - alpha * tf.clip_by_value(tf.round(noise), -2, 2)
    x = tf.clip_by_value(x, x_min, x_max)
    i = tf.add(i, 1)
    return x, y, i, x_max, x_min, noise
Example #10
0
  def testLogitsNotSqueezed(self):
    num_classes = 25
    images = tf.random_uniform([1, 224, 224, 3])
    logits, _ = inception.inception_v1(images,
                                       num_classes=num_classes,
                                       spatial_squeeze=False)

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

    with self.test_session() as sess:
      tf.initialize_all_variables().run()
      logits_out = sess.run(logits)
      self.assertListEqual(list(logits_out.shape), [1, 1, 1, num_classes])
Example #12
0
def build_single_inceptionv1(train_tfdata, is_train, dropout_keep_prob):
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        identity, end_points = inception.inception_v1(
            train_tfdata,
            dropout_keep_prob=dropout_keep_prob,
            is_training=is_train)
        net = slim.avg_pool2d(end_points['Mixed_5c'], [7, 7],
                              stride=1,
                              scope='MaxPool_0a_7x7')
        net = slim.dropout(net, dropout_keep_prob, scope='Dropout_0b')
        feature = tf.squeeze(net, [1, 2])
    return identity, feature
Example #13
0
def run(name, image_size, num_classes):
  with tf.Graph().as_default():
    image = tf.placeholder("float", [1, image_size, image_size, 3], name="input")
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits, _ = inception.inception_v1(image, num_classes, is_training=False, spatial_squeeze=False)
    probabilities = tf.nn.softmax(logits)
    init_fn = slim.assign_from_checkpoint_fn('inception_v1.ckpt', slim.get_model_variables('InceptionV1'))

    with tf.Session() as sess:
        init_fn(sess)
        saver = tf.train.Saver(tf.global_variables())
        saver.save(sess, "output/"+name)
  def testBuildClassificationNetwork(self):
    batch_size = 5
    height, width = 224, 224
    num_classes = 1000

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

        inputs = tf.random_uniform((batch_size, height, width, 3))
        logits, end_points = inception.inception_v1(inputs, num_classes)
        self.assertTrue(logits.op.name.startswith('InceptionV1/Logits'))
        self.assertListEqual(logits.get_shape().as_list(),
                             [batch_size, num_classes])
        self.assertTrue('Predictions' in end_points)
        self.assertListEqual(end_points['Predictions'].get_shape().as_list(),
                             [batch_size, num_classes])
  def testEvaluation(self):
    batch_size = 2
    height, width = 224, 224
    num_classes = 1000

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

    with self.test_session() as sess:
      sess.run(tf.initialize_all_variables())
      output = sess.run(predictions)
      self.assertEquals(output.shape, (batch_size,))
    def _forward_pass(self, input_layer, trainable=True):
        with slim.arg_scope(inception.inception_v1_arg_scope()):
            input_shape = input_layer.get_shape().as_list()
            batch_size = input_shape[0]
            logits, _ = inception.inception_v1(input_layer,
                                               self._hyparams.num_classes,
                                               is_training=False,
                                               spatial_squeeze=False)
            logits = tf.reshape(logits,
                                (batch_size, self._hyparams.num_classes))

        probabilities = tf.nn.softmax(logits)

        return probabilities
Example #18
0
  def testEvaluation(self):
    batch_size = 2
    height, width = 224, 224
    num_classes = 1000

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

    with self.test_session() as sess:
      sess.run(tf.initialize_all_variables())
      output = sess.run(predictions)
      self.assertEquals(output.shape, (batch_size,))
Example #19
0
    def testUnknowBatchSize(self):
        batch_size = 1
        height, width = 224, 224
        num_classes = 1000

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

        with self.test_session() as sess:
            sess.run(tf.initialize_all_variables())
            output = sess.run(logits, {inputs: images.eval()})
            self.assertEquals(output.shape, (batch_size, num_classes))
Example #20
0
	def __call__(self, x_input, return_logits=False):
		"""Constructs model and return probabilities for given input."""
		reuse = True if self.built else None
		with slim.arg_scope(inception.inception_v1_arg_scope()):
			_, end_points = inception.inception_v1(
				x_input, num_classes=self.nb_classes, is_training=False,
				reuse=reuse)
		self.built = True
		self.logits = end_points['Logits']
		# Strip off the extra reshape op at the output
		self.probs = end_points['Predictions'].op.inputs[0]
		if return_logits:
			return self.logits
		else:
			return self.probs
  def testUnknownBatchSize(self):
    batch_size = 1
    height, width = 224, 224
    num_classes = 1000

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

    with self.test_session() as sess:
      sess.run(tf.initialize_all_variables())
      output = sess.run(logits, {inputs: images.eval()})
      self.assertEquals(output.shape, (batch_size, num_classes))
Example #22
0
def get_predicted_y(x, num_classes, args):
    """Calculate predicted label"""

    slim = tf.contrib.slim
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits_inc_v1, end_points_inc_v1 = inception.inception_v1(
            dimension_224(x),
            num_classes=num_classes,
            is_training=False,
            scope='InceptionV1')

    image = (((x + 1.0) * 0.5) * 255.0)
    processed_imgs_res_v1_50 = preprocess_for_model(image, 'resnet_v1_50')
    with slim.arg_scope(resnet_v1.resnet_arg_scope()):
        logits_res_v1_50, end_points_res_v1_50 = resnet_v1.resnet_v1_50(
            dimension_224(processed_imgs_res_v1_50),
            num_classes=num_classes,
            is_training=False,
            scope='resnet_v1_50')

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

    processed_imgs_vgg_16 = preprocess_for_model(image, 'vgg_16')
    with slim.arg_scope(vgg.vgg_arg_scope()):
        logits_vgg_16, end_points_vgg_16 = vgg.vgg_16(
            dimension_224(processed_imgs_vgg_16),
            num_classes=num_classes,
            is_training=False,
            scope='vgg_16')

    end_points_vgg_16['logits'] = end_points_vgg_16['vgg_16/fc8']
    end_points_vgg_16['probs'] = tf.nn.softmax(end_points_vgg_16['logits'])

    logits = [None] * args.num_model
    pred_y = [None] * args.num_model

    logits[0] = end_points_inc_v1['Logits']
    logits[1] = end_points_res_v1_50['logits']
    logits[2] = end_points_vgg_16['logits']

    for i in range(args.num_model):
        pred_y[i] = tf.argmax(tf.nn.softmax(logits[i]), 1)

    return pred_y, logits
Example #23
0
 def testUnknownImageShape(self):
   tf.reset_default_graph()
   batch_size = 2
   height, width = 224, 224
   num_classes = 1000
   input_np = np.random.uniform(0, 1, (batch_size, height, width, 3))
   with self.test_session() as sess:
     inputs = tf.placeholder(tf.float32, shape=(batch_size, None, None, 3))
     logits, end_points = inception.inception_v1(inputs, num_classes)
     self.assertTrue(logits.op.name.startswith('InceptionV1/Logits'))
     self.assertListEqual(logits.get_shape().as_list(),
                          [batch_size, num_classes])
     pre_pool = end_points['Mixed_5c']
     feed_dict = {inputs: input_np}
     tf.initialize_all_variables().run()
     pre_pool_out = sess.run(pre_pool, feed_dict=feed_dict)
     self.assertListEqual(list(pre_pool_out.shape), [batch_size, 7, 7, 1024])
Example #24
0
    def restore_model(self):
        with tf.Graph().as_default():
            # Prepare graph
            self.x_input = tf.placeholder(tf.float32, shape=self.input_shape)

            with tf.contrib.slim.arg_scope(inception.inception_v1_arg_scope()):
                _, end_points = inception.inception_v1(self.x_input, num_classes=self.nb_classes, is_training=False)
                self.pre_labels = tf.argmax(end_points['Predictions'], 1)
                self.features = end_points['Mixed_5c']

            # Restore Model
            saver = tf.train.Saver(tf.contrib.slim.get_model_variables())
            session_creator = tf.train.ChiefSessionCreator(
                scaffold=tf.train.Scaffold(saver=saver),
                checkpoint_filename_with_path=FLAGS.checkpoint_path)
            
            self.sess = tf.train.MonitoredSession(session_creator=session_creator)
 def testUnknownImageShape(self):
   tf.reset_default_graph()
   batch_size = 2
   height, width = 224, 224
   num_classes = 1000
   input_np = np.random.uniform(0, 1, (batch_size, height, width, 3))
   with self.test_session() as sess:
     inputs = tf.placeholder(tf.float32, shape=(batch_size, None, None, 3))
     logits, end_points = inception.inception_v1(inputs, num_classes)
     self.assertTrue(logits.op.name.startswith('InceptionV1/Logits'))
     self.assertListEqual(logits.get_shape().as_list(),
                          [batch_size, num_classes])
     pre_pool = end_points['Mixed_5c']
     feed_dict = {inputs: input_np}
     tf.initialize_all_variables().run()
     pre_pool_out = sess.run(pre_pool, feed_dict=feed_dict)
     self.assertListEqual(list(pre_pool_out.shape), [batch_size, 7, 7, 1024])
Example #26
0
def ens_model(x):

    #input remains in[0,1]
    image = (x * 255.0)
    num_classes = 110
    processed_incv1 = preprocess_for_model(image, 'inception_v1')
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits_inc_v1, end_points_inc_v1 = inception.inception_v1(
            processed_incv1,
            num_classes=num_classes,
            is_training=False,
            scope='InceptionV1')

    processed_imgs_res_v1_50 = preprocess_for_model(image, 'resnet_v1_50')
    with slim.arg_scope(resnet_v1.resnet_arg_scope()):
        logits_res_v1_50, end_points_res_v1_50 = resnet_v1.resnet_v1_50(
            processed_imgs_res_v1_50,
            num_classes=num_classes,
            is_training=False,
            scope='resnet_v1_50')

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

    # image = (((x + 1.0) * 0.5) * 255.0)#.astype(np.uint8)
    processed_imgs_vgg_16 = preprocess_for_model(image, 'vgg_16')
    with slim.arg_scope(vgg.vgg_arg_scope()):
        logits_vgg_16, end_points_vgg_16 = vgg.vgg_16(processed_imgs_vgg_16,
                                                      num_classes=num_classes,
                                                      is_training=False,
                                                      scope='vgg_16')

    end_points_vgg_16['logits'] = end_points_vgg_16['vgg_16/fc8']
    end_points_vgg_16['probs'] = tf.nn.softmax(end_points_vgg_16['logits'])

    ########################
    #one_hot = tf.one_hot(y, num_classes)
    ########################

    logits = (end_points_inc_v1['Logits'] + end_points_res_v1_50['logits'] +
              end_points_vgg_16['logits']) / 3.0
    print('logits.shape:', logits.shape)

    return logits
    # VAL_SUMMARY_DIR = "/data/summary/flowers/val"
    # log_dir = os.path.join(VAL_SUMMARY_DIR, model_name, str(l_rate), datetime.datetime.now().strftime("%Y%m%d-%H%M"))
    flowers_data_dir = "/data/flowers"
    batch_size = 10

    image_size = inception.inception_v1.default_image_size
    val_dataset = flowers.get_split('validation', flowers_data_dir)

    # Load the data
    images, labels = dataset.load_batch(
        val_dataset, batch_size, height=image_size, width=image_size, is_training=False)

    # Create the model:
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits, _ = inception.inception_v1(images, num_classes=5, is_training=False)
        predictions = tf.argmax(logits, 1)

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

    # Choose the metrics to compute:
    names_to_values, names_to_updates = slim.metrics.aggregate_metric_map({
        "accuracy": slim.metrics.streaming_accuracy(predictions, labels),
        'precision': slim.metrics.streaming_precision(predictions, labels),
    })

    # Evaluate the model using 1000 batches of data:
    num_batches = 10
Example #28
0
def create_model(images, labels):
    """
    This methods initialize the inception v1 model with weights generated
    from training on the ImageNet dataset for all layers expect the last.
    The last layer is adjusted to output only 9 classes (instead of the
    1000 required for ImageNet). Note also that the methods set the model
    for fine-tuning meaning that during training only the last layer's
    weights can change.

    :param images: A tensor containing the images.
    :param labels: A tensor representing the correct labels for the images.

    :return restore_op: The operation used to restore the weights of the model.
    :return feed_dict: The feed_dict used for restoring the model.
    :return train_op: The train_op used to train the model.
    :return metrics_to_values: The metrics collected when training.
    :return metrics_to_updates: The metrics update op used when training.
    """
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        #  Load the deep learning model.
        logits, end_points = inception.inception_v1(images,
                                                    num_classes=NUM_CLASSES,
                                                    is_training=False)

        # We are going to train only the last layer of the model.
        trainable_layer = 'InceptionV1/Logits/Conv2d_0c_1x1'

        variables_to_restore = slim.get_variables_to_restore(
            exclude=[trainable_layer])
        variables_to_train = slim.get_variables_by_suffix('', trainable_layer)

        # Transform the labels into one hot encoding.
        one_hot_labels = tf.one_hot(
            labels,
            NUM_CLASSES,
        )

        # Define the loss function.
        loss = tf.losses.softmax_cross_entropy(
            one_hot_labels,
            end_points['Logits'],
        )

        # Select the optimizer.
        optimizer = tf.train.AdamOptimizer(1e-4)

        # Create a train op.
        train_op = tf.contrib.training.create_train_op(
            loss,
            optimizer,
            variables_to_train=variables_to_train,
        )

        predictions = tf.argmax(end_points['Predictions'],
                                1,
                                name="predictions")
        metrics_to_values, metrics_to_updates = \
            slim.metrics.aggregate_metric_map({
                'accuracy': tf.metrics.accuracy(predictions, labels),
                'mean_loss': tf.metrics.mean(loss),
            })

        # Define load predefined model operation.
        restore_op, feed_dict = slim.assign_from_checkpoint(
            'inception_v1.ckpt', variables_to_restore)

        return (
            restore_op,
            feed_dict,
            train_op,
            metrics_to_values,
            metrics_to_updates,
        )
def main(_):
    if not tf.gfile.Exists(FLAGS.checkpoint_path):
        tf.gfile.MkDir(FLAGS.checkpoint_path)
    else:
        if not FLAGS.restore:
            tf.gfile.DeleteRecursively(FLAGS.checkpoint_path)
            tf.gfile.MkDir(FLAGS.checkpoint_path)

    batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3]
    nb_classes = FLAGS.num_classes
    input_images = tf.placeholder(
        tf.float32, [None, FLAGS.image_height, FLAGS.image_width, 3])
    input_labels = tf.placeholder(tf.float32, [None, nb_classes])

    learning_rate = FLAGS.learning_rate
    # add summary
    tf.summary.scalar('learning_rate', learning_rate)

    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits, end_points = inception.inception_v1(input_images,
                                                    num_classes=110,
                                                    is_training=True)

    variables_to_restore = slim.get_variables_to_restore()

    loss_op = tf.nn.softmax_cross_entropy_with_logits(logits=logits,
                                                      labels=input_labels)
    total_loss_op = tf.reduce_mean(loss_op)
    train_op = tf.train.AdamOptimizer(learning_rate).minimize(loss_op)
    tf.summary.scalar('total loss', total_loss_op)

    summary_op = tf.summary.merge_all()

    saver = tf.train.Saver(variables_to_restore)
    summary_writer = tf.summary.FileWriter(FLAGS.checkpoint_path,
                                           tf.get_default_graph())

    init = tf.global_variables_initializer()

    if FLAGS.pretrained_model_path is not None:
        variable_restore_op = slim.assign_from_checkpoint_fn(
            FLAGS.pretrained_model_path,
            slim.get_trainable_variables(),
            ignore_missing_vars=True)

    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
        if FLAGS.restore:
            sess.run(init)
            print('continue training from previous checkpoint')
            ckpt = tf.train.latest_checkpoint(FLAGS.checkpoint_path)
            saver.restore(sess, ckpt)
        else:
            sess.run(init)
            if FLAGS.pretrained_model_path is not None:
                variable_restore_op(sess)

        for epoch in range(FLAGS.max_epochs):
            start = time.time()
            data_generator = load_path_label('./datasets/train_labels.txt',
                                             batch_shape,
                                             onehot=True)
            for step in range(FLAGS.max_steps):
                data = next(data_generator)
                _, total_loss, res = sess.run(
                    [train_op, total_loss_op, summary_op],
                    feed_dict={
                        input_images: data[0],
                        input_labels: data[1]
                    })
                if step % 50 == 0:
                    summary_writer.add_summary(res, step)

                if np.isnan(total_loss):
                    print('Loss diverged, stop training')
                    break

                if step % 10 == 0:
                    avg_time_per_step = (time.time() - start) / 10
                    avg_examples_per_second = (10 * FLAGS.batch_size) / (
                        time.time() - start)
                    start = time.time()
                    print(
                        'Step {:06d}, total loss {:.4f}, {:.2f} seconds/step, {:.2f} examples/second'
                        .format(step, total_loss, avg_time_per_step,
                                avg_examples_per_second))

            saver.save(sess,
                       FLAGS.checkpoint_path + 'robust_model',
                       global_step=epoch)
Example #30
0
def target_graph_large(x, target_class_input, i, x_max, x_min, grad):
    eps = 2.0 * FLAGS.max_epsilon / 255.0
    alpha = eps / 12
    momentum = FLAGS.momentum
    num_classes = 110

    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits_inc_v1, end_points_inc_v1 = inception.inception_v1(
            x, num_classes=num_classes, is_training=False, scope='InceptionV1')

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

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

    # image = (((x + 1.0) * 0.5) * 255.0)#.astype(np.uint8)
    processed_imgs_vgg_16 = preprocess_for_model(image, 'vgg_16')
    with slim.arg_scope(vgg.vgg_arg_scope()):
        logits_vgg_16, end_points_vgg_16 = vgg.vgg_16(processed_imgs_vgg_16,
                                                      num_classes=num_classes,
                                                      is_training=False,
                                                      scope='vgg_16')

    end_points_vgg_16['logits'] = end_points_vgg_16['vgg_16/fc8']
    end_points_vgg_16['probs'] = tf.nn.softmax(end_points_vgg_16['logits'])

    one_hot_target_class = tf.one_hot(target_class_input, num_classes)

    logits = (logits_inc_v1 + logits_vgg_16 + logits_res_v1_50) / 3.0
    # auxlogits = (4 * end_points_v3['AuxLogits'] + end_points_adv_v3['AuxLogits'] + end_points_ens3_adv_v3['AuxLogits'] +
    #              end_points_ens4_adv_v3['AuxLogits'] + 4 * end_points_ensadv_res_v2['AuxLogits']) / 11
    # auxlogits =
    cross_entropy = tf.losses.softmax_cross_entropy(one_hot_target_class,
                                                    logits,
                                                    label_smoothing=0.0,
                                                    weights=1.0)
    # cross_entropy += tf.losses.softmax_cross_entropy(one_hot_target_class,
    #                                                  auxlogits,
    #                                                  label_smoothing=0.0,
    #                                                  weights=0.4)

    noise = tf.gradients(cross_entropy, x)[0]
    noise = noise / tf.reduce_mean(tf.abs(noise), [1, 2, 3], keep_dims=True)
    noise = momentum * grad + noise

    # noise = tf.gradients(cross_entropy, x)[0]
    # noise = noise / tf.reshape(tf.contrib.keras.backend.std(tf.reshape(noise, [FLAGS.batch_size, -1]), axis=1),
    #                            [FLAGS.batch_size, 1, 1, 1])
    # noise = momentum * grad + noise
    # noise = noise / tf.reshape(tf.contrib.keras.backend.std(tf.reshape(noise, [FLAGS.batch_size, -1]), axis=1),
    #                            [FLAGS.batch_size, 1, 1, 1])
    x = x - alpha * tf.clip_by_value(tf.round(noise), -2, 2)
    x = tf.clip_by_value(x, x_min, x_max)
    i = tf.add(i, 1)
    return x, target_class_input, i, x_max, x_min, noise
Example #31
0
def non_target_mi_fgsm_attack(input_dir, output_dir):

    # some parameter
    #eps = 2.0 * max_epsilon / 255.0
    eps = max_epsilon
    batch_shape = [batch_size, 224, 224, 3]

    #_check_or_create_dir(output_dir)

    with tf.Graph().as_default():
        # Prepare graph
        raw_inputs = tf.placeholder(tf.uint8, shape=[None, 224, 224, 3])

        # preprocessing for model input,
        # note that images for all classifier will be normalized to be in [-1, 1]
        #processed_imgs = preprocess_for_model(raw_inputs, 'inception_v1')

        x_input = tf.placeholder(tf.float32, shape=batch_shape)
        x_max = tf.clip_by_value(x_input + eps, -1.0, 1.0)
        x_min = tf.clip_by_value(x_input - eps, -1.0, 1.0)

        # y = tf.constant(np.zeros([batch_size]), tf.int64)
        #y = tf.placeholder(tf.int32, shape=[batch_size])
        y = tf.constant(np.zeros([batch_size]), tf.int64)
        i = tf.constant(0)
        #grad = tf.zeros(shape=batch_shape)
        if rand_init:
            grad = tf.random_uniform(tf.shape(x_input),
                                     tf.cast(-0.05, x_input.dtype),
                                     tf.cast(0.05, x_input.dtype),
                                     dtype=x_input.dtype)
        else:
            grad = tf.zeros(tf.shape(x_input))
        adv_x = x_input + grad
        adv_x = tf.clip_by_value(adv_x, x_min, x_max)
        #ori_x,x_adv, _, _, _, _, _ = tf.while_loop(stop, non_target_graph, [x_input, adv_x, y, i, x_max, x_min, grad])

        #training setting
        train_img = tf.placeholder(tf.float32, shape=[None, 224, 224, 3])
        train_label = tf.placeholder(tf.float32, shape=[None, 110])
        with slim.arg_scope(inception.inception_v1_arg_scope()):
            logits_inc_v1, end_points_inc_v1 = inception.inception_v1(
                train_img,
                num_classes=num_classes,
                is_training=True,
                scope='InceptionV1')

        predict = tf.argmax(tf.nn.softmax(end_points_inc_v1['Logits']), 1)
        logits = end_points_inc_v1['Logits']
        cost = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(labels=train_label,
                                                    logits=logits))
        learning_rate = tf.placeholder(tf.float32, name='learning_rate')
        optimizer = tf.train.GradientDescentOptimizer(
            learning_rate=learning_rate)
        train = optimizer.minimize(cost)
        accuracy = tf.reduce_mean(
            tf.cast(tf.equal(predict, tf.argmax(train_label, 1)), tf.float32))

        ori_x, x_adv, _, _, _, _, _ = tf.while_loop(
            stop, non_target_graph, [x_input, adv_x, y, i, x_max, x_min, grad])
        # Run computation
        s1 = tf.train.Saver(slim.get_model_variables(scope='InceptionV1'))
        s2 = tf.train.Saver(slim.get_model_variables(scope='resnet_v1_50'))
        s3 = tf.train.Saver(slim.get_model_variables(scope='vgg_16'))

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            s1.restore(sess, model_checkpoint_map['inception_v1'])
            s2.restore(sess, model_checkpoint_map['resnet_v1_50'])
            s3.restore(sess, model_checkpoint_map['vgg_16'])

            for time in range(epoch_num):
                train_acc = []
                count = 0
                train_loss = []
                val_acc = []
                val_loss = []
                epoch_learning_rate = init_learning_rate
                for filenames, raw_images, true_labels in load_images_with_true_label(
                        input_dir):
                    raw_images = list(raw_images)
                    adv_images = sess.run(
                        x_adv, feed_dict={x_input: np.array(raw_images[:16])})
                    raw_images[:16] = list(adv_images)
                    labels = one_hot(true_labels, 110)
                    img = (np.array(raw_images) + 1.0) * 0.5 * 255.0
                    img = preprocess_for_model(img, model_type, train=True)
                    train_feed_dict = {
                        train_img: img,
                        train_label: labels,
                        learning_rate: epoch_learning_rate
                    }
                    a, batch_loss = sess.run([train, cost],
                                             feed_dict=train_feed_dict)
                    batch_acc = accuracy.eval(feed_dict=train_feed_dict)
                    train_acc.append(batch_acc)
                    train_loss.append(batch_loss)
                    count += 1
                    if count % 100 == 0:
                        print('acc: ', np.mean(np.array(train_acc)), ' loss: ',
                              np.mean(np.array(train_loss)))
                        #break
                for filenames, raw_images, true_labels in load_images_with_true_label(
                        output_dir, train=False):
                    raw_images = list(raw_images)
                    adv_images = sess.run(
                        x_adv, feed_dict={x_input: np.array(raw_images[:16])})
                    raw_images[:16] = list(adv_images)
                    labels = one_hot(true_labels, 110)
                    img = (np.array(raw_images) + 1.0) * 0.5 * 255.0
                    img = preprocess_for_model(img, model_type, train=True)
                    train_feed_dict = {
                        train_img: img,
                        train_label: labels,
                        learning_rate: epoch_learning_rate
                    }
                    pre, batch_acc = sess.run([predict, accuracy],
                                              feed_dict=train_feed_dict)
                    val_acc.append(batch_acc)
                    val_loss.append(batch_loss)
                    count += 1
                    #break
                    #print(pre,'\n',true_labels,batch_acc)
                print('val_acc: ', np.mean(np.array(val_acc)), ' loss: ',
                      np.mean(np.array(val_loss)))
                s1.save(sess=sess,
                        save_path='./train_model/%s_%s.ckpt' %
                        (model_type, time))
Example #32
0
def non_target_graph(ori_x, x, y, i, x_max, x_min,
                     grad):  #[x_input, adv_x, y, i, x_max, x_min, grad]
    #eps = 2.0 * max_epsilon / 255.0
    eps = max_epsilon
    alpha = eps_iter
    num_classes = 110
    #image = robust_resize(x)
    image = input_diversity(x)
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits_inc_v1, end_points_inc_v1 = inception.inception_v1(
            image, num_classes=num_classes, is_training=False)
    # rescale pixle range from [-1, 1] to [0, 255] for resnet_v1 and vgg's input
    #image = (((image + 1.0) * 0.5) * 255.0)
    image = (image + 1.0) * 0.5 * 255.0
    processed_imgs_res_v1_50 = preprocess_for_model(image, 'resnet_v1_50')
    with slim.arg_scope(resnet_v1.resnet_arg_scope()):
        logits_res_v1_50, end_points_res_v1_50 = resnet_v1.resnet_v1_50(
            processed_imgs_res_v1_50,
            num_classes=num_classes,
            is_training=False,
            scope='resnet_v1_50')
    end_points_res_v1_50['logits'] = tf.squeeze(
        end_points_res_v1_50['resnet_v1_50/logits'], [1, 2])
    end_points_res_v1_50['probs'] = tf.nn.softmax(
        end_points_res_v1_50['logits'])

    #image = (((x + 1.0) * 0.5) * 255.0)#.astype(np.uint8)
    processed_imgs_vgg_16 = preprocess_for_model(image, 'vgg_16')
    with slim.arg_scope(vgg.vgg_arg_scope()):
        logits_vgg_16, end_points_vgg_16 = vgg.vgg_16(processed_imgs_vgg_16,
                                                      num_classes=num_classes,
                                                      is_training=False,
                                                      scope='vgg_16')

    end_points_vgg_16['logits'] = end_points_vgg_16['vgg_16/fc8']
    end_points_vgg_16['probs'] = tf.nn.softmax(end_points_vgg_16['logits'])

    image = (image / 255.0) * 2.0 - 1.0
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits_inc_adv, end_points_inc_adv = inception.inception_v1(
            image,
            num_classes=num_classes,
            is_training=False,
            scope='inception_adv')

    image = (image + 1.0) * 0.5 * 255.0
    #processed_imgs_res_v1_50 = preprocess_for_model(image, 'resnet_v1_50')
    #with slim.arg_scope(resnet_v1.resnet_arg_scope()):
    #  logits_res_adv, end_points_res_adv = resnet_v1.resnet_v1_50(
    #    processed_imgs_res_v1_50, num_classes=num_classes,is_training=False,scope='adv_resnet')
    #end_points_res_adv['logits'] = tf.squeeze(end_points_res_adv['adv_resnet/logits'], [1, 2])
    #end_points_res_adv['probs'] = tf.nn.softmax(end_points_res_v1_50['logits'])

    processed_imgs_vgg_16 = preprocess_for_model(image, 'vgg_16')
    with slim.arg_scope(vgg.vgg_arg_scope()):
        logits_vgg_adv, end_points_vgg_adv = vgg.vgg_16(
            processed_imgs_vgg_16,
            num_classes=num_classes,
            is_training=False,
            scope='adv_vgg')
    end_points_vgg_adv['logits'] = end_points_vgg_adv['adv_vgg/fc8']
    end_points_vgg_adv['probs'] = tf.nn.softmax(end_points_vgg_adv['logits'])

    ########################
    #logits = tf.cond(tf.less(i,cond_number),lambda: end_points_vgg_16['logits'],lambda:end_points_vgg_16['logits'])

    logits = (0.8*end_points_inc_v1['Logits'] + 1.1*end_points_res_v1_50['logits'] \
        + 0.6*end_points_vgg_16['logits'] + end_points_inc_adv['Logits'] \
        + end_points_vgg_adv['logits']) \
       /6.5
    # logits = tf.cond(tf.less(i,cond_number),lambda: logits,lambda:end_points_vgg_16['logits'])

    prediction = (end_points_inc_v1['Predictions'] + end_points_res_v1_50['probs'] \
        + end_points_vgg_16['probs'] + end_points_inc_adv['Predictions'] \
        + end_points_vgg_adv['probs']) \
       /5.0
    pred = tf.argmax(prediction, 1)
    first_round = tf.cast(tf.equal(i, 0), tf.int64)
    y = first_round * pred + (1 - first_round) * y
    one_hot = tf.one_hot(y, num_classes)

    cross_entropy = tf.losses.softmax_cross_entropy(one_hot,
                                                    logits,
                                                    label_smoothing=0.0,
                                                    weights=1.0)
    #another loss function
    #loss = tf.reshape(tf.nn.top_k(prediction,110)[0][:,0],[10,1])-tf.reshape(\
    #       tf.nn.top_k(prediction,110)[0][:,109],[10,1])
    #item1 = tf.reshape(tf.cast(tf.equal(pred,y),tf.float32),[batch_size,1])
    #item2 = tf.reshape(tf.cast(tf.equal(pred,y),tf.float32)-1.0,[batch_size,1])
    #loss = item1 * loss + item2 * loss
    loss = 1.0 * cross_entropy  # +0.2* loss

    noise = tf.gradients(loss, x)[0]
    kernel = gkern(7, sig).astype(np.float32)
    stack_kernel = np.stack([kernel, kernel, kernel]).swapaxes(2, 0)
    stack_kernel = np.expand_dims(stack_kernel, 3)
    noise = tf.nn.depthwise_conv2d(noise,
                                   stack_kernel,
                                   strides=[1, 1, 1, 1],
                                   padding='SAME')

    #noise = noise / tf.reduce_mean(tf.abs(noise), [1,2,3], keep_dims=True)
    noise = noise / tf.reshape(
        tf.contrib.keras.backend.std(tf.reshape(noise, [batch_size, -1]),
                                     axis=1), [batch_size, 1, 1, 1])

    #noise = clip_eta(noise, ord = 2, eps=eps)
    noise = momentum * grad + noise
    noise = noise / tf.reshape(
        tf.contrib.keras.backend.std(tf.reshape(noise, [batch_size, -1]),
                                     axis=1), [batch_size, 1, 1, 1])

    perturb = tf.clip_by_value(tf.round(noise), -10, 10)
    x = x + alpha * perturb
    # x = x + alpha * tf.sign(noise)
    x = tf.clip_by_value(x, -1.0, 1.0)

    #part_x,x = change_size(ori_x,x,size = crop_size)
    #x = x+part_x
    ############################PGD################
    #eta = x - ori_x
    #eta = clip_eta(eta, ord = 2, eps=eps)
    #x = ori_x + eta
    #x = tf.clip_by_value(x, x_min, x_max)
    ##############################################
    i = tf.add(i, 1)
    #random_noise = tf.random_normal(x.shape,6/255,6/255,dtype=x.dtype)
    #x = tf.cond(tf.less(i,num_iter),lambda: x,lambda:random_noise+x)
    #x = tf.clip_by_value(x, -1, 1)
    return ori_x, x, y, i, x_max, x_min, noise
Example #33
0
def non_target_graph(ori_x, x, y, i, x_max, x_min, grad):

    #eps = 2.0 * max_epsilon / 255.0
    eps = max_epsilon
    alpha = eps_iter
    num_classes = 110
    #image = preprocess_for_model(x, 'inception_v1')
    #image = x * 2.0 -1.0
    image = input_diversity(x)
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits_inc_v1, end_points_inc_v1 = inception.inception_v1(
            image,
            num_classes=num_classes,
            is_training=False,
            scope='InceptionV1')

    # rescale pixle range from [-1, 1] to [0, 255] for resnet_v1 and vgg's input
    image = ((image + 1.0) * 0.5) * 255.0
    #image = (image + 1.0)*0.5
    processed_imgs_res_v1_50 = preprocess_for_model(image, 'resnet_v1_50')
    with slim.arg_scope(resnet_v1.resnet_arg_scope()):
        logits_res_v1_50, end_points_res_v1_50 = resnet_v1.resnet_v1_50(
            processed_imgs_res_v1_50,
            num_classes=num_classes,
            is_training=False,
            scope='resnet_v1_50')
    end_points_res_v1_50['logits'] = tf.squeeze(
        end_points_res_v1_50['resnet_v1_50/logits'], [1, 2])
    end_points_res_v1_50['probs'] = tf.nn.softmax(
        end_points_res_v1_50['logits'])

    #image = (((x + 1.0) * 0.5) * 255.0)
    processed_imgs_vgg_16 = preprocess_for_model(image, 'vgg_16')
    with slim.arg_scope(vgg.vgg_arg_scope()):
        logits_vgg_16, end_points_vgg_16 = vgg.vgg_16(processed_imgs_vgg_16,
                                                      num_classes=num_classes,
                                                      is_training=False,
                                                      scope='vgg_16')
    end_points_vgg_16['logits'] = end_points_vgg_16['vgg_16/fc8']
    end_points_vgg_16['probs'] = tf.nn.softmax(end_points_vgg_16['logits'])

    image = (image / 255.0) * 2.0 - 1.0
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits_inc_adv, end_points_inc_adv = inception.inception_v1(
            image,
            num_classes=num_classes,
            is_training=False,
            scope='inception_adv')
    image = (image + 1.0) * 0.5 * 255.0
    #processed_imgs_res_v1_50 = preprocess_for_model(image, 'resnet_v1_50')
    #with slim.arg_scope(resnet_v1.resnet_arg_scope()):
    #  logits_res_adv, end_points_res_adv = resnet_v1.resnet_v1_50(
    #    processed_imgs_res_v1_50, num_classes=num_classes,is_training=False,scope='adv_resnet')
    #end_points_res_adv['logits'] = tf.squeeze(end_points_res_adv['adv_resnet/logits'], [1, 2])
    #end_points_res_adv['probs'] = tf.nn.softmax(end_points_res_v1_50['logits'])

    processed_imgs_vgg_16 = preprocess_for_model(image, 'vgg_16')
    with slim.arg_scope(vgg.vgg_arg_scope()):
        logits_vgg_adv, end_points_vgg_adv = vgg.vgg_16(
            processed_imgs_vgg_16,
            num_classes=num_classes,
            is_training=False,
            scope='adv_vgg')
    end_points_vgg_adv['logits'] = end_points_vgg_adv['adv_vgg/fc8']
    end_points_vgg_adv['probs'] = tf.nn.softmax(end_points_vgg_adv['logits'])

    ########################
    # Using model predictions as ground truth to avoid label leaking
    one_hot = tf.one_hot(y, num_classes)
    logits = (end_points_inc_v1['Logits'] + end_points_res_v1_50['logits'] \
        + end_points_vgg_16['logits'] + end_points_inc_adv['Logits'] \
        + end_points_vgg_adv['logits']) \
       /5.0
    logits = tf.cond(tf.less(i, cond_number), lambda: logits,
                     lambda: end_points_vgg_16['logits'])
    cross_entropy = tf.losses.softmax_cross_entropy(one_hot,
                                                    logits,
                                                    label_smoothing=0.0,
                                                    weights=1.0)
    noise = tf.gradients(cross_entropy, x)[0]

    kernel = gkern(7, sig).astype(np.float32)
    stack_kernel = np.stack([kernel, kernel, kernel]).swapaxes(2, 0)
    stack_kernel = np.expand_dims(stack_kernel, 3)
    noise = tf.nn.depthwise_conv2d(noise,
                                   stack_kernel,
                                   strides=[1, 1, 1, 1],
                                   padding='SAME')

    noise = noise / tf.reshape(
        tf.contrib.keras.backend.std(tf.reshape(noise, [batch_size, -1]),
                                     axis=1), [batch_size, 1, 1, 1])
    noise = momentum * grad + noise
    noise = noise / tf.reshape(
        tf.contrib.keras.backend.std(tf.reshape(noise, [batch_size, -1]),
                                     axis=1), [batch_size, 1, 1, 1])
    x = x - alpha * tf.clip_by_value(tf.round(noise), -10, 10)
    x = tf.clip_by_value(x, x_min, x_max)
    i = tf.add(i, 1)
    ############################PGD################
    #eta = x - ori_x
    #eta = clip_eta(eta, ord = np.inf, eps=eps)
    #x = ori_x + eta
    #x = tf.clip_by_value(x, x_min, x_max)
    ##############################################
    return ori_x, x, y, i, x_max, x_min, noise
Example #34
0
def non_target_graph(ori_x, x, y, i, x_max, x_min, grad):

    #eps = 2.0 * max_epsilon / 255.0
    eps = max_epsilon
    alpha = eps_iter
    num_classes = 110
    #image = preprocess_for_model(x, 'inception_v1')
    image = x
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits_inc_v1, end_points_inc_v1 = inception.inception_v1(
            image,
            num_classes=num_classes,
            is_training=False,
            scope='InceptionV1')

    # rescale pixle range from [-1, 1] to [0, 255] for resnet_v1 and vgg's input
    #image = (((image + 1.0) * 0.5) * 255.0)
    image = (image + 1.0) * 0.5 * 255.0
    processed_imgs_res_v1_50 = preprocess_for_model(image, 'resnet_v1_50')
    with slim.arg_scope(resnet_v1.resnet_arg_scope()):
        logits_res_v1_50, end_points_res_v1_50 = resnet_v1.resnet_v1_50(
            processed_imgs_res_v1_50,
            num_classes=num_classes,
            is_training=False,
            scope='resnet_v1_50',
            reuse=True)
    end_points_res_v1_50['logits'] = tf.squeeze(
        end_points_res_v1_50['resnet_v1_50/logits'], [1, 2])
    end_points_res_v1_50['probs'] = tf.nn.softmax(
        end_points_res_v1_50['logits'])

    #image = (((x + 1.0) * 0.5) * 255.0)#.astype(np.uint8)
    processed_imgs_vgg_16 = preprocess_for_model(image, 'vgg_16')
    with slim.arg_scope(vgg.vgg_arg_scope()):
        logits_vgg_16, end_points_vgg_16 = vgg.vgg_16(processed_imgs_vgg_16,
                                                      num_classes=num_classes,
                                                      is_training=False,
                                                      scope='vgg_16')

    end_points_vgg_16['logits'] = end_points_vgg_16['vgg_16/fc8']
    end_points_vgg_16['probs'] = tf.nn.softmax(end_points_vgg_16['logits'])
    ########################
    #logits = tf.cond(tf.less(i,cond_number),lambda: end_points_vgg_16['logits'],lambda:end_points_vgg_16['logits'])
    logits = end_points_inc_v1['Logits'] / 8 + end_points_res_v1_50[
        'logits'] / 6 + end_points_vgg_16['logits'] / 14
    logits = tf.cond(tf.less(i, cond_number), lambda: logits,
                     lambda: end_points_vgg_16['logits'])

    #pred = tf.argmax((end_points_inc_v1['Predictions']+end_points_res_v1_50['probs']+end_points_vgg_16['probs'])/3.0,1)
    pred = tf.argmax(logits, 1)
    first_round = tf.cast(tf.equal(i, 0), tf.int64)
    y = first_round * pred + (1 - first_round) * y
    one_hot = tf.one_hot(y, num_classes)

    cross_entropy = tf.losses.softmax_cross_entropy(one_hot,
                                                    logits,
                                                    label_smoothing=0.0,
                                                    weights=1.0)
    noise = tf.gradients(cross_entropy, x)[0]
    #noise = noise / tf.reduce_mean(tf.abs(noise), [1,2,3], keep_dims=True)
    noise = noise / tf.reshape(
        tf.contrib.keras.backend.std(tf.reshape(noise, [batch_size, -1]),
                                     axis=1), [batch_size, 1, 1, 1])

    #noise = clip_eta(noise, ord = 2, eps=eps)
    noise = momentum * grad + noise
    noise = noise / tf.reshape(
        tf.contrib.keras.backend.std(tf.reshape(noise, [batch_size, -1]),
                                     axis=1), [batch_size, 1, 1, 1])
    x = x + alpha * tf.clip_by_value(tf.round(noise), -100, 100)
    #x = x + alpha * tf.sign(noise)
    x = tf.clip_by_value(x, x_min, x_max)
    ############################PGD################
    #eta = x - ori_x
    #eta = clip_eta(eta, ord = 2, eps=eps)
    #x = ori_x + eta
    #x = tf.clip_by_value(x, x_min, x_max)
    ##############################################
    i = tf.add(i, 1)
    return ori_x, x, y, i, x_max, x_min, noise
def main(model,
         content_weights,
         style_weights,
         learning_rate,
         alpha,
         beta,
         cycles=float("inf"),
         training_time=float("inf"),
         start_jittering=0.,
         stop_jittering=float("inf"),
         jitter_freq=50,
         display_image_freq=300,
         max_image_dim=512,
         pre_calc_style_grams=0,
         content_targets=0,
         random_initializer=False,
         use_wass=False):

    # Content weights - Dictionary with the tensor names as the keys and the weights as the values
    # Style weights - Same as content weights but for style
    # Alpha, Beta - Total weighting of the content vs style respectively
    # Cycles - how many iterations to perform on each image. Set to float("inf") to remove (must specify time limit instead)
    # Training time - time limit for optimization. Set to float("inf") to remove (must specify training time instead)
    # Stop jittering - Float between 0 and 1. Fraction of the total allowed cycles or training time to jitter the image during optimization. Set to 0 for no image jittering
    # Jitter frequency - Number of training cycles between image shifts
    # Display image frequency - Number of training cycles between displaying the result image
    # Maximum image dimension - Scale down the largest dimension of the content image to match this

    if model in ['Inception_V1', 'Inception_V3']:
        from tensorflow.contrib.slim.nets import inception as model_module
    elif model == 'VGG_19':
        from tensorflow.contrib.slim.nets import vgg as model_module

    if (cycles == float("inf")) and (training_time == float("inf")):
        print("Error: Must specify time or cycle limit")
        return False
    jitter_stop_cycle = float("inf")
    jitter_stop_time = float("inf")
    jitter_start_cycle = 0
    jitter_start_time = 0
    if cycles < float("inf"):
        jitter_start_cycle = start_jittering * cycles
        if stop_jittering < float("inf"):
            jitter_stop_cycle = stop_jittering * cycles
    if training_time < float("inf"):
        jitter_start_time = start_jittering * training_time
        if stop_jittering < float("inf"):
            jitter_stop_time = stop_jittering * training_time

    slim = tf.contrib.slim

    content_image = load_images("./contentpicture", max_image_dim)
    print("Content Image: ", content_image.shape)

    style_image = load_images("./stylepicture",
                              target_shape=content_image.shape)
    print("Style Image: ", style_image.shape)

    g = tf.Graph()
    with g.as_default():
        # Prepare graph
        var_input = tf.placeholder(shape=(None, content_image.shape[1],
                                          content_image.shape[2], 3),
                                   dtype=tf.float32,
                                   name='var_input')
        batch, h, w, ch = content_image.shape
        init_val, scale, corr_matrix = lucid_ops.get_fourier_features(
            content_image)

        decorrelate_matrix = tf.constant(corr_matrix, shape=[3, 3])
        var_decor = tf.reshape(
            tf.matmul(tf.reshape(var_input, [-1, 3]),
                      tf.matrix_inverse(decorrelate_matrix)),
            [1, content_image.shape[1], content_image.shape[2], 3]) * 4.0
        four_space_complex = tf.spectral.rfft2d(
            tf.transpose(var_decor, perm=[0, 3, 1, 2]))
        four_space_complex = four_space_complex / scale
        four_space = tf.concat(
            [tf.real(four_space_complex),
             tf.imag(four_space_complex)], axis=0)

        four_input = tf.Variable(init_val)
        four_to_complex = tf.complex(four_input[0], four_input[1])
        four_to_complex = scale * four_to_complex

        rgb_space = tf.expand_dims(tf.transpose(
            tf.spectral.irfft2d(four_to_complex), perm=[1, 2, 0]),
                                   axis=0)
        rgb_space = rgb_space[:, :h, :w, :ch] / 4.0
        recorr_img = tf.reshape(
            tf.matmul(tf.reshape(rgb_space, [-1, 3]), decorrelate_matrix),
            [1, content_image.shape[1], content_image.shape[2], 3])
        input_img = (recorr_img + 1.0) / 2.0
        VGG_MEANS = np.array([[[[0.485, 0.456, 0.406]]]]).astype('float32')
        VGG_MEANS = tf.constant(VGG_MEANS, shape=[1, 1, 1, 3])
        vgg_input = (input_img - VGG_MEANS) * 255.0
        bgr_input = tf.stack([
            vgg_input[:, :, :, 2], vgg_input[:, :, :, 1], vgg_input[:, :, :, 0]
        ],
                             axis=-1)

        with g.gradient_override_map({'Relu': 'Custom1', 'Relu6': 'Custom2'}):
            if model == 'Inception_V1':
                with slim.arg_scope(model_module.inception_v1_arg_scope()):
                    _, end_points = model_module.inception_v1(
                        input_img,
                        num_classes=1001,
                        spatial_squeeze=False,
                        is_training=False)
            elif model == 'Inception_V3':
                with slim.arg_scope(model_module.inception_v3_arg_scope()):
                    _, end_points = model_module.inception_v3(
                        input_img,
                        num_classes=1001,
                        spatial_squeeze=False,
                        is_training=False)
            elif model == 'VGG_19':
                with slim.arg_scope(model_module.vgg_arg_scope()):
                    _, end_points = model_module.vgg_19(bgr_input,
                                                        num_classes=1000,
                                                        spatial_squeeze=False,
                                                        is_training=False)

        content_placeholders = {}
        content_losses = {}
        total_content_loss = 0
        style_losses = {}
        total_style_loss = 0
        input_grams = {}
        style_gram_placeholders = {}
        mean_placeholders = {}
        tr_cov_placeholders = {}
        root_cov_placeholders = {}
        means = {}
        tr_covs = {}
        root_covs = {}

        for layer in content_weights.keys():
            # Creates the placeholder for importing the content targets and creates the operations to compute the loss at each content layer
            _, h, w, d = g.get_tensor_by_name(layer).get_shape()

            content_placeholders[layer] = tf.placeholder(tf.float32,
                                                         shape=[None, h, w, d])
            content_losses[layer] = tf.reduce_mean(
                tf.abs(content_placeholders[layer] -
                       g.get_tensor_by_name(layer)))
            total_content_loss += content_losses[layer] * content_weights[layer]

        for layer in style_weights.keys():
            # Creates the placeholder for importing the pre-calculated style grams and creates the operations to compute the loss at each style layer
            _, h, w, d = g.get_tensor_by_name(layer).get_shape()
            N = h.value * w.value
            M = d.value
            if use_wass:
                means[layer], cov = wass_style_ops.calc_2_moments(
                    g.get_tensor_by_name(layer))
                eigvals, eigvects = tf.self_adjoint_eig(cov)
                eigroot_mat = tf.diag(tf.sqrt(tf.maximum(eigvals, 0)))
                root_covs[layer] = tf.matmul(tf.matmul(eigvects, eigroot_mat),
                                             eigvects,
                                             transpose_b=True)
                tr_covs[layer] = tf.reduce_sum(tf.maximum(eigvals, 0))
                mean_placeholders[layer] = tf.placeholder(
                    tf.float32, shape=means[layer].get_shape())
                tr_cov_placeholders[layer] = tf.placeholder(
                    tf.float32, shape=tr_covs[layer].get_shape())
                root_cov_placeholders[layer] = tf.placeholder(
                    tf.float32, shape=root_covs[layer].get_shape())
                style_losses[layer] = wass_style_ops.calc_l2wass_dist(
                    mean_placeholders[layer], tr_cov_placeholders[layer],
                    root_cov_placeholders[layer], means[layer], cov)
            else:
                input_grams[layer] = gram(g.get_tensor_by_name(layer))
                style_gram_placeholders[layer] = tf.placeholder(
                    tf.float32, shape=input_grams[layer].get_shape())
                style_losses[layer] = tf.reduce_mean(
                    tf.abs(input_grams[layer] -
                           style_gram_placeholders[layer]))
            total_style_loss += style_weights[layer] * style_losses[layer]

        total_loss = alpha * total_content_loss + beta * total_style_loss

        update = tf.train.AdamOptimizer(learning_rate).minimize(
            total_loss, var_list=[four_input])

        saver = tf.train.Saver(slim.get_model_variables())

        with tf.Session() as sess:
            tf.global_variables_initializer().run()
            restore_model(saver, model, sess)

            if display_image_freq < float("inf"):
                display_image(content_image)
                display_image(style_image)

            style_four_trans = sess.run(
                four_space, feed_dict={var_input: preprocess(style_image)})
            copy_style_four_to_input_op = four_input.assign(style_four_trans)
            sess.run(copy_style_four_to_input_op)

            # Calculates the style grams for each style layer and saves them to feed to their placeholders
            pre_calc_style_grams = {}
            pre_calc_mean_placeholders = {}
            pre_calc_tr_cov_placeholders = {}
            pre_calc_root_cov_placeholders = {}
            for layer in style_weights.keys():
                print(layer)
                if use_wass:
                    pre_calc_mean_placeholders[layer] = sess.run(means[layer])
                    pre_calc_tr_cov_placeholders[layer] = sess.run(
                        tr_covs[layer])
                    pre_calc_root_cov_placeholders[layer] = sess.run(
                        root_covs[layer])
                else:
                    pre_calc_style_grams[layer] = sess.run(input_grams[layer])

            content_four_trans = sess.run(
                four_space, feed_dict={var_input: preprocess(content_image)})
            copy_content_four_to_input_op = four_input.assign(
                content_four_trans)
            sess.run(copy_content_four_to_input_op)

            # Allows content targets to be used if they have already been calculated from a previous iteration
            content_targets = {}
            for layer in content_weights.keys():
                print(layer)
                content_targets[layer] = sess.run(g.get_tensor_by_name(layer))

            if random_initializer:
                reassign_random = four_input.assign(
                    np.random.normal(size=(2, 3, content_image.shape[1],
                                           (content_image.shape[2] + 2) // 2),
                                     scale=0.01))
                sess.run(reassign_random)

            assign_jitter = four_input.assign(four_space)

            # Generates the feed dictionary for session update
            feed_dict = {}
            for layer in content_weights.keys():
                feed_dict[content_placeholders[layer]] = content_targets[layer]
            for layer in style_weights.keys():
                if use_wass:
                    feed_dict[mean_placeholders[
                        layer]] = pre_calc_mean_placeholders[layer]
                    feed_dict[tr_cov_placeholders[
                        layer]] = pre_calc_tr_cov_placeholders[layer]
                    feed_dict[root_cov_placeholders[
                        layer]] = pre_calc_root_cov_placeholders[layer]
                else:
                    feed_dict[style_gram_placeholders[
                        layer]] = pre_calc_style_grams[layer]
            start_time = time.time()
            i = 0
            _, h, w, d = content_image.shape

            while ((i < cycles)
                   and (time.time() - start_time < training_time)):
                # Perform update step
                loss, _, temp_image, tcl, tsl = sess.run([
                    total_loss, update, recorr_img, total_content_loss,
                    total_style_loss
                ],
                                                         feed_dict=feed_dict)
                if (i % jitter_freq == 0 and i < jitter_stop_cycle
                        and (i > jitter_start_cycle
                             or time.time() - start_time > jitter_start_time)
                        and time.time() - start_time < jitter_stop_time):
                    temp_image = np.roll(temp_image,
                                         shift=randint(-1, 1),
                                         axis=randint(1, 2))
                    sess.run(assign_jitter, feed_dict={var_input: temp_image})
                # Print loss updates every 10 iterations
                if (i % 10 == 0):
                    print(loss, i, tsl, tcl)
                # Display image
                if display_image_freq < float("inf"):
                    if i % display_image_freq == 0:
                        #image_out = un_preprocess(np.clip(sess.run(recorr_img), -1., 1.))
                        display_image(
                            un_preprocess(np.clip(temp_image, -1., 1.)), True,
                            i)
                i += 1

            # Display the final image and save it to the folder
            image_out = un_preprocess(sess.run(recorr_img))
            display_image(image_out, save=True, name='final')
            if i >= cycles:
                print("Reached Cycle Limit: ", cycles)
            if (time.time() - start_time > training_time):
                print("Reached Time Limit: ", time.time() - start_time)
def non_target_graph(x, y, i, x_max, x_min, grad):

  eps = 2.0 * max_epsilon / 255.0
  alpha = eps / num_iter
  num_classes = 110

  with slim.arg_scope(inception.inception_v1_arg_scope()):
    logits_inc_v1, end_points_inc_v1 = inception.inception_v1(
      x, num_classes=num_classes, is_training=False, scope='InceptionV1')

  y_inception = tf.reduce_sum(end_points_inc_v1["Logits"] * tf.one_hot(y, num_classes), axis=1)
  conv_grad_in = tf.gradients(y_inception, end_points_inc_v1["Mixed_5c"])[0]
  cam_inception = get_cam(conv_grad_in, end_points_inc_v1["Mixed_5c"])

  cam_mask = tf.to_float(tf.greater(cam_inception, (110.0/255.0)))
  cam_matrix = cam_mask * cam_inception
  cam_matrix = tf.stop_gradient(cam_matrix)

  inc_res_x = tf.image.resize_bilinear(x, [299, 299],align_corners=False)
  with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()):
    logits_inc_res_v2, end_points_inc_res_v2 = inception_resnet_v2.inception_resnet_v2(
      inc_res_x, num_classes=num_classes, is_training=False, scope='InceptionResnetV2')

  with slim.arg_scope(mobilenet_v2.training_scope()):
    logits_mobi_v2, end_points_mobi_v2 = mobilenet_v2.mobilenet_v2_140(
      x, num_classes=num_classes, is_training=False, scope='MobilenetV2')
  end_points_mobi_v2['probs'] = tf.nn.softmax(logits_mobi_v2)

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

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

  # image = (((x + 1.0) * 0.5) * 255.0)#.astype(np.uint8)
  processed_imgs_vgg_16 = preprocess_for_model(image, 'vgg_16')
  with slim.arg_scope(vgg.vgg_arg_scope()):
    logits_vgg_16, end_points_vgg_16 = vgg.vgg_16(
      processed_imgs_vgg_16, num_classes=num_classes, is_training=False, scope='vgg_16')

  end_points_vgg_16['logits'] = end_points_vgg_16['vgg_16/fc8']
  end_points_vgg_16['probs'] = tf.nn.softmax(end_points_vgg_16['logits'])


  # Using model predictions as ground truth to avoid label leaking
  pred = tf.argmax(end_points_inc_v1['Predictions'] 
                 + end_points_res_v1_50['probs'] 
                 + end_points_vgg_16['probs'] 
                 + end_points_inc_res_v2['Predictions']
                 + end_points_mobi_v2['probs'], 1)

  first_round = tf.cast(tf.equal(i, 0), tf.int64)
  y = first_round * pred + (1 - first_round) * y
  one_hot = tf.one_hot(y, num_classes)

  logits = (end_points_inc_v1['Logits']
          + end_points_res_v1_50['logits']
          + end_points_vgg_16['logits']
          + end_points_inc_res_v2['Logits']
          + logits_mobi_v2) / 5.0
  cross_entropy = tf.losses.softmax_cross_entropy(one_hot,
                                                  logits,
                                                  label_smoothing=0.0,
                                                  weights=1.0)

  Auxlogits = end_points_inc_res_v2['AuxLogits']
  Auxcross_entropy = tf.losses.softmax_cross_entropy(one_hot,
                                                  Auxlogits,
                                                  label_smoothing=0.0,
                                                  weights=0.4)

  noise = tf.gradients(cross_entropy + Auxlogits, x)[0]
  noise = noise / tf.reduce_mean(tf.abs(noise), [1,2,3], keep_dims=True)
  noise = momentum * grad + noise
  x = x + alpha * tf.sign(noise)
  x = tf.clip_by_value(x, x_min, x_max)
  i = tf.add(i, 1)
  return x, y, i, x_max, x_min, noise
def main(argv=None):
    print("This script is used to compute accuracy!")
    if len(argv) < 2:
        print("No argv! You need to assign train/test/adv data as below:")
        print("Try: python compute_acc.py train, python compute_acc.py test\n")
        return

    batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3]
    nb_classes = FLAGS.num_classes

    tf.logging.set_verbosity(tf.logging.INFO)
    config = tf.ConfigProto()
    # allocate 50% of GPU memory
    config.gpu_options.allow_growth = True
    config.gpu_options.per_process_gpu_memory_fraction = 0.5

    with tf.Graph().as_default():
        print("Prepare graph...")
        x_input = tf.placeholder(tf.float32, shape=batch_shape)
        noise_input = gaussian_noise_layer(x_input, .0)

        with slim.arg_scope(inception.inception_v1_arg_scope()):
            _, end_points = inception.inception_v1(noise_input,
                                                   num_classes=nb_classes,
                                                   is_training=False)
        predicted_labels = tf.argmax(end_points['Predictions'], 1)

        print("Restore Model...")
        saver = tf.train.Saver(slim.get_model_variables())
        session_creator = tf.train.ChiefSessionCreator(
            config=config,
            scaffold=tf.train.Scaffold(saver=saver),
            checkpoint_filename_with_path=FLAGS.checkpoint_path)

        print("Run computation...")
        with tf.train.MonitoredSession(
                session_creator=session_creator) as sess:
            if argv[1] == 'test':
                INPUT_DIR = FLAGS.test_data_dir
                FLAGS.output_file = './result/test_accuracy.txt'
            elif argv[1] == 'train':
                INPUT_DIR = FLAGS.train_data_dir
                FLAGS.output_file = './result/train_accuracy.txt'
            elif argv[1] == 'adv':
                INPUT_DIR = FLAGS.adv_data_dir
                FLAGS.output_file = './result/adversarial_accuracy.txt'

            data_generator = load_path_label(INPUT_DIR,
                                             batch_shape,
                                             shuffle=False)

            acc_dict = {}
            for i in range(FLAGS.num_classes):
                acc_dict[i] = [0, 0]

            for images, true_labels, _ in tqdm(data_generator):
                labels = sess.run(predicted_labels,
                                  feed_dict={x_input: images})
                for i in range(len(true_labels)):
                    acc_dict[true_labels[i]][1] += 1
                    if labels[i] == true_labels[i]:
                        acc_dict[true_labels[i]][0] += 1

            print("Compute accuracy...")
            with open(FLAGS.output_file, 'w') as f:
                total_true = 0
                total_count = 0
                for i in range(FLAGS.num_classes):
                    total_true += acc_dict[i][0]
                    total_count += acc_dict[i][1]
                    if acc_dict[i][1] == 0:
                        f.writelines("class: %d, accuracy: %d/%d = %.3f \n" %
                                     (i, acc_dict[i][0], acc_dict[i][1], 0))
                    else:
                        f.writelines("class: %d, accuracy: %d/%d = %.3f \n" %
                                     (i, acc_dict[i][0], acc_dict[i][1],
                                      acc_dict[i][0] / acc_dict[i][1]))

                print("Total accuracy: %.3f \n" % (total_true / total_count))
                f.writelines("Total accuracy: %.3f \n" %
                             (total_true / total_count))

            print('Save accuracy result to %s' % FLAGS.output_file)