示例#1
0
  def testRaiseValueErrorWithInvalidDepthMultiplier(self):
    batch_size = 5
    height, width = 299, 299
    num_classes = 1000

    inputs = tf.random_uniform((batch_size, height, width, 3))
    with self.assertRaises(ValueError):
      _ = inception.inception_v3(inputs, num_classes, depth_multiplier=-0.1)
    with self.assertRaises(ValueError):
      _ = inception.inception_v3(inputs, num_classes, depth_multiplier=0.0)
示例#2
0
    def testTrainEvalWithReuse(self):
        train_batch_size = 5
        eval_batch_size = 2
        height, width = 150, 150
        num_classes = 1000

        train_inputs = tf.random_uniform((train_batch_size, height, width, 3))
        inception.inception_v3(train_inputs, num_classes)
        eval_inputs = tf.random_uniform((eval_batch_size, height, width, 3))
        logits, _ = inception.inception_v3(eval_inputs, num_classes, is_training=False, 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,))
示例#3
0
def main(_):
  batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3]
  num_classes = 1001

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

    # 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,
        master=FLAGS.master)

    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):
            out_file.write('{0},{1}\n'.format(filename, label))
示例#4
0
def build_single_inceptionv3(train_tfdata, is_train, dropout_keep_prob, reduce_dim = False):
    train_tfdata_resize = tf.image.resize_images(train_tfdata, (299, 299))
    with slim.arg_scope(inception.inception_v3_arg_scope()):
        identity, end_points = inception.inception_v3(train_tfdata_resize, dropout_keep_prob = dropout_keep_prob, is_training=is_train)
        feature = slim.flatten(end_points['Mixed_7c'])
        if reduce_dim:
            feature = slim.fully_connected(feature, 256, scope='feat')
    return identity, feature
示例#5
0
    def testLogitsNotSqueezed(self):
        num_classes = 25
        images = tf.random_uniform([1, 299, 299, 3])
        logits, _ = inception.inception_v3(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])
示例#6
0
    def testBuildEndPointsWithDepthMultiplierGreaterThanOne(self):
        batch_size = 5
        height, width = 299, 299
        num_classes = 1000

        inputs = tf.random_uniform((batch_size, height, width, 3))
        _, end_points = inception.inception_v3(inputs, num_classes)

        endpoint_keys = [key for key in end_points.keys() if key.startswith("Mixed") or key.startswith("Conv")]

        _, end_points_with_multiplier = inception.inception_v3(
            inputs, num_classes, scope="depth_multiplied_net", depth_multiplier=2.0
        )

        for key in endpoint_keys:
            original_depth = end_points[key].get_shape().as_list()[3]
            new_depth = end_points_with_multiplier[key].get_shape().as_list()[3]
            self.assertEqual(2.0 * original_depth, new_depth)
示例#7
0
    def testHalfSizeImages(self):
        batch_size = 5
        height, width = 150, 150
        num_classes = 1000

        inputs = tf.random_uniform((batch_size, height, width, 3))
        logits, end_points = inception.inception_v3(inputs, num_classes)
        self.assertTrue(logits.op.name.startswith("InceptionV3/Logits"))
        self.assertListEqual(logits.get_shape().as_list(), [batch_size, num_classes])
        pre_pool = end_points["Mixed_7c"]
        self.assertListEqual(pre_pool.get_shape().as_list(), [batch_size, 3, 3, 2048])
示例#8
0
    def testBuildClassificationNetwork(self):
        batch_size = 5
        height, width = 299, 299
        num_classes = 1000

        inputs = tf.random_uniform((batch_size, height, width, 3))
        logits, end_points = inception.inception_v3(inputs, num_classes)
        self.assertTrue(logits.op.name.startswith("InceptionV3/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])
示例#9
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_v3_arg_scope()):
        logits, _ = inception.inception_v3(image, num_classes, is_training=False, spatial_squeeze=False)
    probabilities = tf.nn.softmax(logits)
    init_fn = slim.assign_from_checkpoint_fn('inception_v3.ckpt', slim.get_model_variables('InceptionV3'))

    with tf.Session() as sess:
        init_fn(sess)
        saver = tf.train.Saver(tf.global_variables())
        saver.save(sess, "output/"+name)
示例#10
0
    def testEvaluation(self):
        batch_size = 2
        height, width = 299, 299
        num_classes = 1000

        eval_inputs = tf.random_uniform((batch_size, height, width, 3))
        logits, _ = inception.inception_v3(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 predict(self,preprocessed_inputs):
        """
        Runs inference on model

        Args:
            preprocessed_inputs: processed batch to go to model
        Returns:
            pre_logits: layer right before the logits
        """
        with slim.arg_scope(inception.inception_v3_arg_scope()):
              _,end_points = inception.inception_v3(
                  preprocessed_inputs,num_classes=1001,is_training=self._is_training,reuse=self._reuse)
        return end_points['PreLogits']
def main(_):
  # Images for inception classifier are normalized to be in [-1, 1] interval,
  # eps is a difference between pixels so it should be in [0, 2] interval.
  # Renormalizing epsilon from [0, 255] to [0, 2].
  eps = 2.0 * FLAGS.max_epsilon / 255.0
  batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3]
  num_classes = 1001

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

  all_images_taget_class = load_target_class(FLAGS.input_dir)

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

    with slim.arg_scope(inception.inception_v3_arg_scope()):
      logits, end_points = inception.inception_v3(
          x_input, num_classes=num_classes, is_training=False)

    target_class_input = tf.placeholder(tf.int32, shape=[FLAGS.batch_size])
    one_hot_target_class = tf.one_hot(target_class_input, num_classes)
    cross_entropy = tf.losses.softmax_cross_entropy(one_hot_target_class,
                                                    logits,
                                                    label_smoothing=0.1,
                                                    weights=1.0)
    cross_entropy += tf.losses.softmax_cross_entropy(one_hot_target_class,
                                                     end_points['AuxLogits'],
                                                     label_smoothing=0.1,
                                                     weights=0.4)
    x_adv = x_input - eps * tf.sign(tf.gradients(cross_entropy, x_input)[0])
    x_adv = tf.clip_by_value(x_adv, -1.0, 1.0)

    # 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,
        master=FLAGS.master)

    with tf.train.MonitoredSession(session_creator=session_creator) as sess:
      for filenames, images in load_images(FLAGS.input_dir, batch_shape):
        target_class_for_batch = (
            [all_images_taget_class[n] for n in filenames]
            + [0] * (FLAGS.batch_size - len(filenames)))
        adv_images = sess.run(x_adv,
                              feed_dict={
                                  x_input: images,
                                  target_class_input: target_class_for_batch
                              })
        save_images(adv_images, filenames, FLAGS.output_dir)
示例#13
0
    def testUnknowBatchSize(self):
        batch_size = 1
        height, width = 299, 299
        num_classes = 1000

        inputs = tf.placeholder(tf.float32, (None, height, width, 3))
        logits, _ = inception.inception_v3(inputs, num_classes)
        self.assertTrue(logits.op.name.startswith("InceptionV3/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))
示例#14
0
 def testUnknownImageShape(self):
     tf.reset_default_graph()
     batch_size = 2
     height, width = 299, 299
     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_v3(inputs, num_classes)
         self.assertListEqual(logits.get_shape().as_list(), [batch_size, num_classes])
         pre_pool = end_points["Mixed_7c"]
         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, 8, 8, 2048])
 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_v3_arg_scope()):
         # Inception preprocessing uses [-1, 1]-scaled input.
         x_input = x_input * 2.0 - 1.0
         _, end_points = inception.inception_v3(
             x_input, num_classes=self.num_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
示例#16
0
    def testBuildEndPoints(self):
        batch_size = 5
        height, width = 299, 299
        num_classes = 1000

        inputs = tf.random_uniform((batch_size, height, width, 3))
        _, end_points = inception.inception_v3(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])
        self.assertTrue("Mixed_7c" in end_points)
        pre_pool = end_points["Mixed_7c"]
        self.assertListEqual(pre_pool.get_shape().as_list(), [batch_size, 8, 8, 2048])
        self.assertTrue("PreLogits" in end_points)
        pre_logits = end_points["PreLogits"]
        self.assertListEqual(pre_logits.get_shape().as_list(), [batch_size, 1, 1, 2048])
