-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoencoder.py
147 lines (123 loc) · 4.43 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
import torch
import torch.nn as nn
from math import ceil
class Encoder(nn.Module):
def __init__(
self,
input_size: int,
input_channels:int,
bottleneck_dim:int,
hidden_channels:int = 8
) -> None:
super().__init__()
_n_modules = 5
self.encoder = nn.Sequential(
nn.Conv2d(input_channels, hidden_channels, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(hidden_channels),
nn.ReLU(),
)
# B x C x M x M -> B x C*2^(_n_modules) x M/2^(_n_modules) x M/2^(_n_modules)
# B x 3 x 224 x 224 -> B x 8192 x 4 x 4
for i in range(_n_modules):
self.encoder.add_module(
name=f"ConvBlock-{ceil(input_size/(2**i))}->{ceil(input_size/(2**(i+1)))}-" + str(i),
module=Encoder._conv_block((2**i)*hidden_channels, (2**(i+1))*hidden_channels)
)
# This is what we get after the convolutions
hidden_channels *= (2**_n_modules)
hidden_size = ceil(input_size/(2**_n_modules))
# output
self.encoder.add_module(
name="output",
module=nn.Sequential(
nn.Flatten(),
nn.Linear(hidden_size*hidden_size*hidden_channels, bottleneck_dim),
))
@staticmethod
def _conv_block(input_channels:int, output_channels:int) -> nn.Sequential:
return nn.Sequential(
nn.Conv2d(input_channels, output_channels, kernel_size=3, stride=2, padding=1),
nn.ReLU(),
#nn.MaxPool2d((2,2)),
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.encoder(x)
#print("Encoder output shape:", x.shape)
return x
class Decoder(nn.Module):
def __init__(
self,
input_size: int,
input_channels:int,
bottleneck_dim:int,
hidden_channels:int = 8,
) -> None:
super().__init__()
_n_modules = 5
input_dim = ceil(input_size/(2**_n_modules))
input_dim *= input_dim
input_dim *= hidden_channels*(2**_n_modules)
self.linear = nn.Sequential(
nn.Linear(bottleneck_dim, input_dim),
nn.ReLU(),
)
self.decoder = nn.Sequential()
# B x C x M x M -> B x C/2^(_n_modules) x M*2^(_n_modules) x M*2^(_n_modules)
# B x 8192 x 4 x 4 -> B x 8 x 224 x 224
for i in range(_n_modules, 0, -1):
self.decoder.add_module(
name=f"DeconvBlock-{ceil(input_size/(2**i))}->{ceil(input_size/(2**(i-1)))}-" + str(_n_modules-i),
module=self._deconv_block((2**i)*hidden_channels, (2**(i-1))*hidden_channels)
)
self.decoder.add_module(
name="output",
module=nn.Sequential(
nn.Conv2d(hidden_channels, input_channels, kernel_size=1, stride=1, padding=0),
nn.Sigmoid()
)
)
def _deconv_block(self, input_channels:int, output_channels:int)->nn.Sequential:
return nn.Sequential(
nn.ConvTranspose2d(input_channels, output_channels, kernel_size=4, stride=2, padding=1),
nn.ReLU()
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.linear(x)
x = x.view(x.shape[0], -1, 7, 7)
x = self.decoder(x)
#print("Decoder ouput shape:", x.shape)
return x
class Autoencoder(nn.Module):
def __init__(
self,
input_size: int,
bottleneck_dim:int,
input_channels:int = 3,
) -> None:
super().__init__()
self.encoder = Encoder(
input_size=input_size,
input_channels=input_channels,
bottleneck_dim=bottleneck_dim,
)
self.decoder = Decoder(
input_size=input_size,
input_channels=input_channels,
bottleneck_dim=bottleneck_dim,
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.encoder(x)
x = self.decoder(x)
return x
if __name__ == "__main__":
img_shape = (1,3,224,224)
ae = Autoencoder(
input_size=img_shape[2],
bottleneck_dim=128,
input_channels=img_shape[1]
)
print(ae)
noise = torch.rand(img_shape)
out = ae(noise)
print(out)
print(out.shape, "OK!" if noise.shape == out.shape else f"\nWARNING: Output shape differs from input shape. Should have been: {noise.shape}")