-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
156 lines (134 loc) · 6.25 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
import torch.nn as nn
import torch
class View(nn.Module):
def __init__(self, shape):
super(View, self).__init__()
self.shape = shape
def forward(self, x):
return x.view(*self.shape)
class PrintShape(nn.Module):
def forward(self, x):
print(x.shape)
return x
class RiVAE(nn.Module):
def __init__(self):
super(RiVAE, self).__init__()
# Output = ((I - K + 2P) / S + 1)
# I - a size of input neuron
# K - kernel size
# P - padding
# S - stride
# encoder
self.encoder = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(num_features=32),
nn.MaxPool2d(4),
nn.LeakyReLU(inplace=True),
nn.Dropout(p=0.25),
nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(num_features=64),
nn.LeakyReLU(inplace=True),
nn.Dropout(p=0.25),
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(num_features=64),
nn.LeakyReLU(inplace=True),
nn.Dropout(p=0.25),
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(num_features=64),
nn.LeakyReLU(inplace=True),
nn.Dropout(p=0.25),
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(num_features=64),
nn.LeakyReLU(inplace=True),
nn.Dropout(p=0.25),
nn.Flatten()
)
# mu and log_var
self.mu = nn.Linear(in_features=16384, out_features=50)
self.log_var = nn.Linear(in_features=16384, out_features=50)
# decoder
self.decoder = nn.Sequential(
nn.Linear(in_features=50, out_features=16384),
View((-1, 64, 16, 16)),
nn.ConvTranspose2d(in_channels=64, out_channels=64, kernel_size=3, stride=2, padding=1, output_padding=1),
nn.BatchNorm2d(num_features=64),
nn.LeakyReLU(inplace=True),
nn.Dropout(p=0.25),
nn.ConvTranspose2d(in_channels=64, out_channels=64, kernel_size=3, stride=2, padding=1, output_padding=1),
nn.BatchNorm2d(num_features=64),
nn.LeakyReLU(inplace=True),
nn.Dropout(p=0.25),
nn.ConvTranspose2d(in_channels=64, out_channels=64, kernel_size=3, stride=2, padding=1, output_padding=1),
nn.BatchNorm2d(num_features=64),
nn.LeakyReLU(inplace=True),
nn.Dropout(p=0.25),
nn.ConvTranspose2d(in_channels=64, out_channels=32, kernel_size=3, stride=2, padding=1, output_padding=1),
nn.BatchNorm2d(num_features=32),
nn.LeakyReLU(inplace=True),
nn.Dropout(p=0.25),
nn.ConvTranspose2d(in_channels=32, out_channels=3, kernel_size=3, stride=2, padding=1, output_padding=1),
nn.ReLU(inplace=True)
)
def reparameterize(self, mu, log_var):
"""
mu: mean from the encoder's latent space
log_var: log variance from the encoder's latent space
"""
std = torch.exp(0.5 * log_var) # standard deviation
eps = torch.randn_like(std) # randn_like as we need the same size
sample = mu + (eps * std) # sampling as if coming from the input space
return sample
def forward(self, x):
# encoding
x = self.encoder(x)
# get mu and log_var
mu = self.mu(x)
log_var = self.log_var(x)
# get the latent vector through re-parameterization
z = self.reparameterize(mu, log_var)
# decoding
reconstruction = self.decoder(z)
return reconstruction, mu, log_var
class Generator(nn.Module):
def __init__(self, shape):
super(Generator, self).__init__()
self.network = nn.Sequential(
nn.ConvTranspose2d(in_channels=100, out_channels=shape >> 3, kernel_size=4, stride=1, bias=False),
nn.BatchNorm2d(num_features=shape >> 3),
nn.ReLU(inplace=True),
nn.ConvTranspose2d(in_channels=shape >> 3, out_channels=shape >> 2, kernel_size=4, stride=2, padding=1, bias=False),
nn.BatchNorm2d(num_features=shape >> 2),
nn.ReLU(inplace=True),
nn.ConvTranspose2d(in_channels=shape >> 2, out_channels=shape >> 1, kernel_size=4, stride=2, padding=1, bias=False),
nn.BatchNorm2d(num_features=shape >> 1),
nn.ReLU(inplace=True),
nn.ConvTranspose2d(in_channels=shape >> 1, out_channels=shape, kernel_size=4, stride=2, padding=1, bias=False),
nn.BatchNorm2d(num_features=shape),
nn.ReLU(inplace=True),
nn.ConvTranspose2d(in_channels=shape, out_channels=3, kernel_size=4, stride=2, padding=1, bias=False),
nn.Tanh()
)
def forward(self, input):
output = self.network(input)
return output
class Discriminator(nn.Module):
def __init__(self, shape):
super(Discriminator, self).__init__()
self.network = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=shape, kernel_size=4, stride=2, padding=1, bias=False),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Conv2d(in_channels=shape, out_channels=shape >> 1, kernel_size=4, stride=2, padding=1, bias=False),
nn.BatchNorm2d(num_features=shape >> 1),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Conv2d(in_channels=shape >> 1, out_channels=shape >> 2, kernel_size=4, stride=2, padding=1, bias=False),
nn.BatchNorm2d(num_features=shape >> 2),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Conv2d(in_channels=shape >> 2, out_channels=shape >> 3, kernel_size=4, stride=2, padding=1, bias=False),
nn.BatchNorm2d(num_features=shape >> 3),
nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Conv2d(in_channels=shape >> 3, out_channels=1, kernel_size=4, stride=1, bias=False),
nn.Sigmoid()
)
def forward(self, input):
output = self.network(input)
return output.view(-1, 1).squeeze(1)