当前位置: 首页>>代码示例>>Python>>正文


Python attacks.BasicIterativeMethod方法代码示例

本文整理汇总了Python中cleverhans.attacks.BasicIterativeMethod方法的典型用法代码示例。如果您正苦于以下问题:Python attacks.BasicIterativeMethod方法的具体用法?Python attacks.BasicIterativeMethod怎么用?Python attacks.BasicIterativeMethod使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在cleverhans.attacks的用法示例。


在下文中一共展示了attacks.BasicIterativeMethod方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: create_adv_examples

# 需要导入模块: from cleverhans import attacks [as 别名]
# 或者: from cleverhans.attacks import BasicIterativeMethod [as 别名]
def create_adv_examples(model, input_t, x_to_adv, attack_dict):
    """
    This fn may seem bizarre and pointless, but the point of it is to
    enable the entire attack to be specified as a dict from the command line without
    editing this script, which is convenient for storing the settings used for an attack
    """
    if attack_dict['method'] == 'fgm':
        attack = attacks.FastGradientMethod(model, sess=K.get_session(), back='tf')
    elif attack_dict['method'] == 'bim':
        attack = attacks.BasicIterativeMethod(model, sess=K.get_session(), back='tf')
    elif attack_dict['method'] == 'mim':
        attack = attacks.MomentumIterativeMethod(model, sess=K.get_session(), back='tf')
    else:
        assert False, 'Current attack needs to be added to the create attack fn'
    adv_tensor = attack.generate(input_t, **{k: a for k, a in attack_dict.items() if
                                             k != 'method'})  # 'method' key for this fn use
    x_adv = batch_eval(adv_tensor, input_t, x_to_adv, batch_size=args.batch_size, verbose="Generating adv examples")
    return x_adv 
开发者ID:lsgos,项目名称:uncertainty-adversarial-paper,代码行数:20,代码来源:ROC_curves_cats.py

示例2: setUp

# 需要导入模块: from cleverhans import attacks [as 别名]
# 或者: from cleverhans.attacks import BasicIterativeMethod [as 别名]
def setUp(self):
        super(TestBasicIterativeMethod, self).setUp()

        self.sess = tf.Session()
        self.model = SimpleModel()
        self.attack = BasicIterativeMethod(self.model, sess=self.sess) 
开发者ID:StephanZheng,项目名称:neural-fingerprinting,代码行数:8,代码来源:test_attacks.py

示例3: __init__

# 需要导入模块: from cleverhans import attacks [as 别名]
# 或者: from cleverhans.attacks import BasicIterativeMethod [as 别名]
def __init__(self, model, dtypestr='float32'):
        """
        Creates a BasicIterativeMethod instance in eager execution.
        :model: CNN network, should be an instance of
                cleverhans.model.Model, if not wrap
                the output to probs.
        :dtypestr: datatype in the string format.
        """
        if not isinstance(model, Model):
            model = CallableModelWrapper(model, 'probs')

        super(BasicIterativeMethod, self).__init__(model, dtypestr) 
开发者ID:StephanZheng,项目名称:neural-fingerprinting,代码行数:14,代码来源:attacks_tfe.py

示例4: generate_bim_examples

# 需要导入模块: from cleverhans import attacks [as 别名]
# 或者: from cleverhans.attacks import BasicIterativeMethod [as 别名]
def generate_bim_examples(sess, model, x, y, X, Y, attack_params, verbose, attack_log_fpath):
    """
    Untargeted attack. Y is not needed.
    """
    bim = BasicIterativeMethod(model, back='tf', sess=sess)
    bim_params = {'eps': 0.1, 'eps_iter':0.05, 'nb_iter':10, 'y':y,
                     'ord':np.inf, 'clip_min':0, 'clip_max':1 }
    bim_params = override_params(bim_params, attack_params)

    X_adv = bim.generate_np(X, **bim_params)
    return X_adv 
开发者ID:mzweilin,项目名称:EvadeML-Zoo,代码行数:13,代码来源:cleverhans_wrapper.py

示例5: test_attack_can_be_constructed

# 需要导入模块: from cleverhans import attacks [as 别名]
# 或者: from cleverhans.attacks import BasicIterativeMethod [as 别名]
def test_attack_can_be_constructed(self):
    # The test passes if this raises no exceptions
    self.attack = BasicIterativeMethod(self.model, sess=self.sess) 
开发者ID:tensorflow,项目名称:cleverhans,代码行数:5,代码来源:test_attacks.py

示例6: __init__

# 需要导入模块: from cleverhans import attacks [as 别名]
# 或者: from cleverhans.attacks import BasicIterativeMethod [as 别名]
def __init__(self, model, dtypestr='float32'):
    """
    Creates a BasicIterativeMethod instance in eager execution.
    :param model: cleverhans.model.Model
    :param dtypestr: datatype in the string format.
    """
    if not isinstance(model, Model):
      wrapper_warning()
      model = CallableModelWrapper(model, 'probs')

    super(BasicIterativeMethod, self).__init__(model, dtypestr) 
开发者ID:tensorflow,项目名称:cleverhans,代码行数:13,代码来源:attacks_tfe.py

