-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcertify_mnist_band_top_one.py
102 lines (80 loc) · 3.42 KB
/
certify_mnist_band_top_one.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
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
import torchvision
import torchvision.transforms as transforms
import os
import argparse
import utils_band_top_one as utils
from prog_bar import progress_bar
parser = argparse.ArgumentParser(description='PyTorch MNIST Certification')
parser.add_argument('--band_size', default=4, type=int, help='size of each smoothing band')
parser.add_argument('--size_to_certify', default=5, type=int, help='size_to_certify')
parser.add_argument('--checkpoint', help='checkpoint')
parser.add_argument('--test', action='store_true', help='Use test set (vs validation)')
args = parser.parse_args()
device = 'cuda' if torch.cuda.is_available() else 'cpu'
transform_test = transforms.Compose([
transforms.ToTensor(),
# transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
val_indices = torch.load('validation.t7')
testset = torchvision.datasets.MNIST(root='./data', train=False, download=True, transform=transform_test)
if (args.test):
val_indices = list(set(range(len(testset))) - set(val_indices.numpy().tolist()))
testloader = torch.utils.data.DataLoader(torch.utils.data.Subset(testset,val_indices), batch_size=100, shuffle=False, num_workers=2)
# Model
print('==> Building model..')
checkpoint_dir = 'checkpoints'
if not os.path.exists('./checkpoints'):
os.makedirs('./checkpoints')
class Flatten(nn.Module):
def forward(self, x):
return x.reshape(x.size(0), -1)
net = nn.Sequential(
nn.Conv2d(2, 64, 4, stride=2, padding=1),
nn.ReLU(),
nn.Conv2d(64, 128, 4, stride=2, padding=1),
nn.ReLU(),
Flatten(),
nn.Linear(128*7*7,500),
nn.ReLU(),
nn.Linear(500,100),
nn.ReLU(),
nn.Linear(100, 10)
)
net = net.to(device)
if device == 'cuda':
net = torch.nn.DataParallel(net)
cudnn.benchmark = True
print('==> Resuming from checkpoint..')
#assert os.path.isdir(checkpoint_dir), 'Error: no checkpoint directory found!'
resume_file = '{}/{}'.format(checkpoint_dir, args.checkpoint)
assert os.path.isfile(resume_file)
checkpoint = torch.load(resume_file)
net.load_state_dict(checkpoint['net'])
net.eval()
def test():
global best_acc
correct = 0
cert_correct = 0
cert_incorrect = 0
total = 0
with torch.no_grad():
for batch_idx, (inputs, targets) in enumerate(testloader):
inputs, targets = inputs.to(device), targets.to(device)
total += targets.size(0)
predictions, certyn = utils.predict_and_certify(inputs, net,args.band_size, args.size_to_certify, 10)
correct += (predictions.eq(targets)).sum().item()
cert_correct += (predictions.eq(targets) & certyn).sum().item()
cert_incorrect += (~predictions.eq(targets) & certyn).sum().item()
# progress_bar(batch_idx, len(testloader), 'Acc: %.3f%% (%d/%d) Cert: %.3f%% (%d/%d)'
# % ((100.*correct)/total, correct, total, (100.*cert_correct)/total, cert_correct, total))
print('Using band size ' + str(args.band_size))
print('Total images: ' + str(total))
print('Correct: ' + str(correct) + ' (' + str((100.*correct)/total)+'%)')
print('Certified Correct class: ' + str(cert_correct) + ' (' + str((100.*cert_correct)/total)+'%)')
print('Certified Wrong class: ' + str(cert_incorrect) + ' (' + str((100.*cert_incorrect)/total)+'%)')
test()