-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_multi_office.py
288 lines (234 loc) · 9.93 KB
/
test_multi_office.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
import argparse
import os
import os.path as osp
import utils
import shutil
import time
import yaml
import pandas as pd
import numpy as np
import numpy
import torch
import torch.nn as nn
import torch.backends.cudnn as cudnn
import torch.optim
from torch.utils.data import DataLoader
import torchvision.transforms as transforms
import models
import torch.nn.functional as F
from torch.autograd import Variable
from tensorboardX import SummaryWriter
import logging
import se_transform.transforms as se_transforms
from dataset import NormalDataset, TeacherDataset
from utils import create_logger, AverageMeter, accuracy_2, save_checkpoint, load_state, IterLRScheduler
from utils import accuracy
model_names = sorted(name for name in models.__dict__
if name.islower() and not name.startswith("__")
and callable(models.__dict__[name]))
parser = argparse.ArgumentParser(description='PyTorch ImageNet Training')
parser.add_argument('--config', default='cfgs/config_res50.yaml')
parser.add_argument('--resume-opt', action='store_true')
parser.add_argument('-e', '--evaluate', action='store_true')
parser.add_argument('--port', default='23456', type=str)
class ColorAugmentation(object):
def __init__(self, eig_vec=None, eig_val=None):
if eig_vec == None:
eig_vec = torch.Tensor([
[ 0.4009, 0.7192, -0.5675],
[-0.8140, -0.0045, -0.5808],
[ 0.4203, -0.6948, -0.5836],
])
if eig_val == None:
eig_val = torch.Tensor([[0.2175, 0.0188, 0.0045]])
self.eig_val = eig_val # 1*3
self.eig_vec = eig_vec # 3*3
def __call__(self, tensor):
assert tensor.size(0) == 3
alpha = torch.normal(means=torch.zeros_like(self.eig_val))*0.1
quatity = torch.mm(self.eig_val*alpha, self.eig_vec)
tensor = tensor + quatity.view(3, 1, 1)
return tensor
def main():
global args, best_prec1
args = parser.parse_args()
with open(args.config) as f:
config = yaml.load(f)
for k, v in config['common'].items():
setattr(args, k, v)
torch.cuda.manual_seed(int(time.time())%1000)
# create model
print("=> creating model '{}'".format(args.arch))
if args.arch.startswith('inception_v3'):
print('inception_v3 without aux_logits!')
image_size = 341
input_size = 299
model = models.__dict__[args.arch](aux_logits=True,num_classes = 1000, pretrained = args.pretrained)
else:
image_size = 256
input_size = 226
student_model = models.__dict__[args.arch](num_classes = args.num_classes,
pretrained = args.pretrained,
avgpool_size=input_size/32)
student_model.cuda()
student_params = list(student_model.parameters())
student_optimizer = torch.optim.Adam(student_model.parameters(), args.base_lr*0.1)
args.save_path = "checkpoint/" + args.exp_name
if not osp.exists(args.save_path):
os.mkdir(args.save_path)
tb_logger = SummaryWriter(args.save_path)
logger = create_logger('global_logger', args.save_path+'/log.txt')
for key, val in vars(args).items():
logger.info("{:16} {}".format(key, val))
criterion = nn.CrossEntropyLoss()
print("Build network")
last_iter = -1
best_prec1 = 0
load_state(args.save_path + "/ckptmodel_best.pth.tar", student_model)
cudnn.benchmark = True
# Data loading code
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
se_normalize = se_transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
border_value = int(np.mean([0.485, 0.456, 0.406]) * 255 + 0.5)
test_aug = se_transforms.ImageAugmentation(
True, 0, rot_std=0.0,
scale_u_range=[0.75, 1.333],
affine_std=0,
scale_x_range=None,
scale_y_range=None)
val_dataset = NormalDataset(
args.val_root,
args.val_source,
transform = transforms.Compose([
se_transforms.ScaleAndCrop(
(input_size, input_size),
args.padding, False,
np.array([0.485, 0.456, 0.406]),
np.array([0.229, 0.224, 0.225]))
]),is_train=False, args=args )
val_loader = DataLoader(
val_dataset, batch_size=1, shuffle=False,
num_workers=args.workers)
val_multi_dataset = NormalDataset(
args.val_root,
args.val_source,
transform = transforms.Compose([
se_transforms.ScaleCropAndAugmentAffineMultiple(
16,
(input_size, input_size),
args.padding, True, test_aug, border_value,
np.array([0.485, 0.456, 0.406]),
np.array([0.229, 0.224, 0.225]))
]),is_train=False, args=args )
val_multi_loader = DataLoader(
val_multi_dataset, batch_size=1, shuffle=False,
num_workers=args.workers)
lr_scheduler = torch.optim.lr_scheduler.MultiStepLR(student_optimizer, args.lr_steps, args.lr_gamma)
#logger.info('{}'.format(args))
validate(val_loader, student_model, criterion)
validate_multi(val_multi_loader, student_model, criterion)
def validate(val_loader, model, criterion):
batch_time = AverageMeter(0)
losses = AverageMeter(0)
top1 = AverageMeter(0)
top5 = AverageMeter(0)
# switch to evaluate mode
model.eval()
eval_target = []
eval_output = []
eval_uk = []
logger = logging.getLogger('global_logger')
end = time.time()
for i, (input, target) in enumerate(val_loader):
target = target.cuda(async=True)
input_var = torch.autograd.Variable(input.cuda(), volatile=True)
target_var = torch.autograd.Variable(target, volatile=True)
# compute output
output, output1 = model(input_var)
# measure accuracy and record loss
softmax_output = F.softmax(output, dim=1)
#loss for known class
output1 = F.sigmoid(output1)
eval_target.append(target.cpu().data.numpy())
eval_output.append(softmax_output.cpu().data.numpy())
eval_uk.append(output1.cpu().data.numpy())
prec1, prec5 = accuracy(softmax_output.data, target, topk=(1, 5))
#losses.update(loss.item())
top1.update(prec1.item())
top5.update(prec5.item())
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if i % 1000 == 0:
logger.info('Test: [{0}/{1}]\t'
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
'Prec@1 {top1.val:.3f} ({top1.avg:.3f})\t'
'Prec@5 {top5.val:.3f} ({top5.avg:.3f})\t'.format(
i, len(val_loader), batch_time=batch_time, loss=losses,
top1=top1, top5=top5))
eval_target = np.concatenate(eval_target, axis=0)
eval_output = np.concatenate(eval_output, axis=0)
eval_uk = np.concatenate(eval_uk, axis=0)
evaluator = utils.PredictionEvaluator_2(eval_target, args.num_classes)
for i in range(10):
t_clss_acc, t_aug_cls_acc = evaluator.evaluate(eval_output, eval_uk, i*0.1)
print("epslion {:.2f}, mean_aug_class_acc {}, aug_cls_acc {}".format(i*0.1, t_clss_acc, t_aug_cls_acc))
logger.info(' * Prec@1 {top1.avg:.3f} Prec@5 {top5.avg:.3f}'.format(top1=top1, top5=top5))
model.train(mode=True)
return losses.avg, top1.avg, top5.avg
def validate_multi(val_loader, model, criterion):
batch_time = AverageMeter(0)
losses = AverageMeter(0)
top1 = AverageMeter(0)
top5 = AverageMeter(0)
# switch to evaluate mode
model.eval()
logger = logging.getLogger('global_logger')
end = time.time()
eval_output = []
eval_target = []
eval_uk = []
for i, (input, target) in enumerate(val_loader):
target = target.cuda(async=True)
input_var = torch.autograd.Variable(input.cuda(), volatile=True)
target_var = torch.autograd.Variable(target, volatile=True)
# compute output
output, output1 = model(input_var.squeeze(0).float())
# measure accuracy and record loss
softmax_output = F.softmax(output, dim=1).mean(0)
softmax_output = softmax_output.unsqueeze(0)
output1 = F.sigmoid(output1).mean(0).unsqueeze(0)
#loss for known class
eval_output.append(softmax_output.cpu().data.numpy())
eval_target.append(target_var.cpu().data.numpy())
eval_uk.append(output1.cpu().data.numpy())
prec1, prec5 = accuracy(softmax_output.data, target, topk=(1, 5))
#losses.update(loss.item())
top1.update(prec1.item())
top5.update(prec5.item())
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if i % 1000 == 0:
logger.info('Test: [{0}/{1}]\t'
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
'Prec@1 {top1.val:.3f} ({top1.avg:.3f})\t'
'Prec@5 {top5.val:.3f} ({top5.avg:.3f})\t'.format(
i, len(val_loader), batch_time=batch_time, loss=losses,
top1=top1, top5=top5))
eval_target = np.concatenate(eval_target, axis=0)
eval_output = np.concatenate(eval_output, axis=0)
eval_uk = np.concatenate(eval_uk, axis=0)
evaluator = utils.PredictionEvaluator_2(eval_target, args.num_classes)
for i in range(100):
t_clss_acc, t_aug_cls_acc = evaluator.evaluate(eval_output, eval_uk, i*0.01)
logger.info("epslion {:.2f}, mean_aug_class_acc {}, aug_cls_acc {}".format(i*0.01, t_clss_acc, t_aug_cls_acc))
logger.info(' * Prec@1 {top1.avg:.3f} Prec@5 {top5.avg:.3f}'.format(top1=top1, top5=top5))
model.train(mode=True)
return losses.avg, top1.avg, top5.avg
if __name__ == '__main__':
main()