示例#17
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_v3_arg_scope()):
            _, end_points = inception.inception_v3(

                x_input, num_classes=self.nb_classes, is_training=False,

                reuse=reuse)

        self.built = True
        self.logits = end_points['Logits']
        self.probs = end_points['Predictions'].op.inputs[0]
        if return_logits:
            return self.logits
        else:
            return self.probs
示例#18
0
def main(_):
    # eps = FLAGS.max_epsilon / 255.0
    eps = 2.0 / 255.0

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

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

    with tf.Graph().as_default():
        # Prepare graph
        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)

        # 2-step FGSM attack:
        noisy_images = x_input + eps * tf.sign(tf.random_normal(batch_shape))
        x_noisy = tf.clip_by_value(noisy_images, x_min, x_max)

        with slim.arg_scope(inception.inception_v3_arg_scope()):
            _, end_points = inception.inception_v3(x_noisy,
                                                   num_classes=num_classes,
                                                   is_training=False)

        predictions = end_points['Predictions']

        # 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,
            master=FLAGS.master)

        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):
                    pred = sess.run(predictions, feed_dict={x_input: images})
                    for filename, p in zip(filenames, list(pred)):
                        p_str = " ".join([str(i) for i in list(p)])
                        out_file.write('{0},{1}\n'.format(filename, p_str))
示例#19
0
    def get_inception_v3(self, images, reuse=None, scope=None):
        arg_scope = inception.inception_v3_arg_scope()
        with slim.arg_scope(arg_scope):
            with tf.variable_scope(scope or 'image_feature_extractor',
                                   reuse=reuse):
                images = tf.image.resize_bilinear(images,
                                                  size=[299, 299],
                                                  align_corners=False)
                scaled_input_tensor = tf.scalar_mul((1.0 / 255), images)
                scaled_input_tensor = tf.subtract(scaled_input_tensor, 0.5)
                scaled_input_tensor = tf.multiply(scaled_input_tensor, 2.0)

                logits, end_points = inception.inception_v3(
                    scaled_input_tensor,
                    is_training=False,
                    num_classes=1001,
                    reuse=reuse)
                h = end_points['PreLogits']
                h = slim.flatten(h)
        return h
def main(_):
    batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3]
    num_classes = 1001

    node_lookup = ReadableLabel(
        "data/inception-v3/imagenet1000_clsid_to_human.txt")

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

        inception_feature = end_points['PreLogits']

        # 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,
            master=FLAGS.master)

        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, features = sess.run(
                    [predicted_labels, inception_feature],
                    feed_dict={x_input: images})
                print(features.shape)
                for filename, label in zip(filenames, labels):
                    # out_file.write('{0},{1}\n'.format(filename, label))
                    print(filename, label,
                          node_lookup.get_human_readable(label))
示例#21
0
    def __init__(self, model_path=INCEPTION_V3_PATH, image_size=299):
        cache_dir = os.sep.join(model_path.split(os.sep)[:-1])
        get_file(fname=model_path.split(os.sep)[-1],
                 cache_dir=cache_dir,
                 cache_subdir='',
                 origin=INCEPTION_V3_URL,
                 untar=True)

        self._get_session()
        self.checkpoint_file = model_path
        self.image_size = image_size
        self._get_inception_preprocessing(inception_size=299)

        self.arg_scope = inception.inception_v3_arg_scope()
        with slim.arg_scope(self.arg_scope):
            self.logits, self.end_points = inception.inception_v3(
                self.scaled_input_tensor,
                is_training=False,
                num_classes=1001,
                reuse=False)
        self._restore()
 def logits_and_labels(self, xs_ph):
     xs_ph = xs_ph * 2.0 - 1.0
     batch_size = xs_ph.get_shape().as_list()[0]
     xs_ph_tile = tf.tile(tf.expand_dims(xs_ph, 1), [1, self.num_ensemble, 1, 1, 1])
     xs_ph_tile = tf.reshape(xs_ph_tile, (-1,) + self.x_shape)
     
     with slim.arg_scope(inception.inception_v3_arg_scope()):
         with tf.variable_scope("RandDisc"):
             xs_ph_tile = self.iterative_clustering_layer(source=xs_ph_tile, n_clusters=self.n_clusters,
                                                            sigma=10, alpha=10, noise_level_1=self.noise_level,
                                                            noise_level_2=self.noise_level)
         logits, end_points = inception.inception_v3(
             xs_ph_tile, 
             num_classes=self.n_class, 
             is_training=False, 
             reuse=tf.AUTO_REUSE)
         logits = tf.reshape(logits, [batch_size, self.num_ensemble, -1])
         logits = tf.reduce_mean(logits, axis = 1)
         
         predicted_labels = tf.argmax(logits, 1)
     return logits, predicted_labels
def exercise8():
    CLASS_NAME_REGEX = re.compile(r'^n\d+\s+(.*)\s*$', re.M | re.U)

    def prepare_image(fname, show=False):
        channels = 3
        test_image = mpimg.imread(os.path.join(fname))[:, :, :channels]
        test_image = test_image.astype(np.float64)
        test_image_shape = test_image.shape
        if show:
            plt.imshow(test_image)
            plt.axis('off')
            plt.show()
        test_image = test_image.reshape(-1, 3)
        scaler = StandardScaler()
        test_image = scaler.fit_transform(test_image)
        return test_image.reshape(test_image_shape)

    def load_class_names():
        with open(os.path.join('datasets', 'inception', 'imagenet_class_names.txt'), 'rb') as f:
            content = f.read().decode('utf-8')
            return CLASS_NAME_REGEX.findall(content)

    test_img = prepare_image('cat.jpg', False)
    # fetch_pretrained_inception_v3()
    class_names = ['background'] + load_class_names()

    X = tf.placeholder(tf.float32, shape=[None, 299, 299, 3])
    with slim.arg_scope(inception.inception_v3_arg_scope()):
        logits, end_points = inception.inception_v3(X, num_classes=1001, is_training=False)
    predictions = end_points['Predictions']
    saver = tf.train.Saver()

    X_test = test_img.reshape((-1,) + test_img.shape)
    with tf.Session() as sess:
        saver.restore(sess, INCEPTION_V3_CHECKPOINT_PATH)
        predictions_val = sess.run(predictions, feed_dict={X: X_test})
        top5 = np.argpartition(predictions_val[0], -5)[-5:]
        top5 = reversed(top5[np.argsort(predictions_val[0][top5])])
        for i in top5:
            print('{0}: {1:.2f}%'.format(class_names[i], 100 * predictions_val[0][i]))
  def __call__(self, x_input):
    """Constructs model and return probabilities for given input."""
    reuse = True if self.built else None


    if not False:
      with slim.arg_scope(inception.inception_v3_arg_scope()):
        _, end_points = inception.inception_v3(
            x_input, num_classes=self.num_classes, is_training=False,
            reuse=reuse)
    else:
      import inception_resnet_v2
      with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()):
        _, end_points = inception_resnet_v2.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 probs
