-
Notifications
You must be signed in to change notification settings - Fork 24
/
eval.py
313 lines (258 loc) · 12.8 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
import argparse
import os
import random
import time
import warnings
import numpy as np
import pprint
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.distributed as dist
import torch.optim
import torch.multiprocessing as mp
import torch.utils.data
import torch.utils.data.distributed
import torch.nn.functional as F
from datasets.cifar10 import CIFAR10_LT
from datasets.cifar100 import CIFAR100_LT
from datasets.places import Places_LT
from datasets.imagenet import ImageNet_LT
from datasets.ina2018 import iNa2018
from models import resnet
from models import resnet_places
from models import resnet_cifar
from utils import config, update_config, create_logger
from utils import AverageMeter, ProgressMeter
from utils import accuracy, calibration
from methods import LearnableWeightScaling
def parse_args():
parser = argparse.ArgumentParser(description='MiSLAS evaluation')
parser.add_argument('--cfg',
help='experiment configure file name',
required=True,
type=str)
parser.add_argument('opts',
help="Modify config options using the command-line",
default=None,
nargs=argparse.REMAINDER)
args = parser.parse_args()
update_config(config, args)
return args
best_acc1 = 0
def main():
args = parse_args()
logger, model_dir = create_logger(config, args.cfg)
logger.info('\n' + pprint.pformat(args))
logger.info('\n' + str(config))
if config.deterministic:
seed = 0
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
random.seed(seed)
np.random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
if config.gpu is not None:
warnings.warn('You have chosen a specific GPU. This will completely '
'disable data parallelism.')
if config.dist_url == "env://" and config.world_size == -1:
config.world_size = int(os.environ["WORLD_SIZE"])
config.distributed = config.world_size > 1 or config.multiprocessing_distributed
ngpus_per_node = torch.cuda.device_count()
if config.multiprocessing_distributed:
# Since we have ngpus_per_node processes per node, the total world_size
# needs to be adjusted accordingly
config.world_size = ngpus_per_node * config.world_size
# Use torch.multiprocessing.spawn to launch distributed processes: the
# main_worker process function
mp.spawn(main_worker, nprocs=ngpus_per_node, args=(ngpus_per_node, config, logger))
else:
# Simply call main_worker function
main_worker(config.gpu, ngpus_per_node, config, logger, model_dir)
def main_worker(gpu, ngpus_per_node, config, logger, model_dir):
global best_acc1
config.gpu = gpu
# start_time = time.strftime("%Y%m%d_%H%M%S", time.localtime())
if config.gpu is not None:
logger.info("Use GPU: {} for training".format(config.gpu))
if config.distributed:
if config.dist_url == "env://" and config.rank == -1:
config.rank = int(os.environ["RANK"])
if config.multiprocessing_distributed:
# For multiprocessing distributed training, rank needs to be the
# global rank among all the processes
config.rank = config.rank * ngpus_per_node + gpu
dist.init_process_group(backend=config.dist_backend, init_method=config.dist_url,
world_size=config.world_size, rank=config.rank)
if config.dataset == 'cifar10' or config.dataset == 'cifar100':
model = getattr(resnet_cifar, config.backbone)()
classifier = getattr(resnet_cifar, 'Classifier')(feat_in=64, num_classes=config.num_classes)
elif config.dataset == 'imagenet' or config.dataset == 'ina2018':
model = getattr(resnet, config.backbone)()
classifier = getattr(resnet, 'Classifier')(feat_in=2048, num_classes=config.num_classes)
elif config.dataset == 'places':
model = getattr(resnet_places, config.backbone)(pretrained=True)
classifier = getattr(resnet_places, 'Classifier')(feat_in=2048, num_classes=config.num_classes)
block = getattr(resnet_places, 'Bottleneck')(2048, 512, groups=1,
base_width=64, dilation=1,
norm_layer=nn.BatchNorm2d)
lws_model = LearnableWeightScaling(num_classes=config.num_classes)
if not torch.cuda.is_available():
logger.info('using CPU, this will be slow')
elif config.distributed:
# For multiprocessing distributed, DistributedDataParallel constructor
# should always set the single device scope, otherwise,
# DistributedDataParallel will use all available devices.
if config.gpu is not None:
torch.cuda.set_device(config.gpu)
model.cuda(config.gpu)
classifier.cuda(config.gpu)
lws_model.cuda(config.gpu)
# When using a single GPU per process and per
# DistributedDataParallel, we need to divide the batch size
# ourselves based on the total number of GPUs we have
config.batch_size = int(config.batch_size / ngpus_per_node)
config.workers = int((config.workers + ngpus_per_node - 1) / ngpus_per_node)
model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[config.gpu])
classifier = torch.nn.parallel.DistributedDataParallel(classifier, device_ids=[config.gpu])
lws_model = torch.nn.parallel.DistributedDataParallel(lws_model, device_ids=[config.gpu])
if config.dataset == 'places':
block.cuda(config.gpu)
block = torch.nn.parallel.DistributedDataParallel(block, device_ids=[config.gpu])
else:
model.cuda()
classifier.cuda()
lws_model.cuda()
# DistributedDataParallel will divide and allocate batch_size to all
# available GPUs if device_ids are not set
model = torch.nn.parallel.DistributedDataParallel(model)
classifier = torch.nn.parallel.DistributedDataParallel(classifier)
lws_model = torch.nn.parallel.DistributedDataParallel(lws_model)
if config.dataset == 'places':
block.cuda()
block = torch.nn.parallel.DistributedDataParallel(block)
elif config.gpu is not None:
torch.cuda.set_device(config.gpu)
model = model.cuda(config.gpu)
classifier = classifier.cuda(config.gpu)
lws_model = lws_model.cuda(config.gpu)
if config.dataset == 'places':
block.cuda(config.gpu)
else:
# DataParallel will divide and allocate batch_size to all available GPUs
model = torch.nn.DataParallel(model).cuda()
classifier = torch.nn.DataParallel(classifier).cuda()
lws_model = torch.nn.DataParallel(lws_model).cuda()
if config.dataset == 'places':
block = torch.nn.DataParallel(block).cuda()
# optionally resume from a checkpoint
if config.resume:
if os.path.isfile(config.resume):
logger.info("=> loading checkpoint '{}'".format(config.resume))
if config.gpu is None:
checkpoint = torch.load(config.resume)
else:
# Map model to be loaded to specified single gpu.
loc = 'cuda:{}'.format(config.gpu)
checkpoint = torch.load(config.resume, map_location=loc)
if config.gpu is not None:
# best_acc1 may be from a checkpoint from a different GPU
best_acc1 = best_acc1.to(config.gpu)
model.load_state_dict(checkpoint['state_dict_model'])
classifier.load_state_dict(checkpoint['state_dict_classifier'])
if config.dataset == 'places':
block.load_state_dict(checkpoint['state_dict_block'])
if config.mode == 'stage2':
lws_model.load_state_dict(checkpoint['state_dict_lws_model'])
logger.info("=> loaded checkpoint '{}' (epoch {})"
.format(config.resume, checkpoint['epoch']))
else:
logger.info("=> no checkpoint found at '{}'".format(config.resume))
# Data loading code
if config.dataset == 'cifar10':
dataset = CIFAR10_LT(config.distributed, root=config.data_path, imb_factor=config.imb_factor,
batch_size=config.batch_size, num_works=config.workers)
elif config.dataset == 'cifar100':
dataset = CIFAR100_LT(config.distributed, root=config.data_path, imb_factor=config.imb_factor,
batch_size=config.batch_size, num_works=config.workers)
elif config.dataset == 'places':
dataset = Places_LT(config.distributed, root=config.data_path,
batch_size=config.batch_size, num_works=config.workers)
elif config.dataset == 'imagenet':
dataset = ImageNet_LT(config.distributed, root=config.data_path,
batch_size=config.batch_size, num_works=config.workers)
elif config.dataset == 'ina2018':
dataset = iNa2018(config.distributed, root=config.data_path,
batch_size=config.batch_size, num_works=config.workers)
val_loader = dataset.eval
criterion = nn.CrossEntropyLoss().cuda(config.gpu)
if config.dataset != 'places':
block = None
validate(val_loader, model, classifier, lws_model, criterion, config, logger, block)
def validate(val_loader, model, classifier, lws_model, criterion, config, logger, block=None):
batch_time = AverageMeter('Time', ':6.3f')
losses = AverageMeter('Loss', ':.3f')
top1 = AverageMeter('Acc@1', ':6.3f')
top5 = AverageMeter('Acc@5', ':6.3f')
progress = ProgressMeter(
len(val_loader),
[batch_time, losses, top1, top5],
prefix='Eval: ')
# switch to evaluate mode
model.eval()
if config.dataset == 'places':
block.eval()
classifier.eval()
class_num = torch.zeros(config.num_classes).cuda()
correct = torch.zeros(config.num_classes).cuda()
confidence = np.array([])
pred_class = np.array([])
true_class = np.array([])
with torch.no_grad():
end = time.time()
for i, (images, target) in enumerate(val_loader):
if config.gpu is not None:
images = images.cuda(config.gpu, non_blocking=True)
if torch.cuda.is_available():
target = target.cuda(config.gpu, non_blocking=True)
# compute output
if config.dataset == 'places':
feat = block(model(images))
else:
feat = model(images)
output = classifier(feat)
output = lws_model(output)
loss = criterion(output, target)
# measure accuracy and record loss
acc1, acc5 = accuracy(output, target, topk=(1, 5))
losses.update(loss.item(), images.size(0))
top1.update(acc1[0], images.size(0))
top5.update(acc5[0], images.size(0))
_, predicted = output.max(1)
target_one_hot = F.one_hot(target, config.num_classes)
predict_one_hot = F.one_hot(predicted, config.num_classes)
class_num = class_num + target_one_hot.sum(dim=0).to(torch.float)
correct = correct + (target_one_hot + predict_one_hot == 2).sum(dim=0).to(torch.float)
prob = torch.softmax(output, dim=1)
confidence_part, pred_class_part = torch.max(prob, dim=1)
confidence = np.append(confidence, confidence_part.cpu().numpy())
pred_class = np.append(pred_class, pred_class_part.cpu().numpy())
true_class = np.append(true_class, target.cpu().numpy())
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if i % config.print_freq == 0:
progress.display(i, logger)
acc_classes = correct / class_num
head_acc = acc_classes[config.head_class_idx[0]:config.head_class_idx[1]].mean() * 100
med_acc = acc_classes[config.med_class_idx[0]:config.med_class_idx[1]].mean() * 100
tail_acc = acc_classes[config.tail_class_idx[0]:config.tail_class_idx[1]].mean() * 100
logger.info('* Acc@1 {top1.avg:.3f}% Acc@5 {top5.avg:.3f}% HAcc {head_acc:.3f}% MAcc {med_acc:.3f}% TAcc {tail_acc:.3f}%.'.format(top1=top1, top5=top5, head_acc=head_acc, med_acc=med_acc, tail_acc=tail_acc))
cal = calibration(true_class, pred_class, confidence, num_bins=15)
logger.info('* ECE {ece:.3f}%.'.format(ece=cal['expected_calibration_error'] * 100))
return top1.avg, cal['expected_calibration_error'] * 100
if __name__ == '__main__':
main()