-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.py
368 lines (287 loc) · 12.8 KB
/
encoder.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import os
import time
import torch
import numpy as np
import pandas as pd
import torch.nn as nn
import torch.optim as optim
import matplotlib.pyplot as plt
import torch.nn.functional as F
from tqdm import tqdm
class Autoencoder(nn.Module):
def __init__(self, latent_channels=16, input_shape=(28,28)):
"""
Autoencoder class for image data compression and reconstruction.
Args:
channels (int, optional): Number of input channels. Default is 4.
depth (int, optional): Depth of the encoder and decoder, controlling
the number of convolutional layers. Default is 3.
conv_depth (int, optional): Depth of each convolutional layer (number of filters)
Attributes:
encoder (Encoder): The encoder part of the autoencoder.
decoder (Decoder): The decoder part of the autoencoder.
"""
super(Autoencoder, self).__init__()
self.encoder = Encoder(latent_channels,input_shape)
self.decoder = Decoder(latent_channels,input_shape)
def forward(self, x):
encoded = self.encoder(x)
decoded = self.decoder(encoded)
return decoded
class Encoder(nn.Module):
def __init__(self, latent_channels=16, input_shape=(28,28)):
"""
Encoder class for the autoencoder, responsible for feature extraction.
Args:
channels (int): Number of input channels.
depth (int, optional): Depth of the encoder, controlling the number
of convolutional layers. Default is 3.
conv_depth (int, optional): Depth of each convolutional layer (number of filters)
"""
super(Encoder, self).__init__()
self.encoder = nn.Sequential(
nn.Conv2d(4, 64, kernel_size=3, padding=1),
nn.ReLU(),
#nn.AdaptiveAvgPool2d((14, 14)),
nn.MaxPool2d(kernel_size=(2,2), stride=(2,2)),
nn.Conv2d(64, 128, kernel_size=3, padding=1),
nn.ReLU(),
#nn.AdaptiveAvgPool2d((7, 7)),
nn.MaxPool2d(kernel_size=(2,2), stride=(2,2)),
nn.Conv2d(128, 128, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(128, latent_channels, kernel_size=3, padding=1),
nn.ReLU()
)
def forward(self, x):
encoded = self.encoder(x)
return encoded
class Decoder(nn.Module):
def __init__(self,latent_channels=16,input_shape=(28,28)):
"""
Decoder class for the autoencoder, responsible for image reconstruction.
Args:
channels (int): Number of output channels.
depth (int, optional): Depth of the decoder, controlling the number
of convolutional layers. Default is 3.
conv_depth (int, optional): Depth of each convolutional layer (number of filters)
"""
super(Decoder, self).__init__()
if input_shape==[14,14]:
padding=0
else:
padding=1
decoder_layers = []
decoder_layers.append(nn.ConvTranspose2d(latent_channels, 64, kernel_size=4, stride=2, padding=1))
decoder_layers.append(nn.ReLU())
decoder_layers.append(nn.ConvTranspose2d(64, 4, kernel_size=4, stride=2, padding=padding))
decoder_layers.append(nn.Sigmoid())
self.decoder = nn.Sequential(*decoder_layers)
def forward(self, x):
decoded = self.decoder(x)
return decoded
class FlatAutoencoder(nn.Module):
def __init__(self, image_size, embedding_dim=128, channels=4):
"""
Autoencoder class for image data compression and reconstruction.
Args:
channels (int, optional): Number of input channels. Default is 4.
depth (int, optional): Depth of the encoder and decoder, controlling
the number of convolutional layers. Default is 3.
conv_depth (int, optional): Depth of each convolutional layer (number of filters)
Attributes:
encoder (Encoder): The encoder part of the autoencoder.
decoder (Decoder): The decoder part of the autoencoder.
"""
super(FlatAutoencoder, self).__init__()
self.encoder = FlatEncoder(image_size,channels,embedding_dim)
self.shape_before_flattening = self.encoder.shape_before_flatten(channels,image_size)
self.decoder = FlatDecoder(embedding_dim,self.shape_before_flattening,channels,image_size)
def forward(self, x):
encoded = self.encoder(x)
#print(f"encoded shape: {encoded.shape}")
decoded = self.decoder(encoded)
#print(f'decoded shape: {decoded.shape}')
#return decoded
return decoded
class FlatEncoder(nn.Module):
def __init__(self, image_size, channels, latent_dim):
super(FlatEncoder, self).__init__()
# define convolutional blocks
self.conv1 = nn.Conv2d(channels, 64, kernel_size=3, stride=2, padding=1)
self.conv2 = nn.Conv2d(64, 128, kernel_size=3, stride=2, padding=1)
self.conv3 = nn.Conv2d(128, latent_dim, kernel_size=3, stride=2, padding=1)
# store shape before flatten
self.shape_before_flattening = None
# compute the flattened size after convolutions
self.flattened_size = self.shape_after_flatten(channels,image_size)
# define fully connected layer to create embeddings
#self.fc = nn.Linear(self.flattened_size, latent_dim)
self.fc = nn.Linear(latent_dim, latent_dim)
def shape_before_flatten(self,channels,image_size):
'''
Calculate the shape of data before flattening to dense layers
Used in decoder to structure reshape layer (1D -> 3D)
'''
x = torch.randn((1,channels,image_size,image_size))
x = F.relu(self.conv1(x))
x = F.relu(self.conv2(x))
x = F.relu(self.conv3(x))
# store the shape before flattening
self.shape_before_flattening = x.shape[1:]
return self.shape_before_flattening
def shape_after_flatten(self,channels,image_size):
'''
Calculate shape of data after flattening from 3D to 1D
Used to determine dense unit input shape (Conv -> Dense)
'''
x = torch.randn((1,channels,image_size,image_size))
x = F.relu(self.conv1(x))
x = F.relu(self.conv2(x))
x = F.relu(self.conv3(x))
# flatten the tensor
x = x.view(x.size(0), -1)
return list(x.shape[1:])[0]
def forward(self, x):
# move through conv blocks
x = F.relu(self.conv1(x))
x = F.relu(self.conv2(x))
x = F.relu(self.conv3(x))
# flatten the tensor
#x = x.view(x.size(0), -1)
# apply global average pooling to the tensor
x = torch.mean(x,dim=(2,3))
# apply fully connected layer to generate embeddings
x = self.fc(x)
return x
class FlatDecoder(nn.Module):
def __init__(self, latent_dim, shape_before_flattening, channels, image_size):
super(FlatDecoder, self).__init__()
# define fully connected layer to unflatten the embeddings
self.fc = nn.Linear(latent_dim, np.prod(shape_before_flattening))
# store the shape before flattening
self.reshape_dim = shape_before_flattening
# correct reconstruction shape
if image_size == 14: padding=1
else: padding=0
# handle 2x2 image properly
if image_size == 2:
zero_pad = 2
else:
zero_pad = 1
# define transpose convolutional layers
self.deconv1 = nn.ConvTranspose2d(latent_dim, 128, kernel_size=3, stride=2, padding=1, output_padding=0)
self.deconv2 = nn.ConvTranspose2d(128, 64, kernel_size=4, stride=2, padding=1, output_padding=padding)
self.deconv3 = nn.ConvTranspose2d(64, 32, kernel_size=4, stride=2, padding=zero_pad, output_padding=0)
# define final convolutional layer to generate output image
self.conv1 = nn.Conv2d(32, channels, kernel_size=3, stride=1, padding=1)
def forward(self, x):
# apply fully connected layer to unflatten the embeddings
x = self.fc(x)
# reshape the tensor to match shape before flattening
x = x.view(x.size(0), *self.reshape_dim)
# transpose conv blocks
x = F.relu(self.deconv1(x))
x = F.relu(self.deconv2(x))
x = F.relu(self.deconv3(x))
# normal conv block at end with sigmoid activation
x = torch.sigmoid(self.conv1(x))
return x
def plot_autoencoder_reconstructions(model, val_loader, epoch, device, run_id=None):
'''
Plot train image next to reconstruction
Args:
model (Autoencoder): model to evaluate
val_loader (DualLoader)
'''
model.eval()
next_batch, _ = next(iter(val_loader))
next_image = next_batch[0][0]
fig, axs = plt.subplots(1,2,figsize=(8,4))
#fig.suptitle(f"Autoencoder Results | Latent Dim = 256")
# plotting full size data
original_image = next_image[:3].permute(1,2,0)
axs[0].imshow(original_image)
axs[0].set_title(f"Original ({original_image.shape[0]}x{original_image.shape[1]})")
prediction = model(next_batch[0].to(device))[0][:3].permute(1,2,0).to('cpu').detach()
axs[1].imshow(prediction)
axs[1].set_title(f"Reconstructed ({prediction.shape[0]}x{prediction.shape[1]})")
# turn off tickmarks
axs[0].set_xticks([])
axs[0].set_yticks([])
axs[1].set_xticks([])
axs[1].set_yticks([])
if run_id is not None:
plt.savefig(f"experiments/models/{run_id}/reconstruction_epoch{epoch}.png")
else:
plt.savefig(f"encoder_test_results/reconstuction_epoch_{epoch}.png")
return
def train_autoencoder(autoencoder, train_loader, val_loader, num_epochs=10, loss="binary_crossentropy", run_id=None):
'''
Train an autoencoder on a data loader
Args:
autoencoder (Autoencoder): model to be trained
train_loader (DataLoader): dataset to train on
val_loader (DataLoader): dataset to validate on
num_epochs: number of epochs to train
'''
# create directory for results if it doesn't exist
if "encoder_test_results" not in os.listdir():
os.makedirs("encoder_test_results")
if loss=="binary_crossentropy":
criterion = nn.BCELoss()
elif loss=="mse":
criterion = nn.MSELoss()
optimizer = optim.Adam(autoencoder.parameters(), lr=0.001)
epoch_losses = []
val_epoch_losses = []
print("Training autoencoder...")
overall_start_time = time.time()
for epoch in range(num_epochs):
epoch_start_time = time.time()
running_loss = 0.0
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# train through epoch
autoencoder = autoencoder.to(device)
autoencoder.train() # set model to training
for images,y in tqdm(train_loader):
inputs, _, _ = images
inputs = inputs.to(device)
optimizer.zero_grad()
outputs = autoencoder(inputs) # bug: input 2x2 outputs 4x4
loss = criterion(outputs, inputs)
loss.backward()
optimizer.step()
running_loss += loss.item()
epoch_loss = round(running_loss / len(train_loader), 5)
epoch_losses.append(epoch_loss)
# validate every epoch
autoencoder.eval() # set model to eval
val_loss = 0.0
with torch.no_grad():
for images, y in val_loader:
inputs, _, _ = images
inputs = inputs.to(device)
outputs = autoencoder(inputs)
val_loss += criterion(outputs, inputs).item()
epoch_val_loss = round(val_loss / len(val_loader), 5)
val_loss = round(val_loss / len(val_loader), 5)
val_epoch_losses.append(val_loss)
# log results and plot predictions by epoch
print(f"Epoch {epoch+1}/{num_epochs}, Train Loss: {epoch_loss}, Val Loss: {epoch_val_loss} Epoch Time: {round(time.time()-epoch_start_time,2)} seconds")
plot_autoencoder_reconstructions(autoencoder,val_loader,epoch,device,run_id=run_id)
# save model after every epoch
if run_id is not None:
torch.save(autoencoder,f"experiments/models/{run_id}/autoencoder_epoch_{epoch}.pth")
else:
torch.save(autoencoder,f"encoder_test_results/autoencoder_epoch_{epoch}.pth")
data = {'epoch': range(1, len(epoch_losses) + 1),
'train_loss': epoch_losses,
'val_loss': val_epoch_losses}
df = pd.DataFrame(data)
if run_id is not None:
df.to_csv(f"experiments/models/{run_id}/autoencoder_loss.csv")
else:
df.to_csv("encoder_test_results/loss.csv", index=False)
print(f"Finished Training - Total Train Time = {round(time.time()-overall_start_time,2)}")
return