-
Notifications
You must be signed in to change notification settings - Fork 38
/
train.py
270 lines (208 loc) · 8.55 KB
/
train.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import random
import os
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torch.utils.data
import numpy as np
from torch.autograd import Variable
from torchvision import datasets
from torchvision import transforms
from model_compat import DSN
from data_loader import GetLoader
from functions import SIMSE, DiffLoss, MSE
from test import test
######################
# params #
######################
source_image_root = os.path.join('.', 'dataset', 'mnist')
target_image_root = os.path.join('.', 'dataset', 'mnist_m')
model_root = 'model'
cuda = True
cudnn.benchmark = True
lr = 1e-2
batch_size = 32
image_size = 28
n_epoch = 100
step_decay_weight = 0.95
lr_decay_step = 20000
active_domain_loss_step = 10000
weight_decay = 1e-6
alpha_weight = 0.01
beta_weight = 0.075
gamma_weight = 0.25
momentum = 0.9
manual_seed = random.randint(1, 10000)
random.seed(manual_seed)
torch.manual_seed(manual_seed)
#######################
# 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))
])
dataset_source = datasets.MNIST(
root=source_image_root,
train=True,
transform=img_transform
)
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
)
dataloader_target = torch.utils.data.DataLoader(
dataset=dataset_target,
batch_size=batch_size,
shuffle=True,
num_workers=8
)
#####################
# load model #
#####################
my_net = DSN()
#####################
# setup optimizer #
#####################
def exp_lr_scheduler(optimizer, step, init_lr=lr, lr_decay_step=lr_decay_step, step_decay_weight=step_decay_weight):
# Decay learning rate by a factor of step_decay_weight every lr_decay_step
current_lr = init_lr * (step_decay_weight ** (step / lr_decay_step))
if step % lr_decay_step == 0:
print 'learning rate is set to %f' % current_lr
for param_group in optimizer.param_groups:
param_group['lr'] = current_lr
return optimizer
optimizer = optim.SGD(my_net.parameters(), lr=lr, momentum=momentum, weight_decay=weight_decay)
loss_classification = torch.nn.CrossEntropyLoss()
loss_recon1 = MSE()
loss_recon2 = SIMSE()
loss_diff = DiffLoss()
loss_similarity = torch.nn.CrossEntropyLoss()
if cuda:
my_net = my_net.cuda()
loss_classification = loss_classification.cuda()
loss_recon1 = loss_recon1.cuda()
loss_recon2 = loss_recon2.cuda()
loss_diff = loss_diff.cuda()
loss_similarity = loss_similarity.cuda()
for p in my_net.parameters():
p.requires_grad = True
#############################
# training network #
#############################
len_dataloader = min(len(dataloader_source), len(dataloader_target))
dann_epoch = np.floor(active_domain_loss_step / len_dataloader * 1.0)
current_step = 0
for epoch in xrange(n_epoch):
data_source_iter = iter(dataloader_source)
data_target_iter = iter(dataloader_target)
i = 0
while i < len_dataloader:
###################################
# target data training #
###################################
data_target = data_target_iter.next()
t_img, t_label = data_target
my_net.zero_grad()
loss = 0
batch_size = len(t_label)
input_img = torch.FloatTensor(batch_size, 3, image_size, image_size)
class_label = torch.LongTensor(batch_size)
domain_label = torch.ones(batch_size)
domain_label = domain_label.long()
if cuda:
t_img = t_img.cuda()
t_label = t_label.cuda()
input_img = input_img.cuda()
class_label = class_label.cuda()
domain_label = domain_label.cuda()
input_img.resize_as_(t_img).copy_(t_img)
class_label.resize_as_(t_label).copy_(t_label)
target_inputv_img = Variable(input_img)
target_classv_label = Variable(class_label)
target_domainv_label = Variable(domain_label)
if current_step > active_domain_loss_step:
p = float(i + (epoch - dann_epoch) * len_dataloader / (n_epoch - dann_epoch) / len_dataloader)
p = 2. / (1. + np.exp(-10 * p)) - 1
# activate domain loss
result = my_net(input_data=target_inputv_img, mode='target', rec_scheme='all', p=p)
target_privte_code, target_share_code, target_domain_label, target_rec_code = result
target_dann = gamma_weight * loss_similarity(target_domain_label, target_domainv_label)
loss += target_dann
else:
target_dann = Variable(torch.zeros(1).float().cuda())
result = my_net(input_data=target_inputv_img, mode='target', rec_scheme='all')
target_privte_code, target_share_code, _, target_rec_code = result
target_diff= beta_weight * loss_diff(target_privte_code, target_share_code)
loss += target_diff
target_mse = alpha_weight * loss_recon1(target_rec_code, target_inputv_img)
loss += target_mse
target_simse = alpha_weight * loss_recon2(target_rec_code, target_inputv_img)
loss += target_simse
loss.backward()
optimizer.step()
###################################
# source data training #
###################################
data_source = data_source_iter.next()
s_img, s_label = data_source
my_net.zero_grad()
batch_size = len(s_label)
input_img = torch.FloatTensor(batch_size, 3, image_size, image_size)
class_label = torch.LongTensor(batch_size)
domain_label = torch.zeros(batch_size)
domain_label = domain_label.long()
loss = 0
if cuda:
s_img = s_img.cuda()
s_label = s_label.cuda()
input_img = input_img.cuda()
class_label = class_label.cuda()
domain_label = domain_label.cuda()
input_img.resize_as_(input_img).copy_(s_img)
class_label.resize_as_(s_label).copy_(s_label)
source_inputv_img = Variable(input_img)
source_classv_label = Variable(class_label)
source_domainv_label = Variable(domain_label)
if current_step > active_domain_loss_step:
# activate domain loss
result = my_net(input_data=source_inputv_img, mode='source', rec_scheme='all', p=p)
source_privte_code, source_share_code, source_domain_label, source_class_label, source_rec_code = result
source_dann = gamma_weight * loss_similarity(source_domain_label, source_domainv_label)
loss += source_dann
else:
source_dann = Variable(torch.zeros(1).float().cuda())
result = my_net(input_data=source_inputv_img, mode='source', rec_scheme='all')
source_privte_code, source_share_code, _, source_class_label, source_rec_code = result
source_classification = loss_classification(source_class_label, source_classv_label)
loss += source_classification
source_diff = beta_weight * loss_diff(source_privte_code, source_share_code)
loss += source_diff
source_mse = alpha_weight * loss_recon1(source_rec_code, source_inputv_img)
loss += source_mse
source_simse = alpha_weight * loss_recon2(source_rec_code, source_inputv_img)
loss += source_simse
loss.backward()
optimizer = exp_lr_scheduler(optimizer=optimizer, step=current_step)
optimizer.step()
i += 1
current_step += 1
print 'source_classification: %f, source_dann: %f, source_diff: %f, ' \
'source_mse: %f, source_simse: %f, target_dann: %f, target_diff: %f, ' \
'target_mse: %f, target_simse: %f' \
% (source_classification.data.cpu().numpy(), source_dann.data.cpu().numpy(), source_diff.data.cpu().numpy(),
source_mse.data.cpu().numpy(), source_simse.data.cpu().numpy(), target_dann.data.cpu().numpy(),
target_diff.data.cpu().numpy(),target_mse.data.cpu().numpy(), target_simse.data.cpu().numpy())
# print 'step: %d, loss: %f' % (current_step, loss.cpu().data.numpy())
torch.save(my_net.state_dict(), model_root + '/dsn_mnist_mnistm_epoch_' + str(epoch) + '.pth')
test(epoch=epoch, name='mnist')
test(epoch=epoch, name='mnist_m')
print 'done'