Upload anime-ai-detect-fucker/attacker/base.py with huggingface_hub
Browse files
anime-ai-detect-fucker/attacker/base.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class Attacker:
|
| 2 |
+
def __init__(self, model, img_transform=(lambda x: x, lambda x: x)):
|
| 3 |
+
self.model = model # 必须是pytorch的model
|
| 4 |
+
'''self.model.eval()
|
| 5 |
+
for k, v in self.model.named_parameters():
|
| 6 |
+
v.requires_grad = False'''
|
| 7 |
+
self.img_transform = img_transform
|
| 8 |
+
self.forward = lambda attacker, images, labels: attacker.step(images, labels, attacker.loss)
|
| 9 |
+
|
| 10 |
+
def set_para(self, **kwargs):
|
| 11 |
+
for k, v in kwargs.items():
|
| 12 |
+
setattr(self, k, v)
|
| 13 |
+
|
| 14 |
+
def set_forward(self, forward):
|
| 15 |
+
self.forward = forward
|
| 16 |
+
|
| 17 |
+
def step(self, images, labels, loss):
|
| 18 |
+
pass
|
| 19 |
+
|
| 20 |
+
def set_loss(self, loss):
|
| 21 |
+
self.loss = loss
|
| 22 |
+
|
| 23 |
+
def attack(self, images, labels):
|
| 24 |
+
pass
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class Empty:
|
| 28 |
+
def __enter__(self):
|
| 29 |
+
pass
|
| 30 |
+
|
| 31 |
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
| 32 |
+
pass
|