Skip to content

Commit

Permalink
fix preprocessing bug
Browse files Browse the repository at this point in the history
  • Loading branch information
0SillyMonkey0 committed Dec 4, 2019
1 parent 2ba0576 commit 79ec95f
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion advbox/attacks/cw2_pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ def __init__(self,model):
super(CW_L2_Attack, self).__init__(model)

self._model=model._model
mean, std = model._preprocess
self.mean = torch.from_numpy(mean)
self.std = torch.from_numpy(std)


def _apply(self,
Expand All @@ -64,6 +67,8 @@ def _apply(self,
pre_label = adversary.original_label

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.mean = self.mean.to(device)
self.std = self.std.to(device)
#定向
if adversary.is_targeted_attack:
#攻击目标标签 必须使用one hot编码
Expand Down Expand Up @@ -105,7 +110,7 @@ def _apply(self,
optimizer.zero_grad()
#定义新输入
newimg = torch.tanh(modifier + timg) * boxmul + boxplus
output=self._model(newimg)
output=self._model((newimg - self.mean) / self.std)
#定义cw中的损失函数
loss2=torch.dist(newimg,(torch.tanh(timg) * boxmul + boxplus),p=2)
real=torch.max(output*tlab)
Expand Down

0 comments on commit 79ec95f

Please sign in to comment.