-
Notifications
You must be signed in to change notification settings - Fork 5
/
example.py
209 lines (180 loc) · 9.19 KB
/
example.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
# Code reused from: https://github.com/kuangliu/pytorch-cifar
import torch
import torch.backends.cudnn as cudnn
import torchvision
import torchvision.transforms as transforms
import os
import net
import losses
import tools
from torchmetrics import AUROC
import random
import numpy
import torchnet as tnt
base_seed = 42
random.seed(base_seed)
numpy.random.seed(base_seed)
torch.manual_seed(base_seed)
torch.cuda.manual_seed(base_seed)
cudnn.benchmark = False
cudnn.deterministic = True
device = 'cuda' if torch.cuda.is_available() else 'cpu'
best_acc = 0 # best test accuracy
start_epoch = 1 # start from epoch one
# Data
print('==> Preparing data...')
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.507, 0.486, 0.440), (0.267, 0.256, 0.276)),])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.507, 0.486, 0.440), (0.267, 0.256, 0.276)),])
trainset = torchvision.datasets.CIFAR100(root='data/cifar100', train=True, download=True, transform=transform_train)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True, num_workers=0, worker_init_fn=lambda worker_id: random.seed(base_seed + worker_id))
testset = torchvision.datasets.CIFAR100(root='data/cifar100', train=False, download=True, transform=transform_test)
testloader = torch.utils.data.DataLoader(testset, batch_size=64, shuffle=False)
# Model
print('==> Building model...')
model = net.ResNet34(num_c=100)
model = model.to(device)
#########################################################
#criterion = nn.CrossEntropyLoss()
criterion = losses.DisMaxLossSecondPart(model.classifier)
#########################################################
optimizer = torch.optim.SGD(model.parameters(), lr=0.1, momentum=0.9, nesterov=True, weight_decay=1*1e-4)
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[150, 200, 250], gamma=0.1)
def train(epoch):
print('Epoch: %d' % epoch)
model.train()
train_loss = 0
correct = 0
total = 0
for batch_idx, (inputs, targets) in enumerate(trainloader):
inputs, targets = inputs.to(device), targets.to(device)
optimizer.zero_grad()
#######################################################
inputs, targets = criterion.preprocess(inputs, targets)
#######################################################
outputs = model(inputs)
loss = criterion(outputs, targets)
loss.backward()
optimizer.step()
train_loss += loss.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
tools.progress_bar(batch_idx, len(trainloader), 'Loss: %.4f | Acc: %.4f%% (%d/%d)'
% (train_loss/(batch_idx+1), 100.*correct/total, correct, total))
def test(epoch):
global best_acc
model.eval()
test_loss = 0
correct = 0
total = 0
with torch.no_grad():
for batch_idx, (inputs, targets) in enumerate(testloader):
inputs, targets = inputs.to(device), targets.to(device)
outputs = model(inputs)
loss = criterion(outputs, targets)
test_loss += loss.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
tools.progress_bar(batch_idx, len(testloader), 'Loss: %.4f | Acc: %.4f%% (%d/%d)'
% (test_loss/(batch_idx+1), 100.*correct/total, correct, total))
# Save checkpoint
acc = 100.*correct/total
if acc > best_acc:
print('Saving...')
state = {
'model': model.state_dict(),
'acc': acc,
'epoch': epoch,}
if not os.path.isdir('checkpoint'):
os.mkdir('checkpoint')
torch.save(state, 'checkpoint/ckpt.pth')
best_acc = acc
def detect(inloader, oodloader):
auroc = AUROC(pos_label=1)
auroctnt = tnt.meter.AUCMeter()
model.eval()
with torch.no_grad():
for _, (inputs, targets) in enumerate(inloader):
inputs, targets = inputs.to(device), targets.to(device)
targets.fill_(1)
outputs = model(inputs)
#probabilities = torch.nn.Softmax(dim=1)(outputs)
#score = probabilities.max(dim=1)[0] # this is the maximum probability score
#entropies = -(probabilities * torch.log(probabilities)).sum(dim=1)
#score = -entropies # this is the negative entropy score
# the negative entropy score is the best option for the IsoMax loss
# outputs are equal to logits, which in turn are equivalent to negative distances
#score = outputs.max(dim=1)[0] # this is the minimum distance score
# the minimum distance score is the best option for the IsoMax+ loss
#############################################################################################################################
scores = model.classifier.scores(outputs) # this returns the score considering the score type defined in the model.classifier
#############################################################################################################################
auroc.update(scores, targets)
auroctnt.add(scores, targets)
for _, (inputs, targets) in enumerate(oodloader):
inputs, targets = inputs.to(device), targets.to(device)
targets.fill_(0)
outputs = model(inputs)
#probabilities = torch.nn.Softmax(dim=1)(outputs)
#score = probabilities.max(dim=1)[0] # this is the maximum probability score
#entropies = -(probabilities * torch.log(probabilities)).sum(dim=1)
#score = -entropies # this is the negative entropy score
# the negative entropy score is the best option for the IsoMax loss
# outputs are equal to logits, which in turn are equivalent to negative distances
#score = outputs.max(dim=1)[0] # this is the minimum distance score
# the minimum distance score is the best option for the IsoMax+ loss
#############################################################################################################################
scores = model.classifier.scores(outputs) # this returns the score considering the score type defined in the model.classifier
#############################################################################################################################
auroc.update(scores, targets)
auroctnt.add(scores, targets)
return auroc.compute(), auroctnt.value()[0]
total_epochs = 300
for epoch in range(start_epoch, start_epoch + total_epochs):
print()
for param_group in optimizer.param_groups:
print("LEARNING RATE: ", param_group["lr"])
train(epoch)
test(epoch)
scheduler.step()
checkpoint = torch.load('checkpoint/ckpt.pth')
model.load_state_dict(checkpoint['model'])
test_acc = checkpoint['acc']
print()
print("###################################################")
print("Test Accuracy (%): {0:.4f}".format(test_acc))
print("###################################################")
print()
dataroot = os.path.expanduser(os.path.join('data', 'Imagenet_resize'))
oodset = torchvision.datasets.ImageFolder(dataroot, transform=transform_test)
oodloader = torch.utils.data.DataLoader(oodset, batch_size=64, shuffle=False, num_workers=4)
auroc = detect(testloader, oodloader)
print()
print("#################################################################################################################")
print("Detection performance for ImageNet Resize as Out-of-Distribution [AUROC] (%): {0:.4f}".format(100. * auroc[0].item()), auroc[1])
print("#################################################################################################################")
print()
dataroot = os.path.expanduser(os.path.join('data', 'LSUN_resize'))
oodset = torchvision.datasets.ImageFolder(dataroot, transform=transform_test)
oodloader = torch.utils.data.DataLoader(oodset, batch_size=64, shuffle=False, num_workers=4)
auroc = detect(testloader, oodloader)
print()
print("#################################################################################################################")
print("Detection performance for LSUN Resize as Out-of-Distribution [AUROC] (%): {0:.4f}".format(100. * auroc[0].item()), auroc[1])
print("#################################################################################################################")
print()
oodset = torchvision.datasets.SVHN(root='data/svhn', split="test", download=True, transform=transform_test)
oodloader = torch.utils.data.DataLoader(oodset, batch_size=64, shuffle=False, num_workers=4)
auroc = detect(testloader, oodloader)
print()
print("#################################################################################################################")
print("Detection performance for SVHN as Out-of-Distribution [AUROC] (%): {0:.4f}".format(100. * auroc[0].item()), auroc[1])
print("#################################################################################################################")
print()