-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathVAE.py
More file actions
138 lines (108 loc) · 4.39 KB
/
VAE.py
File metadata and controls
138 lines (108 loc) · 4.39 KB
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
# Copyright 2019 Stanislav Pidhorskyi
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
from __future__ import print_function
import torch.utils.data
from scipy import misc
from torch import optim
from torchvision.utils import save_image
from net import *
import numpy as np
import pickle
import time
import random
import os
from dlutils import batch_provider
from dlutils.pytorch.cuda_helper import *
im_size = 128
def loss_function(recon_x, x, mu, logvar):
BCE = torch.mean((recon_x - x)**2)
# see Appendix B from VAE paper:
# Kingma and Welling. Auto-Encoding Variational Bayes. ICLR, 2014
# https://arxiv.org/abs/1312.6114
# 0.5 * sum(1 + log(sigma^2) - mu^2 - sigma^2)
KLD = -0.5 * torch.mean(torch.mean(1 + logvar - mu.pow(2) - logvar.exp(), 1))
return BCE, KLD * 0.1
def process_batch(batch):
data = [misc.imresize(x, [im_size, im_size]).transpose((2, 0, 1)) for x in batch]
x = torch.from_numpy(np.asarray(data, dtype=np.float32)).cuda() / 127.5 - 1.
x = x.view(-1, 3, im_size, im_size)
return x
def main():
batch_size = 128
z_size = 512
vae = VAE(zsize=z_size, layer_count=5)
vae.cuda()
vae.train()
vae.weight_init(mean=0, std=0.02)
lr = 0.0005
vae_optimizer = optim.Adam(vae.parameters(), lr=lr, betas=(0.5, 0.999), weight_decay=1e-5)
train_epoch = 40
sample1 = torch.randn(128, z_size).view(-1, z_size, 1, 1)
for epoch in range(train_epoch):
vae.train()
with open('data_fold_%d.pkl' % (epoch % 5), 'rb') as pkl:
data_train = pickle.load(pkl)
print("Train set size:", len(data_train))
random.shuffle(data_train)
batches = batch_provider(data_train, batch_size, process_batch, report_progress=True)
rec_loss = 0
kl_loss = 0
epoch_start_time = time.time()
if (epoch + 1) % 8 == 0:
vae_optimizer.param_groups[0]['lr'] /= 4
print("learning rate change!")
i = 0
for x in batches:
vae.train()
vae.zero_grad()
rec, mu, logvar = vae(x)
loss_re, loss_kl = loss_function(rec, x, mu, logvar)
(loss_re + loss_kl).backward()
vae_optimizer.step()
rec_loss += loss_re.item()
kl_loss += loss_kl.item()
#############################################
os.makedirs('results_rec', exist_ok=True)
os.makedirs('results_gen', exist_ok=True)
epoch_end_time = time.time()
per_epoch_ptime = epoch_end_time - epoch_start_time
# report losses and save samples each 60 iterations
m = 60
i += 1
if i % m == 0:
rec_loss /= m
kl_loss /= m
print('\n[%d/%d] - ptime: %.2f, rec loss: %.9f, KL loss: %.9f' % (
(epoch + 1), train_epoch, per_epoch_ptime, rec_loss, kl_loss))
rec_loss = 0
kl_loss = 0
with torch.no_grad():
vae.eval()
x_rec, _, _ = vae(x)
resultsample = torch.cat([x, x_rec]) * 0.5 + 0.5
resultsample = resultsample.cpu()
save_image(resultsample.view(-1, 3, im_size, im_size),
'results_rec/sample_' + str(epoch) + "_" + str(i) + '.png')
x_rec = vae.decode(sample1)
resultsample = x_rec * 0.5 + 0.5
resultsample = resultsample.cpu()
save_image(resultsample.view(-1, 3, im_size, im_size),
'results_gen/sample_' + str(epoch) + "_" + str(i) + '.png')
del batches
del data_train
print("Training finish!... save training results")
torch.save(vae.state_dict(), "VAEmodel.pkl")
if __name__ == '__main__':
main()