大家好,欢迎来到IT知识分享网。
使用变分自编码器生成图像
简介
使用minst手写数字数据集训练变分自编码器,使之能够在编码解码后能够自动生成手写数字
具体原理图如下
引入库
import os
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
from torchvision import transforms
from torchvision.utils import save_image
import mpimg
import cv2
import matplotlib.pyplot as plt
设备配置
torch.cuda.set_device(0) # 这句用来设置pytorch在哪块GPU上运行,这里假设使用序号为1的这块GPU.
device = torch.device(‘cuda’ if torch.cuda.is_available() else ‘cpu’)
在当前目录,创建不存在的目录ave_samples
sample_dir = ‘ave_samples’
if not os.path.exists(sample_dir):
os.makedirs(sample_dir)
定义一些超参数
image_size = 784
h_dim = 400
z_dim = 20
num_epochs = 200
batch_size = 128
learning_rate = 0.001
下载MNIST训练集,这里因已下载,故download=False
dataset = torchvision.datasets.MNIST(root=‘data’,
train=True,
transform=transforms.ToTensor(),
download=True)
数据加载
data_loader = torch.utils.data.DataLoader(dataset=dataset,
batch_size=batch_size,
shuffle=True)
定义AVE模型
class VAE(nn.Module):
def init(self, image_size=784, h_dim=400, z_dim=20):
super(VAE, self).init()
self.fc1 = nn.Linear(image_size, h_dim)
self.fc2 = nn.Linear(h_dim, z_dim)
self.fc3 = nn.Linear(h_dim, z_dim)
self.fc4 = nn.Linear(z_dim, h_dim)
self.fc5 = nn.Linear(h_dim, image_size)
def encode(self, x):
h = F.relu(self.fc1(x))
return self.fc2(h), self.fc3(h)
def reparameterize(self, mu, log_var):
std = torch.exp(log_var / 2)
eps = torch.randn_like(std)
return mu + eps * std
def decode(self, z):
h = F.relu(self.fc4(z))
return F.sigmoid(self.fc5(h))
def forward(self, x):
mu, log_var = self.encode(x)
z = self.reparameterize(mu, log_var)
x_reconst = self.decode(z)
return x_reconst, mu, log_var
model = VAE().to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
开始训练模型
for epoch in range(num_epochs):
model.train()
for i, (x, _) in enumerate(data_loader):
# 前向传播
model.zero_grad()
x = x.to(device).view(-1, image_size)
x_reconst, mu, log_var = model(x)
# Compute reconstruction loss and kl divergence
# For KL divergence, see Appendix B in VAE paper or http://yunjey47.tistory.com/43
reconst_loss = F.binary_cross_entropy(x_reconst, x, size_average=False)
kl_div = - 0.5 * torch.sum(1 + log_var - mu.pow(2) - log_var.exp())
# 反向传播及优化器
loss = reconst_loss + kl_div
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i + 1) % 100 == 0:
print("Epoch[{}/{}], Step [{}/{}], Reconst Loss: {:.4f}, KL Div: {:.4f}"
.format(epoch + 1, num_epochs, i + 1, len(data_loader), reconst_loss.item(), kl_div.item()))
with torch.no_grad():
# 保存采样图像,即潜在向量Z通过解码器生成的新图像
z = torch.randn(batch_size, z_dim).to(device)
out = model.decode(z).view(-1, 1, 28, 28)
save_image(out, os.path.join(sample_dir, 'sampled-{}.png'.format(epoch + 1)))
# 保存重构图像,即原图像通过解码器生成的图像
out, _, _ = model(x)
x_concat = torch.cat([x.view(-1, 1, 28, 28), out.view(-1, 1, 28, 28)], dim=3)
save_image(x_concat, os.path.join(sample_dir, 'reconst-{}.png'.format(epoch + 1)))
reconsPath = ‘./ave_samples/reconst-30.png’
Image = cv2.imread(reconsPath)
plt.imshow(Image) # 显示图片
plt.axis(‘off’) # 不显示坐标轴
plt.show()
genPath = ‘./ave_samples/sampled-30.png’
Image = cv2.imread(genPath)
plt.imshow(Image) # 显示图片
plt.axis(‘off’) # 不显示坐标轴
plt.show()`
[4]: http://adrai.github.io/flowchart.js/
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/24045.html