-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRES_VAE2.py
203 lines (157 loc) · 6.34 KB
/
RES_VAE2.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.data
use_cuda = torch.cuda.is_available()
GPU_indx = 0
device = torch.device(GPU_indx if use_cuda else "cpu")
class ResDown(nn.Module):
"""
Residual down sampling block for the encoder
"""
def __init__(self, channel_in, channel_out, scale=2):
super(ResDown, self).__init__()
self.conv1 = nn.Conv2d(channel_in, channel_out // 2, 3, 1, 1)
self.BN1 = nn.BatchNorm2d(channel_out // 2)
self.conv2 = nn.Conv2d(channel_out // 2, channel_out, 3, 1, 1)
self.BN2 = nn.BatchNorm2d(channel_out)
self.conv3 = nn.Conv2d(channel_in, channel_out, 3, 1, 1)
self.AvePool = nn.AvgPool2d(scale, scale)
def forward(self, x):
skip = self.conv3(self.AvePool(x))
x = F.rrelu(self.BN1(self.conv1(x)))
x = self.AvePool(x)
x = self.BN2(self.conv2(x))
x = F.rrelu(x + skip)
return x
class ResUp(nn.Module):
"""
Residual up sampling block for the decoder
"""
def __init__(self, channel_in, channel_out, scale=2):
super(ResUp, self).__init__()
self.conv1 = nn.Conv2d(channel_in, channel_out // 2, 3, 1, 1)
self.BN1 = nn.BatchNorm2d(channel_out // 2)
self.conv2 = nn.Conv2d(channel_out // 2, channel_out, 3, 1, 1)
self.BN2 = nn.BatchNorm2d(channel_out)
self.conv3 = nn.Conv2d(channel_in, channel_out, 3, 1, 1)
self.UpNN = nn.Upsample(scale_factor=scale, mode="nearest")
def forward(self, x):
skip = self.conv3(self.UpNN(x))
x = F.rrelu(self.BN1(self.conv1(x)))
x = self.UpNN(x)
x = self.BN2(self.conv2(x))
x = F.rrelu(x + skip)
return x
class Encoder(nn.Module):
"""
Encoder block
Built for a 3x64x64 image and will result in a latent vector of size z x 1 x 1
As the network is fully convolutional it will work for images LARGER than 64
For images sized 64 * n where n is a power of 2, (1, 2, 4, 8 etc) the latent feature map size will be z x n x n
When in .eval() the Encoder will not sample from the distribution and will instead output mu as the encoding vector
and log_var will be None
"""
def __init__(self, channels, ch=64, z=512):
super(Encoder, self).__init__()
self.conv1 = ResDown(channels, ch) # 64
self.conv2 = ResDown(ch, 2 * ch) # 32
self.conv3 = ResDown(2 * ch, 4 * ch) # 16
self.conv4 = ResDown(768, 8 * ch) # 8
#self.conv4 = ResDown(4 * ch, 8 * ch) # 8
self.conv5 = ResDown(8 * ch, 8 * ch) # 4
#self.conv5 = ResDown(8 * ch, 8 * ch) # 4
self.conv_mu = nn.Conv2d(8 * ch, z, 2, 2) # 2
self.conv_log_var = nn.Conv2d(8 * ch, z, 2, 2) # 2
def sample(self, mu, log_var):
std = torch.exp(0.5 * log_var)
eps = torch.randn_like(std)
return mu + eps * std
def forward(self, x, cond):
#print("x", x.shape) #x torch.Size([16, 3, 64, 64])
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
x_cond = torch.cat((x, cond), dim=1)
#x_cond torch.Size([48, 768, 8, 8])
#print("x_cond", x_cond.shape)
#x before 4 torch.Size([16, 256, 8, 8])
#print("x before 4", x.shape)
x = self.conv4(x_cond)
#x_cond torch.Size([16, 1024, 4, 4])
x = self.conv5(x)
if self.training:
mu = self.conv_mu(x)
log_var = self.conv_log_var(x)
x = self.sample(mu, log_var)
else:
mu = self.conv_mu(x)
x = mu
log_var = None
return x, mu, log_var
class Decoder(nn.Module):
"""
Decoder block
Built to be a mirror of the encoder block
"""
def __init__(self, channels, ch=64, z=512):
super(Decoder, self).__init__()
self.conv1 = ResUp(z, ch * 8)
self.conv2 = ResUp(ch * 8, ch * 8)
self.conv3 = ResUp(ch * 8, ch * 4)
self.conv4 = ResUp(ch * 4, ch * 2)
self.conv5 = ResUp(ch * 2, ch)
self.conv6 = ResUp(ch, ch // 2)
self.conv7 = nn.Conv2d(ch // 2, 3, 3, 1, 1) # the second dim is output image channel = 3
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
x = self.conv4(x)
x = self.conv5(x)
x = self.conv6(x)
x = self.conv7(x)
return x
class VAE(nn.Module):
"""
VAE network, uses the above encoder and decoder blocks
"""
def __init__(self, channel_in, ch=64, z=512, condition_dim=512):
super(VAE, self).__init__()
"""Res VAE Network
channel_in = number of channels of the image
z = the number of channels of the latent representation
(for a 64x64 image this is the size of the latent vector)
"""
self.latent_dim = z
self.condition_dim = condition_dim
channel_in = 3
self.encoder = Encoder(channel_in, ch=ch, z=z)
self.decoder = Decoder(channel_in, ch=ch, z=z + self.condition_dim)
def forward(self, x, image_embed):
# image_embed = [batch, embed dim]
#image_embed = torch.randn(x.shape[0], self.condition_dim).to(device)
image_embed = torch.reshape(image_embed, [-1, 512, 1, 1])
ones = torch.ones(x.shape[0], 512, 8, 8).to(device)
condition = ones * image_embed # [16, 32, 64, 64]
# x = torch.Size([128, 3, 64, 64])
#x = torch.cat((x, condition), dim=1)
#print("condition2", condition.shape) # condition2 torch.Size([112, 512, 4, 4])
# encoding =[batch, latent=128, 1, 1]
encoding, mu, log_var = self.encoder(x, condition)
# encoding = [batch, latent + image_embed, 1, 1]
encoding = torch.cat((encoding, image_embed), dim=1)
recon = self.decoder(encoding)
return recon, mu, log_var
def sample(self, mu, log_var):
std = torch.exp(0.5 * log_var)
eps = torch.randn_like(std)
return mu + eps * std
def reparameterization(self, z_mean, z_log_var):
""" Performs the reparameterization trick"""
eps = torch.randn(z_mean.shape[0], self.latent_dim, 1, 1).to(device)
# z_mean = torch.randn(32, 128,1,1)
# z_log_var = torch.randn(32, 128,1,1)
z = z_mean + torch.exp(z_log_var * .5) * eps
# z_cond = torch.cat([z, input_label], dim=1)
return z