示例#25
0
def create_inception_network(image_tensor,
                             cnn_output_num=1024,
                             target_num=512,
                             is_training=True,
                             space_name="embedding_net",
                             reuse=False):

    with tf.variable_scope(space_name, reuse=reuse):  # 変数を共有
        with slim.arg_scope(inception.inception_v3_arg_scope()):
            outputs_cnn, endpoints = inception.inception_v3(
                image_tensor,
                num_classes=cnn_output_num,
                is_training=is_training)
        normalizer_params = {'is_training': is_training}
        with slim.arg_scope([slim.layers.fully_connected],
                            activation_fn=tf.nn.relu,
                            normalizer_fn=slim.batch_norm,
                            normalizer_params=normalizer_params):
            hidden = slim.layers.flatten(outputs_cnn)
            outputs = slim.layers.fully_connected(hidden, target_num)

    return outputs
示例#26
0
    def __init__(self):
        self.x_input = tf.placeholder('float32', [batch_size, 299, 299, 3],
                                      name='x_input_input_to_facenet')
        # self.r_input = tf.placeholder('float32', [batch_size, 299, 299, 3], name='reference_input_input_to_facenet')
        self.y_input = tf.placeholder(tf.int64, [batch_size],
                                      name='true_label')
        # self.phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train')
        # input_v3 = self.r_input * 2-1
        # out_160 = set_loader.prewhitenfacenet(self.x_input)
        # t_target_image_160 = set_loader.prewhitenfacenet(self.reference_input)
        #resgn
        # self.net_g = SRGAN_g(self.x_input, is_train=False, reuse=tf.AUTO_REUSE)
        with slim.arg_scope(inception.inception_v3_arg_scope()):
            _, end_points = inception.inception_v3(self.x_input,
                                                   num_classes=1001,
                                                   is_training=False,
                                                   reuse=tf.AUTO_REUSE)

        # with slim.arg_scope(inception.inception_v3_arg_scope()):
        #     _, end_points = inception.inception_v3(
        #         self.net_g.outputs, num_classes=1001, is_training=False, reuse=tf.AUTO_REUSE)
        self.pre_softmax = end_points['Predictions']
        #calculate the predict label

        # self.pre_softmax = tf.transpose(set_loader.softmax(distance1))    #

        y_xent = tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=self.y_input, logits=self.pre_softmax)

        self.xent = tf.reduce_sum(y_xent)  #

        self.y_pred = tf.argmax(self.pre_softmax, 1)

        self.correct_prediction = tf.equal(self.y_pred, self.y_input)

        self.num_correct = tf.reduce_sum(
            tf.cast(self.correct_prediction, tf.int64))
        self.accuracy = tf.reduce_mean(
            tf.cast(self.correct_prediction, tf.float32))
示例#27
0
def google_model_make_prediction(filename):
    image = os.path.join('{0}{1}'.format(c.image_dir, filename))

    verify_image(image)

    tf.reset_default_graph()
    X = tf.placeholder(tf.float32, shape=[None, 299, 299, 3], name="X")
    with slim.arg_scope(inception.inception_v3_arg_scope()):
        logits, end_points = inception.inception_v3(X, num_classes=1001, is_training=False)
    with tf.name_scope("ouput"):
        flower_logits = tf.layers.dense(tf.squeeze(end_points["PreLogits"], axis=[1, 2]), 5, name="flower_logits")
        Y_proba = tf.nn.softmax(flower_logits, name="Y_proba")
    saver = tf.train.Saver()
    init = tf.global_variables_initializer()
    X_input = prepare_image(mpimg.imread(image), 299, 299, .01)
    X_input.shape

    with tf.Session() as sess:
        init.run()
        saver.restore(sess, c.model_dir + c.model_name)
        probs = sess.run(Y_proba, feed_dict={X: X_input.reshape(-1,299,299,3)})
        return [round(f * 100, 2) for f in probs[0]]
示例#28
0
  def testBuildEndPoints(self):
    batch_size = 5
    height, width = 299, 299
    num_classes = 1000

    inputs = tf.random_uniform((batch_size, height, width, 3))
    _, end_points = inception.inception_v3(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])
    self.assertTrue('Mixed_7c' in end_points)
    pre_pool = end_points['Mixed_7c']
    self.assertListEqual(pre_pool.get_shape().as_list(),
                         [batch_size, 8, 8, 2048])
    self.assertTrue('PreLogits' in end_points)
    pre_logits = end_points['PreLogits']
    self.assertListEqual(pre_logits.get_shape().as_list(),
                         [batch_size, 1, 1, 2048])
def main(_):
    batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3]
    num_classes = 1001
    itr = 30
    tf.logging.set_verbosity(tf.logging.INFO)
    #print("input dir = {0}".format(FLAGS.input_dir))
    with tf.Graph().as_default():
        # Prepare graph
        x_input = tf.placeholder(tf.float32, shape=batch_shape)

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

        # 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,
            master=FLAGS.master)

        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):
                    for idxx in range(batch_shape[0]):
                        images[idxx, :, :, :] = defend_tv(
                            images[idxx])  # tv defense
                    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))
示例#30
0
def construct_inception():
    X = tf.placeholder(tf.float32, shape=[None, 299, 299, 3], name="X")
    y = tf.placeholder(tf.int32, shape=[None])
    training = tf.placeholder_with_default(False, shape=[])
    with slim.arg_scope(inception.inception_v3_arg_scope()):
        logits, end_points = inception.inception_v3(X,
                                                    num_classes=1001,
                                                    is_training=training)

    inception_saver = tf.train.Saver()

    prelogits = tf.squeeze(end_points["PreLogits"], axis=[1, 2])

    n_outputs = 10

    # connect logits to n_outputs
    with tf.name_scope("new_output_layer"):
        new_logits = tf.layers.dense(prelogits, n_outputs, name="new_logits")
        Y_proba = tf.nn.softmax(new_logits, name="Y_proba")

    with tf.name_scope("train"):
        xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
            logits=new_logits, labels=y)
        loss = tf.reduce_mean(xentropy)
        optimizer = tf.train.AdamOptimizer()

        #new_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope="new_logits") ###### only train 'new_logits'
        #training_op = optimizer.minimize(loss, var_list=new_vars)                             ###### only train 'new_logits'
        training_op = optimizer.minimize(loss)

    with tf.name_scope("eval"):
        correct = tf.nn.in_top_k(new_logits, y, 1)
        accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))

    with tf.name_scope("init_and_save"):
        init = tf.global_variables_initializer()
        saver = tf.train.Saver()
示例#31
0
def create_model(x, reuse=None):
  """Create model graph.

  Args:
    x: input images
    reuse: reuse parameter which will be passed to underlying variable scopes.
      Should be None first call and True every subsequent call.

  Returns:
    (logits, end_points) - tuple of model logits and enpoints

  Raises:
    ValueError: if model type specified by --model_name flag is invalid.
  """
  if FLAGS.model_name == 'inception_v3':
    with slim.arg_scope(inception.inception_v3_arg_scope()):
      return inception.inception_v3(
          x, num_classes=NUM_CLASSES, is_training=False, reuse=reuse)
  elif FLAGS.model_name == 'inception_resnet_v2':
    with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()):
      return inception_resnet_v2.inception_resnet_v2(
          x, num_classes=NUM_CLASSES, is_training=False, reuse=reuse)
  else:
    raise ValueError('Invalid model name: %s' % (FLAGS.model_name))
示例#32
0
def create_model(x, reuse=None):
  """Create model graph.

  Args:
    x: input images
    reuse: reuse parameter which will be passed to underlying variable scopes.
      Should be None first call and True every subsequent call.

  Returns:
    (logits, end_points) - tuple of model logits and enpoints

  Raises:
    ValueError: if model type specified by --model_name flag is invalid.
  """
  if FLAGS.model_name == 'inception_v3':
    with slim.arg_scope(inception.inception_v3_arg_scope()):
      return inception.inception_v3(
          x, num_classes=NUM_CLASSES, is_training=False, reuse=reuse)
  elif FLAGS.model_name == 'inception_resnet_v2':
    with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()):
      return inception_resnet_v2.inception_resnet_v2(
          x, num_classes=NUM_CLASSES, is_training=False, reuse=reuse)
  else:
    raise ValueError('Invalid model name: %s' % (FLAGS.model_name))
