-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAE_Architecture_2507_2.py
86 lines (70 loc) · 2.63 KB
/
AE_Architecture_2507_2.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
import torch
from torch import nn
from image_size_calculations import image_size_after_convolution
encoded_space_dim = 50
class Encoder(nn.Module):
def __init__(self, image_size):
super().__init__()
image_size = image_size_after_convolution(image_size, 3, 1, 2)
image_size = image_size_after_convolution(image_size, 3, 1, 2)
#image_size = image_size / 2 #max pooling
image_size = image_size_after_convolution(image_size, 3, 1, 2)
### Convolutional layers
self.cnn = nn.Sequential(
nn.Conv2d(3, 8, 3, stride=2, padding=1),
nn.ReLU(True),
nn.MaxPool2d(2, stride=1),
nn.BatchNorm2d(8),
nn.Conv2d(8, 16, 3, stride=2, padding=1),
nn.ReLU(True),
nn.MaxPool2d(2, stride=1),
nn.BatchNorm2d(16),
nn.Conv2d(16, 32, 3, stride=2, padding=1),
nn.ReLU(True)
)
# Flatten layer
self.flatten = nn.Flatten(start_dim=1)
# Linear layers
self.lin = nn.Sequential(
nn.Linear(image_size * image_size * 32, 128),
nn.ReLU(True),
nn.Linear(128, encoded_space_dim)
)
def forward(self, x):
x = self.cnn(x)
x = self.flatten(x)
x = self.lin(x)
return x
class Decoder(nn.Module):
def __init__(self, image_size):
super().__init__()
image_size = image_size_after_convolution(image_size, 3, 1, 2)
image_size = image_size_after_convolution(image_size, 3, 1, 2)
#image_size = image_size / 2 #max pooling
image_size = image_size_after_convolution(image_size, 3, 1, 2)
# 1 - Linear layers
self.lin = nn.Sequential(
nn.Linear(encoded_space_dim, 128),
nn.ReLU(True),
nn.Linear(128, image_size * image_size * 32),
)
# 2 - Flatten layer
self.unflatten = nn.Unflatten(dim=1, unflattened_size=(32, image_size, image_size))
### 3 - Convolutional layers
self.cnn = nn.Sequential(
nn.ConvTranspose2d(32, 16, 3, stride=2, padding=1, output_padding=1),
nn.ReLU(True),
nn.BatchNorm2d(16),
#nn.Upsample(scale_factor=1),
nn.ConvTranspose2d(16, 8, 3, stride=2, padding=1, output_padding=1),
nn.BatchNorm2d(8),
#nn.Upsample(scale_factor=1),
nn.ReLU(True),
nn.ConvTranspose2d(8, 3, 3, stride=2, padding=1, output_padding=1)
)
def forward(self, x):
x = self.lin(x)
x = self.unflatten(x)
x = self.cnn(x)
x = torch.sigmoid(x)
return x