-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.py
694 lines (598 loc) · 22.1 KB
/
eval.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
import argparse
import os
import shutil
import sys
import warnings
from collections import defaultdict
from enum import Enum
import gdown
import matplotlib.pyplot as plt
import numpy as np
import torch
import torch.backends.cudnn as cudnn
import torch.nn.functional as F
import torchvision
import torchvision.transforms as transforms
from PIL import Image
from torch import cuda
from torch.utils.data import DataLoader
from torchvision import datasets, models
from tqdm import tqdm
from transformers import ViTFeatureExtractor
from Edge_images.generate_datasets import (
STL10,
CannyDataset,
DexiNedTestDataset,
CannyStylizedDataset,
DexiNedStylizedTestDataset,
DualDataset,
)
from models.morphclr import MorphCLRDualEval, MorphCLRSingleEval
from vit import VIT_pretrained
warnings.filterwarnings("ignore", category=UserWarning)
VIT_INPUT_SIZE = 224
convert_tensor_fn = transforms.ToTensor()
upscale_image_fn = transforms.Resize(VIT_INPUT_SIZE)
VIT_MODEL_TYPE = "google/vit-base-patch16-224-in21k"
feature_extractor = ViTFeatureExtractor.from_pretrained(VIT_MODEL_TYPE)
class ModelType(Enum):
SIMCLR = 1
MORPHCLRSINGLE = 2
MORPHCLRDUAL = 3
VIT = 4
torch_model_names = sorted(
name
for name in models.__dict__
if name.islower() and not name.startswith("__") and callable(models.__dict__[name])
)
parser = argparse.ArgumentParser(description="PyTorch SimCLR")
parser.add_argument(
"-data", metavar="DIR", default="./datasets", help="path to dataset"
)
parser.add_argument(
"-dataset-name",
default="stl10",
help="dataset name",
choices=[
"stl10",
"stl10_canny",
"stl10_dexined",
],
)
parser.add_argument(
"-a",
"--arch",
metavar="ARCH",
default="resnet18",
choices=torch_model_names,
help="model architecture: "
+ " | ".join(torch_model_names)
+ " (default: resnet18)",
)
parser.add_argument(
"-m",
"--model-type",
default="simclr",
choices=["simclr", "morphclr_single", "morphclr_dual", "vit"],
help="model type: simclr, morphclr_single, morphclr_dual, or vit",
)
parser.add_argument(
"-c",
"--checkpoint",
default="simclr_resnet50_50-epochs_stl10_100-epochs.pt",
type=str,
help="file name of the checkpoint",
)
parser.add_argument(
"-b",
"--batch-size",
default=256,
type=int,
metavar="N",
help="mini-batch size (default: 256), this is the total "
"batch size of all GPUs on the current node when "
"using Data Parallel or Distributed Data Parallel",
)
parser.add_argument("--disable-cuda", action="store_true", help="Disable CUDA")
parser.add_argument("--gpu-index", default=0, type=int, help="Gpu index.")
class StylizedSTL10Dataset(torch.utils.data.Dataset):
def __init__(self, source_dir, model_type=ModelType.SIMCLR):
data = []
style_labels = []
content_labels = []
print("[INFO] Preparing stylzed images from: {}".format(source_dir))
for source_path in tqdm(sorted(os.listdir(source_dir))):
source_example_path = sorted(
os.listdir(os.path.join(source_dir, source_path))
)
source_example_path = [
os.path.join(source_dir, source_path, x) for x in source_example_path
]
dir_style_labels = torch.tensor(
[int(x.split("/")[-1].split("_")[3]) for x in source_example_path]
)
style_labels.append(dir_style_labels)
content_style_labels = torch.tensor(
[int(source_path) - 1] * dir_style_labels.shape[0]
)
content_labels.append(content_style_labels)
tensor_images = [
convert_tensor_fn(Image.open(x).convert("RGB"))
for x in source_example_path
]
if model_type == ModelType.VIT:
tensor_images = [upscale_image_fn(x) for x in tensor_images]
vit_extracted = feature_extractor(tensor_images, return_tensors="pt")
tensor_images = vit_extracted["pixel_values"]
else:
tensor_images = torch.stack(tensor_images)
data.append(tensor_images)
data = torch.cat(data, dim=0)
style_labels = torch.cat(style_labels, dim=0).reshape(-1, 1)
content_labels = torch.cat(content_labels, dim=0).reshape(-1, 1)
target = torch.cat((style_labels, content_labels), dim=-1)
self.data = data
self.target = target
self.model_type = model_type
def __len__(self):
return self.data.shape[0]
def __getitem__(self, idx):
return self.data[idx], self.target[idx]
def compute_accuracies_local(
model, device, data_loader, model_name, model_type=ModelType.SIMCLR
):
# logit dim equals number of classes
model = model.eval()
model = model.to(device)
accuracies = defaultdict(int)
# Loop over all examples in test set
for data_and_target in tqdm(data_loader):
# Send the data and label to the device
if model_type == ModelType.VIT:
data, target = data_and_target
data = data["pixel_values"][0]
elif (
model_type == ModelType.MORPHCLRSINGLE
or model_type == ModelType.MORPHCLRDUAL
):
edge_data, non_edge_data, target = data_and_target
if len(edge_data.shape) == 3:
edge_data = edge_data.unsqueeze(1)
# If the image is of grayscale, repeat the dimension to create 3 channels
if edge_data.shape[1] == 1:
edge_data = edge_data.repeat(1, 3, 1, 1)
data = torch.stack([edge_data, non_edge_data], dim=0)
target = target.flatten()
else:
data, target = data_and_target
data, target = data.to(device), target.to(device)
# Forward pass the data through the model
output = model(data)
preds = torch.argmax(output, dim=1)
for i in range(output.shape[0]):
if preds[i] == target[i]:
accuracies[int(preds[i])] += 1 / 800
file_path = "accuracies.csv"
if not os.path.exists(file_path):
with open(file_path, "a") as f:
f.write(
"exp," + ",".join(["class_{}".format(i + 1) for i in range(10)]) + "\n"
)
with open(file_path, "a") as f:
f.write(
"{},".format(model_name)
+ ",".join([str(accuracies.get(i, 0)) for i in range(10)])
+ "\n"
)
print("Accuracies : {}".format(accuracies))
return accuracies
def compute_accuracy_and_ratio(model, device, data_loader, model_name, model_type):
# logit dim equals number of classes
print("[INFO] Computing accuracy and style-based decision ratio.")
model = model.eval()
model = model.to(device)
accuracies = defaultdict(int)
style_decisions = defaultdict(int)
content_decisions = defaultdict(int)
for data_and_target in tqdm(data_loader):
if (
model_type == ModelType.MORPHCLRSINGLE
or model_type == ModelType.MORPHCLRDUAL
):
edge_data, non_edge_data, target = data_and_target
if len(edge_data.shape) == 3:
edge_data = edge_data.unsqueeze(1)
# If the image is of grayscale, repeat the dimension to create 3 channels
if edge_data.shape[1] == 1:
edge_data = edge_data.repeat(1, 3, 1, 1)
data = torch.stack([edge_data, non_edge_data], dim=0)
else:
data, target = data_and_target
y_style, y_content = target[:, 0], target[:, 1]
y_style = y_style.to(device)
y_content = y_content.to(device)
preds = model(data.to(device))
preds = torch.argmax(preds, dim=1)
for i in range(preds.shape[0]):
if preds[i] == y_content[i]:
accuracies[int(preds[i])] += 1 / 800
content_decisions[int(preds[i])] += 1
if preds[i] == y_style[i]:
style_decisions[int(preds[i])] += 1
print("Accuracy: {}".format(accuracies))
print("# of texture-based decision per class: {}".format(style_decisions))
print("# of shape-based decision per class: {}".format(content_decisions))
file_path = "stylized_accuracies.csv"
if not os.path.exists(file_path):
with open(file_path, "a") as f:
f.write(
"exp,"
+ ",".join(["class_{}_acc".format(i + 1) for i in range(10)])
+ ","
+ ",".join(["class_{}_style".format(i + 1) for i in range(10)])
+ ","
+ ",".join(["class_{}_content".format(i + 1) for i in range(10)])
+ "\n"
)
with open(file_path, "a") as f:
f.write(
"{},".format(model_name)
+ ",".join([str(accuracies.get(i, 0)) for i in range(10)])
+ ","
+ ",".join([str(style_decisions.get(i, 0)) for i in range(10)])
+ ","
+ ",".join([str(content_decisions.get(i, 0)) for i in range(10)])
+ "\n"
)
return accuracies, style_decisions, content_decisions
def get_stl10_data_loader(
data_root,
download,
shuffle=False,
model_type=ModelType.SIMCLR,
batch_size=256,
stylization=False,
stylized_folder_path=None,
):
if not stylization or not stylized_folder_path:
print("[INFO] Preparing STL10 data loader.")
if model_type == ModelType.VIT:
stl10_transform = transforms.Compose(
[transforms.ToTensor(), upscale_image_fn, feature_extractor]
)
elif model_type == ModelType.SIMCLR:
stl10_transform = transforms.ToTensor()
test_dataset = datasets.STL10(
data_root, split="test", download=download, transform=stl10_transform
)
else:
print("[INFO] Preparing stylized STL10 data loader.")
test_dataset = StylizedSTL10Dataset(stylized_folder_path, model_type=model_type)
test_loader = DataLoader(
test_dataset,
batch_size=batch_size,
num_workers=10,
drop_last=False,
shuffle=shuffle,
)
return test_loader
def get_stl10_canny_dual_data_loader(
data_root,
download,
shuffle=False,
batch_size=256,
model_type=ModelType.SIMCLR,
stylization=False,
stylized_folder_path=None,
**kwarg
):
if not stylization or not stylized_folder_path:
print("[INFO] Preparing STL10 canny data loader.")
test_dataset = DualDataset(
CannyDataset(root=data_root, split="test", transform=transforms.ToTensor()),
STL10(root=data_root, split="test", transform=transforms.ToTensor()),
)
else:
print("[INFO] Preparing stylized STL10 canny data loader.")
test_dataset = DualDataset(
CannyStylizedDataset(
source_dir=stylized_folder_path,
model_type=model_type,
),
StylizedSTL10Dataset(
source_dir=stylized_folder_path,
model_type=model_type,
),
)
test_loader = DataLoader(
test_dataset,
batch_size=batch_size,
num_workers=10,
drop_last=False,
shuffle=shuffle,
)
return test_loader
def get_stl10_dexined_dual_data_loader(
data_root,
download,
shuffle=False,
batch_size=256,
model_type=ModelType.SIMCLR,
stylization=False,
stylized_folder_path=None,
**kwarg
):
if not stylization or not stylized_folder_path:
print("[INFO] Preparing STL10 DexiNed data loader.")
test_dataset = DualDataset(
DexiNedTestDataset(
csv_file="./Edge_images/Dexi/test/labels.csv",
root_dir="./Edge_images/Dexi/test",
transform=transforms.ToTensor(),
),
STL10(root=data_root, split="test", transform=transforms.ToTensor()),
)
else:
print("[INFO] Preparing stylized STL10 DexiNed data loader.")
test_dataset = DualDataset(
DexiNedStylizedTestDataset(
csv_file="./Edge_images/Dexi_stylized/labels.csv",
root_dir="./Edge_images/Dexi_stylized",
transform=transforms.ToTensor(),
),
StylizedSTL10Dataset(
source_dir=stylized_folder_path,
model_type=model_type,
),
)
test_loader = DataLoader(
test_dataset,
batch_size=batch_size,
num_workers=10,
drop_last=False,
shuffle=shuffle,
)
return test_loader
# FGSM attack code
def fgsm_attack(image, epsilon, data_grad, has_canny=False):
# Collect the element-wise sign of the data gradient
sign_data_grad = data_grad.sign()
# Create the perturbed image by adjusting each pixel of the input image
perturbed_image = image + epsilon * sign_data_grad
# Adding clipping to maintain [0,1] range
if has_canny:
perturbed_image = torch.stack(
[
torch.clamp(perturbed_image[0], 0, 255),
torch.clamp(perturbed_image[1], 0, 1),
]
)
else:
perturbed_image = torch.clamp(perturbed_image, 0, 1)
# Return the perturbed image
return perturbed_image
def test_adversarial(
model, criterion, device, test_loader, epsilon, model_type=ModelType.SIMCLR
):
# Accuracy counter
correct = 0
adv_examples = []
# Important: Set model to eval(), otherwise the dropout might cause the result inaccurate
model.eval()
# Loop over all examples in test set
for data_and_target in tqdm(test_loader):
# Send the data and label to the device
if model_type == ModelType.VIT:
data, target = data_and_target
data = data["pixel_values"][0]
elif (
model_type == ModelType.MORPHCLRSINGLE
or model_type == ModelType.MORPHCLRDUAL
):
edge_data, non_edge_data, target = data_and_target
if len(edge_data.shape) == 3:
edge_data = edge_data.unsqueeze(1)
# If the image is of grayscale, repeat the dimension to create 3 channels
if edge_data.shape[1] == 1:
edge_data = edge_data.repeat(1, 3, 1, 1)
data = torch.stack([edge_data, non_edge_data], dim=0)
target = target.flatten()
else:
data, target = data_and_target
data, target = data.to(device), target.to(device)
# Set requires_grad attribute of tensor. Important for Attack
data.requires_grad = True
# Forward pass the data through the model
output = model(data)
init_pred = output.max(1, keepdim=True)[
1
] # get the index of the max log-probability
# If the initial prediction is wrong, dont bother attacking, just move on
if init_pred.item() != target.item():
continue
# Calculate the loss
loss = criterion(output, target)
# Zero all existing gradients
model.zero_grad()
# Calculate gradients of model in backward pass
loss.backward()
# Collect datagrad
data_grad = data.grad.data
# Call FGSM Attack
has_canny = (
type(test_loader.dataset) == DualDataset
and type(test_loader.dataset.dataset1) == CannyDataset
)
perturbed_data = fgsm_attack(data, epsilon, data_grad, has_canny)
# Re-classify the perturbed image
output = model(perturbed_data)
# Check for success
final_pred = output.max(1, keepdim=True)[
1
] # get the index of the max log-probability
if final_pred.item() == target.item():
correct += 1
# Special case for saving 0 epsilon examples
if (epsilon == 0) and (len(adv_examples) < 5):
adv_ex = perturbed_data.squeeze().detach().cpu().numpy()
adv_examples.append((init_pred.item(), final_pred.item(), adv_ex))
else:
# Save some adv examples for visualization later
if len(adv_examples) < 5:
adv_ex = perturbed_data.squeeze().detach().cpu().numpy()
adv_examples.append((init_pred.item(), final_pred.item(), adv_ex))
# Calculate final accuracy for this epsilon
final_acc = correct / float(len(test_loader))
print(
"Epsilon: {}\tTest Accuracy = {} / {} = {}".format(
epsilon, correct, len(test_loader), final_acc
)
)
# Return the accuracy and an adversarial example
return final_acc, adv_examples
def compute_adversarial_accuracies(
adv_epsilons,
model,
device,
test_loader,
model_name,
model_type=ModelType.SIMCLR,
):
print("[INFO] Computing adversarial accuracies and epsilons.")
result_root = "./adv_results"
if not os.path.exists(result_root):
os.makedirs(result_root)
adv_accuracies = []
adv_examples = []
criterion = torch.nn.CrossEntropyLoss().to(device)
with open(os.path.join(result_root, "{}.csv".format(model_name)), "a") as f:
f.write("eps,acc\n")
# Run test for each epsilon
for eps in adv_epsilons:
acc, ex = test_adversarial(
model, criterion, device, test_loader, eps, model_type
)
adv_accuracies.append(acc)
adv_examples.append(ex)
with open(os.path.join(result_root, "{}.csv".format(model_name)), "a") as f:
f.write("{},{}\n".format(eps, acc))
plt.figure(figsize=(5, 5))
plt.plot(adv_epsilons, adv_accuracies, "*-")
plt.yticks(np.arange(0, 1.1, step=0.1))
plt.xticks(np.arange(0, 0.06, step=0.01))
plt.title("Accuracy vs Epsilon")
plt.xlabel("Epsilon")
plt.ylabel("Accuracy")
plt.savefig(
os.path.join(result_root, "{}.png".format(model_name)), bbox_inches="tight"
)
return adv_accuracies, adv_examples
def main():
args = parser.parse_args()
# Check if GPU is available
if not args.disable_cuda and torch.cuda.is_available():
args.device = torch.device("cuda:" + str(args.gpu_index))
cudnn.deterministic = True
cudnn.benchmark = True
else:
args.device = torch.device("cpu")
# Set model type
if args.model_type == "simclr":
model_type = ModelType.SIMCLR
elif args.model_type == "morphclr_single":
model_type = ModelType.MORPHCLRSINGLE
elif args.model_type == "morphclr_dual":
model_type = ModelType.MORPHCLRDUAL
else:
model_type = ModelType.VIT
# print("[INFO] Downloading Stylized STL10")
# file_id = "1aTVhLVG1pbsFWmoV-KoYB00YSPCSqyEv"
# gdrive_url = "https://drive.google.com/uc?id={}".format(file_id)
stylized_folder_name = "inter_class_stylized_dataset"
stylized_folder_path = "stylization/{}".format(stylized_folder_name)
# zip_name = stylized_folder_name + ".zip"
# gdown.download(gdrive_url, zip_name, quiet=False)
# shutil.unpack_archive(zip_name, stylized_folder_path)
# os.remove(zip_name)
print("[INFO] Starting evaluation...")
model_path = os.path.join("./checkpoints/finetune/", args.checkpoint)
print("[INFO] CUDA Device: {}".format(args.device))
print("[INFO] Using fine-tuned checkpoint: {}".format(model_path))
if model_type == ModelType.SIMCLR:
model = torchvision.models.resnet18(pretrained=False, num_classes=10).to(
args.device
)
checkpoint = torch.load(model_path)
model.load_state_dict(checkpoint["state_dict"])
get_dataloader_fn = get_stl10_data_loader
elif model_type == ModelType.MORPHCLRSINGLE:
model = MorphCLRSingleEval(base_model="resnet18")
checkpoint = torch.load(model_path)
model.load_state_dict(checkpoint["state_dict"])
if args.dataset_name.endswith("canny"):
get_dataloader_fn = get_stl10_canny_dual_data_loader
elif args.dataset_name.endswith("dexined"):
get_dataloader_fn = get_stl10_dexined_dual_data_loader
else:
raise Exception("Cannot use standard STL10 dataset for MorphCLR models.")
elif model_type == ModelType.MORPHCLRDUAL:
model = MorphCLRDualEval(base_model="resnet18")
checkpoint = torch.load(model_path)
model.load_state_dict(checkpoint["state_dict"])
if args.dataset_name.endswith("canny"):
get_dataloader_fn = get_stl10_canny_dual_data_loader
elif args.dataset_name.endswith("dexined"):
get_dataloader_fn = get_stl10_dexined_dual_data_loader
else:
raise Exception("Cannot use standard STL10 dataset for MorphCLR models.")
else:
# Hardcoded VIT checkpoint path
model = VIT_pretrained(model_path, device=args.device)
get_dataloader_fn = get_stl10_data_loader
model = model.to(args.device)
model = model.eval()
print("[INFO] Model checkpoint loaded.")
# Evaluate for standard STL10 accuracies
test = get_dataloader_fn(
data_root=args.data,
download=True,
batch_size=args.batch_size,
model_type=model_type,
)
stl10_accuracies = compute_accuracies_local(
model=model,
device=args.device,
data_loader=test,
model_name=args.checkpoint.split(".")[0],
model_type=model_type,
)
# Evaluate for stylized STL10 accuracies
stylized_loader = get_dataloader_fn(
data_root=args.data,
download=False,
stylization=True,
stylized_folder_path=stylized_folder_path,
batch_size=args.batch_size,
model_type=model_type,
)
stl10_stylized_metrics = compute_accuracy_and_ratio(
model=model,
device=args.device,
data_loader=stylized_loader,
model_name=args.checkpoint.split(".")[0],
model_type=model_type,
)
# Evaluate for adversarial accuracies
test = get_dataloader_fn(
data_root=args.data, download=False, batch_size=1, model_type=model_type
)
compute_adversarial_accuracies(
adv_epsilons=[0, 0.01, 0.02, 0.03, 0.04, 0.05],
model=model,
device=args.device,
test_loader=test,
model_name=args.checkpoint.split(".")[0],
model_type=model_type,
)
if __name__ == "__main__":
main()