示例#33
0
def inception_model_fn(features, labels, mode, params):
  """Inception v3 model using Estimator API."""
  del params

  num_classes = FLAGS.num_classes
  training_active = (mode == tf.estimator.ModeKeys.TRAIN)
  eval_active = (mode == tf.estimator.ModeKeys.EVAL)

  if training_active:
    size = FLAGS.train_batch_size // FLAGS.num_shards
  else:
    size = FLAGS.eval_batch_size
  input_transform_fn = TensorTranspose(size, is_input=True)
  features = input_transform_fn(features)

  with slim.arg_scope(inception.inception_v3_arg_scope(
      use_fused_batchnorm=FLAGS.use_fused_batchnorm)):
    logits, end_points = inception.inception_v3(
        features,
        num_classes,
        is_training=training_active,
        depth_multiplier=FLAGS.depth_multiplier)

  predictions = {
      'classes': tf.argmax(input=logits, axis=1),
      'probabilities': tf.nn.softmax(logits, name='softmax_tensor')
  }

  if mode == tf.estimator.ModeKeys.PREDICT:
    return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)

  if 'AuxLogits' in end_points:
    aux_loss = tf.losses.softmax_cross_entropy(
        onehot_labels=labels,
        logits=end_points['AuxLogits'],
        weights=0.4,
        label_smoothing=0.1,
        scope='aux_loss')
    tf.losses.add_loss(aux_loss)

  prediction_loss = tf.losses.softmax_cross_entropy(
      onehot_labels=labels,
      logits=logits,
      weights=1.0,
      label_smoothing=0.1)
  tf.losses.add_loss(prediction_loss)
  loss = tf.losses.get_total_loss(add_regularization_losses=True)

  initial_learning_rate = FLAGS.learning_rate * FLAGS.train_batch_size / 256
  final_learning_rate = 0.01 * initial_learning_rate

  train_op = None
  if training_active:
    # Multiply the learning rate by 0.1 every 30 epochs.
    training_set_len = imagenet.get_split_size('train')
    batches_per_epoch = training_set_len // FLAGS.train_batch_size
    learning_rate = tf.train.exponential_decay(
        learning_rate=initial_learning_rate,
        global_step=tf.train.get_global_step(),
        decay_steps=_LEARNING_RATE_DECAY_EPOCHS * batches_per_epoch,
        decay_rate=_LEARNING_RATE_DECAY,
        staircase=True)

    # Set a minimum boundary for the learning rate.
    learning_rate = tf.maximum(
        learning_rate, final_learning_rate, name='learning_rate')

    # tf.summary.scalar('learning_rate', learning_rate)

    if FLAGS.optimizer == 'sgd':
      tf.logging.info('Using SGD optimizer')
      optimizer = tf.train.GradientDescentOptimizer(
          learning_rate=FLAGS.learning_rate)
    elif FLAGS.optimizer == 'momentum':
      tf.logging.info('Using Momentum optimizer')
      optimizer = tf.train.MomentumOptimizer(
          learning_rate=FLAGS.learning_rate, momentum=0.9)
    else:
      tf.logging.fatal('Unknown optimizer:', FLAGS.optimizer)

    if FLAGS.use_tpu:
      optimizer = tpu_optimizer.CrossShardOptimizer(optimizer)

    update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
    with tf.control_dependencies(update_ops):
      train_op = optimizer.minimize(
          loss, global_step=tf.train.get_or_create_global_step())

  eval_metrics = None
  if eval_active:
    def metric_fn(labels, logits):
      predictions = tf.argmax(input=logits, axis=1)
      accuracy = tf.metrics.accuracy(tf.argmax(input=labels, axis=1),
                                     predictions)
      return {'accuracy': accuracy}
    eval_metrics = (metric_fn, [labels, logits])

  return tpu_estimator.TPUEstimatorSpec(
      mode=mode, loss=loss, train_op=train_op, eval_metrics=eval_metrics)
# inception_v3网络的图片输入尺寸,检查点文件
img_size = inception.inception_v3.default_image_size
checkpoint_file = os.path.join("inception_v3", "inception_v3.ckpt")

tf.reset_default_graph()
slim = tf.contrib.slim

# 设置输入
input_img = tf.placeholder("float", [None, img_size, img_size, 3])

# 载入inception v3模型
with tf.Session() as sess:
    arg_scope = inception.inception_v3_arg_scope()

    with slim.arg_scope(arg_scope):
        _, end_points = inception.inception_v3(input_img, is_training=False)

        saver = tf.train.Saver()
        saver.restore(sess, checkpoint_file)

    for video_data in data.data:

        # 获取此视频时间序列特征的存放路径
        seq_path = os.path.join('data', 'sequences', video_data[2] + '-' + str(seq_length) +
                                '-features')

        # 检查是否已经存在该视频的特征
        if os.path.isfile(seq_path + '.npy'):
            pbar.update(1)
            continue
def main(_):
    # Images for inception classifier are normalized to be in [-1, 1] interval,
    # eps is a difference between pixels so it should be in [0, 2] interval.
    # Renormalizing epsilon from [0, 255] to [0, 2].
    eps = 2.0 * FLAGS.max_epsilon / 255.0
    alpha = 2.0 * FLAGS.iter_alpha / 255.0
    num_iter = FLAGS.num_iter
    batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3]
    num_classes = 1001

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

    all_images_taget_class = load_target_class(FLAGS.input_dir)

    with tf.Graph().as_default():
        # Prepare graph
        x_input = tf.placeholder(tf.float32, shape=batch_shape)
        x_adv = x_input
        target_class_input = tf.placeholder(tf.int32, shape=[FLAGS.batch_size])
        one_hot_target_class = tf.one_hot(target_class_input, num_classes)

        with slim.arg_scope(inception.inception_v3_arg_scope()):
            logits, end_points = inception.inception_v3(
                x_input,
                num_classes=num_classes,
                is_training=False,
                reuse=False)
            cross_entropy = tf.losses.softmax_cross_entropy(
                one_hot_target_class, logits, label_smoothing=0.1, weights=1.0)
        grad = tf.gradients(cross_entropy, x_adv)[0]

        # 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,
            master=FLAGS.master)

        with tf.train.MonitoredSession(
                session_creator=session_creator) as sess:
            for filenames, images_, values in load_images(
                    FLAGS.input_dir, batch_shape):
                max_epsilon = FLAGS.max_epsilon * 2 / 255.0
                target_class_for_batch = ([
                    all_images_taget_class[filename] for filename in filenames
                ] + [0] * (FLAGS.batch_size - len(filenames)))
                max_clip = np.clip(images_ + eps, -1, 1.0)
                min_clip = np.clip(images_ - eps, -1, 1.0)
                grad_value = sess.run(grad,
                                      feed_dict={
                                          x_input:
                                          values,
                                          target_class_input:
                                          target_class_for_batch
                                      })
                grad_value = grad_value / (
                    np.std(grad_value, axis=(1, 2, 3), keepdims=True) + 1e-15)
                values = values - 0.2 * float(
                    float(FLAGS.step_n) - float(FLAGS.step)) / float(
                        FLAGS.step_n) * 0.5 * grad_value * max_epsilon
                values = np.maximum(values, min_clip)
                values = np.minimum(values, max_clip)
                save_images(values, filenames, FLAGS.output_dir)
		min_after_dequeue = min_queue_size
	)
	tf.summary.image('images',image_batch)
	#reshape to labels to vector
	return image_batch,tf.reshape(label_batch,[BATCH_SIZE])

