How to use cleverhans - 10 common examples

To help you get started, we’ve selected a few cleverhans examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github tensorflow / cleverhans / tests_tf / test_attacks.py View on Github external
def setUp(self):
    super(TestFastGradientMethod, self).setUp()

    self.attack = FastGradientMethod(self.model, sess=self.sess)
github tensorflow / cleverhans / tests_tf / test_attacks.py View on Github external
Model.__init__(self, scope, nb_classes, locals())

  def fprop(self, x, **kwargs):
    del kwargs
    with tf.variable_scope(self.scope, reuse=tf.AUTO_REUSE):
      w1 = tf.constant([[1.5, .3], [-2, 0.3]],
                       dtype=tf.as_dtype(x.dtype))
      w2 = tf.constant([[-2.4, 1.2], [0.5, -2.3]],
                       dtype=tf.as_dtype(x.dtype))
    h1 = tf.nn.sigmoid(tf.matmul(x, w1))
    res = tf.matmul(h1, w2)
    return {self.O_LOGITS: res,
            self.O_PROBS: tf.nn.softmax(res)}


class TrivialModel(Model):
  """
  A linear model with two weights
  """

  def __init__(self, scope='trivial', nb_classes=2, **kwargs):
    del kwargs
    Model.__init__(self, scope, nb_classes, locals())

  def fprop(self, x, **kwargs):
    del kwargs
    with tf.variable_scope(self.scope, reuse=tf.AUTO_REUSE):
      w1 = tf.constant([[1, -1]], dtype=tf.float32)
    res = tf.matmul(x, w1)
    return {self.O_LOGITS: res,
            self.O_PROBS: tf.nn.softmax(res)}
