forked from rosinality/mac-network-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
44 lines (36 loc) · 1.1 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
import sys
import pickle
from collections import Counter
import torch
from tqdm import tqdm
from torch.utils.data import DataLoader
from dataset import CLEVR, collate_data, transform
batch_size = 64
n_epoch = 180
train_set = DataLoader(
CLEVR(sys.argv[1], 'val', transform=None),
batch_size=batch_size,
num_workers=4,
collate_fn=collate_data,
)
net = torch.load(sys.argv[2])
net.eval()
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
for epoch in range(n_epoch):
dataset = iter(train_set)
pbar = tqdm(dataset)
family_correct = Counter()
family_total = Counter()
for image, question, q_len, answer, family in pbar:
image, question = image.to(device), question.to(device)
output = net(image, question, q_len)
correct = output.detach().argmax(1) == answer.to(device)
for c, fam in zip(correct, family):
if c:
family_correct[fam] += 1
family_total[fam] += 1
print(
'Avg Acc: {:.5f}'.format(
sum(family_correct.values()) / sum(family_total.values())
)
)