#preprocessing and training
with tf.device('/cpu:0'):
	print('starting to load data. please wait...')
	key,image,label  = parse_data([FLOWERS_DIR])
	scaled,label = format_input(image,label)
	image_batch,label_batch = gen_batch(scaled,label)

training = tf.placeholder_with_default(False, shape=[])
with slim.arg_scope(inception.inception_v3_arg_scope()):
	old_logits,end_points = inception.inception_v3(
					image_batch,num_classes=1001,is_training=is_training)

	#saver = tf.train.Saver()
	#reuse_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)
	#reuse_vars_dict = {var.name: var for var in reuse_vars}
	#original_saver = tf.train.Saver(reuse_vars_dict)
	original_saver = tf.train.Saver()
with tf.name_scope('flower_output'):
	pre_logits = end_points['PreLogits']
	print(pre_logits.op.name)
	#logits = tf.layers.conv2d(pre_logits,kernel_initializer=tf.contrib.layers.variance_scaling_initializer(),
	#	kernel_size=1,strides=1,filters=5,padding='SAME',name='flower_logits')
	#prediction = tf.nn.softmax(logits,name='flower_softmax')
	#logits_full = tf.reshape(logits,shape=[-1,NUM_OUT])
	#logits_full = tf.squeeze(logits)
	pool = tf.squeeze(pre_logits)
示例#37
0
# 
# The below code runs a Tensorflow session to generate targeted adversarial images. In each case, there's a specific target class that we're trying to trick the image classifier to output.
# 
# *Note - this currently isn't working - it's generating adversarial images, but they aren't correctly targeted*

# In[ ]:


all_images_target_class = {image_metadata["ImageId"][i]+".png": image_metadata["TargetClass"][i]
                           for i in image_metadata.index}

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

    with slim.arg_scope(inception.inception_v3_arg_scope()):
        logits, end_points = inception.inception_v3(
            x_input, num_classes=num_classes, is_training=False)

    target_class_input = tf.placeholder(tf.int32, shape=[batch_size])
    one_hot_target_class = tf.one_hot(target_class_input, num_classes)
    cross_entropy = tf.losses.softmax_cross_entropy(one_hot_target_class,
                                                    logits,
                                                    label_smoothing=0.1,
                                                    weights=1.0)
    cross_entropy += tf.losses.softmax_cross_entropy(one_hot_target_class,
                                                     end_points['AuxLogits'],
                                                     label_smoothing=0.1,
                                                     weights=0.4)
    x_adv = x_input - eps * tf.sign(tf.gradients(cross_entropy, x_input)[0])
    x_adv = tf.clip_by_value(x_adv, -1.0, 1.0)

    saver = tf.train.Saver(slim.get_model_variables())
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)
示例#39
0
        "data/inception-v3/imagenet1000_clsid_to_human.txt")
    inception_graph = tf.Graph()
    inception_sess = tf.Session(graph=inception_graph)

    slim = tf.contrib.slim
    batch_shape = [1, 299, 299, 3]
    num_classes = 1001
    latest_checkpoint = tf.train.latest_checkpoint(checkpoint_path)

    with inception_graph.as_default(), inception_sess.as_default() as sess:
        # Prepare graph
        x_input = tf.placeholder(tf.float32, shape=batch_shape)

        with slim.arg_scope(inception.inception_v3_arg_scope()):
            _, end_points = inception.inception_v3(x_input,
                                                   num_classes=num_classes,
                                                   is_training=False,
                                                   dropout_keep_prob=1.0)

        inception_feature = end_points['PreLogits']
        predicted_labels = tf.argmax(end_points['Predictions'], 1)

        # Run computation
        print("Restoring Saved Variables from Checkpoint: {}".format(
            latest_checkpoint))
        saver = tf.train.Saver(slim.get_model_variables())
        saver.restore(sess, latest_checkpoint)

        z = 0
        n = 82
        data = bson.decode_file_iter(open(input_bson_filename, 'rb'))
