This repository has been archived by the owner on Jun 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
model.py
257 lines (222 loc) · 8.6 KB
/
model.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
''' model
This file contains VAE model definition for MNIST, FASHIONMNIST, SVHN and CELEBA dataset.
'''
import torch
import torch.utils.data
from torch import nn
class MNISTEncoder(nn.Module):
def __init__(self, nc, nef, nz, isize, device):
super(MNISTEncoder, self).__init__()
# Device
self.device = device
# Encoder: (nc, isize, isize) -> (nef*8, isize//8, isize//8)
self.encoder = nn.Sequential(
nn.Conv2d(nc, nef, 4, 2, padding=1),
nn.BatchNorm2d(nef),
nn.ReLU(True),
nn.Conv2d(nef, nef * 2, 4, 2, padding=1),
nn.BatchNorm2d(nef * 2),
nn.ReLU(True),
nn.Conv2d(nef * 2, nef * 4, 4, 2, padding=1),
nn.BatchNorm2d(nef * 4),
nn.ReLU(True),
nn.Conv2d(nef * 4, nef * 8, 4, 2, padding=1),
nn.BatchNorm2d(nef * 8),
nn.ReLU(True)
)
out_size = isize // 16
self.fc1 = nn.Linear(nef * 8 * out_size * out_size, nz)
def forward(self, inputs):
# Batch size
batch_size = inputs.size(0)
hidden = self.encoder(inputs)
# Reshape
hidden = hidden.view(batch_size, -1)
latent_z = self.fc1(hidden)
return latent_z
class MNISTDecoder(nn.Module):
def __init__(self, nc, ndf, nz, isize):
super(MNISTDecoder, self).__init__()
# Map the latent vector to the feature map space
self.ndf = ndf
self.out_size = isize // 16
self.fc1 = nn.Sequential(
nn.Linear(nz, 2 * 2 * 1024),
nn.ReLU(True),
)
# Decoder: (ndf*8, isize//16, isize//16) -> (nc, isize, isize)
self.decoder_conv = nn.Sequential(
nn.ConvTranspose2d(ndf * 8, ndf * 4, kernel_size=4, stride=2, padding=1),
nn.BatchNorm2d(ndf * 4),
nn.ReLU(True),
nn.ConvTranspose2d(ndf * 4, ndf * 2, kernel_size=4, stride=2, padding=1),
nn.BatchNorm2d(ndf * 2),
nn.ReLU(True),
nn.ConvTranspose2d(ndf * 2, ndf, kernel_size=4, stride=2, padding=1),
nn.BatchNorm2d(ndf),
nn.ReLU(True),
nn.ConvTranspose2d(ndf, nc, kernel_size=4, stride=2, padding=1),
nn.Sigmoid()
)
def forward(self, input):
input = self.fc1(input)
input = input.view(input.size(0), 1024, 2, 2)
output = self.decoder_conv(input)
return output
class SVHNEncoder(nn.Module):
def __init__(self, nc, nef, nz, isize, device):
super(SVHNEncoder, self).__init__()
# Device
self.device = device
# Encoder: (nc, isize, isize) -> (nef*8, isize//8, isize//8)
self.encoder = nn.Sequential(
nn.Conv2d(nc, nef, 4, 2, padding=1),
nn.BatchNorm2d(nef),
nn.ReLU(True),
nn.Conv2d(nef, nef * 2, 4, 2, padding=1),
nn.BatchNorm2d(nef * 2),
nn.ReLU(True),
nn.Conv2d(nef * 2, nef * 4, 4, 2, padding=1),
nn.BatchNorm2d(nef * 4),
nn.ReLU(True),
nn.Conv2d(nef * 4, nef * 8, 4, 2, padding=1),
nn.BatchNorm2d(nef * 8),
nn.ReLU(True)
)
out_size = isize // 16
self.fc1 = nn.Linear(nef * 8 * out_size * out_size, nz)
def forward(self, inputs):
# Batch size
batch_size = inputs.size(0)
hidden = self.encoder(inputs)
hidden = hidden.view(batch_size, -1)
latent_z = self.fc1(hidden)
return latent_z
class SVHNDecoder(nn.Module):
def __init__(self, nc, ndf, nz, isize):
super(SVHNDecoder, self).__init__()
# Map the latent vector to the feature map space
self.ndf = ndf
self.out_size = isize // 16
self.fc1 = nn.Sequential(
nn.Linear(nz, 2 * 2 * 1024),
nn.ReLU(True)
)
# Decoder: (ndf*8, isize//16, isize//16) -> (nc, isize, isize)
self.conv1 = nn.ConvTranspose2d(ndf * 8, ndf * 4, kernel_size=4, stride=2, padding=1)
self.conv2 = nn.ConvTranspose2d(ndf * 4, ndf * 2, kernel_size=4, stride=2, padding=1)
self.conv3 = nn.ConvTranspose2d(ndf * 2, ndf, kernel_size=4, stride=2, padding=1)
self.conv4 = nn.ConvTranspose2d(ndf, nc, kernel_size=4, stride=2, padding=1)
self.decoder_conv = nn.Sequential(
self.conv1,
nn.BatchNorm2d(ndf * 4),
nn.ReLU(True),
self.conv2,
nn.BatchNorm2d(ndf * 2),
nn.ReLU(True),
self.conv3,
nn.BatchNorm2d(ndf),
nn.ReLU(True),
self.conv4,
nn.Sigmoid()
)
def forward(self, input):
input = self.fc1(input)
input = input.view(input.size(0), 1024, 2, 2)
output = self.decoder_conv(input)
return output
class CELEBEncoder(nn.Module):
def __init__(self, nc, nef, nz, isize, device):
super(CELEBEncoder, self).__init__()
# Device
self.device = device
# Encoder: (nc, isize, isize) -> (nef*8, isize//8, isize//8)
self.encoder = nn.Sequential(
nn.Conv2d(nc, nef, 4, 2, padding=1),
nn.ReLU(True),
nn.BatchNorm2d(nef),
nn.Conv2d(nef, nef * 2, 4, 2, padding=1),
nn.ReLU(True),
nn.BatchNorm2d(nef * 2),
nn.Conv2d(nef * 2, nef * 4, 4, 2, padding=1),
nn.ReLU(True),
nn.BatchNorm2d(nef * 4),
nn.Conv2d(nef * 4, nef * 8, 4, 2, padding=1),
nn.ReLU(True),
nn.BatchNorm2d(nef * 8),
)
out_size = isize // 16
self.fc1 = nn.Linear(nef * 8 * out_size * out_size, nz)
def forward(self, inputs):
# Batch size
batch_size = inputs.size(0)
hidden = self.encoder(inputs)
hidden = hidden.view(batch_size, -1)
latent_z = self.fc1(hidden)
return latent_z
class CELEBDecoder(nn.Module):
def __init__(self, nc, ndf, nz, isize):
super(CELEBDecoder, self).__init__()
# Map the latent vector to the feature map space
self.ndf = ndf
self.out_size = isize // 16
self.fc1 = nn.Sequential(
nn.Linear(nz, 4 * 4 * 1024),
nn.ReLU(True)
)
# Decoder: (ndf*8, isize//16, isize//16) -> (nc, isize, isize)
self.conv1 = nn.ConvTranspose2d(ndf * 8, ndf * 4, kernel_size=4, stride=2, padding=1)
self.conv2 = nn.ConvTranspose2d(ndf * 4, ndf * 2, kernel_size=4, stride=2, padding=1)
self.conv3 = nn.ConvTranspose2d(ndf * 2, ndf, kernel_size=4, stride=2, padding=1)
self.conv4 = nn.ConvTranspose2d(ndf, nc, kernel_size=4, stride=2, padding=1)
self.decoder_conv = nn.Sequential(
self.conv1,
nn.ReLU(True),
nn.BatchNorm2d(ndf * 4),
self.conv2,
nn.ReLU(True),
nn.BatchNorm2d(ndf * 2),
self.conv3,
nn.ReLU(True),
nn.BatchNorm2d(ndf),
self.conv4,
nn.Sigmoid()
)
def forward(self, input):
input = self.fc1(input)
input = input.view(input.size(0), 1024, 4, 4)
output = self.decoder_conv(input)
return output
class VAE(nn.Module):
def __init__(self, dataset="MNIST", nc=1, ndf=32, nef=32, nz=16, isize=128, latent_noise_scale=1e-3,
device=torch.device("cuda:0"), is_train=True):
super(VAE, self).__init__()
self.nz = nz
self.is_train = is_train
self.latent_noise_scale = latent_noise_scale
if dataset == "MNIST" or dataset == "FASHIONMNIST" :
# Encoder
self.encoder = MNISTEncoder(nc=nc, nef=nef, nz=nz, isize=isize, device=device)
# Decoder
self.decoder = MNISTDecoder(nc=nc, ndf=ndf, nz=nz, isize=isize)
elif dataset == "SVHN":
# Encoder
self.encoder = SVHNEncoder(nc=nc, nef=nef, nz=nz, isize=isize, device=device)
# Decoder
self.decoder = SVHNDecoder(nc=nc, ndf=ndf, nz=nz, isize=isize)
elif dataset == "CELEB":
# Encoder
self.encoder = CELEBEncoder(nc=nc, nef=nef, nz=nz, isize=isize, device=device)
# Decoder
self.decoder = CELEBDecoder(nc=nc, ndf=ndf, nz=nz, isize=isize)
def forward(self, images):
z = self.encode(images)
if self.is_train:
z_noise = self.latent_noise_scale * torch.randn((images.size(0), self.nz), device=z.device)
else:
z_noise = 0.0
return self.decode(z + z_noise), z
def encode(self, images):
return self.encoder(images)
def decode(self, z):
return self.decoder(z)