-
Notifications
You must be signed in to change notification settings - Fork 61
/
test.py
56 lines (41 loc) · 1.73 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
import argparse
import torch
from torch.utils.data import DataLoader
from mini_imagenet import MiniImageNet
from samplers import CategoriesSampler
from convnet import Convnet
from utils import pprint, set_gpu, count_acc, Averager, euclidean_metric
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--gpu', default='0')
parser.add_argument('--load', default='./save/proto-1/max-acc.pth')
parser.add_argument('--batch', type=int, default=2000)
parser.add_argument('--way', type=int, default=5)
parser.add_argument('--shot', type=int, default=1)
parser.add_argument('--query', type=int, default=30)
args = parser.parse_args()
pprint(vars(args))
set_gpu(args.gpu)
dataset = MiniImageNet('test')
sampler = CategoriesSampler(dataset.label,
args.batch, args.way, args.shot + args.query)
loader = DataLoader(dataset, batch_sampler=sampler,
num_workers=8, pin_memory=True)
model = Convnet().cuda()
model.load_state_dict(torch.load(args.load))
model.eval()
ave_acc = Averager()
for i, batch in enumerate(loader, 1):
data, _ = [_.cuda() for _ in batch]
k = args.way * args.shot
data_shot, data_query = data[:k], data[k:]
x = model(data_shot)
x = x.reshape(args.shot, args.way, -1).mean(dim=0)
p = x
logits = euclidean_metric(model(data_query), p)
label = torch.arange(args.way).repeat(args.query)
label = label.type(torch.cuda.LongTensor)
acc = count_acc(logits, label)
ave_acc.add(acc)
print('batch {}: {:.2f}({:.2f})'.format(i, ave_acc.item() * 100, acc * 100))
x = None; p = None; logits = None