示例#40
0
def main(_):

    # Images for inception classifier are normalized to be in [-1, 1] interval,
    # eps is a difference between pixels so it should be in [0, 2] interval.
    # Renormalizing epsilon from [0, 255] to [0, 2].
    eps = 2.0 * FLAGS.max_epsilon / 255.0
    alpha = 2.0 * FLAGS.iter_alpha / 255.0
    num_iter = FLAGS.num_iter
    batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3]
    num_classes = 1001

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

    label_smoothing = 0.1
    coeff_mul_alpha = 0.019
    AUX_ENS_V2 = 2.4
    AUX_INC_V3 = 0.87
    aux_weight = AUX_INC_V3
    model_mode = 0

    with tf.Graph().as_default():
        # Prepare graph
        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)

        with tf.variable_scope('model_a'):
            with slim.arg_scope(inception.inception_v3_arg_scope()):
                inception.inception_v3(x_input,
                                       num_classes=num_classes,
                                       is_training=False)
        with tf.variable_scope('model_b'):
            with slim.arg_scope(inception.inception_v3_arg_scope()):
                inception.inception_v3(x_input,
                                       num_classes=num_classes,
                                       is_training=False)
        with tf.variable_scope('model_c'):
            with slim.arg_scope(
                    inception_resnet_v2.inception_resnet_v2_arg_scope()):
                original_logits, _ = inception_resnet_v2.inception_resnet_v2(
                    x_input, num_classes=num_classes, is_training=False)

        x_adv = x_input

        def wrap_guess_target_class(logits, arr_kth_largest):
            arr_logits = np.array(logits)
            kth_largest = int(arr_kth_largest)
            return np.array(
                [arr.argsort()[-kth_largest] for arr in arr_logits],
                dtype=np.int32)

        target_class_input = tf.py_func(
            wrap_guess_target_class,
            [original_logits, np.array(5)], tf.int32)

        one_hot_target_class = tf.one_hot(target_class_input, num_classes)

        for i_iter in range(num_iter):
            model_mode = i_iter % 4
            if i_iter >= 16:
                model_mode = 3

            if i_iter == 0:
                label_smoothing = 0.1
                coeff_mul_alpha = 0.019
            elif i_iter == 10:
                label_smoothing = 0
                coeff_mul_alpha = 0.031

            if model_mode == 1:
                with tf.variable_scope('model_a'):
                    with slim.arg_scope(inception.inception_v3_arg_scope()):
                        logits, end_points = inception.inception_v3(
                            x_adv,
                            num_classes=num_classes,
                            is_training=False,
                            reuse=True)
            elif model_mode == 0:
                with tf.variable_scope('model_b'):
                    with slim.arg_scope(inception.inception_v3_arg_scope()):
                        logits, end_points = inception.inception_v3(
                            x_adv,
                            num_classes=num_classes,
                            is_training=False,
                            reuse=True)
            elif model_mode == 2:
                with tf.variable_scope('model_a'):
                    with slim.arg_scope(inception.inception_v3_arg_scope()):
                        logits, end_points = inception.inception_v3(
                            x_adv,
                            num_classes=num_classes,
                            is_training=False,
                            reuse=True)
            else:
                with tf.variable_scope('model_c'):
                    with slim.arg_scope(inception_resnet_v2.
                                        inception_resnet_v2_arg_scope()):
                        logits, end_points = inception_resnet_v2.inception_resnet_v2(
                            x_adv,
                            num_classes=num_classes,
                            is_training=False,
                            reuse=True)

            if model_mode == 3:
                aux_weight = AUX_ENS_V2
            else:
                aux_weight = AUX_INC_V3

            cross_entropy = tf.losses.softmax_cross_entropy(
                one_hot_target_class,
                logits,
                label_smoothing=label_smoothing,
                weights=1.0)
            cross_entropy += tf.losses.softmax_cross_entropy(
                one_hot_target_class,
                end_points['AuxLogits'],
                label_smoothing=label_smoothing,
                weights=aux_weight)
            compute_gradient = tf.gradients(cross_entropy, x_adv)[0]

            if model_mode == 2:
                with tf.variable_scope('model_b'):
                    with slim.arg_scope(inception.inception_v3_arg_scope()):
                        logits_2, end_points_2 = inception.inception_v3(
                            x_adv,
                            num_classes=num_classes,
                            is_training=False,
                            reuse=True)
                cross_entropy_2 = tf.losses.softmax_cross_entropy(
                    one_hot_target_class,
                    logits_2,
                    label_smoothing=label_smoothing,
                    weights=1.0)
                cross_entropy_2 += tf.losses.softmax_cross_entropy(
                    one_hot_target_class,
                    end_points_2['AuxLogits'],
                    label_smoothing=label_smoothing,
                    weights=aux_weight)
                compute_gradient_2 = tf.gradients(cross_entropy_2, x_adv)[0]

                equal_gradient_sign = tf.cast(
                    tf.equal(tf.sign(compute_gradient),
                             tf.sign(compute_gradient_2)), tf.float32)
                compute_gradient = tf.multiply(
                    tf.add(compute_gradient, compute_gradient_2),
                    equal_gradient_sign)

            gradient_clip = tf.clip_by_value(compute_gradient, -0.0001, 0.0001)
            multiplier = 1.0
            if model_mode == 3:
                if i_iter < 13:
                    multiplier = (1 - 0.0025 * i_iter) * 2.5 * alpha
                else:
                    multiplier = (1 - coeff_mul_alpha * i_iter) * 2.5 * alpha
            elif model_mode == 2:
                multiplier = (1 - coeff_mul_alpha * i_iter) * 3.6 * alpha
            else:
                multiplier = (1 - coeff_mul_alpha * i_iter) * 1.2 * alpha

            x_next = x_adv - multiplier * tf.add(
                0.5 * tf.sign(compute_gradient), 0.5 * gradient_clip * 10000)

            x_next = tf.clip_by_value(x_next, x_min, x_max)
            x_adv = x_next

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())

            all_vars = tf.global_variables()
            model_a_vars = [
                k for k in all_vars if k.name.startswith('model_a')
            ]  # FLAGS.model_a_scope
            model_b_vars = [
                k for k in all_vars if k.name.startswith('model_b')
            ]  # FLAGS.model_b_scope
            model_c_vars = [
                k for k in all_vars if k.name.startswith('model_c')
            ]  # FLAGS.model_c_scope

            model_a_checkpoint = 'inception_v3.ckpt'
            model_b_checkpoint = 'adv_inception_v3.ckpt'
            model_c_checkpoint = 'ens_adv_inception_resnet_v2.ckpt'

            tf.train.Saver(convert_dict(model_a_vars,
                                        model_a_checkpoint)).restore(
                                            sess, model_a_checkpoint)
            tf.train.Saver(convert_dict(model_b_vars,
                                        model_b_checkpoint)).restore(
                                            sess, model_b_checkpoint)
            tf.train.Saver(convert_dict(model_c_vars,
                                        model_c_checkpoint)).restore(
                                            sess, model_c_checkpoint)

            for filenames, images in load_images(FLAGS.input_dir, batch_shape):
                adv_images = sess.run(x_adv, feed_dict={x_input: images})
                save_images(adv_images, filenames, FLAGS.output_dir)
    def generator_loss(gan_model, *args, **kwargs):
        # Calculate the original loss from GAN
        wass_loss = tfgan.losses.wasserstein_generator_loss(
            gan_model, *args, **kwargs)

        # Synthesis input images
        adv_perbs = gan_model.generated_data * 128.0 + 128.0
        adv_perbs_mask = tf.cast((adv_perbs[:, :, :, 0] < FLAGS.adv_filter) |
                                 (adv_perbs[:, :, :, 1] < FLAGS.adv_filter) |
                                 (adv_perbs[:, :, :, 2] < FLAGS.adv_filter),
                                 tf.float32)
        adv_perbs_mask = tf.tile(tf.expand_dims(adv_perbs_mask, 3),
                                 [1, 1, 1, 3])
        adv_perbs_large = tf.pad(
            adv_perbs * adv_perbs_mask, [[0, 0]] +
            [[int(math.floor(image_pad)),
              int(math.ceil(image_pad))]] * 2 + [[0, 0]])

        adv_transform_rot = tf.Variable(np.zeros((FLAGS.batch_size, 3, 3)),
                                        dtype=tf.float32)
        adv_transform_rot = tf.scatter_nd_update(
            adv_transform_rot, [[x, 0, 0] for x in xrange(FLAGS.batch_size)],
            tf.math.cos(adv_theta))
        adv_transform_rot = tf.scatter_nd_update(
            adv_transform_rot, [[x, 0, 1] for x in xrange(FLAGS.batch_size)],
            -tf.math.sin(adv_theta))
        adv_transform_rot = tf.scatter_nd_update(
            adv_transform_rot, [[x, 0, 2] for x in xrange(FLAGS.batch_size)],
            (1 - tf.math.cos(adv_theta) + tf.math.sin(adv_theta)) * 299.0 / 2)
        adv_transform_rot = tf.scatter_nd_update(
            adv_transform_rot, [[x, 1, 0] for x in xrange(FLAGS.batch_size)],
            tf.math.sin(adv_theta))
        adv_transform_rot = tf.scatter_nd_update(
            adv_transform_rot, [[x, 1, 1] for x in xrange(FLAGS.batch_size)],
            tf.math.cos(adv_theta))
        adv_transform_rot = tf.scatter_nd_update(
            adv_transform_rot, [[x, 1, 2] for x in xrange(FLAGS.batch_size)],
            (1 - tf.math.cos(adv_theta) - tf.math.sin(adv_theta)) * 299.0 / 2)
        adv_transform_rot = tf.scatter_nd_update(
            adv_transform_rot, [[x, 2, 2] for x in xrange(FLAGS.batch_size)],
            tf.ones(shape=(FLAGS.batch_size, )))

        adv_transform_move = tf.Variable(np.zeros((FLAGS.batch_size, 3, 3)),
                                         dtype=tf.float32)
        adv_transform_move = tf.scatter_nd_update(
            adv_transform_move, [[x, 0, 0] for x in xrange(FLAGS.batch_size)],
            tf.ones(shape=(FLAGS.batch_size, )))
        adv_transform_move = tf.scatter_nd_update(
            adv_transform_move, [[x, 0, 2] for x in xrange(FLAGS.batch_size)],
            adv_dx)
        adv_transform_move = tf.scatter_nd_update(
            adv_transform_move, [[x, 1, 1] for x in xrange(FLAGS.batch_size)],
            tf.ones(shape=(FLAGS.batch_size, )))
        adv_transform_move = tf.scatter_nd_update(
            adv_transform_move, [[x, 1, 2] for x in xrange(FLAGS.batch_size)],
            adv_dy)
        adv_transform_move = tf.scatter_nd_update(
            adv_transform_move, [[x, 2, 2] for x in xrange(FLAGS.batch_size)],
            tf.ones(shape=(FLAGS.batch_size, )))

        adv_transform = tf.reshape(
            tf.matmul(adv_transform_rot, adv_transform_move), [-1, 9])[:, :8]
        adv_perbs_trans = tf.contrib.image.transform(adv_perbs_large,
                                                     adv_transform)
        adv_perbs_trans_mask = tf.cast((adv_perbs_trans[:, :, :, 0] > 0) &
                                       (adv_perbs_trans[:, :, :, 1] > 0) &
                                       (adv_perbs_trans[:, :, :, 2] > 0),
                                       tf.float32)
        adv_perbs_trans_mask = tf.tile(tf.expand_dims(adv_perbs_trans_mask, 3),
                                       [1, 1, 1, 3])

        adv_images = tf.tile(
            tf.expand_dims(tf.cast(image_orig, tf.float32), 0),
            [FLAGS.batch_size, 1, 1, 1])
        adv_images = adv_perbs_trans * adv_perbs_trans_mask + adv_images * (
            1 - adv_perbs_trans_mask)
        adv_images_to_save = tf.identity(tf.map_fn(tf.image.encode_png,
                                                   tf.cast(
                                                       adv_images, tf.uint8),
                                                   dtype=tf.string),
                                         name='adv_images_to_save')
        tf.summary.image('adv_images',
                         tf.cast(adv_images, tf.uint8),
                         max_outputs=FLAGS.batch_size)

        adv_inputs = adv_images / 255.0
        with tf.variable_scope('Inception'):
            with slim.arg_scope(inception.inception_v3_arg_scope()):
                adv_logits, _ = inception.inception_v3(adv_inputs,
                                                       num_classes=1001,
                                                       is_training=False)
        adv_logits_to_save = tf.identity(adv_logits, name='adv_logits_to_save')
        tf.summary.histogram('generator_adv_classes',
                             tf.argmax(adv_logits, axis=1))

        # Add the adversarial loss
        with tf.name_scope('generator_loss'):
            # Came from: https://github.com/carlini/nn_robust_attacks/blob/master/l2_attack.py
            adv_targets = tf.one_hot(
                tf.constant([FLAGS.adv_target] * FLAGS.batch_size,
                            dtype=tf.int32), 1001)
            adv_target_logit = tf.reduce_sum(adv_targets * adv_logits, 1)
            adv_others_logit = tf.reduce_max(
                (1 - adv_targets) * adv_logits - (adv_targets * 10000), 1)

            adv_loss = tf.reduce_mean(
                tf.maximum(
                    0.0, adv_others_logit - adv_target_logit +
                    FLAGS.adv_confidence))
            tf.summary.scalar('generator_adv_loss', adv_loss)

            total_loss = wass_loss + FLAGS.adv_lambda * adv_loss
            tf.summary.scalar('generator_total_loss', total_loss)

        return total_loss
