-
Notifications
You must be signed in to change notification settings - Fork 100
/
test.py
95 lines (72 loc) · 2.27 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import os
import torch.backends.cudnn as cudnn
import torch.utils.data
from torchvision import transforms
from data_loader import GetLoader
from torchvision import datasets
def test(dataset_name):
assert dataset_name in ['MNIST', 'mnist_m']
model_root = 'models'
image_root = os.path.join('dataset', dataset_name)
cuda = True
cudnn.benchmark = True
batch_size = 128
image_size = 28
alpha = 0
"""load data"""
img_transform_source = transforms.Compose([
transforms.Resize(image_size),
transforms.ToTensor(),
transforms.Normalize(mean=(0.1307,), std=(0.3081,))
])
img_transform_target = transforms.Compose([
transforms.Resize(image_size),
transforms.ToTensor(),
transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
])
if dataset_name == 'mnist_m':
test_list = os.path.join(image_root, 'mnist_m_test_labels.txt')
dataset = GetLoader(
data_root=os.path.join(image_root, 'mnist_m_test'),
data_list=test_list,
transform=img_transform_target
)
else:
dataset = datasets.MNIST(
root='dataset',
train=False,
transform=img_transform_source,
)
dataloader = torch.utils.data.DataLoader(
dataset=dataset,
batch_size=batch_size,
shuffle=False,
num_workers=8
)
""" test """
my_net = torch.load(os.path.join(
model_root, 'mnist_mnistm_model_epoch_current.pth'
))
my_net = my_net.eval()
if cuda:
my_net = my_net.cuda()
len_dataloader = len(dataloader)
data_target_iter = iter(dataloader)
i = 0
n_total = 0
n_correct = 0
while i < len_dataloader:
# test model using target data
data_target = data_target_iter.next()
t_img, t_label = data_target
batch_size = len(t_label)
if cuda:
t_img = t_img.cuda()
t_label = t_label.cuda()
class_output, _ = my_net(input_data=t_img, alpha=alpha)
pred = class_output.data.max(1, keepdim=True)[1]
n_correct += pred.eq(t_label.data.view_as(pred)).cpu().sum()
n_total += batch_size
i += 1
accu = n_correct.data.numpy() * 1.0 / n_total
return accu