-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoencoder.py
185 lines (143 loc) · 5.58 KB
/
autoencoder.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
import numpy as np
import torch
import os
import cv2
from torch.utils.data import DataLoader
import matplotlib.pyplot as plt
k = 10
#orizoume to device gia ekpaideush sthn GPU
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
#8etoume to learning rate
lr = 0.00005
#pairnoume tis listes me ta onomata apo tous fakelous
train_image_names = os.listdir('./DataBase')
train_image_names.sort()
test_image_names = os.listdir('./test')
test_image_names.sort()
train_images = []
test_images = []
#prosthetoume se kathe lista ta dianusmata eikonwn
for img_name in train_image_names:
train_images.append(cv2.imread('./DataBase/'+img_name, cv2.IMREAD_GRAYSCALE).flatten())
for img_name in test_image_names:
test_images.append(cv2.imread('./test/'+img_name, cv2.IMREAD_GRAYSCALE).flatten())
train_images = np.array(train_images, dtype=np.double)
test_images = np.array(test_images, dtype=np.double)
#kanonikopoioume tis times twn eikonwn sto diasthma [0,1]
train_images /= 255
test_images /= 255
train_curve = []
test_curve = []
#orizoume thn arxitektonikh tou autoencoder
class Net(torch.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.encode = torch.nn.Sequential(
torch.nn.Linear(10000, 1000),
torch.nn.LeakyReLU(0.2),
torch.nn.Linear(1000, k)
)
self.decode = torch.nn.Sequential(
torch.nn.Linear(k, 1000),
torch.nn.LeakyReLU(0.2),
torch.nn.Linear(1000, 10000),
)
def encoder(self, x):
return self.encode(x)
def decoder(self, x):
return self.decode(x)
def forward(self, input):
return self.decoder(self.encoder(input))
#orizoume th domh tou dataset mas
class myDataset(torch.utils.data.Dataset):
def __init__(self, data):
self.data = data
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
data = self.data[idx]
sample = {'data' : data}
return sample
#dhmiourgoume ta train & test datasets
train_dataset = myDataset(train_images)
test_dataset = myDataset(test_images)
#dhmiourgoume tis train & test dataloaders me batch size 5 & 11 antistoixa
train_dataloader = DataLoader(train_dataset, batch_size = 5, shuffle=True, num_workers = 0)
test_dataloader = DataLoader(test_dataset, batch_size = 11, shuffle=False, num_workers = 0)
#arxikopoioume to diktyo mas
net = Net().double().to(device)
#orizoume ton optimizer
optimizer = torch.optim.Adam(net.parameters(), lr=lr)
#orizoume to loss pou 8a xrhsimopoihsoume
mse_loss = torch.nn.MSELoss()
#ekpaideuoume gia 300 epoxes
for epoch in range(300):
epoch_loss_train = 0
for a, data in enumerate(train_dataloader):
#pairnoume ena batch apo ta dedomena ekpaideushs
x = data['data'].double().to(device)
#pairnoume thn eksodo tou diktuou
x_rec = net(x)
#upologizoume to sfalma anakataskeuhs tou batch
rec_loss = mse_loss(x_rec,x)
#mhdenizoume ta gradients ths prohgoumenhs epoxhs,
#pairnoume ta nea gradients me vash to loss pou upologisame
#& ananewnoume ta varh tou diktuou
net.zero_grad()
rec_loss.backward()
optimizer.step()
epoch_loss_train += rec_loss.item()
#evaluation
net.eval()
epoch_loss_test = 0
for b, data in enumerate(test_dataloader):
#pairnoume ena batch apo ta dedomena elegxou
x = data['data'].double().to(device)
#pairnoume thn eksodo tou diktuou
x_rec = net(x)
#upologizoume to sfalma anakataskeuhs tou batch
rec_loss = mse_loss(x_rec,x)
epoch_loss_test += rec_loss.item()
net.train()
#ektupwnoume to train & test loss
print('Epoch : ',epoch)
print('Train Loss :', epoch_loss_train/(a+1))
print('Test Loss :', epoch_loss_test/(b+1))
train_curve.append(epoch_loss_train/(a+1))
test_curve.append(epoch_loss_test/(b+1))
#apo8hkeuoume tis kampules ekpaideushs & elegxou
plt.figure(figsize=(15,10))
plt.plot(train_curve)
plt.plot(test_curve)
plt.yscale('log')
plt.title('Loss curves for autoencoder with latent size='+str(k))
plt.legend(['Train Reconstruction Loss', 'Test Reconstruction Loss'])
plt.savefig('./reconstruction_loss_'+str(k)+'.png')
plt.close()
test_representations = []
train_representations = []
#prosthetoume se kathe lista ta dianusmata eikonwn
for img_name in train_image_names:
img = cv2.imread('./DataBase/'+img_name, cv2.IMREAD_GRAYSCALE).flatten()
tensor_img = torch.tensor(img,device=device).double().unsqueeze(0)
latent_representation = net.encode(tensor_img)
train_representations.append(latent_representation.detach().cpu().numpy())
#prosthetoume se kathe lista ta dianusmata eikonwn
for img_name in test_image_names:
img = cv2.imread('./test/'+img_name, cv2.IMREAD_GRAYSCALE).flatten()
tensor_img = torch.tensor(img,device=device).double().unsqueeze(0)
latent_representation = net.encode(tensor_img)
test_representations.append(latent_representation.detach().cpu().numpy())
success = 0
for i in range(len(test_representations)):
dist = np.zeros((100))
query = test_representations[i]
for j in range(len(train_representations)):
dist[j]=((query-train_representations[j])**2).mean()
min_val = dist.min()
if train_image_names[list(dist).index(min_val)] == test_image_names[i]:
success += 1
print('Success rate for k='+str(k)+':',success/11*100,'%')
torch.save(net, './autoencoder_'+str(k)+'.pth')