-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_classification_models.py
302 lines (225 loc) · 10 KB
/
eval_classification_models.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# grouped & sorted imports
import os
import torch
import torch.nn as nn
from PIL import ImageFile
from torch.utils.data import DataLoader
from dataset import ImageFolder
from torchvision import transforms
from torchvision.transforms import Compose, Resize, CenterCrop, ToTensor
import argparse
from robustness import model_utils
from robustness.datasets import ImageNet
ImageFile.LOAD_TRUNCATED_IMAGES = True
from timm import create_model
class Normalize(nn.Module):
def __init__(self, ms=None):
super(Normalize, self).__init__()
if ms == None:
self.ms = [(0.485, 0.456, 0.406), (0.229, 0.224, 0.225)]
else:
self.ms = ms
def forward(self, input):
x = input.clone()
for i in range(x.shape[1]):
x[:, i] = (x[:, i] - self.ms[0][i]) / self.ms[1][i]
return x
# simplified functions
def get_transforms():
transform = Compose([
Resize(DATA_SIZE),
CenterCrop(CROP_SIZE),
ToTensor()
])
return transform
def save_img(img, file_dir):
transforms.ToPILImage()(img.cpu()).save(file_dir)
def get_dataloader(dataset, batch_size, num_workers, shuffle=False):
return DataLoader(dataset, batch_size=batch_size, num_workers=num_workers, shuffle=shuffle)
def vit_small_patch16_224():
model = create_model('vit_small_patch16_224', pretrained=True)
mean, std = model.default_cfg['mean'], model.default_cfg['std']
print(f"mean: {mean}, std: {std}")
model = torch.nn.Sequential(Normalize(ms=[mean, std]), model)
model = model.to(DEVICE)
model.eval()
return model
def resnet50():
model = create_model('resnet50', pretrained=True)
mean, std = model.default_cfg['mean'], model.default_cfg['std']
print(f"mean: {mean}, std: {std}")
model = torch.nn.Sequential(Normalize(ms=[mean, std]), model)
model = model.to(DEVICE)
model.eval()
return model
def resnet50_adv():
model = create_model('resnet50', pretrained=True)
mean, std = model.default_cfg['mean'], model.default_cfg['std']
print(f"mean: {mean}, std: {std}")
model = torch.nn.Sequential(Normalize(ms=[mean, std]), model)
model = model
model.eval()
return model
def vit_tiny_patch16_224():
model = create_model('vit_tiny_patch16_224', pretrained=True)
mean, std = model.default_cfg['mean'], model.default_cfg['std']
print(f"mean: {mean}, std: {std}")
model = torch.nn.Sequential(Normalize(ms=[mean, std]), model)
model = model.to(DEVICE)
model.eval()
return model
def resnet152d():
model = create_model('resnet152d', pretrained=True)
mean, std = model.default_cfg['mean'], model.default_cfg['std']
print(f"mean: {mean}, std: {std}")
model = torch.nn.Sequential(Normalize(ms=[mean, std]), model)
model = model.to(DEVICE)
model.eval()
return model
def densenet161():
model = create_model('densenet161', pretrained=True)
mean, std = model.default_cfg['mean'], model.default_cfg['std']
print(f"mean: {mean}, std: {std}")
model = torch.nn.Sequential(Normalize(ms=[mean, std]), model)
model = model.to(DEVICE)
model.eval()
return model
def swin_tiny_patch4_window7_224():
model = create_model('swin_tiny_patch4_window7_224', pretrained=True)
mean, std = model.default_cfg['mean'], model.default_cfg['std']
print(f"mean: {mean}, std: {std}")
model = torch.nn.Sequential(Normalize(ms=[mean, std]), model)
model = model.to(DEVICE)
model.eval()
return model
def swin_small_patch4_window7_224():
model = create_model('swin_small_patch4_window7_224', pretrained=True)
mean, std = model.default_cfg['mean'], model.default_cfg['std']
print(f"mean: {mean}, std: {std}")
model = torch.nn.Sequential(Normalize(ms=[mean, std]), model)
model = model.to(DEVICE)
model.eval()
return model
def robust_model(model, chk_path):
"""
Loads robust models from robustness package as well as there
transformations
"""
imagenet_ds = ImageNet('/')
model, _ = model_utils.make_and_restore_model(arch=model, dataset=imagenet_ds,
resume_path=chk_path, parallel=False, add_custom_forward=True)
return model
def get_parser():
parser = argparse.ArgumentParser()
parser.add_argument('--save_dir', default="output", type=str,
help='Where to save the adversarial examples, and other results')
parser.add_argument('--data_path', default="path/to/dataset", type=str,
help='The clean images root directory')
parser.add_argument('--batch_size', default=128, type=int, help='Batch size for the dataloader')
args = parser.parse_args()
return args
if __name__ == "__main__":
args = get_parser()
# Constants grouped
DATASET_PATH = args.data_path
BATCH_SIZE = args.batch_size
SAVE_DIR = args.save_dir
DATA_SIZE = 256
CROP_SIZE = 224
NUM_WORKERS = 4
os.makedirs(SAVE_DIR, exist_ok=True)
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
ALL_CLASSES = sorted(os.listdir(DATASET_PATH))
# dataset and model creation
dataset = ImageFolder(DATASET_PATH, get_transforms())
dataloader = get_dataloader(dataset, BATCH_SIZE, NUM_WORKERS)
models = [vit_tiny_patch16_224, resnet50, vit_small_patch16_224, resnet152d, densenet161, swin_tiny_patch4_window7_224, swin_small_patch4_window7_224]
names = ['vit_tiny_patch16_224', 'resnet50', 'vit_small_patch16_224', 'resnet152d', 'densenet161', 'swin_tiny_patch4_window7_224', 'swin_small_patch4_window7_224']
accuracy_per_model = []
with torch.no_grad():
for i, model in enumerate(models):
model = model()
correct = 0
total = 0
for batch_number, (images, labels) in enumerate(dataloader):
images = images.to(DEVICE)
labels = labels.to(DEVICE)
logits = model(images)
_, predicted = torch.max(logits.data, 1)
correct += (predicted == labels).sum().item()
total += labels.size(0)
accuracy = correct / total
accuracy_per_model.append(accuracy)
print(f"Model {names[i]} Accuracy: {accuracy}")
with open(f'{SAVE_DIR}/model_results.txt', 'a') as f:
f.write(f"Model {names[i]} Accuracy: {accuracy}\n")
average_accuracy = sum(accuracy_per_model) / len(models)
print(f"Average Accuracy: {average_accuracy}")
with open(f'{SAVE_DIR}/model_results.txt', 'a') as f:
f.write(f"Average Accuracy: {average_accuracy}\n")
pretrained_models_path={"eps_0": "pretrained_models/resnet50_l2_eps0.ckpt",
"eps0.5": "pretrained_models/resnet50_linf_eps0.5.ckpt",
"eps2.0": "pretrained_models/resnet50_linf_eps2.0.ckpt",
"eps4.0": "pretrained_models/resnet50_linf_eps4.0.ckpt",
"eps8.0": "pretrained_models/resnet50_linf_eps8.0.ckpt"}
accuracy_per_model = []
with torch.no_grad():
for eps, path in pretrained_models_path.items():
model = robust_model("resnet50", path)
model.eval()
model.to(DEVICE)
correct = 0
total = 0
for i, (images, labels) in enumerate(dataloader):
images, labels = images.to(DEVICE), labels.to(DEVICE)
preds = model(images, with_image=False)
_, pre = torch.max(preds, 1)
correct += torch.sum(pre == labels)
total += images.shape[0]
print(total, end="\r")
accuracy = 100 * correct / total
accuracy_per_model.append(accuracy)
print(f"Accuracy: {accuracy}")
with open(f'{SAVE_DIR}/model_robust.txt', 'a') as f:
f.write(f"ResNet-50 {eps} Accuracy: {accuracy}\n")
average_accuracy = sum(accuracy_per_model) / len(pretrained_models_path)
print(f"Average Accuracy: {average_accuracy}")
with open(f'{SAVE_DIR}/model_robust.txt', 'a') as f:
f.write(f"Average Accuracy across Models: {average_accuracy}\n")
from stylised_models import deit_tiny_distilled_patch16_224, deit_small_distilled_patch16_224
pretrained_models_path={
"DeiT-S Distill": [deit_small_distilled_patch16_224, "pretrained_models/deit_s_sin_dist.pth"],
"DeiT-T Distill": [deit_tiny_distilled_patch16_224, "pretrained_models/deit_t_sin_dist.pth"],
"DeiT-T": ["deit_tiny_patch16_224","pretrained_models/deit_t_sin.pth"],
"DeiT-S": ["deit_small_patch16_224","pretrained_models/deit_s_sin.pth"],
}
accuracy_per_model = []
with torch.no_grad():
for model_name, values in pretrained_models_path.items():
if "Distill" in model_name:
model = values[0]()
else:
model = create_model(values[0], pretrained=False)
checkpoint = torch.load(values[1], map_location='cpu')
msg = model.load_state_dict(checkpoint['model'], strict=False)
print(msg)
model.eval()
model.to(DEVICE)
correct = 0
total = 0
for i, (images, labels) in enumerate(dataloader):
images, labels = images.to(DEVICE), labels.to(DEVICE)
preds = model(images)
_, pre = torch.max(preds, 1)
correct += torch.sum(pre == labels)
total += images.shape[0]
print(total, end="\r")
accuracy = 100 * correct / total
accuracy_per_model.append(accuracy)
print(f"Accuracy: {accuracy}")
with open(f'{SAVE_DIR}/model_style.txt', 'a') as f:
f.write(f"Stylised {model} Accuracy: {accuracy}\n")
average_accuracy = sum(accuracy_per_model) / len(pretrained_models_path)
print(f"Average Accuracy: {average_accuracy}")
with open(f'{SAVE_DIR}/model_style.txt', 'a') as f:
f.write(f"Average Accuracy across Models: {average_accuracy}\n")