-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoencoder.py
119 lines (95 loc) · 4.86 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
import torch.nn as nn
import torch
class AutoEncoder(nn.Module):
def __init__(self, encoder_layer_sizes, decoder_layer_sizes, latent_dim=512, bw=False):
super(AutoEncoder, self).__init__()
self.latent_dim = latent_dim
self.encoder = Encoder(encoder_layer_sizes, latent_dim, bw)
self.decoder = Decoder(decoder_layer_sizes, latent_dim, bw)
def forward(self, x):
z = self.encoder.forward(x)
recon_x = self.decoder.forward(z[-1])
return recon_x[-1]
class Encoder(nn.Module):
def __init__(self, block_sizes, latent_dim, bw):
super(Encoder, self).__init__()
bw_pool = Identity()
lin_size = 2048
if bw:
bw_pool = nn.AvgPool2d(2)
lin_size = 512
self.blocks = nn.ModuleList([nn.Sequential(nn.Conv2d(1, 64, 3, padding=1),
*(nn.LeakyReLU(), nn.Conv2d(64, 64, 3, padding=1))*block_sizes[0],
bw_pool),
nn.Sequential(*(nn.LeakyReLU(), nn.Conv2d(64, 64, 3, padding=1))*block_sizes[1],
nn.LeakyReLU(),
nn.Conv2d(64, 128, 3, padding=1, stride=2)),
nn.Sequential(*(nn.LeakyReLU(), nn.Conv2d(128, 128, 3, padding=1))*block_sizes[2],
nn.LeakyReLU(),
nn.Conv2d(128, 128, 3, padding=1, stride=2)),
nn.Sequential(nn.LeakyReLU(),
nn.AvgPool2d(4),
nn.BatchNorm2d(128),
Flatten(),
*(nn.Linear(lin_size, lin_size), nn.LeakyReLU())*block_sizes[3],
nn.Linear(lin_size, latent_dim))
]
)
def forward(self, x):
outputs = [x]
for m in self.blocks:
outputs.append(m(outputs[-1]))
return outputs[1:]
class Decoder(nn.Module):
def __init__(self, block_sizes, latent_dim, bw):
super(Decoder, self).__init__()
bw_pool = Identity()
#if bw:
# bw_pool = nn.ConvTranspose2d(64, 64, 3, padding=1, stride=2, output_padding=1)
self.blocks = nn.ModuleList([nn.Sequential(nn.Linear(512, 2048),
*(nn.LeakyReLU(), nn.Linear(2048, 2048))*block_sizes[0],
nn.LeakyReLU(),
View((-1, 128, 4, 4)),
nn.BatchNorm2d(128),
nn.ConvTranspose2d(128, 128, 6, padding=0, stride=3, output_padding=1),
),
nn.Sequential(nn.ConvTranspose2d(128, 128, 3, padding=1, stride=2, output_padding=1),
*(nn.LeakyReLU(), nn.Conv2d(128, 128, 3, padding=1))*block_sizes[1],
nn.LeakyReLU(),
),
nn.Sequential(nn.ConvTranspose2d(128, 64, 3, padding=1, stride=2, output_padding=1),
*(nn.LeakyReLU(), nn.Conv2d(64, 64, 3, padding=1))*block_sizes[2],
),
nn.Sequential(bw_pool,
*(nn.LeakyReLU(), nn.Conv2d(64, 64, 3, padding=1))*block_sizes[3],
nn.Conv2d(64, 1, 3, padding=1))
]
)
def forward(self, z):
outputs = [z]
for m in self.blocks:
outputs.append(m(outputs[-1]))
return outputs[1:]
class View(nn.Module):
def __init__(self, shape):
super().__init__()
self.shape = shape
def forward(self, input):
return input.view(*self.shape)
class Flatten(nn.Module):
def __init__(self, *args):
super().__init__()
def forward(self, x):
return x.view(x.shape[0], -1)
class Unpooling(nn.Module):
def __init__(self, size):
super().__init__()
self.maxunp = nn.MaxUnpool2d(size)
def forward(self, x):
ind = torch.ones(x.shape).to(x.device, dtype=torch.int64)
return self.maxunp(x, ind)
class Identity(nn.Module):
def __init__(self, *args, **kwargs):
super().__init__()
def forward(self, x):
return x