-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
80 lines (71 loc) · 2.67 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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision.models.resnet import BasicBlock, ResNet
def vae_loss(recon_x, x, mu, logvar):
# Reconstruction loss (assuming Bernoulli distribution)
try:
recon_loss = F.binary_cross_entropy(recon_x, x, reduction="sum")
# KL divergence
kl_div = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
except Exception as E:
print(E)
try:
recon_loss = F.mse_loss(recon_x, x, reduction="sum")
kl_div = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
except Exception as E:
print(E)
# set loss to a default value
recon_loss = torch.tensor(0.1).to(recon_x.device)
kl_div = torch.tensor(0.1).to(recon_x.device)
return recon_loss + kl_div
class UnFlatten(nn.Module):
def forward(self, input):
return input.view(input.size(0), 512, 1, 1)
class VAE(nn.Module):
def __init__(self, image_channels=3, latent_dim=128):
super(VAE, self).__init__()
# Encoder (ResNet-18)
self.encoder = nn.Sequential(
*list(ResNet(BasicBlock, [2, 2, 2, 2]).children())[:-1], nn.Flatten()
)
self.fc_mu = nn.Linear(512, latent_dim)
self.fc_logvar = nn.Linear(512, latent_dim)
# Decoder
self.decoder_input = nn.Linear(latent_dim, 512)
self.decoder = nn.Sequential(
UnFlatten(), # Output: 512x1x1
nn.ConvTranspose2d(
512, 256, kernel_size=4, stride=2, padding=1
), # Output: 256x2x2
nn.ReLU(),
nn.ConvTranspose2d(
256, 128, kernel_size=4, stride=2, padding=1
), # Output: 128x4x4
nn.ReLU(),
nn.ConvTranspose2d(
128, 64, kernel_size=4, stride=2, padding=1
), # Output: 64x8x8
nn.ReLU(),
nn.ConvTranspose2d(
64, 32, kernel_size=4, stride=2, padding=1
), # Output: 32x16x16
nn.ReLU(),
nn.ConvTranspose2d(
32, image_channels, kernel_size=4, stride=2, padding=1
), # Output: 3x32x32
nn.Sigmoid(),
)
def reparameterize(self, mu, logvar):
std = torch.exp(0.5 * logvar)
eps = torch.randn_like(std)
return mu + eps * std
def forward(self, x):
# Encode
x_encoded = self.encoder(x)
mu = self.fc_mu(x_encoded)
logvar = self.fc_logvar(x_encoded)
z = self.reparameterize(mu, logvar)
# Decode
x_reconstructed = self.decoder(self.decoder_input(z))
return x_reconstructed, mu, logvar