forked from NaJaeMin92/pytorch-DANN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
212 lines (165 loc) · 6.67 KB
/
utils.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
import numpy as np
import matplotlib.pyplot as plt
from torch.autograd import Function
from sklearn.manifold import TSNE
import torch
import mnist
import mnistm
import itertools
import os
class ReverseLayerF(Function):
@staticmethod
def forward(ctx, x, alpha):
ctx.alpha = alpha
return x.view_as(x)
@staticmethod
def backward(ctx, grad_output):
output = grad_output.neg() * ctx.alpha
return output, None
def optimizer_scheduler(optimizer, p):
"""
Adjust the learning rate of optimizer
:param optimizer: optimizer for updating parameters
:param p: a variable for adjusting learning rate
:return: optimizer
"""
for param_group in optimizer.param_groups:
param_group['lr'] = 0.01 / (1. + 10 * p) ** 0.75
return optimizer
def one_hot_embedding(labels, num_classes=10):
"""Embedding labels to one-hot form.
Args:
labels: (LongTensor) class labels, sized [N,].
num_classes: (int) number of classes.
Returns:
(tensor) encoded labels, sized [N, #classes].
"""
y = torch.eye(num_classes)
return y[labels]
def save_model(encoder, classifier, discriminator, training_mode):
print('Saving models ...')
save_folder = 'trained_models'
if not os.path.exists(save_folder):
os.makedirs(save_folder)
torch.save(encoder.state_dict(), 'trained_models/encoder_' + str(training_mode) + '.pt')
torch.save(classifier.state_dict(), 'trained_models/classifier_' + str(training_mode) + '.pt')
if training_mode == 'dann':
torch.save(discriminator.state_dict(), 'trained_models/discriminator_' + str(training_mode) + '.pt')
print('The model has been successfully saved!')
def plot_embedding(X, y, d, training_mode):
x_min, x_max = np.min(X, 0), np.max(X, 0)
X = (X - x_min) / (x_max - x_min)
y = list(itertools.chain.from_iterable(y))
y = np.asarray(y)
plt.figure(figsize=(10, 10))
for i in range(len(d)): # X.shape[0] : 1024
# plot colored number
if d[i] == 0:
colors = (0.0, 0.0, 1.0, 1.0)
else:
colors = (1.0, 0.0, 0.0, 1.0)
plt.text(X[i, 0], X[i, 1], str(y[i]),
color=colors,
fontdict={'weight': 'bold', 'size': 9})
plt.xticks([]), plt.yticks([])
save_folder = 'saved_plot'
if not os.path.exists(save_folder):
os.makedirs(save_folder)
fig_name = 'saved_plot/' + str(training_mode) + '.png'
plt.savefig(fig_name)
print('{} has been successfully saved!'.format(fig_name))
def visualize(encoder, training_mode):
# Draw 512 samples in test_data
source_test_loader = mnist.mnist_test_loader
target_test_loader = mnistm.mnistm_test_loader
# Get source_test samples
source_label_list = []
source_img_list = []
for i, test_data in enumerate(source_test_loader):
if i >= 16: # to get only 512 samples
break
img, label = test_data
label = label.numpy()
img = img.cuda()
img = torch.cat((img, img, img), 1) # MNIST channel 1 -> 3
source_label_list.append(label)
source_img_list.append(img)
source_img_list = torch.stack(source_img_list)
source_img_list = source_img_list.view(-1, 3, 28, 28)
# Get target_test samples
target_label_list = []
target_img_list = []
for i, test_data in enumerate(target_test_loader):
if i >= 16:
break
img, label = test_data
label = label.numpy()
img = img.cuda()
target_label_list.append(label)
target_img_list.append(img)
target_img_list = torch.stack(target_img_list)
target_img_list = target_img_list.view(-1, 3, 28, 28)
# Stack source_list + target_list
combined_label_list = source_label_list
combined_label_list.extend(target_label_list)
combined_img_list = torch.cat((source_img_list, target_img_list), 0)
source_domain_list = torch.zeros(512).type(torch.LongTensor)
target_domain_list = torch.ones(512).type(torch.LongTensor)
combined_domain_list = torch.cat((source_domain_list, target_domain_list), 0).cuda()
print("Extracting features to draw t-SNE plot...")
combined_feature = encoder(combined_img_list) # combined_feature : 1024,2352
tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=3000)
dann_tsne = tsne.fit_transform(combined_feature.detach().cpu().numpy())
print('Drawing t-SNE plot ...')
plot_embedding(dann_tsne, combined_label_list, combined_domain_list, training_mode)
def visualize_input():
source_test_loader = mnist.mnist_test_loader
target_test_loader = mnistm.mnistm_test_loader
# Get source_test samples
source_label_list = []
source_img_list = []
for i, test_data in enumerate(source_test_loader):
if i >= 16: # to get only 512 samples
break
img, label = test_data
label = label.numpy()
img = img.cuda()
img = torch.cat((img, img, img), 1) # MNIST channel 1 -> 3
source_label_list.append(label)
source_img_list.append(img)
source_img_list = torch.stack(source_img_list)
source_img_list = source_img_list.view(-1, 3, 28, 28)
# Get target_test samples
target_label_list = []
target_img_list = []
for i, test_data in enumerate(target_test_loader):
if i >= 16:
break
img, label = test_data
label = label.numpy()
img = img.cuda()
target_label_list.append(label)
target_img_list.append(img)
target_img_list = torch.stack(target_img_list)
target_img_list = target_img_list.view(-1, 3, 28, 28)
# Stack source_list + target_list
combined_label_list = source_label_list
combined_label_list.extend(target_label_list)
combined_img_list = torch.cat((source_img_list, target_img_list), 0)
source_domain_list = torch.zeros(512).type(torch.LongTensor)
target_domain_list = torch.ones(512).type(torch.LongTensor)
combined_domain_list = torch.cat((source_domain_list, target_domain_list), 0).cuda()
print("Extracting features to draw t-SNE plot...")
combined_feature = combined_img_list # combined_feature : 1024,3,28,28
combined_feature = combined_feature.view(1024, -1) # flatten
# print(type(combined_feature), combined_feature.shape)
tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=3000)
dann_tsne = tsne.fit_transform(combined_feature.detach().cpu().numpy())
print('Drawing t-SNE plot ...')
plot_embedding(dann_tsne, combined_label_list, combined_domain_list, 'input')
def set_model_mode(mode='train', models=None):
for model in models:
if mode == 'train':
model.train()
else:
model.eval()