-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathsave_examples.py
124 lines (100 loc) · 3.87 KB
/
save_examples.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
import torch
import torch.backends
import torch.nn as nn
from torch.utils.data import DataLoader
from torchvision import datasets, transforms
from tqdm import tqdm
import numpy as np
import cv2
import attacks
from models import ResNet50, VGG16
def validate(model, val_loader, device):
model.eval()
total_samples = 0
correct_pred = 0
accuracy = None
with torch.no_grad():
with tqdm(val_loader, desc='Val') as pbar:
for i, (x, y) in enumerate(pbar):
x = x.float().to(device)
y = y.long().to(device)
outputs = model(x)
_, y_pred = torch.max(outputs.data, 1)
correct_pred += (y_pred == y).sum().item()
total_samples += x.size(0)
accuracy = correct_pred / total_samples
pbar.set_postfix(acc=accuracy)
return accuracy
def main(args):
val_data = datasets.CIFAR10('./data', train=False, download=True,
transform=transforms.Compose([
transforms.ToTensor(),
]))
val_loader = DataLoader(val_data,
batch_size=args.batch_size,
shuffle=False,
num_workers=args.num_workers)
checkpoint = torch.load(args.checkpoint, map_location=args.device)
# model = VGG16()
model = ResNet50()
model.load_state_dict(checkpoint['state_dict'])
model.to(args.device)
model.eval()
criterion = nn.CrossEntropyLoss()
# val_acc = validate(model, val_loader, args.device)
# print('Baseline accuracy: {}'.format(val_acc))
# Semantic adversarial examples.
model.eval()
count = 0
with tqdm(val_loader, desc='adv') as pbar:
for i, (x, y) in enumerate(pbar):
x = x.float().to(args.device)
y = y.long().to(args.device)
outputs = model(x)
_, y_pred = torch.max(outputs.data, 1)
x_adv, y_adv, factor = attacks.contrast_gradient(
x,
y,
model,
criterion,
step_size=args.step_size,
alpha=args.alpha,
beta=args.beta,
max_iter=args.max_iter,
device=args.device,
verbose=False
)
if y_adv.item() != y_pred.item() and y_pred.item() == y.item():
img = x.squeeze(0).cpu().numpy().transpose(1, 2, 0) * 255.0
img = img.astype(np.uint8)
img = img[..., ::-1]
img_adv = x_adv.detach().squeeze(0).cpu().numpy().transpose(1, 2, 0) * 255.0
img_adv = img_adv.astype(np.uint8)
img_adv = img_adv[..., ::-1]
cv2.imwrite('examples/contrast/{}_y_{}_y_pred_{}_y_adv_{}_factor_{:.3f}.png'
.format(i, y.item(), y_pred.item(), y_adv.item(), factor.item()), img_adv)
cv2.imwrite('examples/contrast/{}_y_{}_y_pred_{}.png'
.format(i, y.item(), y_pred.item()), img)
count += 1
pbar.set_postfix(num_examples='{}/{}'.format(count, args.num_examples))
if count == args.num_examples:
break
if __name__ == '__main__':
class Args:
alpha = 0.7
beta = 1.3
max_iter = 10 # T
step_size = 2.5 * (beta - alpha) / (2 * max_iter)
checkpoint = 'weights/resnet50.pth.tar'
num_workers = 0
batch_size = 1
num_examples = 20
if torch.cuda.is_available():
device = torch.device('cuda')
elif hasattr(torch.backends, 'mps') and torch.backends.mps.is_available():
device = torch.device('mps')
else:
device = torch.device('cpu')
# device = torch.device('cpu')
args = Args()
main(args)