-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathproser_unknown_detection.py
297 lines (266 loc) · 12.7 KB
/
proser_unknown_detection.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
import argparse
import os
import os.path as osp
from copy import deepcopy
import matplotlib.pyplot as plt
import numpy as np
import torch
import torch.backends.cudnn as cudnn
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
from sklearn.metrics import roc_auc_score
from torch.optim.lr_scheduler import MultiStepLR
from models import *
from utils import ensure_path, progress_bar
from models.utils import pprint, set_gpu, ensure_path, Averager, Timer, count_acc, compute_confidence_interval, one_hot,Identity
from torch.distributions import Categorical
import copy
import random
def traindummy(epoch,net):
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=args.lr*0.01,momentum=0.9, weight_decay=5e-4)
print('\nEpoch: %d' % epoch)
net.train()
train_loss = 0
correct = 0
total = 0
alpha=args.alpha
for batch_idx, (inputs, targets) in enumerate(trainloader):
inputs, targets = inputs.to(device), targets.to(device)
optimizer.zero_grad()
totallenth=len(inputs)
halflenth=int(len(inputs)/2)
beta=torch.distributions.beta.Beta(alpha, alpha).sample([]).item()
prehalfinputs=inputs[:halflenth]
prehalflabels=targets[:halflenth]
laterhalfinputs=inputs[halflenth:]
laterhalflabels=targets[halflenth:]
index = torch.randperm(prehalfinputs.size(0)).cuda()
pre2embeddings=pre2block(net,prehalfinputs)
mixed_embeddings = beta * pre2embeddings + (1 - beta) * pre2embeddings[index]
dummylogit=dummypredict(net,laterhalfinputs)
lateroutputs=net(laterhalfinputs)
latterhalfoutput=torch.cat((lateroutputs,dummylogit),1)
prehalfoutput=torch.cat((latter2blockclf1(net,mixed_embeddings),latter2blockclf2(net,mixed_embeddings)),1)
maxdummy,_=torch.max(dummylogit.clone(),dim=1)
maxdummy=maxdummy.view(-1,1)
dummpyoutputs=torch.cat((lateroutputs.clone(),maxdummy),dim=1)
for i in range(len(dummpyoutputs)):
nowlabel=laterhalflabels[i]
dummpyoutputs[i][nowlabel]=-1e9
dummytargets=torch.ones_like(laterhalflabels)*args.known_class
outputs=torch.cat((prehalfoutput,latterhalfoutput),0)
loss1= criterion(prehalfoutput, (torch.ones_like(prehalflabels)*args.known_class).long().cuda())
loss2=criterion(latterhalfoutput,laterhalflabels )
loss3= criterion(dummpyoutputs, dummytargets)
loss=0.01*loss1+args.lamda1*loss2+args.lamda2*loss3
loss.backward()
optimizer.step()
train_loss += loss.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
if args.shmode==False:
progress_bar(batch_idx, len(trainloader), 'Loss: %.3f | Acc: %.3f%% (%d/%d) L1 %.3f, L2 %.3f'\
% (train_loss/(batch_idx+1), 100.*correct/total, correct, total , loss1.item(), loss2.item(), ))
def valdummy(epoch,net,mainepoch):
net.eval()
CONF_AUC=False
CONF_DeltaP=True
auclist1=[]
auclist2=[]
linspace=[0]
closelogits=torch.zeros((len(closeset),args.known_class+1)).cuda()
openlogits=torch.zeros((len(openset),args.known_class+1)).cuda()
with torch.no_grad():
for batch_idx, (inputs, targets) in enumerate(closerloader):
inputs, targets = inputs.to(device), targets.to(device)
batchnum=len(targets)
logits=net(inputs)
dummylogit=dummypredict(net,inputs)
maxdummylogit,_=torch.max(dummylogit,1)
maxdummylogit=maxdummylogit.view(-1,1)
totallogits=torch.cat((logits,maxdummylogit),dim=1)
closelogits[batch_idx*batchnum:batch_idx*batchnum+batchnum,:]=totallogits
for batch_idx, (inputs, targets) in enumerate(openloader):
inputs, targets = inputs.to(device), targets.to(device)
batchnum=len(targets)
logits=net(inputs)
dummylogit=dummypredict(net,inputs)
maxdummylogit,_=torch.max(dummylogit,1)
maxdummylogit=maxdummylogit.view(-1,1)
totallogits=torch.cat((logits,maxdummylogit),dim=1)
openlogits[batch_idx*batchnum:batch_idx*batchnum+batchnum,:]=totallogits
Logitsbatchsize=200
maxauc=0
maxaucbias=0
for biasitem in linspace:
if CONF_AUC:
for temperature in [1024.0]:
closeconf=[]
openconf=[]
closeiter=int(len(closelogits)/Logitsbatchsize)
openiter=int(len(openlogits)/Logitsbatchsize)
for batch_idx in range(closeiter):
logitbatch=closelogits[batch_idx*Logitsbatchsize:batch_idx*Logitsbatchsize+Logitsbatchsize,:]
logitbatch[:,-1]=logitbatch[:,-1]+biasitem
embeddings=nn.functional.softmax(logitbatch/temperature,dim=1)
conf=embeddings[:,-1]
closeconf.append(conf.cpu().numpy())
closeconf=np.reshape(np.array(closeconf),(-1))
closelabel=np.ones_like(closeconf)
for batch_idx in range(openiter):
logitbatch=openlogits[batch_idx*Logitsbatchsize:batch_idx*Logitsbatchsize+Logitsbatchsize,:]
logitbatch[:,-1]=logitbatch[:,-1]+biasitem
embeddings=nn.functional.softmax(logitbatch/temperature,dim=1)
conf=embeddings[:,-1]
openconf.append(conf.cpu().numpy())
openconf=np.reshape(np.array(openconf),(-1))
openlabel=np.zeros_like(openconf)
totalbinary=np.hstack([closelabel,openlabel])
totalconf=np.hstack([closeconf,openconf])
auc1=roc_auc_score(1-totalbinary,totalconf)
auc2=roc_auc_score(totalbinary,totalconf)
print('Temperature:',temperature,'bias',biasitem,'AUC_by_confidence',auc2)
auclist1.append(np.max([auc1,auc2]))
if CONF_DeltaP:
for temperature in [1024.0]:
closeconf=[]
openconf=[]
closeiter=int(len(closelogits)/Logitsbatchsize)
openiter=int(len(openlogits)/Logitsbatchsize)
for batch_idx in range(closeiter):
logitbatch=closelogits[batch_idx*Logitsbatchsize:batch_idx*Logitsbatchsize+Logitsbatchsize,:]
logitbatch[:,-1]=logitbatch[:,-1]+biasitem
embeddings=nn.functional.softmax(logitbatch/temperature,dim=1)
dummyconf=embeddings[:,-1].view(-1,1)
maxknownconf,_=torch.max(embeddings[:,:-1],dim=1)
maxknownconf=maxknownconf.view(-1,1)
conf=dummyconf-maxknownconf
closeconf.append(conf.cpu().numpy())
closeconf=np.reshape(np.array(closeconf),(-1))
closelabel=np.ones_like(closeconf)
for batch_idx in range(openiter):
logitbatch=openlogits[batch_idx*Logitsbatchsize:batch_idx*Logitsbatchsize+Logitsbatchsize,:]
logitbatch[:,-1]=logitbatch[:,-1]+biasitem
embeddings=nn.functional.softmax(logitbatch/temperature,dim=1)
dummyconf=embeddings[:,-1].view(-1,1)
maxknownconf,_=torch.max(embeddings[:,:-1],dim=1)
maxknownconf=maxknownconf.view(-1,1)
conf=dummyconf-maxknownconf
openconf.append(conf.cpu().numpy())
openconf=np.reshape(np.array(openconf),(-1))
openlabel=np.zeros_like(openconf)
totalbinary=np.hstack([closelabel,openlabel])
totalconf=np.hstack([closeconf,openconf])
auc1=roc_auc_score(1-totalbinary,totalconf)
auc2=roc_auc_score(totalbinary,totalconf)
print('Temperature:',temperature,'bias',biasitem,'AUC_by_Delta_confidence',auc1)
auclist1.append(np.max([auc1,auc2]))
return np.max(np.array(auclist1))
def finetune_proser(epoch=59):
print('Now processing epoch',epoch)
net=getmodel(args)
print('==> Resuming from checkpoint..')
assert os.path.isdir(model_path), 'Error: no checkpoint directory found!'
modelname='Modelof_Epoch'+str(epoch)+'.pth'
checkpoint = torch.load(osp.join(model_path,save_path2,modelname))
net.load_state_dict(checkpoint['net'])
net.clf2=nn.Linear(640,args.dummynumber)
net=net.cuda()
FineTune_MAX_EPOCH=10
wholebestacc=0
for finetune_epoch in range(FineTune_MAX_EPOCH):
traindummy(finetune_epoch,net)
if (finetune_epoch+1)%10==0:
finetuneacc=valdummy(finetune_epoch,net,epoch)
return wholebestacc
def dummypredict(net,x):
if args.backbone=="WideResnet":
out = net.conv1(x)
out = net.layer1(out)
out = net.layer2(out)
out = net.layer3(out)
out = F.relu(net.bn1(out))
out = F.avg_pool2d(out, 8)
out = out.view(out.size(0), -1)
out = net.clf2(out)
return out
def pre2block(net,x):
if args.backbone=="WideResnet":
out = net.conv1(x)
out = net.layer1(out)
out = net.layer2(out)
return out
def latter2blockclf1(net,x):
if args.backbone=="WideResnet":
out = net.layer3(x)
out = F.relu(net.bn1(out))
out = F.avg_pool2d(out, 8)
out = out.view(out.size(0), -1)
out = net.linear(out)
return out
def latter2blockclf2(net,x):
if args.backbone=="WideResnet":
out = net.layer3(x)
out = F.relu(net.bn1(out))
out = F.avg_pool2d(out, 8)
out = out.view(out.size(0), -1)
out = net.clf2(out)
return out
def getmodel(args):
print('==> Building model..')
if args.backbone=='WideResnet':
net=Wide_ResNet(28, 10, 0.3, args.known_class)
net=net.cuda()
return net
if __name__=="__main__":
parser = argparse.ArgumentParser(description='PyTorch CIFAR10 Training')
parser.add_argument('--lr', default=0.1, type=float, help='learning rate')
parser.add_argument('--resume', '-r', action='store_true', help='resume from checkpoint')
parser.add_argument('--model_type', default='Proser', type=str, help='Recognition Method')
parser.add_argument('--backbone', default='WideResnet', type=str, help='Backbone type.')
parser.add_argument('--dataset', default='cifar10_relabel',type=str,help='dataset configuration')
parser.add_argument('--gpu', default='0',type=str,help='use gpu')
parser.add_argument('--known_class', default=6,type=int,help='number of known class')
parser.add_argument('--seed', default='9',type=int,help='random seed for dataset generation.')
parser.add_argument('--lamda1', default='1',type=float,help='trade-off between loss')
parser.add_argument('--lamda2', default='1',type=float,help='trade-off between loss')
parser.add_argument('--alpha', default='1',type=float,help='alpha value for beta distribution')
parser.add_argument('--dummynumber', default=1,type=int,help='number of dummy label.')
parser.add_argument('--shmode',action='store_true')
args = parser.parse_args()
pprint(vars(args))
os.environ['CUDA_VISIBLE_DEVICES'] =args.gpu
device = 'cuda' if torch.cuda.is_available() else 'cpu'
best_acc = 0
start_epoch = 0
print('==> Preparing data..')
if args.dataset=='cifar10_relabel':
from data.cifar10_relabel import CIFAR10 as Dataset
trainset=Dataset('train',seed=args.seed)
knownlist,unknownlist=trainset.known_class_show()
trainloader=torch.utils.data.DataLoader(trainset, batch_size=128, shuffle=True, num_workers=4)
closeset=Dataset('testclose',seed=args.seed)
closerloader=torch.utils.data.DataLoader(closeset, batch_size=500, shuffle=True, num_workers=4)
openset=Dataset('testopen',seed=args.seed)
openloader=torch.utils.data.DataLoader(openset, batch_size=500, shuffle=True, num_workers=4)
save_path1 = osp.join('results','D{}-M{}-B{}'.format(args.dataset,args.model_type, args.backbone,))
model_path = osp.join('results','D{}-M{}-B{}'.format(args.dataset,'softmax', args.backbone,))
save_path2 = 'LR{}-K{}-U{}-Seed{}'.format(str(args.lr), knownlist,unknownlist,str(args.seed))
args.save_path = osp.join(save_path1, save_path2)
ensure_path(save_path1, remove=False)
ensure_path(args.save_path, remove=False)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
torch.manual_seed(0)
torch.cuda.manual_seed_all(0)
np.random.seed(0)
random.seed(0)
torch.manual_seed(0)
torch.cuda.manual_seed(0)
globalacc=0
finetune_proser(59)