本文整理汇总了Python中cleverhans.model.CallableModelWrapper方法的典型用法代码示例。如果您正苦于以下问题:Python model.CallableModelWrapper方法的具体用法?Python model.CallableModelWrapper怎么用?Python model.CallableModelWrapper使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cleverhans.model
的用法示例。
在下文中一共展示了model.CallableModelWrapper方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from cleverhans import model [as 别名]
# 或者: from cleverhans.model import CallableModelWrapper [as 别名]
def __init__(self, model, back='tf', sess=None, dtypestr='float32'):
"""
Create a FastGradientMethod instance.
Note: the model parameter should be an instance of the
cleverhans.model.Model abstraction provided by CleverHans.
"""
if not isinstance(model, Model):
model = CallableModelWrapper(model, 'probs')
super(FastGradientMethod, self).__init__(model, back, sess, dtypestr)
self.feedable_kwargs = {
'eps': self.np_dtype,
'y': self.np_dtype,
'y_target': self.np_dtype,
'clip_min': self.np_dtype,
'clip_max': self.np_dtype
}
self.structural_kwargs = ['ord']
示例2: __init__
# 需要导入模块: from cleverhans import model [as 别名]
# 或者: from cleverhans.model import CallableModelWrapper [as 别名]
def __init__(self, model, back='tf', sess=None, dtypestr='float32'):
"""
Create a BasicIterativeMethod instance.
Note: the model parameter should be an instance of the
cleverhans.model.Model abstraction provided by CleverHans.
"""
if not isinstance(model, Model):
model = CallableModelWrapper(model, 'probs')
super(BasicIterativeMethod, self).__init__(model, back, sess, dtypestr)
self.feedable_kwargs = {
'eps': self.np_dtype,
'eps_iter': self.np_dtype,
'y': self.np_dtype,
'y_target': self.np_dtype,
'clip_min': self.np_dtype,
'clip_max': self.np_dtype
}
self.structural_kwargs = ['ord', 'nb_iter']
示例3: __init__
# 需要导入模块: from cleverhans import model [as 别名]
# 或者: from cleverhans.model import CallableModelWrapper [as 别名]
def __init__(self, model, back='tf', sess=None, dtypestr='float32'):
"""
Create a SpatialTransformationMethod instance.
Note: the model parameter should be an instance of the
cleverhans.model.Model abstraction provided by CleverHans.
"""
if not isinstance(model, Model):
model = CallableModelWrapper(model, 'probs')
super(SpatialTransformationMethod, self).__init__(
model, back, sess, dtypestr)
self.feedable_kwargs = {
'n_samples': self.np_dtype,
'dx_min': self.np_dtype,
'dx_max': self.np_dtype,
'n_dxs': self.np_dtype,
'dy_min': self.np_dtype,
'dy_max': self.np_dtype,
'n_dys': self.np_dtype,
'angle_min': self.np_dtype,
'angle_max': self.np_dtype,
'n_angles': self.np_dtype,
'black_border_size': self.np_dtype,
}
示例4: __init__
# 需要导入模块: from cleverhans import model [as 别名]
# 或者: from cleverhans.model import CallableModelWrapper [as 别名]
def __init__(self, model, sess, dtypestr='float32', **kwargs):
"""
Note: the model parameter should be an instance of the
cleverhans.model.Model abstraction provided by CleverHans.
"""
if not isinstance(model, Model):
wrapper_warning_logits()
model = CallableModelWrapper(model, 'logits')
super(CarliniWagnerL2, self).__init__(model, sess, dtypestr, **kwargs)
self.feedable_kwargs = ('y', 'y_target')
self.structural_kwargs = [
'batch_size', 'confidence', 'targeted', 'learning_rate',
'binary_search_steps', 'max_iterations', 'abort_early',
'initial_const', 'clip_min', 'clip_max'
]
示例5: __init__
# 需要导入模块: from cleverhans import model [as 别名]
# 或者: from cleverhans.model import CallableModelWrapper [as 别名]
def __init__(self, model, sess, dtypestr='float32', **kwargs):
"""
Note: the model parameter should be an instance of the
cleverhans.model.Model abstraction provided by CleverHans.
"""
if not isinstance(model, Model):
wrapper_warning_logits()
model = CallableModelWrapper(model, 'logits')
super(ElasticNetMethod, self).__init__(model, sess, dtypestr, **kwargs)
self.feedable_kwargs = ('y', 'y_target')
self.structural_kwargs = [
'beta', 'decision_rule', 'batch_size', 'confidence',
'targeted', 'learning_rate', 'binary_search_steps',
'max_iterations', 'abort_early', 'initial_const', 'clip_min',
'clip_max'
]
示例6: test_output_layer
# 需要导入模块: from cleverhans import model [as 别名]
# 或者: from cleverhans.model import CallableModelWrapper [as 别名]
def test_output_layer(self):
def model(**kwargs):
del kwargs
return True
# The following two calls should not raise Exceptions
wrap = CallableModelWrapper(model, 'probs')
wrap = CallableModelWrapper(model, 'logits')
示例7: __init__
# 需要导入模块: from cleverhans import model [as 别名]
# 或者: from cleverhans.model import CallableModelWrapper [as 别名]
def __init__(self, m, back='tf', sess=None):
if not isinstance(m, model.Model):
m = model.CallableModelWrapper(m, 'probs')
super(RandomAttack, self).__init__(m, back, sess)
self.feedable_kwargs = {
'eps': np.float32,
'num_samples': np.float32,
'num_batches': np.float32,
'y': np.float32,
'y_target': np.float32,
'clip_min': np.float32,
'clip_max': np.float32
}
self.structural_kwargs = ['ord']
示例8: spsa_attack
# 需要导入模块: from cleverhans import model [as 别名]
# 或者: from cleverhans.model import CallableModelWrapper [as 别名]
def spsa_attack():
# Use tf for evaluation on adversarial data
sess = tf.Session()
x_op = tf.placeholder(tf.float32, shape=(None, 3, 32, 32,))
y_op = tf.placeholder(tf.float32, shape=(1,))
# Convert pytorch model to a tf_model and wrap it in cleverhans
tf_model_fn = convert_pytorch_model_to_tf(menet_model)
cleverhans_model = CallableModelWrapper(tf_model_fn, output_layer='logits')
# Create an SPSA attack
spsa = SPSA(cleverhans_model, sess=sess)
spsa_params = {
'eps': config['epsilon'],
'nb_iter': config['num_steps'],
'clip_min': 0.,
'clip_max': 1.,
'spsa_samples': args.spsa_sample, # in this case, the batch_size is equal to spsa_samples
'spsa_iters': 1,
}
adv_x_op = spsa.generate(x_op, y_op, **spsa_params)
adv_preds_op = tf_model_fn(adv_x_op)
# Evaluation against SPSA attacks
correct = 0
total = 0
for batch_idx, (inputs, targets) in enumerate(test_loader):
adv_preds = sess.run(adv_preds_op, feed_dict={x_op: inputs, y_op: targets})
correct += (np.argmax(adv_preds, axis=1) == targets).sum().float()
total += len(inputs)
sys.stdout.write(
"\rBlack-box SPSA attack... Acc: %.3f%% (%d/%d)" % (100. * correct / total, correct, total))
sys.stdout.flush()
print('Accuracy under SPSA attack: %.3f%%' % (100. * correct / total))
示例9: test_output_layer
# 需要导入模块: from cleverhans import model [as 别名]
# 或者: from cleverhans.model import CallableModelWrapper [as 别名]
def test_output_layer(self):
"""
Test that the CallableModelWrapper can be constructed without causing Exceptions
"""
def model(**kwargs):
"""Mock model"""
del kwargs
return True
# The following two calls should not raise Exceptions
CallableModelWrapper(model, 'probs')
CallableModelWrapper(model, 'logits')
示例10: __init__
# 需要导入模块: from cleverhans import model [as 别名]
# 或者: from cleverhans.model import CallableModelWrapper [as 别名]
def __init__(self, model, dtypestr='float32', **kwargs):
"""
Creates a FastGradientMethod instance in eager execution.
:model: cleverhans.model.Model
:dtypestr: datatype in the string format.
"""
del kwargs
if not isinstance(model, Model):
wrapper_warning()
model = CallableModelWrapper(model, 'probs')
super(FastGradientMethod, self).__init__(model, dtypestr)
示例11: __init__
# 需要导入模块: from cleverhans import model [as 别名]
# 或者: from cleverhans.model import CallableModelWrapper [as 别名]
def __init__(self, model, sess=None, dtypestr='float32', **kwargs):
"""
Note: the model parameter should be an instance of the
cleverhans.model.Model abstraction provided by CleverHans.
"""
if not isinstance(model, Model):
wrapper_warning_logits()
model = CallableModelWrapper(model, 'logits')
super(VirtualAdversarialMethod, self).__init__(model, sess, dtypestr,
**kwargs)
self.feedable_kwargs = ('eps', 'xi', 'clip_min', 'clip_max')
self.structural_kwargs = ['num_iterations']
示例12: __init__
# 需要导入模块: from cleverhans import model [as 别名]
# 或者: from cleverhans.model import CallableModelWrapper [as 别名]
def __init__(self, model, sess, dtypestr='float32', **kwargs):
"""
Create a DeepFool instance.
"""
if not isinstance(model, Model):
wrapper_warning_logits()
model = CallableModelWrapper(model, 'logits')
super(DeepFool, self).__init__(model, sess, dtypestr, **kwargs)
self.structural_kwargs = [
'overshoot', 'max_iter', 'clip_max', 'clip_min', 'nb_candidate'
]