-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
71 lines (50 loc) · 2.24 KB
/
test.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
import os
import tqdm
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
from common.meter import Meter
from common.utils import compute_accuracy, load_model, setup_run, by
from models.dataloader.samplers import CategoriesSampler
from models.dataloader.data_utils import dataset_builder
from models.renet import RENet
def evaluate(epoch, model, loader, args=None, set='val'):
model.eval()
loss_meter = Meter()
acc_meter = Meter()
label = torch.arange(args.way).repeat(args.query).cuda()
k = args.way * args.shot
tqdm_gen = tqdm.tqdm(loader)
with torch.no_grad():
for i, (data, labels) in enumerate(tqdm_gen, 1):
data = data.cuda()
model.module.mode = 'encoder'
data = model(data)
data_shot, data_query = data[:k], data[k:]
model.module.mode = 'cca'
logits = model((data_shot.unsqueeze(0).repeat(args.num_gpu, 1, 1, 1, 1), data_query))
loss = F.cross_entropy(logits, label)
acc = compute_accuracy(logits, label)
loss_meter.update(loss.item())
acc_meter.update(acc)
tqdm_gen.set_description(f'[{set:^5}] epo:{epoch:>3} | avg.loss:{loss_meter.avg():.4f} | avg.acc:{by(acc_meter.avg())} (curr:{acc:.3f})')
return loss_meter.avg(), acc_meter.avg(), acc_meter.confidence_interval()
def test_main(model, args):
''' load model '''
model = load_model(model, os.path.join(args.save_path, 'max_acc.pth'))
''' define test dataset '''
Dataset = dataset_builder(args)
test_set = Dataset('test', args)
sampler = CategoriesSampler(test_set.label, args.test_episode, args.way, args.shot + args.query)
test_loader = DataLoader(test_set, batch_sampler=sampler, num_workers=8, pin_memory=True)
''' evaluate the model with the dataset '''
_, test_acc, test_ci = evaluate("best", model, test_loader, args, set='test')
print(f'[final] epo:{"best":>3} | {by(test_acc)} +- {test_ci:.3f}')
return test_acc, test_ci
if __name__ == '__main__':
args = setup_run(arg_mode='test')
''' define model '''
model = RENet(args).cuda()
model = nn.DataParallel(model, device_ids=args.device_ids)
test_main(model, args)