-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattacks.py
More file actions
29 lines (21 loc) · 855 Bytes
/
Copy pathattacks.py
File metadata and controls
29 lines (21 loc) · 855 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import torch
import torch.nn.functional as F
from config import DEVICE # fixed filename
def semantic_flow_attack(model, image, label, epsilon=0.03, steps=10, alpha=0.005):
perturbed = image.clone().detach().to(DEVICE)
label = label.to(DEVICE)
perturbed.requires_grad = True
for _ in range(steps):
output = model(perturbed)
print(type(output), len(output))
print(output)
loss = F.cross_entropy(output[0], label)
model.zero_grad()
loss.backward()
grad_sign = perturbed.grad.sign()
perturbed = perturbed + alpha * grad_sign
# Clip and project to epsilon ball
perturbed = torch.clamp(perturbed, image - epsilon, image + epsilon)
perturbed = torch.clamp(perturbed, 0, 1).detach()
perturbed.requires_grad = True
return perturbed