示例7: main

# 需要导入模块: from cleverhans import attacks [as 别名]
# 或者: from cleverhans.attacks import BasicIterativeMethod [as 别名]
def main(argv):
    checkpoint = tf.train.latest_checkpoint(FLAGS.checkpoint_dir)

    if checkpoint is None:
        raise ValueError("Couldn't find latest checkpoint in " +
                         FLAGS.checkpoint_dir)

    train_start = 0
    train_end = 60000
    test_start = 0
    test_end = 10000
    X_train, Y_train, X_test, Y_test = data_mnist(train_start=train_start,
                                                  train_end=train_end,
                                                  test_start=test_start,
                                                  test_end=test_end)

    assert Y_train.shape[1] == 10

    # NOTE: for compatibility with Madry Lab downloadable checkpoints,
    # we cannot enclose this in a scope or do anything else that would
    # change the automatic naming of the variables.
    model = MadryMNIST()

    x_input = tf.placeholder(tf.float32, shape=[None, 784])
    x_image = tf.placeholder(tf.float32, shape=[None, 28, 28, 1])
    y = tf.placeholder(tf.float32, shape=[None, 10])

    if FLAGS.attack_type == 'fgsm':
        fgsm = FastGradientMethod(model)
        fgsm_params = {'eps': 0.3, 'clip_min': 0., 'clip_max': 1.}
        adv_x = fgsm.generate(x_image, **fgsm_params)
    elif FLAGS.attack_type == 'bim':
        bim = BasicIterativeMethod(model)
        bim_params = {'eps': 0.3, 'clip_min': 0., 'clip_max': 1.,
                      'nb_iter': 50,
                      'eps_iter': .01}
        adv_x = bim.generate(x_image, **bim_params)
    else:
        raise ValueError(FLAGS.attack_type)
    preds_adv = model.get_probs(adv_x)

    saver = tf.train.Saver()

    with tf.Session() as sess:
        # Restore the checkpoint
        saver.restore(sess, checkpoint)

        # Evaluate the accuracy of the MNIST model on adversarial examples
        eval_par = {'batch_size': FLAGS.batch_size}
        t1 = time.time()
        acc = model_eval(
            sess, x_image, y, preds_adv, X_test, Y_test, args=eval_par)
        t2 = time.time()
        print("Took", t2 - t1, "seconds")
        print('Test accuracy on adversarial examples: %0.4f\n' % acc) 
开发者ID:StephanZheng,项目名称:neural-fingerprinting,代码行数:57,代码来源:attack_model.py

示例8: main

# 需要导入模块: from cleverhans import attacks [as 别名]
# 或者: from cleverhans.attacks import BasicIterativeMethod [as 别名]
def main(argv):
  checkpoint = tf.train.latest_checkpoint(FLAGS.checkpoint_dir)

  if checkpoint is None:
    raise ValueError("Couldn't find latest checkpoint in " +
                     FLAGS.checkpoint_dir)

  train_start = 0
  train_end = 60000
  test_start = 0
  test_end = 10000
  X_train, Y_train, X_test, Y_test = data_mnist(train_start=train_start,
                                                train_end=train_end,
                                                test_start=test_start,
                                                test_end=test_end)

  assert Y_train.shape[1] == 10

  # NOTE: for compatibility with Madry Lab downloadable checkpoints,
  # we cannot enclose this in a scope or do anything else that would
  # change the automatic naming of the variables.
  model = MadryMNIST()

  x_input = tf.placeholder(tf.float32, shape=[None, 784])
  x_image = tf.placeholder(tf.float32, shape=[None, 28, 28, 1])
  y = tf.placeholder(tf.float32, shape=[None, 10])

  if FLAGS.attack_type == 'fgsm':
    fgsm = FastGradientMethod(model)
    fgsm_params = {'eps': 0.3, 'clip_min': 0., 'clip_max': 1.}
    adv_x = fgsm.generate(x_image, **fgsm_params)
  elif FLAGS.attack_type == 'bim':
    bim = BasicIterativeMethod(model)
    bim_params = {'eps': 0.3, 'clip_min': 0., 'clip_max': 1.,
                  'nb_iter': 50,
                  'eps_iter': .01}
    adv_x = bim.generate(x_image, **bim_params)
  else:
    raise ValueError(FLAGS.attack_type)
  preds_adv = model.get_probs(adv_x)

  saver = tf.train.Saver()

  with tf.Session() as sess:
    # Restore the checkpoint
    saver.restore(sess, checkpoint)

    # Evaluate the accuracy of the MNIST model on adversarial examples
    eval_par = {'batch_size': FLAGS.batch_size}
    t1 = time.time()
    acc = model_eval(
        sess, x_image, y, preds_adv, X_test, Y_test, args=eval_par)
    t2 = time.time()
    print("Took", t2 - t1, "seconds")
    print('Test accuracy on adversarial examples: %0.4f\n' % acc) 
开发者ID:tensorflow,项目名称:cleverhans,代码行数:57,代码来源:attack_model.py


注:本文中的cleverhans.attacks.BasicIterativeMethod方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。