github tensorflow / cleverhans / tests_tf / test_attacks.py View on Github external
"""

  def __init__(self, scope='trivial', nb_classes=2, **kwargs):
    del kwargs
    Model.__init__(self, scope, nb_classes, locals())

  def fprop(self, x, **kwargs):
    del kwargs
    with tf.variable_scope(self.scope, reuse=tf.AUTO_REUSE):
      w1 = tf.constant([[1, -1]], dtype=tf.float32)
    res = tf.matmul(x, w1)
    return {self.O_LOGITS: res,
            self.O_PROBS: tf.nn.softmax(res)}


class DummyModel(Model):
  """
  A simple model based on slim
  """

  def __init__(self, scope='dummy_model', nb_classes=10, **kwargs):
    del kwargs
    Model.__init__(self, scope, nb_classes, locals())

  def fprop(self, x, **kwargs):
    del kwargs
    with tf.variable_scope(self.scope, reuse=tf.AUTO_REUSE):
      net = slim.flatten(x)
      net = slim.fully_connected(net, 60)
      logits = slim.fully_connected(net, 10, activation_fn=None)
      return {self.O_LOGITS: logits,
              self.O_PROBS: tf.nn.softmax(logits)}
github tensorflow / cleverhans / tests_tf / test_model.py View on Github external
def test_get_logits(self):
    # Define empty model
    model = Model('model', 10, {})
    x = []

    # Exception is thrown when `get_logits` not implemented
    with self.assertRaises(Exception) as context:
      model.get_logits(x)
    self.assertTrue(context.exception)
github tensorflow / cleverhans / tests_tf / test_evaluation.py View on Github external
def test_cache(self):
    """test_cache: Test that _CorrectFactory can be cached"""
    model = Model()
    factory_1 = _CorrectFactory(model)
    factory_2 = _CorrectFactory(model)
    cache = {}
    cache[factory_1] = True
    self.assertTrue(factory_2 in cache)
github tensorflow / cleverhans / tests_tf / test_basic_iterative.py View on Github external
# Define TF model graph
        self.model = model_mnist()
        self.predictions = self.model(self.x)
        print("Defined TensorFlow model graph.")

        def evaluate():
            # Evaluate the accuracy of the MNIST model on legitimate test examples
            accuracy = model_eval(self.sess, self.x, self.y, self.predictions, self.X_test, self.Y_test)
            print('Test accuracy on legitimate test examples: ' + str(accuracy))

        # Define TF model graph
        self.model = model_mnist()
        self.predictions = self.model(self.x)
        # Train the MNIST model
        model_train(self.sess, self.x, self.y, self.predictions, X_train, Y_train, evaluate=evaluate)
github deepgenerativeclassifier / DeepBayes / test_attacks / attack.py View on Github external
def test_attacks(data_name, model_name, attack_method, eps, batch_size=100, 
                 targeted=False, save=False):

    # Set TF random seed to improve reproducibility
    tf.set_random_seed(1234)

    # Create TF session
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)
    print("Created TensorFlow session.")
    set_log_level(logging.DEBUG)

    if data_name == 'mnist':
        from cleverhans.utils_mnist import data_mnist
        X_train, Y_train, X_test, Y_test = data_mnist(train_start=0, train_end=60000,
                                                      test_start=0, test_end=10000)
    if data_name in ['cifar10', 'plane_frog']:
        from import_data_cifar10 import load_data_cifar10
        if data_name == 'plane_frog':
            labels = [0, 6]
        else:
            labels = None
        data_path = '../cifar_data/'
        X_train, X_test, Y_train, Y_test = load_data_cifar10(data_path, labels=labels, conv=True)
    
    source_samples, img_rows, img_cols, channels = X_test.shape
    nb_classes = Y_test.shape[1]
github tensorflow / cleverhans / tests_tf / test_utils_tf.py View on Github external
def test_clip_by_value_numpy_dtype(self):
    # Test that it's possible to use clip_by_value while mixing numpy and tf
    clip_min = np.zeros((1,))
    clip_max = tf.ones((1,))
    x = tf.ones((1,))
    # The point of this test is just to make sure the casting logic doesn't raise an exception
    utils_tf.clip_by_value(x, clip_min, clip_max)
github tensorflow / cleverhans / tests_tf / test_dataset.py View on Github external
"""Tests for cleverhans.dataset"""
from cleverhans.dataset import Dataset
from cleverhans.devtools.checks import CleverHansTest


class LightweightDataset(Dataset):
  """
  A dataset that does not actually load any data so it is cheap to run
  in tests.
  """


class TestDataset(CleverHansTest):
  """
  Tests for the Dataset class
  """

  def test_factory(self):
    """test_factory: Test that dataset->factory->dataset preserves type"""
    d1 = LightweightDataset()
    factory = d1.get_factory()
    d2 = factory()
    self.assertTrue(type(d1) is type(d2))
github tensorflow / cleverhans / tests_tf / test_attacks.py View on Github external
def test_generate_np_targeted_gives_adversarial_example(self):
    x_val = np.random.rand(10, 1000)
    x_val = np.array(x_val, dtype=np.float32)

    feed_labs = np.zeros((10, 10))
    feed_labs[np.arange(10), np.random.randint(0, 9, 10)] = 1
    x_adv = self.attack.generate_np(x_val,
                                    clip_min=-5., clip_max=5.,
                                    y_target=feed_labs)
    new_labs = np.argmax(self.sess.run(self.model.get_logits(x_adv)), axis=1)

    worked = np.mean(np.argmax(feed_labs, axis=1) == new_labs)
    self.assertTrue(worked > .9)


class TestDeepFool(CleverHansTest):
  def setUp(self):
    super(TestDeepFool, self).setUp()

    self.sess = tf.Session()
    self.model = SimpleModel()
    self.attack = DeepFool(self.model, sess=self.sess)

  def test_generate_np_gives_adversarial_example(self):
    x_val = np.random.rand(100, 2)
    x_val = np.array(x_val, dtype=np.float32)

    x_adv = self.attack.generate_np(x_val, overshoot=0.02, max_iter=50,
                                    nb_candidate=2, clip_min=-5,
                                    clip_max=5)

    orig_labs = np.argmax(self.sess.run(self.model.get_logits(x_val)), axis=1)