示例#42
0
def main():
    params = {
        'batch_size': 16,
        'checkpoint_path':
        "test_models/inception_resnet_v2/ens/ens_adv_inception_resnet_v2.ckpt",  # "test_models/inception_v3/ens3/ens3_adv_inception_v3.ckpt",
        'images_benign_dir':
        '/home/yantao/datasets/imagenet_100image/original',
        'images_adv_dir': "images_adv",
        'model_name': 'inception_resnet_v2',
        'IMAGE_SIZE': 299,
        "NUM_CLASSES": 1001,
    }
    sess = tf.Session()
    images_benign, _ = load_images(dir_path=params['images_benign_dir'],
                                   size=[299, 299],
                                   zero_one_bound=True)
    images_adv, _ = load_images(dir_path=params['images_adv_dir'],
                                size=[299, 299],
                                zero_one_bound=True)

    input_ph = tf.placeholder(
        tf.float32,
        shape=[None, params['IMAGE_SIZE'], params['IMAGE_SIZE'], 3])
    if params['model_name'] == 'inception_v3':
        with slim.arg_scope(inception.inception_v3_arg_scope()):
            logits, _ = inception.inception_v3(
                input_ph,
                num_classes=params['NUM_CLASSES'],
                is_training=False,
                reuse=None)
    elif params['model_name'] == 'inception_resnet_v2':
        with slim.arg_scope(
                inception_resnet_v2.inception_resnet_v2_arg_scope()):
            logits, _ = inception_resnet_v2.inception_resnet_v2(
                input_ph,
                num_classes=params['NUM_CLASSES'],
                is_training=False,
                reuse=None)
    else:
        raise ValueError('Invalid model name: %s' % (params['model_name']))

    saver = tf.train.Saver()
    saver.restore(sess, params['checkpoint_path'])

    idx = 0
    pbar = tqdm(total=int(len(images_benign) / params['batch_size']) + 1)
    pd_labels_benign = []
    pd_labels_adv = []
    while (idx * params['batch_size'] <= len(images_benign)):
        if (idx + 1) * params['batch_size'] <= len(images_benign):
            temp_images_benign = images_benign[idx *
                                               params['batch_size']:(idx + 1) *
                                               params['batch_size']]
            temp_images_adv = images_adv[idx * params['batch_size']:(idx + 1) *
                                         params['batch_size']]
        else:
            temp_images_benign = images_benign[idx * params['batch_size']:]
            temp_images_adv = images_adv[idx * params['batch_size']:]

        feed_dict = {
            input_ph: temp_images_benign,
        }
        temp_outputs = sess.run(logits, feed_dict=feed_dict)
        temp_labels = temp_outputs.argmax(axis=1).tolist()
        pd_labels_benign += temp_labels

        feed_dict = {
            input_ph: temp_images_adv,
        }
        temp_outputs = sess.run(logits, feed_dict=feed_dict)
        temp_labels = temp_outputs.argmax(axis=1).tolist()
        pd_labels_adv += temp_labels
        pbar.update()
        idx += 1
    pbar.close()

    corrects = 0
    for l_benign, l_adv in zip(pd_labels_benign, pd_labels_adv):
        if l_adv == l_benign:
            corrects += 1
    acc = float(corrects) / len(pd_labels_benign)
    print('labels benign: ', pd_labels_benign)
    print('labels adv: ', pd_labels_adv)
    print('acc: ', acc)
train_size = imagenet_size()
num_batches = int(float(train_size) / BATCH_SIZE)

x = tf.placeholder(dtype=tf.float32, shape=[None, 299, 299, 3])
y = tf.placeholder(dtype=tf.float32, shape=[None, 1000])
lr = tf.placeholder(tf.float32)
# keep_prob = tf.placeholder(tf.float32)

with tf.device('/cpu:0'):
    q = tf.FIFOQueue(BATCH_SIZE, [tf.float32, tf.float32],
                     shapes=[[299, 299, 3], [1000]])
    enqueue_op = q.enqueue_many([x, y])
    x_b, y_b = q.dequeue_many(BATCH_SIZE / 4)

logits, end_points = inception.inception_v3(x_b, is_training=True)

with tf.name_scope('cross_entropy'):
    cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=y_b,
                                                            logits=logits)

with tf.name_scope('l2_loss'):
    l2_loss = tf.reduce_sum(
        5e-4 *
        tf.stack([tf.nn.l2_loss(v) for v in tf.get_collection('weights')]))
    tf.summary.scalar('l2_loss', l2_loss)

with tf.name_scope('loss'):
    loss = cross_entropy + l2_loss
    tf.summary.scalar('loss', loss)
 def calculate_logits(self, inputs):
     with slim.arg_scope(inception.inception_v3_arg_scope()):
         logits, _ = inception.inception_v3(inputs, num_classes=self.n_classes, is_training=False)
     return logits
