-
Notifications
You must be signed in to change notification settings - Fork 23
/
main.py
297 lines (264 loc) · 9.16 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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import os
import time
import pdb
from tqdm import *
import torch
from torch import nn
from torch.autograd import Variable
from torch.utils.data import DataLoader
from torch.nn import Parameter
from torchvision import transforms
from torchvision.datasets import MNIST
from torchvision.utils import save_image
from sklearn.cluster import KMeans
import numpy as np
from tqdm import *
from metrics import *
from sklearn.manifold import TSNE
from matplotlib import pyplot as plt
import pandas as pd
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
class AutoEncoder(nn.Module):
def __init__(self):
super(AutoEncoder, self).__init__()
self.encoder = nn.Sequential(
nn.Linear(28 * 28, 500),
nn.ReLU(True),
nn.Linear(500, 500),
nn.ReLU(True),
nn.Linear(500, 500),
nn.ReLU(True),
nn.Linear(500, 2000),
nn.ReLU(True),
nn.Linear(2000, 10))
self.decoder = nn.Sequential(
nn.Linear(10, 2000),
nn.ReLU(True),
nn.Linear(2000, 500),
nn.ReLU(True),
nn.Linear(500, 500),
nn.ReLU(True),
nn.Linear(500, 500),
nn.ReLU(True),
nn.Linear(500, 28 * 28))
self.model = nn.Sequential(self.encoder, self.decoder)
def encode(self, x):
return self.encoder(x)
def forward(self, x):
x = self.model(x)
return x
class ClusteringLayer(nn.Module):
def __init__(self, n_clusters=10, hidden=10, cluster_centers=None, alpha=1.0):
super(ClusteringLayer, self).__init__()
self.n_clusters = n_clusters
self.alpha = alpha
self.hidden = hidden
if cluster_centers is None:
initial_cluster_centers = torch.zeros(
self.n_clusters,
self.hidden,
dtype=torch.float
).cuda()
nn.init.xavier_uniform_(initial_cluster_centers)
else:
initial_cluster_centers = cluster_centers
self.cluster_centers = Parameter(initial_cluster_centers)
def forward(self, x):
norm_squared = torch.sum((x.unsqueeze(1) - self.cluster_centers)**2, 2)
numerator = 1.0 / (1.0 + (norm_squared / self.alpha))
power = float(self.alpha + 1) / 2
numerator = numerator**power
t_dist = (numerator.t() / torch.sum(numerator, 1)).t() #soft assignment using t-distribution
return t_dist
class DEC(nn.Module):
def __init__(self, n_clusters=10, autoencoder=None, hidden=10, cluster_centers=None, alpha=1.0):
super(DEC, self).__init__()
self.n_clusters = n_clusters
self.alpha = alpha
self.hidden = hidden
self.cluster_centers = cluster_centers
self.autoencoder = autoencoder
self.clusteringlayer = ClusteringLayer(self.n_clusters, self.hidden, self.cluster_centers, self.alpha)
def target_distribution(self, q_):
weight = (q_ ** 2) / torch.sum(q_, 0)
return (weight.t() / torch.sum(weight, 1)).t()
def forward(self, x):
x = self.autoencoder.encode(x)
return self.clusteringlayer(x)
def visualize(self, epoch,x):
fig = plt.figure()
ax = plt.subplot(111)
x = self.autoencoder.encode(x).detach()
x = x.cpu().numpy()[:2000]
x_embedded = TSNE(n_components=2).fit_transform(x)
plt.scatter(x_embedded[:,0], x_embedded[:,1])
fig.savefig('plots/mnist_{}.png'.format(epoch))
plt.close(fig)
def add_noise(img):
noise = torch.randn(img.size()) * 0.2
noisy_img = img + noise
return noisy_img
def save_checkpoint(state, filename, is_best):
"""Save checkpoint if a new best is achieved"""
if is_best:
print("=> Saving new checkpoint")
torch.save(state, filename)
else:
print("=> Validation Accuracy did not improve")
def pretrain(**kwargs):
data = kwargs['data']
model = kwargs['model']
num_epochs = kwargs['num_epochs']
savepath = kwargs['savepath']
checkpoint = kwargs['checkpoint']
start_epoch = checkpoint['epoch']
parameters = list(autoencoder.parameters())
optimizer = torch.optim.Adam(parameters, lr=1e-3, weight_decay=1e-5)
train_loader = DataLoader(dataset=data,
batch_size=128,
shuffle=True)
for epoch in range(start_epoch, num_epochs):
for data in train_loader:
img = data.float()
noisy_img = add_noise(img)
noisy_img = noisy_img.to(device)
img = img.to(device)
# ===================forward=====================
output = model(noisy_img)
output = output.squeeze(1)
output = output.view(output.size(0), 28*28)
loss = nn.MSELoss()(output, img)
# ===================backward====================
optimizer.zero_grad()
loss.backward()
optimizer.step()
# ===================log========================
print('epoch [{}/{}], MSE_loss:{:.4f}'
.format(epoch + 1, num_epochs, loss.item()))
state = loss.item()
is_best = False
if state < checkpoint['best']:
checkpoint['best'] = state
is_best = True
save_checkpoint({
'state_dict': model.state_dict(),
'best': state,
'epoch':epoch
}, savepath,
is_best)
def train(**kwargs):
data = kwargs['data']
labels = kwargs['labels']
model = kwargs['model']
num_epochs = kwargs['num_epochs']
savepath = kwargs['savepath']
checkpoint = kwargs['checkpoint']
start_epoch = checkpoint['epoch']
features = []
train_loader = DataLoader(dataset=data,
batch_size=128,
shuffle=False)
for i, batch in enumerate(train_loader):
img = batch.float()
img = img.to(device)
features.append(model.autoencoder.encode(img).detach().cpu())
features = torch.cat(features)
# ============K-means=======================================
kmeans = KMeans(n_clusters=10, random_state=0).fit(features)
cluster_centers = kmeans.cluster_centers_
cluster_centers = torch.tensor(cluster_centers, dtype=torch.float).cuda()
model.clusteringlayer.cluster_centers = torch.nn.Parameter(cluster_centers)
# =========================================================
y_pred = kmeans.predict(features)
accuracy = acc(y.cpu().numpy(), y_pred)
print('Initial Accuracy: {}'.format(accuracy))
loss_function = nn.KLDivLoss(size_average=False)
optimizer = torch.optim.SGD(params=model.parameters(), lr=0.1, momentum=0.9)
print('Training')
row = []
for epoch in range(start_epoch, num_epochs):
batch = data
img = batch.float()
img = img.to(device)
output = model(img)
target = model.target_distribution(output).detach()
out = output.argmax(1)
if epoch % 20 == 0:
print('plotting')
dec.visualize(epoch, img)
loss = loss_function(output.log(), target) / output.shape[0]
optimizer.zero_grad()
loss.backward()
optimizer.step()
accuracy = acc(y.cpu().numpy(), out.cpu().numpy())
row.append([epoch, accuracy])
print('Epochs: [{}/{}] Accuracy:{}, Loss:{}'.format(epoch, num_epochs, accuracy, loss))
state = loss.item()
is_best = False
if state < checkpoint['best']:
checkpoint['best'] = state
is_best = True
save_checkpoint({
'state_dict': model.state_dict(),
'best': state,
'epoch':epoch
}, savepath,
is_best)
df = pd.DataFrame(row, columns=['epochs', 'accuracy'])
df.to_csv('log.csv')
def load_mnist():
# the data, shuffled and split between train and test sets
train = MNIST(root='./data/',
train=True,
transform=transforms.ToTensor(),
download=True)
test = MNIST(root='./data/',
train=False,
transform=transforms.ToTensor())
x_train, y_train = train.train_data, train.train_labels
x_test, y_test = test.test_data, test.test_labels
x = torch.cat((x_train, x_test), 0)
y = torch.cat((y_train, y_test), 0)
x = x.reshape((x.shape[0], -1))
x = np.divide(x, 255.)
print('MNIST samples', x.shape)
return x, y
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='train',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--batch_size', default=128, type=int)
parser.add_argument('--pretrain_epochs', default=20, type=int)
parser.add_argument('--train_epochs', default=200, type=int)
parser.add_argument('--save_dir', default='saves')
args = parser.parse_args()
print(args)
epochs_pre = args.pretrain_epochs
batch_size = args.batch_size
x, y = load_mnist()
autoencoder = AutoEncoder().to(device)
ae_save_path = 'saves/sim_autoencoder.pth'
if os.path.isfile(ae_save_path):
print('Loading {}'.format(ae_save_path))
checkpoint = torch.load(ae_save_path)
autoencoder.load_state_dict(checkpoint['state_dict'])
else:
print("=> no checkpoint found at '{}'".format(ae_save_path))
checkpoint = {
"epoch": 0,
"best": float("inf")
}
pretrain(data=x, model=autoencoder, num_epochs=epochs_pre, savepath=ae_save_path, checkpoint=checkpoint)
dec_save_path='saves/dec.pth'
dec = DEC(n_clusters=10, autoencoder=autoencoder, hidden=10, cluster_centers=None, alpha=1.0).to(device)
if os.path.isfile(dec_save_path):
print('Loading {}'.format(dec_save_path))
checkpoint = torch.load(dec_save_path)
dec.load_state_dict(checkpoint['state_dict'])
else:
print("=> no checkpoint found at '{}'".format(dec_save_path))
checkpoint = {
"epoch": 0,
"best": float("inf")
}
train(data=x, labels=y, model=dec, num_epochs=args.train_epochs, savepath=dec_save_path, checkpoint=checkpoint)