Upload anime-ai-detect-fucker/attacker/PGD.py with huggingface_hub
Browse files
anime-ai-detect-fucker/attacker/PGD.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from copy import deepcopy
|
| 2 |
+
|
| 3 |
+
import torch
|
| 4 |
+
from torch import nn
|
| 5 |
+
from torch.cuda import amp
|
| 6 |
+
from tqdm import tqdm
|
| 7 |
+
|
| 8 |
+
from .base import Attacker, Empty
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class PGD(Attacker):
|
| 12 |
+
def __init__(self, model, img_transform=(lambda x: x, lambda x: x), use_amp=False):
|
| 13 |
+
super().__init__(model, img_transform)
|
| 14 |
+
self.use_amp = use_amp
|
| 15 |
+
self.call_back = None
|
| 16 |
+
self.img_loader = None
|
| 17 |
+
self.img_hook = None
|
| 18 |
+
|
| 19 |
+
self.scaler = amp.GradScaler(enabled=use_amp)
|
| 20 |
+
|
| 21 |
+
def set_para(self, eps=8, alpha=lambda: 8, iters=20, **kwargs):
|
| 22 |
+
super().set_para(eps=eps, alpha=alpha, iters=iters, **kwargs)
|
| 23 |
+
|
| 24 |
+
def set_call_back(self, call_back):
|
| 25 |
+
self.call_back = call_back
|
| 26 |
+
|
| 27 |
+
def set_img_loader(self, img_loader):
|
| 28 |
+
self.img_loader = img_loader
|
| 29 |
+
|
| 30 |
+
def step(self, images, labels, loss):
|
| 31 |
+
with amp.autocast(enabled=self.use_amp):
|
| 32 |
+
images.requires_grad = True
|
| 33 |
+
outputs = self.model(images).logits
|
| 34 |
+
|
| 35 |
+
self.model.zero_grad()
|
| 36 |
+
cost = loss(outputs,
|
| 37 |
+
labels) # +outputs[2].view(-1)[0]*0+outputs[1].view(-1)[0]*0+outputs[0].view(-1)[0]*0 #support DDP
|
| 38 |
+
|
| 39 |
+
self.scaler.scale(cost).backward()
|
| 40 |
+
|
| 41 |
+
adv_images = (images + self.alpha() * images.grad.sign()).detach_()
|
| 42 |
+
eta = torch.clamp(adv_images - self.ori_images, min=-self.eps, max=self.eps)
|
| 43 |
+
images = self.img_transform[0](
|
| 44 |
+
torch.clamp(self.img_transform[1](self.ori_images + eta), min=0, max=1).detach_())
|
| 45 |
+
|
| 46 |
+
return images
|
| 47 |
+
|
| 48 |
+
def set_data(self, images, labels):
|
| 49 |
+
self.ori_images = deepcopy(images)
|
| 50 |
+
self.images = images
|
| 51 |
+
self.labels = labels
|
| 52 |
+
|
| 53 |
+
def __iter__(self):
|
| 54 |
+
self.atk_step = 0
|
| 55 |
+
return self
|
| 56 |
+
|
| 57 |
+
def __next__(self):
|
| 58 |
+
self.atk_step += 1
|
| 59 |
+
if self.atk_step > self.iters:
|
| 60 |
+
raise StopIteration
|
| 61 |
+
|
| 62 |
+
with self.model.no_sync() if isinstance(self.model, nn.parallel.DistributedDataParallel) else Empty():
|
| 63 |
+
self.model.eval()
|
| 64 |
+
|
| 65 |
+
self.images = self.forward(self, self.images, self.labels)
|
| 66 |
+
|
| 67 |
+
self.model.zero_grad()
|
| 68 |
+
self.model.train()
|
| 69 |
+
|
| 70 |
+
return self.ori_images, self.images.detach(), self.labels
|
| 71 |
+
|
| 72 |
+
def attack(self, images, labels, step_func=None):
|
| 73 |
+
# images = deepcopy(images)
|
| 74 |
+
self.ori_images = deepcopy(images)
|
| 75 |
+
|
| 76 |
+
for i in tqdm(range(self.iters)):
|
| 77 |
+
self.model.eval()
|
| 78 |
+
|
| 79 |
+
images = self.forward(self, images, labels)
|
| 80 |
+
|
| 81 |
+
self.model.zero_grad()
|
| 82 |
+
self.model.train()
|
| 83 |
+
if self.call_back:
|
| 84 |
+
self.call_back(self.ori_images, images.detach(), labels)
|
| 85 |
+
|
| 86 |
+
if self.img_hook is not None:
|
| 87 |
+
images = self.img_hook(self.ori_images, images.detach())
|
| 88 |
+
|
| 89 |
+
if step_func:
|
| 90 |
+
step_func(i + 1)
|
| 91 |
+
|
| 92 |
+
return images
|