-
Notifications
You must be signed in to change notification settings - Fork 100
/
main.py
159 lines (124 loc) · 4.5 KB
/
main.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
147
148
149
150
151
152
153
154
155
156
157
158
159
import random
import os
import sys
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torch.utils.data
import numpy as np
from data_loader import GetLoader
from torchvision import datasets
from torchvision import transforms
from model import CNNModel
from test import test
source_dataset_name = 'MNIST'
target_dataset_name = 'mnist_m'
source_image_root = os.path.join('dataset', source_dataset_name)
target_image_root = os.path.join('dataset', target_dataset_name)
model_root = 'models'
cuda = True
cudnn.benchmark = True
lr = 1e-3
batch_size = 128
image_size = 28
n_epoch = 100
manual_seed = random.randint(1, 10000)
random.seed(manual_seed)
torch.manual_seed(manual_seed)
# 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))
])
dataset_source = datasets.MNIST(
root='dataset',
train=True,
transform=img_transform_source,
download=True
)
dataloader_source = torch.utils.data.DataLoader(
dataset=dataset_source,
batch_size=batch_size,
shuffle=True,
num_workers=8)
train_list = os.path.join(target_image_root, 'mnist_m_train_labels.txt')
dataset_target = GetLoader(
data_root=os.path.join(target_image_root, 'mnist_m_train'),
data_list=train_list,
transform=img_transform_target
)
dataloader_target = torch.utils.data.DataLoader(
dataset=dataset_target,
batch_size=batch_size,
shuffle=True,
num_workers=8)
# load model
my_net = CNNModel()
# setup optimizer
optimizer = optim.Adam(my_net.parameters(), lr=lr)
loss_class = torch.nn.NLLLoss()
loss_domain = torch.nn.NLLLoss()
if cuda:
my_net = my_net.cuda()
loss_class = loss_class.cuda()
loss_domain = loss_domain.cuda()
for p in my_net.parameters():
p.requires_grad = True
# training
best_accu_t = 0.0
for epoch in range(n_epoch):
len_dataloader = min(len(dataloader_source), len(dataloader_target))
data_source_iter = iter(dataloader_source)
data_target_iter = iter(dataloader_target)
for i in range(len_dataloader):
p = float(i + epoch * len_dataloader) / n_epoch / len_dataloader
alpha = 2. / (1. + np.exp(-10 * p)) - 1
# training model using source data
data_source = data_source_iter.next()
s_img, s_label = data_source
my_net.zero_grad()
batch_size = len(s_label)
domain_label = torch.zeros(batch_size).long()
if cuda:
s_img = s_img.cuda()
s_label = s_label.cuda()
domain_label = domain_label.cuda()
class_output, domain_output = my_net(input_data=s_img, alpha=alpha)
err_s_label = loss_class(class_output, s_label)
err_s_domain = loss_domain(domain_output, domain_label)
# training model using target data
data_target = data_target_iter.next()
t_img, _ = data_target
batch_size = len(t_img)
domain_label = torch.ones(batch_size).long()
if cuda:
t_img = t_img.cuda()
domain_label = domain_label.cuda()
_, domain_output = my_net(input_data=t_img, alpha=alpha)
err_t_domain = loss_domain(domain_output, domain_label)
err = err_t_domain + err_s_domain + err_s_label
err.backward()
optimizer.step()
sys.stdout.write('\r epoch: %d, [iter: %d / all %d], err_s_label: %f, err_s_domain: %f, err_t_domain: %f' \
% (epoch, i + 1, len_dataloader, err_s_label.data.cpu().numpy(),
err_s_domain.data.cpu().numpy(), err_t_domain.data.cpu().item()))
sys.stdout.flush()
torch.save(my_net, '{0}/mnist_mnistm_model_epoch_current.pth'.format(model_root))
print('\n')
accu_s = test(source_dataset_name)
print('Accuracy of the %s dataset: %f' % ('mnist', accu_s))
accu_t = test(target_dataset_name)
print('Accuracy of the %s dataset: %f\n' % ('mnist_m', accu_t))
if accu_t > best_accu_t:
best_accu_s = accu_s
best_accu_t = accu_t
torch.save(my_net, '{0}/mnist_mnistm_model_epoch_best.pth'.format(model_root))
print('============ Summary ============= \n')
print('Accuracy of the %s dataset: %f' % ('mnist', best_accu_s))
print('Accuracy of the %s dataset: %f' % ('mnist_m', best_accu_t))
print('Corresponding model was save in ' + model_root + '/mnist_mnistm_model_epoch_best.pth')