-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
212 lines (171 loc) · 6.98 KB
/
Copy pathmodel.py
File metadata and controls
212 lines (171 loc) · 6.98 KB
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
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from torchvision import datasets, transforms
# Define an VAE model
#TODO: implement KL divergence
class VAE(nn.Module):
def __init__(self):
super(VAE, self).__init__()
self.encoder = nn.Sequential(
# Input shape: (batch, 3, 64, 64)
# First Conv Layer
nn.Conv2d(3, 16, kernel_size=3, stride=2, padding=1),
nn.ReLU(),
# Second Conv Layer
nn.Conv2d(16, 8, kernel_size=3, stride=2, padding=1),
nn.Sigmoid(),
# Flatten the image
nn.Flatten(), #shape: (batch, 8*16*16)
)
self.encoder_mu = nn.Linear(8*16*16, 8*16*16)
self.encoder_logvar = nn.Linear(8*16*16, 8*16*16)
self.decoder = nn.Sequential(
# Reshape the image
nn.Unflatten(1, (8,16,16)),
# First DeConv Layer
nn.ConvTranspose2d(8, 16, kernel_size=3, stride=2, padding=1, output_padding=1),
nn.ReLU(),
# Second DeConv Layer
nn.ConvTranspose2d(16, 3, kernel_size=3, stride=2, padding=1, output_padding=1),
nn.Sigmoid()
# Output shape: (batch, 3, 64, 64)
)
def Sample(self, mu, logvar):
if self.training:
std = torch.exp(logvar * 0.5) #standard deviation
eps = torch.empty_like(std).normal_() #error term ##same shape with std, but is ~ N(0,1)
z = mu + (std * eps)
return z
else:
return mu
def forward(self, x):
x_encode = self.encoder(x)
x_mu, x_logvar = self.encoder_mu(x_encode), self.encoder_logvar(x_encode)
x_latent = self.Sample(x_mu, x_logvar)
x_reconstruct = self.decoder(x_latent)
return x_reconstruct, x_latent, x_mu, x_logvar
# Define the autoencoder model
#Conv2d: (in_channels, out_channels, kernel_size, stride=1, padding=0)
class Autoencoder(nn.Module):
def __init__(self):
super(Autoencoder, self).__init__()
self.encoder = nn.Sequential(
# Input shape: (batch, 3, 64, 64)
# First Conv Layer
nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
# Second Conv Layer
nn.Conv2d(16, 8, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2), #shape: (batch, 8, 16, 16)
# Flatten the image
nn.Flatten(), #shape: (batch, 8*16*16)
# Dense Layer to reduce it to two dimensions
nn.Linear(8*16*16, 2)
)
self.decoder = nn.Sequential(
# Dense Layer to recover it back to 8*16*16
nn.Linear(2, 8*16*16),
# Reshape the image
nn.Unflatten(1, (8,16,16)),
# First DeConv Layer
nn.ConvTranspose2d(8, 16, kernel_size=3, stride=2, padding=1, output_padding=1),
nn.ReLU(),
# Second DeConv Layer
nn.ConvTranspose2d(16, 3, kernel_size=3, stride=2, padding=1, output_padding=1),
nn.Sigmoid()
# Output shape: (batch, 3, 64, 64)
)
def forward(self, x):
x_latent = self.encoder(x)
x_reconstruct = self.decoder(x_latent)
return x_reconstruct, x_latent
#Conv2d: (in_channels, out_channels, kernel_size, stride=1, padding=0)
class Autoencoder_paper(nn.Module):
# Reference: https://www.ncbi.nlm.nih.gov/pmc/articles/PMC8622369/
def __init__(self):
super(Autoencoder_paper, self).__init__()
self.encoder = nn.Sequential(
# Input shape: (batch, 3, 64, 64)
# First Conv Layer
nn.Conv2d(3, 16, kernel_size=3, stride=2, padding=1),
nn.ReLU(),
# Second Conv Layer
nn.Conv2d(16, 8, kernel_size=3, stride=2, padding=1),
nn.Sigmoid()
# Flatten the image
#nn.Flatten(), #shape: (batch, 8*16*16)
# Dense Layer to reduce it to two dimensions
#nn.Linear(8*16*16, 2)
)
self.decoder = nn.Sequential(
# Dense Layer to recover it back to 8*16*16
#nn.Linear(2, 8*16*16),
# Reshape the image
#nn.Unflatten(1, (8,16,16)),
# First DeConv Layer
nn.ConvTranspose2d(8, 16, kernel_size=3, stride=2, padding=1, output_padding=1),
nn.ReLU(),
# Second DeConv Layer
nn.ConvTranspose2d(16, 3, kernel_size=3, stride=2, padding=1, output_padding=1),
nn.Sigmoid()
# Output shape: (batch, 3, 64, 64)
)
def forward(self, x):
x_latent = self.encoder(x)
x_reconstruct = self.decoder(x_latent)
return x_reconstruct, x_latent
class Autoencoder_mod(nn.Module):
# Reference: https://www.ncbi.nlm.nih.gov/pmc/articles/PMC8622369/
def __init__(self):
super(Autoencoder_mod, self).__init__()
self.encoder = nn.Sequential(
# Input shape: (batch, 3, 64, 64)
# First Conv Layer
nn.Conv2d(3, 16, kernel_size=3, stride=2, padding=1),
nn.ReLU(),
# Second Conv Layer
nn.Conv2d(16, 8, kernel_size=3, stride=2, padding=1),
nn.Sigmoid(),
# Flatten the image
nn.Flatten(), #shape: (batch, 8*16*16)
# Dense Layer to reduce it to two dimensions
nn.Linear(8*16*16, 4*16*16)
)
self.decoder = nn.Sequential(
# Dense Layer to recover it back to 8*16*16
nn.Linear(4*16*16, 8*16*16),
# Reshape the image
nn.Unflatten(1, (8,16,16)),
# First DeConv Layer
nn.ConvTranspose2d(8, 16, kernel_size=3, stride=2, padding=1, output_padding=1),
nn.ReLU(),
# Second DeConv Layer
nn.ConvTranspose2d(16, 3, kernel_size=3, stride=2, padding=1, output_padding=1),
nn.Sigmoid()
# Output shape: (batch, 3, 64, 64)
)
def forward(self, x):
x_latent = self.encoder(x)
x_reconstruct = self.decoder(x_latent)
return x_reconstruct, x_latent
if __name__ == "__main__":
#testing the model strucutre
val_dataset = torch.load('./torch_dataset/val_dataset.pt')
val_dataloader = DataLoader(val_dataset, batch_size=3, shuffle=False)
#defining model
model = VAE()
#input one image
for data in val_dataloader:
# format data
X, imgPATH = data
X = X.float()
# model outputs
X_rec, X_latent = model(X)
print("shape of X is:", X.size())
print("shape of X_latent is:", X_latent.size())
print("shape of X_rec is:", X_rec.size())
break