-
Notifications
You must be signed in to change notification settings - Fork 27
/
osr.py
233 lines (194 loc) · 9.75 KB
/
osr.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
import os
import argparse
import datetime
import time
import csv
import pandas as pd
import importlib
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.optim import lr_scheduler
import torch.multiprocessing as mp
import torch.backends.cudnn as cudnn
from models import gan
from models.models import classifier32, classifier32ABN
from datasets.osr_dataloader import MNIST_OSR, CIFAR10_OSR, CIFAR100_OSR, SVHN_OSR, Tiny_ImageNet_OSR
from utils import Logger, save_networks, load_networks
from core import train, train_cs, test
parser = argparse.ArgumentParser("Training")
# Dataset
parser.add_argument('--dataset', type=str, default='mnist', help="mnist | svhn | cifar10 | cifar100 | tiny_imagenet")
parser.add_argument('--dataroot', type=str, default='./data')
parser.add_argument('--outf', type=str, default='./log')
parser.add_argument('--out-num', type=int, default=50, help='For CIFAR100')
# optimization
parser.add_argument('--batch-size', type=int, default=64)
parser.add_argument('--lr', type=float, default=0.1, help="learning rate for model")
parser.add_argument('--gan_lr', type=float, default=0.0002, help="learning rate for gan")
parser.add_argument('--max-epoch', type=int, default=100)
parser.add_argument('--stepsize', type=int, default=30)
parser.add_argument('--temp', type=float, default=1.0, help="temp")
parser.add_argument('--num-centers', type=int, default=1)
# model
parser.add_argument('--weight-pl', type=float, default=0.1, help="weight for center loss")
parser.add_argument('--beta', type=float, default=0.1, help="weight for entropy loss")
parser.add_argument('--model', type=str, default='classifier32')
# misc
parser.add_argument('--nz', type=int, default=100)
parser.add_argument('--ns', type=int, default=1)
parser.add_argument('--eval-freq', type=int, default=1)
parser.add_argument('--print-freq', type=int, default=100)
parser.add_argument('--gpu', type=str, default='0')
parser.add_argument('--seed', type=int, default=0)
parser.add_argument('--use-cpu', action='store_true')
parser.add_argument('--save-dir', type=str, default='../log')
parser.add_argument('--loss', type=str, default='ARPLoss')
parser.add_argument('--eval', action='store_true', help="Eval", default=False)
parser.add_argument('--cs', action='store_true', help="Confusing Sample", default=False)
def main_worker(options):
torch.manual_seed(options['seed'])
os.environ['CUDA_VISIBLE_DEVICES'] = options['gpu']
use_gpu = torch.cuda.is_available()
if options['use_cpu']: use_gpu = False
if use_gpu:
print("Currently using GPU: {}".format(options['gpu']))
cudnn.benchmark = True
torch.cuda.manual_seed_all(options['seed'])
else:
print("Currently using CPU")
# Dataset
print("{} Preparation".format(options['dataset']))
if 'mnist' in options['dataset']:
Data = MNIST_OSR(known=options['known'], dataroot=options['dataroot'], batch_size=options['batch_size'], img_size=options['img_size'])
trainloader, testloader, outloader = Data.train_loader, Data.test_loader, Data.out_loader
elif 'cifar10' == options['dataset']:
Data = CIFAR10_OSR(known=options['known'], dataroot=options['dataroot'], batch_size=options['batch_size'], img_size=options['img_size'])
trainloader, testloader, outloader = Data.train_loader, Data.test_loader, Data.out_loader
elif 'svhn' in options['dataset']:
Data = SVHN_OSR(known=options['known'], dataroot=options['dataroot'], batch_size=options['batch_size'], img_size=options['img_size'])
trainloader, testloader, outloader = Data.train_loader, Data.test_loader, Data.out_loader
elif 'cifar100' in options['dataset']:
Data = CIFAR10_OSR(known=options['known'], dataroot=options['dataroot'], batch_size=options['batch_size'], img_size=options['img_size'])
trainloader, testloader = Data.train_loader, Data.test_loader
out_Data = CIFAR100_OSR(known=options['unknown'], dataroot=options['dataroot'], batch_size=options['batch_size'], img_size=options['img_size'])
outloader = out_Data.test_loader
else:
Data = Tiny_ImageNet_OSR(known=options['known'], dataroot=options['dataroot'], batch_size=options['batch_size'], img_size=options['img_size'])
trainloader, testloader, outloader = Data.train_loader, Data.test_loader, Data.out_loader
options['num_classes'] = Data.num_classes
# Model
print("Creating model: {}".format(options['model']))
if options['cs']:
net = classifier32ABN(num_classes=options['num_classes'])
else:
net = classifier32(num_classes=options['num_classes'])
feat_dim = 128
if options['cs']:
print("Creating GAN")
nz, ns = options['nz'], 1
if 'tiny_imagenet' in options['dataset']:
netG = gan.Generator(1, nz, 64, 3)
netD = gan.Discriminator(1, 3, 64)
else:
netG = gan.Generator32(1, nz, 64, 3)
netD = gan.Discriminator32(1, 3, 64)
fixed_noise = torch.FloatTensor(64, nz, 1, 1).normal_(0, 1)
criterionD = nn.BCELoss()
# Loss
options.update(
{
'feat_dim': feat_dim,
'use_gpu': use_gpu
}
)
Loss = importlib.import_module('loss.'+options['loss'])
criterion = getattr(Loss, options['loss'])(**options)
if use_gpu:
net = nn.DataParallel(net).cuda()
criterion = criterion.cuda()
if options['cs']:
netG = nn.DataParallel(netG, device_ids=[i for i in range(len(options['gpu'].split(',')))]).cuda()
netD = nn.DataParallel(netD, device_ids=[i for i in range(len(options['gpu'].split(',')))]).cuda()
fixed_noise.cuda()
model_path = os.path.join(options['outf'], 'models', options['dataset'])
if not os.path.exists(model_path):
os.makedirs(model_path)
if options['dataset'] == 'cifar100':
model_path += '_50'
file_name = '{}_{}_{}_{}_{}'.format(options['model'], options['loss'], 50, options['item'], options['cs'])
else:
file_name = '{}_{}_{}_{}'.format(options['model'], options['loss'], options['item'], options['cs'])
if options['eval']:
net, criterion = load_networks(net, model_path, file_name, criterion=criterion)
results = test(net, criterion, testloader, outloader, epoch=0, **options)
print("Acc (%): {:.3f}\t AUROC (%): {:.3f}\t OSCR (%): {:.3f}\t".format(results['ACC'], results['AUROC'], results['OSCR']))
return results
params_list = [{'params': net.parameters()},
{'params': criterion.parameters()}]
if options['dataset'] == 'tiny_imagenet':
optimizer = torch.optim.Adam(params_list, lr=options['lr'])
else:
optimizer = torch.optim.SGD(params_list, lr=options['lr'], momentum=0.9, weight_decay=1e-4)
if options['cs']:
optimizerD = torch.optim.Adam(netD.parameters(), lr=options['gan_lr'], betas=(0.5, 0.999))
optimizerG = torch.optim.Adam(netG.parameters(), lr=options['gan_lr'], betas=(0.5, 0.999))
if options['stepsize'] > 0:
scheduler = lr_scheduler.MultiStepLR(optimizer, milestones=[30,60,90,120])
start_time = time.time()
for epoch in range(options['max_epoch']):
print("==> Epoch {}/{}".format(epoch+1, options['max_epoch']))
if options['cs']:
train_cs(net, netD, netG, criterion, criterionD,
optimizer, optimizerD, optimizerG,
trainloader, epoch=epoch, **options)
train(net, criterion, optimizer, trainloader, epoch=epoch, **options)
if options['eval_freq'] > 0 and (epoch+1) % options['eval_freq'] == 0 or (epoch+1) == options['max_epoch']:
print("==> Test", options['loss'])
results = test(net, criterion, testloader, outloader, epoch=epoch, **options)
print("Acc (%): {:.3f}\t AUROC (%): {:.3f}\t OSCR (%): {:.3f}\t".format(results['ACC'], results['AUROC'], results['OSCR']))
save_networks(net, model_path, file_name, criterion=criterion)
if options['stepsize'] > 0: scheduler.step()
elapsed = round(time.time() - start_time)
elapsed = str(datetime.timedelta(seconds=elapsed))
print("Finished. Total elapsed time (h:m:s): {}".format(elapsed))
return results
if __name__ == '__main__':
args = parser.parse_args()
options = vars(args)
options['dataroot'] = os.path.join(options['dataroot'], options['dataset'])
img_size = 32
results = dict()
from split import splits_2020 as splits
for i in range(len(splits[options['dataset']])):
known = splits[options['dataset']][len(splits[options['dataset']])-i-1]
if options['dataset'] == 'cifar100':
unknown = splits[options['dataset']+'-'+str(options['out_num'])][len(splits[options['dataset']])-i-1]
elif options['dataset'] == 'tiny_imagenet':
img_size = 64
options['lr'] = 0.001
unknown = list(set(list(range(0, 200))) - set(known))
else:
unknown = list(set(list(range(0, 10))) - set(known))
options.update(
{
'item': i,
'known': known,
'unknown': unknown,
'img_size': img_size
}
)
dir_name = '{}_{}'.format(options['model'], options['loss'])
dir_path = os.path.join(options['outf'], 'results', dir_name)
if not os.path.exists(dir_path):
os.makedirs(dir_path)
if options['dataset'] == 'cifar100':
file_name = '{}_{}.csv'.format(options['dataset'], options['out_num'])
else:
file_name = options['dataset'] + '.csv'
res = main_worker(options)
res['unknown'] = unknown
res['known'] = known
results[str(i)] = res
df = pd.DataFrame(results)
df.to_csv(os.path.join(dir_path, file_name))