-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvae.py
144 lines (115 loc) · 5.54 KB
/
vae.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
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
class NeuralNet:
def __init__(self, height, width, channel, device, ngpu, learning_rate=1e-3):
self.height, self.width, self.channel = height, width, channel
self.device, self.ngpu = device, ngpu
self.learning_rate = learning_rate
self.encoder = (
Encoder(height=self.height, width=self.width, channel=self.channel,
ngpu=self.ngpu).to(self.device)
)
self.decoder = (
Decoder(height=self.height, width=self.width, channel=self.channel,
ngpu=self.ngpu).to(self.device)
)
self.models = [self.encoder, self.decoder]
# GPU 설정
for idx_m, model in enumerate(self.models):
if self.device.type == 'cuda' and getattr(model, 'ngpu', 0) > 0:
self.models[idx_m] = nn.DataParallel(model, list(range(model.ngpu)))
# 파라미터 수 출력
self.num_params = 0
for idx_m, model in enumerate(self.models):
for p in model.parameters():
self.num_params += p.numel()
print(f"The number of parameters: {self.num_params:.4f}")
self.params = list(self.encoder.parameters()) + list(self.decoder.parameters())
self.optimizer = optim.Adam(self.params, lr=self.learning_rate)
def to(self, device):
for idx_m, model in enumerate(self.models):
self.models[idx_m] = model.to(device)
self.device = device
def train(self, mode=True):
for model in self.models:
model.train(mode)
class Flatten(nn.Module):
def forward(self, input):
return input.view(input.size(0), -1)
class Encoder(nn.Module):
def __init__(self, height, width, channel, ngpu):
super(Encoder, self).__init__()
self.height, self.width, self.channel = height, width, channel
self.ngpu = ngpu
self.encoder_conv = nn.Sequential(
nn.Conv2d(in_channels=self.channel, out_channels=4, kernel_size=3, stride=1, padding='same'),
nn.ELU(),
nn.Conv2d(in_channels=4, out_channels=4, kernel_size=3, stride=1, padding='same'),
nn.ELU(),
nn.AvgPool2d(2),
nn.Conv2d(in_channels=4, out_channels=8, kernel_size=3, stride=1, padding='same'),
nn.ELU(),
nn.Conv2d(in_channels=8, out_channels=8, kernel_size=3, stride=1, padding='same'),
nn.ELU(),
nn.AvgPool2d(2),
nn.Conv2d(in_channels=8, out_channels=8, kernel_size=3, stride=1, padding='same'),
nn.ELU(),
nn.Conv2d(in_channels=8, out_channels=16, kernel_size=3, stride=1, padding='same'),
nn.ELU(),
nn.MaxPool2d(2),
nn.Conv2d(in_channels=16, out_channels=16, kernel_size=3, stride=1, padding='same'),
nn.ELU(),
nn.Conv2d(in_channels=16, out_channels=16, kernel_size=3, stride=1, padding='same'),
nn.ELU(),
nn.MaxPool2d(3),
)
# self.conv_mu = nn.Conv2d(16, 16, kernel_size=1)
# self.conv_sigma = nn.Conv2d(16, 16, kernel_size=1)
def forward(self, input):
conv_out = self.encoder_conv(input)
# mu = self.conv_mu(conv_out)
# sigma = torch.exp(0.5 * self.conv_sigma(conv_out))
# epsilon = torch.randn_like(sigma)
# z_sample = mu + sigma * epsilon
return conv_out
class Decoder(nn.Module):
def __init__(self, height, width, channel, ngpu):
super(Decoder, self).__init__()
self.height, self.width, self.channel = height, width, channel
self.ngpu = ngpu
self.decoder_dense = nn.Sequential(
nn.Conv2d(in_channels=16, out_channels=16, kernel_size=3, padding='same'),
nn.ELU()
)
self.decoder_conv = nn.Sequential(
nn.Conv2d(in_channels=16, out_channels=16, kernel_size=3, stride=1, padding='same'),
nn.ELU(),
nn.Conv2d(in_channels=16, out_channels=16, kernel_size=3, stride=1, padding='same'),
nn.ELU(),
nn.Upsample(scale_factor=3, mode='nearest'),
nn.Conv2d(in_channels=16, out_channels=16, kernel_size=3, stride=1, padding='same'),
nn.ELU(),
nn.Conv2d(in_channels=16, out_channels=8, kernel_size=3, stride=1, padding='same'),
nn.ELU(),
nn.Upsample(scale_factor=2, mode='nearest'),
nn.Conv2d(in_channels=8, out_channels=8, kernel_size=3, stride=1, padding='same'),
nn.ELU(),
nn.Conv2d(in_channels=8, out_channels=8, kernel_size=3, stride=1, padding='same'),
nn.ELU(),
nn.Upsample(scale_factor=2, mode='nearest'),
nn.Conv2d(in_channels=8, out_channels=4, kernel_size=3, stride=1, padding='same'),
nn.ELU(),
nn.Conv2d(in_channels=4, out_channels=4, kernel_size=3, stride=1, padding='same'),
nn.ELU(),
nn.Upsample(scale_factor=2, mode='nearest'),
nn.Conv2d(in_channels=4, out_channels=4, kernel_size=3, stride=1, padding='same'),
nn.ELU(),
nn.Conv2d(in_channels=4, out_channels=self.channel, kernel_size=3, stride=1, padding='same'),
nn.ELU(),
)
def forward(self, input):
dense_out = self.decoder_dense(input)
x_hat = self.decoder_conv(dense_out)
return x_hat