コード例 #1
0
    def test_jsma_batch_with_feed(self):
        with tf.Session() as sess:
            X = np.random.rand(1, 13)

            # construct a simple graph that will require extra placeholders
            x = tf.placeholder('float', shape=(None, 13))
            keep_prob = tf.placeholder('float')
            W = tf.Variable(tf.random_normal([13, 10]))
            b = tf.Variable(tf.random_normal([10]))
            logits = tf.nn.dropout(tf.add(tf.matmul(x, W), b),
                                   keep_prob=keep_prob)
            sess.run(tf.global_variables_initializer())

            # jsma should work without generating an error
            jacobian = attacks_tf.jacobian_graph(logits, x, 10)
            attacks_tf.jsma_batch(sess,
                                  x,
                                  logits,
                                  jacobian,
                                  X,
                                  theta=1.,
                                  gamma=0.25,
                                  clip_min=0,
                                  clip_max=1,
                                  nb_classes=10,
                                  feed={keep_prob: 1.0})
コード例 #2
0
 def jsma_wrap(x_val):
     jsma_batch(sess,
                x,
                preds,
                grads,
                x_val,
                1,
                0.1,
                -1,
                1,
                num_classes,
                y_target=None)
コード例 #3
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
    batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3]
    num_classes = 1001
    BUILD_MODEL = True

    from cleverhans.attacks_tf import jacobian_graph, jsma_batch

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

    with tf.Graph().as_default() as d_graph:
        # Prepare graph

        model = InceptionModel(num_classes)

        if BUILD_MODEL:
            print("Build the model and try to save the current graph")
            x_input = tf.placeholder(tf.float32, shape=batch_shape)
            preds = model(x_input)
            grads = jacobian_graph(preds, x_input, num_classes)
            saver = tf.train.Saver(slim.get_model_variables())
            tf.add_to_collection("x_input", x_input)
            tf.add_to_collection("preds", preds)
            tf.add_to_collection("grads", grads)
        else:
            saver = tf.train.Saver(
                filename='model/saliency_map_model-1000.meta')

        # Run computation

        with tf.Session() as sess:
            #print("Session is closed:",sess._is_closed())

            if BUILD_MODEL:
                saver.save(sess, 'saliency_map_model', global_step=1000)
            else:
                saver.restore(sess, "model/saliency_map_model-1000")
                x_input = tf.get_collection('x_input')[0]
                preds = tf.get_collection('preds')[0]
                grads = tf.get_collection('grads')[0]

            for filenames, images in load_images(FLAGS.input_dir, batch_shape):

                adv_images = jsma_batch(sess,
                                        x_input,
                                        preds,
                                        grads,
                                        images,
                                        1,
                                        0.1,
                                        -1,
                                        1,
                                        num_classes,
                                        y_target=None)

                save_images(adv_images, filenames, FLAGS.output_dir)
コード例 #4
0
 def jsma_wrap(x_val, y_target):
   return jsma_batch(
       self.sess,
       x,
       preds,
       grads,
       x_val,
       self.theta,
       self.gamma,
       self.clip_min,
       self.clip_max,
       nb_classes,
       y_target=y_target)
コード例 #5
0
 def generate_jsma_examples(self, sess, X, y, targets=None,
     clip_min=None, clip_max=None, theta=1., gamma=0.25):
   """Wrapper around Cleverhans' underlying JSMA generation code"""
   from cleverhans import attacks_tf
   if clip_min is None: clip_min = np.min(X)
   if clip_max is None: clip_max = np.max(X)
   if targets is None:
     targets = onehot((np.argmax(y, 1) + 1) % self.num_classes, self.num_classes)
   elif isint(targets):
     targets = onehot([targets]*len(X), self.num_classes)
   jacobian = attacks_tf.jacobian_graph(self.logits, self.X, self.num_classes)
   return attacks_tf.jsma_batch(sess, self.X, self.logits, jacobian, X,
     theta=theta, gamma=gamma, clip_min=clip_min, clip_max=clip_max,
     nb_classes=self.num_classes, y_target=targets)
コード例 #6
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
    batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3]
    num_classes = 1001

    from cleverhans.attacks_tf import jacobian_graph, jsma_batch

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

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

        model = InceptionModel(num_classes)
        print("Variable type for model:", type(model))

        preds = model(x_input)

        # 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:
            print("Session is closed:", sess._is_closed())

            def jsma_wrap(x_val):
                jsma_batch(sess,
                           x,
                           preds,
                           grads,
                           x_val,
                           1,
                           0.1,
                           -1,
                           1,
                           num_classes,
                           y_target=None)

            x_adv = tf.py_func(jsma_wrap, [x_input], tf.float32)
            grads = jacobian_graph(preds, x_input, num_classes)

            for filenames, images in load_images(FLAGS.input_dir, batch_shape):

                adv_images = jsma_batch(sess,
                                        x_input,
                                        preds,
                                        grads,
                                        images,
                                        1,
                                        0.1,
                                        -1,
                                        1,
                                        num_classes,
                                        y_target=None)

                save_images(adv_images, filenames, FLAGS.output_dir)