-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.py
233 lines (172 loc) · 8.41 KB
/
solver.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
import torch
import torch.nn as nn
import os
from torch import optim
from model import encoder, classifier, discriminator
from sklearn.metrics import confusion_matrix, accuracy_score
from data_loader import get_loader
import copy
import torch.nn.functional as F
class Solver(object):
def __init__(self, args):
self.args = args
self.s_train_loader, self.s_test_loader, self.t_train_loader, self.t_test_loader = get_loader(args)
self.ce = nn.CrossEntropyLoss()
self.bce = nn.BCELoss()
self.best_acc = 0
self.enc = encoder(self.args).cuda()
self.clf = classifier(self.args).cuda()
self.fd = discriminator(self.args).cuda()
print('--------Network--------')
print(self.enc)
print(self.clf)
print('--------Feature Disc--------')
print(self.fd)
self.fake_label = torch.FloatTensor(self.args.batch_size, 1).fill_(0).cuda()
self.real_label = torch.FloatTensor(self.args.batch_size, 1).fill_(1).cuda()
if not args.method == 'src':
if os.path.exists(os.path.join(self.args.model_path, 'src_enc.pt')):
print("Loading Source model...")
self.enc.load_state_dict(torch.load(os.path.join(self.args.model_path, 'src_enc.pt')))
self.clf.load_state_dict(torch.load(os.path.join(self.args.model_path, 'src_clf.pt')))
else:
print("Training Source model...")
self.src()
self.test()
def test_dataset(self, db='t_test'):
self.enc.eval()
self.clf.eval()
actual = []
pred = []
if db.lower() == 's_train':
loader = self.s_train_loader
elif db.lower() == 's_test':
loader = self.s_test_loader
elif db.lower() == 't_train':
loader = self.t_train_loader
else:
loader = self.t_test_loader
for data in loader:
img, label = data
img = img.cuda()
with torch.no_grad():
class_out = self.clf(self.enc(img))
_, predicted = torch.max(class_out.data, 1)
actual += label.tolist()
pred += predicted.tolist()
acc = accuracy_score(y_true=actual, y_pred=pred) * 100
cm = confusion_matrix(y_true=actual, y_pred=pred, labels=range(self.args.num_classes))
return acc, cm
def test(self):
s_train_acc, cm = self.test_dataset('s_train')
print("Source Tr Acc: %.2f" % s_train_acc)
if self.args.cm:
print(cm)
s_test_acc, cm = self.test_dataset('s_test')
print("Source Te Acc: %.2f" % s_test_acc)
if self.args.cm:
print(cm)
t_train_acc, cm = self.test_dataset('t_train')
print("Target Tr Acc: %.2f" % t_train_acc)
if self.args.cm:
print(cm)
t_test_acc, cm = self.test_dataset('t_test')
print("Target Te Acc: %.2f" % t_test_acc)
if self.args.cm:
print(cm)
return s_train_acc, s_test_acc, t_train_acc, t_test_acc
def get_local(self, domain_out, labels):
y_onehot = F.one_hot(labels, self.args.num_classes).float()
domain_out = domain_out * y_onehot
domain_out = domain_out.sum(1, keepdim=True)
return domain_out
def src(self):
total_iters = 0
self.best_acc = 0
s_iter_per_epoch = len(iter(self.s_train_loader))
self.args.src_test_epoch = max(self.args.src_epochs // 10, 1)
self.optimizer = optim.Adam(list(self.enc.parameters()) + list(self.clf.parameters()), self.args.lr, betas=[0.5, 0.999], weight_decay=self.args.weight_decay)
for epoch in range(self.args.src_epochs):
self.clf.train()
self.enc.train()
for i, (source, s_labels) in enumerate(self.s_train_loader):
total_iters += 1
source, s_labels = source.cuda(), s_labels.cuda()
s_logits = self.clf(self.enc(source))
s_clf_loss = self.ce(s_logits, s_labels)
loss = s_clf_loss
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
if i % 50 == 0 or i == (s_iter_per_epoch - 1):
print('Ep: %d/%d, iter: %d/%d, total_iters: %d, s_err: %.4f'
% (epoch + 1, self.args.src_epochs, i + 1, s_iter_per_epoch, total_iters, s_clf_loss))
if (epoch + 1) % self.args.src_test_epoch == 0:
s_test_acc, cm = self.test_dataset('s_test')
print("Source test acc: %0.2f" % (s_test_acc))
if self.args.cm:
print(cm)
if s_test_acc > self.best_acc:
self.best_acc = s_test_acc
best_enc = copy.deepcopy(self.enc.state_dict())
best_clf = copy.deepcopy(self.clf.state_dict())
torch.save(best_enc, os.path.join(self.args.model_path, 'src_enc.pt'))
torch.save(best_clf, os.path.join(self.args.model_path, 'src_clf.pt'))
self.enc.load_state_dict(best_enc)
self.clf.load_state_dict(best_clf)
def mdc(self):
s_iter_per_epoch = len(self.s_train_loader)
t_iter_per_epoch = len(self.t_train_loader)
min_len = min(s_iter_per_epoch, t_iter_per_epoch)
total_iters = 0
self.best_acc = 0
print("Source iters per epoch: %d" % (s_iter_per_epoch))
print("Target iters per epoch: %d" % (t_iter_per_epoch))
print("iters per epoch: %d" % (min(s_iter_per_epoch, t_iter_per_epoch)))
self.c_optimizer = optim.Adam(list(self.enc.parameters()) + list(self.clf.parameters()), self.args.lr, betas=[0.5, 0.999], weight_decay=self.args.weight_decay)
self.fd_optimizer = optim.Adam(list(self.fd.parameters()), self.args.lr, betas=[0.5, 0.999], weight_decay=self.args.weight_decay)
for epoch in range(self.args.adapt_epochs):
self.clf.train()
self.enc.train()
self.fd.train()
for i, (source_data, target_data) in enumerate(zip(self.s_train_loader, self.t_train_loader)):
total_iters += 1
source, s_labels = source_data
source, s_labels = source.cuda(), s_labels.cuda()
target, t_labels = target_data
target, t_labels = target.cuda(), t_labels.cuda()
s_deep = self.enc(source)
s_out = self.clf(s_deep)
t_deep = self.enc(target)
t_out = self.clf(t_deep)
# Train discriminator
s_fd_out = self.fd(s_deep.detach())
t_fd_out = self.fd(t_deep.detach())
s_domain_err = self.bce(s_fd_out, self.real_label)
t_domain_err = self.bce(t_fd_out, self.fake_label)
disc_loss = (s_domain_err + t_domain_err) / 2
self.fd_optimizer.zero_grad()
disc_loss.backward()
self.fd_optimizer.step()
# Train Encoder
s_fd_out = self.fd(s_deep)
t_fd_out = self.fd(t_deep)
s_loss_conf = -(torch.log(s_fd_out + 1e-6).mean() + torch.log(1 - s_fd_out + 1e-6).mean()) / 2
t_loss_conf = -(torch.log(t_fd_out + 1e-6).mean() + torch.log(1 - t_fd_out + 1e-6).mean()) / 2
generator_loss = (s_loss_conf + t_loss_conf) * 0.01 / 2
s_clf_loss = self.ce(s_out, s_labels)
loss = s_clf_loss + generator_loss
self.c_optimizer.zero_grad()
loss.backward()
self.c_optimizer.step()
if i % 50 == 0 or i == (min_len - 1):
print('Ep: %d/%d, iter: %d/%d, total_iters: %d, s_err: %.4f, d_err: %.4f, g_loss: %.4f'
% (epoch + 1, self.args.adapt_epochs, i + 1, min_len, total_iters, s_clf_loss, disc_loss, generator_loss))
if (epoch + 1) % self.args.adapt_test_epoch == 0:
t_test_acc, cm = self.test_dataset('t_test')
print("Target test acc: %0.2f" % (t_test_acc))
if self.args.cm:
print(cm)
torch.save(self.enc.state_dict(), os.path.join(self.args.model_path, 'mdc_enc.pt'))
torch.save(self.clf.state_dict(), os.path.join(self.args.model_path, 'mdc_clf.pt'))
torch.save(self.fd.state_dict(), os.path.join(self.args.model_path, 'mdc_disc.pt'))