-
Notifications
You must be signed in to change notification settings - Fork 38
/
test.py
146 lines (109 loc) · 3.88 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
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
import os
import torch.backends.cudnn as cudnn
import torch.utils.data
from torch.autograd import Variable
from torchvision import transforms
from data_loader import GetLoader
from torchvision import datasets
from model_compat import DSN
import torchvision.utils as vutils
def test(epoch, name):
###################
# params #
###################
cuda = True
cudnn.benchmark = True
batch_size = 64
image_size = 28
###################
# load data #
###################
img_transform = transforms.Compose([
transforms.Resize(image_size),
transforms.ToTensor(),
transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
])
model_root = 'model'
if name == 'mnist':
mode = 'source'
image_root = os.path.join('dataset', 'mnist')
dataset = datasets.MNIST(
root=image_root,
train=False,
transform=img_transform
)
dataloader = torch.utils.data.DataLoader(
dataset=dataset,
batch_size=batch_size,
shuffle=False,
num_workers=8
)
elif name == 'mnist_m':
mode = 'target'
image_root = os.path.join('dataset', 'mnist_m', 'mnist_m_test')
test_list = os.path.join('dataset', 'mnist_m', 'mnist_m_test_labels.txt')
dataset = GetLoader(
data_root=image_root,
data_list=test_list,
transform=img_transform
)
dataloader = torch.utils.data.DataLoader(
dataset=dataset,
batch_size=batch_size,
shuffle=False,
num_workers=8
)
else:
print 'error dataset name'
####################
# load model #
####################
my_net = DSN()
checkpoint = torch.load(os.path.join(model_root, 'dsn_mnist_mnistm_epoch_' + str(epoch) + '.pth'))
my_net.load_state_dict(checkpoint)
my_net.eval()
if cuda:
my_net = my_net.cuda()
####################
# transform image #
####################
def tr_image(img):
img_new = (img + 1) / 2
return img_new
len_dataloader = len(dataloader)
data_iter = iter(dataloader)
i = 0
n_total = 0
n_correct = 0
while i < len_dataloader:
data_input = data_iter.next()
img, label = data_input
batch_size = len(label)
input_img = torch.FloatTensor(batch_size, 3, image_size, image_size)
class_label = torch.LongTensor(batch_size)
if cuda:
img = img.cuda()
label = label.cuda()
input_img = input_img.cuda()
class_label = class_label.cuda()
input_img.resize_as_(input_img).copy_(img)
class_label.resize_as_(label).copy_(label)
inputv_img = Variable(input_img)
classv_label = Variable(class_label)
result = my_net(input_data=inputv_img, mode='source', rec_scheme='share')
pred = result[3].data.max(1, keepdim=True)[1]
result = my_net(input_data=inputv_img, mode=mode, rec_scheme='all')
rec_img_all = tr_image(result[-1].data)
result = my_net(input_data=inputv_img, mode=mode, rec_scheme='share')
rec_img_share = tr_image(result[-1].data)
result = my_net(input_data=inputv_img, mode=mode, rec_scheme='private')
rec_img_private = tr_image(result[-1].data)
if i == len_dataloader - 2:
vutils.save_image(rec_img_all, name + '_rec_image_all.png', nrow=8)
vutils.save_image(rec_img_share, name + '_rec_image_share.png', nrow=8)
vutils.save_image(rec_img_private, name + '_rec_image_private.png', nrow=8)
n_correct += pred.eq(classv_label.data.view_as(pred)).cpu().sum()
n_total += batch_size
i += 1
accu = n_correct * 1.0 / n_total
print 'epoch: %d, accuracy of the %s dataset: %f' % (epoch, name, accu)