提交 94fea877 编写于 作者: wgzqz's avatar wgzqz

Make gradient.shape==image.shape in the models.

上级 d9ddb835
......@@ -87,10 +87,12 @@ class Adversary(object):
:return: bool
"""
assert adversarial_example is not None
assert self.__original.shape == adversarial_example.shape
ok = self._is_successful(adversarial_label)
if ok:
self.__adversarial_example = adversarial_example.reshape(
self.__original.shape)
self.__adversarial_example = adversarial_example
self.__adversarial_label = adversarial_label
return ok
......
......@@ -3,6 +3,7 @@ Attack methods
"""
from .base import Attack
from .deepfool import DeepFoolAttack
from .gradientsign import FGSM
from .gradientsign import GradientSignAttack
from .iterator_gradientsign import IFGSM
......
......@@ -45,9 +45,8 @@ class GradientSignAttack(Attack):
adversary.original_label)])
gradient_sign = np.sign(gradient) * (max_ - min_)
original = adversary.original.reshape(gradient_sign.shape)
for epsilon in epsilons:
adv_img = original + epsilon * gradient_sign
adv_img = adversary.original + epsilon * gradient_sign
adv_img = np.clip(adv_img, min_, max_)
adv_label = np.argmax(self.model.predict([(adv_img, 0)]))
logging.info('epsilon = {:.3f}, pre_label = {}, adv_label={}'.
......
......@@ -35,7 +35,7 @@ class IteratorGradientSignAttack(Attack):
min_, max_ = self.model.bounds()
for epsilon in epsilons:
adv_img = None
adv_img = adversary.original
for _ in range(steps):
if adversary.is_targeted_attack:
gradient = self.model.gradient([(adversary.original,
......@@ -45,8 +45,6 @@ class IteratorGradientSignAttack(Attack):
gradient = self.model.gradient([(adversary.original,
adversary.original_label)])
gradient_sign = np.sign(gradient) * (max_ - min_)
if adv_img is None:
adv_img = adversary.original.reshape(gradient_sign.shape)
adv_img = adv_img + gradient_sign * epsilon
adv_img = np.clip(adv_img, min_, max_)
adv_label = np.argmax(self.model.predict([(adv_img, 0)]))
......
......@@ -102,4 +102,4 @@ class PaddleModel(Model):
grad, = self._exe.run(self._program,
feed=feeder.feed(image_batch),
fetch_list=[self._gradient])
return grad
return grad.reshape(image_batch[0][0].shape)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册