示例#45
0
def _iterative_fast_gradient_attack(ensemble,
                                    eps,
                                    alpha,
                                    num_iter,
                                    tic=time.time()):
    """ Constructs the tensorflow graph that implements this attack. 
      Note this does not execute the attack; that is for the caller to do.
  """
    # hardcode since we are in a hurry...
    batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3]
    num_classes = 1001

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

    target_class_input = tf.placeholder(tf.int32, shape=[FLAGS.batch_size])
    one_hot_target_class = tf.one_hot(
        target_class_input,
        num_classes)  # note: labels are smoothed in loss function

    # initially x_adv is the same as the input
    x_adv = x_input

    # initialize networks
    if 'InceptionV3' in ensemble:
        with slim.arg_scope(inception.inception_v3_arg_scope()):
            inception.inception_v3(x_input,
                                   num_classes=num_classes,
                                   is_training=False,
                                   scope='InceptionV3')

    if 'resnet_v2_101' in ensemble:
        with slim.arg_scope(resnet_v2.resnet_arg_scope()):
            resnet_v2.resnet_v2_101(x_input,
                                    num_classes=num_classes,
                                    is_training=False,
                                    scope='resnet_v2_101')

    if 'InceptionResnetV2' in ensemble:
        with slim.arg_scope(ir.inception_resnet_v2_arg_scope()):
            ir.inception_resnet_v2(x_input,
                                   num_classes=num_classes,
                                   is_training=False,
                                   scope='InceptionResnetV2')

    if 'Adv-InceptionV3' in ensemble:
        with slim.arg_scope(inception.inception_v3_arg_scope()):
            inception.inception_v3(x_input,
                                   num_classes=num_classes,
                                   is_training=False,
                                   scope='Adv-InceptionV3')

    print('[INFO]: initial setup; net runtime: %0.2f seconds\n' %
          (time.time() - tic))
    print('[INFO]: ensemble contents:', ensemble)

    for ts in range(num_iter):
        # TODO: this code is gross; clean up if time permits...

        #--------------------------------------------------
        # contribution from InceptionV3
        #--------------------------------------------------
        if 'InceptionV3' in ensemble:
            with slim.arg_scope(inception.inception_v3_arg_scope()):
                logits, end_points = inception.inception_v3(
                    x_adv,
                    num_classes=num_classes,
                    is_training=False,
                    reuse=True,
                    scope='InceptionV3')

            cross_entropy = tf.losses.softmax_cross_entropy(
                one_hot_target_class, logits, label_smoothing=0.1, weights=1.0)
            cross_entropy += tf.losses.softmax_cross_entropy(
                one_hot_target_class,
                end_points['AuxLogits'],
                label_smoothing=0.1,
                weights=0.4)
        else:
            cross_entropy = 0

        #--------------------------------------------------
        # contribution from resnet_v2
        #--------------------------------------------------
        if 'resnet_v2_101' in ensemble:
            with slim.arg_scope(resnet_v2.resnet_arg_scope()):
                logits_2, _ = resnet_v2.resnet_v2_101(x_adv,
                                                      num_classes=num_classes,
                                                      is_training=False,
                                                      reuse=True,
                                                      scope='resnet_v2_101')

                # Note: ResnetV2 logits has shape (BATCH_SIZE, 1, 1, N_CLASSES)
                #       Hence the squeeze below.
                cross_entropy_2 = tf.losses.softmax_cross_entropy(
                    one_hot_target_class,
                    tf.squeeze(logits_2),
                    label_smoothing=0.1,
                    weights=1.0)
        else:
            cross_entropy_2 = 0

        #--------------------------------------------------
        # contribution from ensemble {resnet_v2, inception_v3}
        #--------------------------------------------------
        if 'InceptionResnetV2' in ensemble:
            with slim.arg_scope(ir.inception_resnet_v2_arg_scope()):
                logits_3, _ = ir.inception_resnet_v2(x_adv,
                                                     num_classes=num_classes,
                                                     is_training=False,
                                                     reuse=True,
                                                     scope='InceptionResnetV2')

            cross_entropy_3 = tf.losses.softmax_cross_entropy(
                one_hot_target_class,
                logits_3,
                label_smoothing=0.1,
                weights=1.0)
        else:
            cross_entropy_3 = 0

        #--------------------------------------------------
        # contribution from InceptionV3-adv
        #--------------------------------------------------
        if 'Adv-InceptionV3' in ensemble:
            with slim.arg_scope(inception.inception_v3_arg_scope()):
                logits_4, end_points_4 = inception.inception_v3(
                    x_adv,
                    num_classes=num_classes,
                    is_training=False,
                    reuse=True,
                    scope='Adv-InceptionV3')

            cross_entropy_4 = tf.losses.softmax_cross_entropy(
                one_hot_target_class,
                logits_4,
                label_smoothing=0.1,
                weights=1.0)
            cross_entropy_4 += tf.losses.softmax_cross_entropy(
                one_hot_target_class,
                end_points_4['AuxLogits'],
                label_smoothing=0.1,
                weights=0.4)
        else:
            cross_entropy_4 = 0

        print(
            '[INFO]: added %d models for timestep %d/%d; net runtime: %0.2f seconds'
            % (len(ensemble), ts + 1, num_iter, time.time() - tic))

        #--------------------------------------------------
        # gradient step
        #--------------------------------------------------
        cross_entropy_avg = (cross_entropy + cross_entropy_2 +
                             cross_entropy_3 + cross_entropy_4) / len(ensemble)

        nabla_x = tf.gradients(cross_entropy_avg, x_adv)[0]

        # EXPERIMENT: add some random noise to gradient (avoid "overfitting"?)
        #if False:
        #  print('[WARNING]: using experimental noisy gradient!')
        #  nabla_x = nabla_x + tf.random_normal(tf.shape(nabla_x), mean=0, stddev=1e-2)

        # EXPERIMENT: avoid moving in directions corresponding to small values of gradient
        #if False:
        #  print('[WARNING]: using experimental gradient clipping!')
        #  # 1e-2 too large of a threshold
        #  nabla_x = tf.where(tf.abs(nabla_x) < 1e-3, tf.zeros(tf.shape(nabla_x)), nabla_x)

        x_next = x_adv - alpha * tf.sign(nabla_x)

        # Always clip at the end
        x_next = tf.clip_by_value(x_next, x_min, x_max)
        x_adv = x_next

    #--------------------------------------------------
    # set up model weight loading
    #--------------------------------------------------
    savers = []
    if 'InceptionV3' in ensemble:
        savers.append(
            (tf.train.Saver(slim.get_model_variables(scope='InceptionV3')),
             './Weights/inception_v3.ckpt'))
    if 'resnet_v2_101' in ensemble:
        savers.append(
            (tf.train.Saver(slim.get_model_variables(scope='resnet_v2_101')),
             './Weights/resnet_v2_101.ckpt'))
    if 'InceptionResnetV2' in ensemble:
        savers.append((tf.train.Saver(
            slim.get_model_variables(scope='InceptionResnetV2')),
                       './Weights/ens_adv_inception_resnet_v2.ckpt'))
    if 'Adv-InceptionV3' in ensemble:
        savers.append(
            (tf.train.Saver(slim.get_model_variables(scope='Adv-InceptionV3')),
             './Weights/InceptionV3-adv.ckpt'))

    print('[INFO]: FG(S)M setup complete; took %0.2f seconds\n' %
          (time.time() - tic))

    return x_adv, [x_input, target_class_input], savers
示例#46
0
	image = imresize(image, (target_width, target_height))

	return image.astype(np.float32) / 255


from tensorflow.contrib.slim.nets import inception
import tensorflow.contrib.slim as slim


tf.reset_default_graph()

X = tf.placeholder(tf.float32, shape = [None, height, width, channels], name = 'X')
training = tf.placeholder_with_default(False, shape = [])
with slim.arg_scope(inception.inception_v3_arg_scope()):
	logits, end_points = inception.inception_v3(X, num_classes = 1001, is_training = training)

saver = tf.train.Saver()

prelogits = tf.squeeze(end_points['PreLogits'], axis = [1,2])
n_outputs = len(bear_classes)

with tf.name_scope('new_output'):
	bear_logits = tf.layers.dense(prelogits, n_outputs, name = 'bear_logits')
	y_proba = tf.nn.softmax(bear_logits, name = 'Y_proba')

y = tf.placeholder(tf.int32, shape = [None])

training_rate = .01

with tf.name_scope('train'):
def _inception_v3(x):
    with tf.name_scope("inception_v3"):
        x_ = tf.image.resize_images(x, (299, 299))
        y, _ = inception.inception_v3(x_, num_classes=10, is_training=True)
        return y