-
Notifications
You must be signed in to change notification settings - Fork 7
/
attack.py
144 lines (135 loc) · 4.95 KB
/
attack.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import os
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import DataLoader, random_split, Subset, ConcatDataset
from torchvision.datasets import *
from torchvision.transforms.transforms import *
from torchvision.transforms.functional import *
from torchvision.utils import save_image
from tqdm import tqdm
from torchplus.utils import Init, ClassificationAccuracy
if __name__ == "__main__":
batch_size = 8
log_epoch = 2
class_num = 40
root_dir = "D:/log/paper1/logZZPMAIN.attack"
dataset_dir = "E:/datasets/at&t face database"
target_pkl = "D:/log/paper1/logZZPMAIN/Model_Nov25_14-45-14_zzp-asus_main AT and T face/mynet_50.pkl"
h = 112
w = 92
alpha = 50000
beta = 1000
gama = 0.001
learning_rate = 0.1
momentum = 0.9
init = Init(
seed=9970,
log_root_dir=root_dir,
backup_filename=__file__,
tensorboard=True,
comment=f"attack AT and T face",
)
output_device = init.get_device()
writer = init.get_writer()
log_dir = init.get_log_dir()
data_workers = 2
def Process(im_flatten):
maxValue = torch.max(im_flatten)
minValue = torch.min(im_flatten)
im_flatten = im_flatten - minValue
im_flatten = im_flatten / (maxValue - minValue)
return im_flatten
def Attack(mynet, target_label):
aim_flatten = torch.zeros(1, h * w).to(output_device)
v = torch.zeros(1, h * w).to(output_device)
aim_flatten.requires_grad = True
costn_1 = 10
b = 0
# g = 0
out = mynet.forward(aim_flatten.detach())
after_softmax = F.softmax(out, dim=-1)
predict = torch.argmax(after_softmax)
writer.add_image(
f"original input image {target_label}",
aim_flatten.detach().reshape(1, h, w),
target_label,
)
writer.add_text(
f"original input image predict label {target_label}",
f"predict label: {predict.item()}",
)
for i in tqdm(range(alpha), desc="Computing"):
out = mynet.forward(aim_flatten)
if aim_flatten.grad is not None:
aim_flatten.grad.zero_()
out = out.reshape(1, class_num)
target_class = torch.tensor([target_label]).to(output_device)
cost = nn.CrossEntropyLoss()(out, target_class)
cost.backward()
aim_grad = aim_flatten.grad
# see https://pytorch.org/docs/stable/generated/torch.optim.SGD.html#torch.optim.SGD
aim_flatten = aim_flatten - learning_rate * (momentum * v + aim_grad)
aim_flatten = Process(aim_flatten)
aim_flatten = torch.clamp(aim_flatten.detach(), 0, 1)
aim_flatten.requires_grad = True
if cost >= costn_1:
b = b + 1
if b > beta:
break
else:
b = 0
costn_1 = cost
if cost < gama:
break
out = mynet.forward(aim_flatten.detach())
after_softmax = F.softmax(out, dim=-1)
predict = torch.argmax(after_softmax)
writer.add_image(
f"inverted image {target_label}",
aim_flatten.detach().reshape(1, h, w),
target_label,
)
writer.add_text(
f"inverted image predict label {target_label}",
f"predict label: {predict.item()}",
)
save_image(
aim_flatten.detach().reshape(h, w), f"{log_dir}/inverted_{target_label}.png"
)
class Net(nn.Module):
def __init__(self, input_features, output_features):
super(Net, self).__init__()
self.input_features = input_features
self.output_features = output_features
self.regression = nn.Linear(
in_features=self.input_features, out_features=self.output_features
)
def forward(self, x):
x = self.regression(x)
return x
class MLP(nn.Module):
def __init__(self, input_features, output_features):
super(MLP, self).__init__()
self.input_features = input_features
self.middle_features = 3000
self.output_features = output_features
self.fc = nn.Linear(
in_features=self.input_features, out_features=self.middle_features
)
self.regression = nn.Linear(
in_features=self.middle_features, out_features=self.output_features
)
def forward(self, x):
x = self.fc(x)
x = self.regression(x)
return x
mynet = Net(h * w, class_num).to(output_device).train(False)
assert os.path.exists(target_pkl)
mynet.load_state_dict(
torch.load(open(target_pkl, "rb"), map_location=output_device)
)
for i in tqdm(range(class_num), desc="Attack Class"):
Attack(mynet=mynet, target_label=i)
writer.close()