本文整理汇总了Python中cleverhans.attacks.DeepFool方法的典型用法代码示例。如果您正苦于以下问题:Python attacks.DeepFool方法的具体用法?Python attacks.DeepFool怎么用?Python attacks.DeepFool使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cleverhans.attacks
的用法示例。
在下文中一共展示了attacks.DeepFool方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
# 需要导入模块: from cleverhans import attacks [as 别名]
# 或者: from cleverhans.attacks import DeepFool [as 别名]
def setUp(self):
super(TestDeepFool, self).setUp()
self.sess = tf.Session()
self.model = SimpleModel()
self.attack = DeepFool(self.model, sess=self.sess)
示例2: query
# 需要导入模块: from cleverhans import attacks [as 别名]
# 或者: from cleverhans.attacks import DeepFool [as 别名]
def query(self, X_train, Y_train, labeled_idx, amount):
unlabeled_idx = get_unlabeled_idx(X_train, labeled_idx)
unlabeled = X_train[unlabeled_idx]
keras_wrapper = KerasModelWrapper(self.model)
sess = K.get_session()
deep_fool = DeepFool(keras_wrapper, sess=sess)
deep_fool_params = {'over_shoot': 0.02,
'clip_min': 0.,
'clip_max': 1.,
'nb_candidate': Y_train.shape[1],
'max_iter': 10}
true_predictions = np.argmax(self.model.predict(unlabeled, batch_size=256), axis=1)
adversarial_predictions = np.copy(true_predictions)
while np.sum(true_predictions != adversarial_predictions) < amount:
adversarial_images = np.zeros(unlabeled.shape)
for i in range(0, unlabeled.shape[0], 100):
print("At {i} out of {n}".format(i=i, n=unlabeled.shape[0]))
if i+100 > unlabeled.shape[0]:
adversarial_images[i:] = deep_fool.generate_np(unlabeled[i:], **deep_fool_params)
else:
adversarial_images[i:i+100] = deep_fool.generate_np(unlabeled[i:i+100], **deep_fool_params)
pertubations = adversarial_images - unlabeled
norms = np.linalg.norm(np.reshape(pertubations,(unlabeled.shape[0],-1)), axis=1)
adversarial_predictions = np.argmax(self.model.predict(adversarial_images, batch_size=256), axis=1)
norms[true_predictions == adversarial_predictions] = np.inf
deep_fool_params['max_iter'] *= 2
selected_indices = np.argpartition(norms, amount)[:amount]
del keras_wrapper
del deep_fool
gc.collect()
return np.hstack((labeled_idx, unlabeled_idx[selected_indices]))
示例3: setUp
# 需要导入模块: from cleverhans import attacks [as 别名]
# 或者: from cleverhans.attacks import DeepFool [as 别名]
def setUp(self):
super(TestDeepFool, self).setUp()
self.sess = tf.Session()
self.model = SimpleModel()
self.attack = DeepFool(self.model, sess=self.sess)