第六节 图片风格迁移

- 图片风格迁移
- 用GAN生成MNIST
- 用DCGAN生成更复杂的图片## 图片风格迁移 Neural Style Transfer matplotlib inlinefrom __future__ import division
from torchvision import models
from torchvision import transforms
from PIL import Image
import argparse
import torch
import torchvision
import torch.nn as nn
import numpy as npimport matplotlib.pyplot as pltdevice = torch.device("cuda" if torch.cuda.is_available() else "cpu")def load_image(image_path, transform=None, max_size=None, shape=None):image = Image.open(image_path)if max_size:scale = max_size / max(image.size)size= np.array(image.size) * scaleimage = image.resize(size.astype(int), Image.ANTIALIAS)if shape:image = image.resize(shape, Image.LANCZOS)if transform:image = transform(image).unsqueeze(0)return image.to(device)transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])
]) # 来自ImageNet的mean和variancecontent = load_image("png/content.png", transform, max_size=400)
stype = load_image("png/style.png", transform, shape=[content.size(2), content.size(3)])# content = load_image("png/content.png", transforms.Compose([
#     transforms.ToTensor(),
# ]), max_size=400)
# style = load_image("png/style.png", transforms.Compose([
#     transforms.ToTensor(),
# ]), shape=[content.size(2), content.size(3)])#%%stype.shape#%%unloader = transforms.ToPILImage()  # reconvert into PIL imageplt.ion()def imshow(tensor, title=None):image = tensor.cpu().clone()  # we clone the tensor to not do changes on itimage = image.squeeze(0)      # remove the fake batch dimensionimage = unloader(image)plt.imshow(image)if title is not None:plt.title(title)plt.pause(0.001) # pause a bit so that plots are updatedplt.figure()
imshow(style[0], title='Image')
# content.shape#%%class VGGNet(nn.Module):def __init__(self):super(VGGNet, self).__init__()self.select = ['0', '5', '10', '19', '28']self.vgg = models.vgg19(pretrained=True).featuresdef forward(self, x):features = []for name, layer in self.vgg._modules.items():x = layer(x)if name in self.select:features.append(x)return featurestarget = content.clone().requires_grad_(True)
optimizer = torch.optim.Adam([target], lr=0.003, betas=[0.5, 0.999])
vgg = VGGNet().to(device).eval()#%%target_features = vgg(target)#%%total_step = 2000
style_weight = 100.
for step in range(total_step):target_features = vgg(target)content_features = vgg(content)style_features = vgg(style)style_loss = 0content_loss = 0for f1, f2, f3 in zip(target_features, content_features, style_features):content_loss += torch.mean((f1-f2)**2)_, c, h, w = f1.size()f1 = f1.view(c, h*w)f3 = f3.view(c, h*w)# 计算gram matrixf1 = torch.mm(f1, f1.t())f3 = torch.mm(f3, f3.t())style_loss += torch.mean((f1-f3)**2)/(c*h*w)loss = content_loss + style_weight * style_loss# 更新targetoptimizer.zero_grad()loss.backward()optimizer.step()if step % 10 == 0:print("Step [{}/{}], Content Loss: {:.4f}, Style Loss: {:.4f}".format(step, total_step, content_loss.item(), style_loss.item()))  #%%denorm = transforms.Normalize((-2.12, -2.04, -1.80), (4.37, 4.46, 4.44))
img = target.clone().squeeze()
img = denorm(img).clamp_(0, 1)
plt.figure()
imshow(img, title='Target Image')#%% md## Generative Adversarial Networks#%%batch_size=32
transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize(mean=(0.5, 0.5, 0.5),std=(0.5, 0.5, 0.5))
])mnist_data = torchvision.datasets.MNIST("./mnist_data", train=True, download=True, transform=transform)
dataloader = torch.utils.data.DataLoader(dataset=mnist_data,batch_size=batch_size,shuffle=True)#%%image_size = 784hidden_size = 256
# discriminator
D = nn.Sequential(nn.Linear(image_size, hidden_size),nn.LeakyReLU(0.2),nn.Linear(hidden_size, hidden_size),nn.LeakyReLU(0.2),nn.Linear(hidden_size, 1),nn.Sigmoid()
)latent_size = 64
# Generator
G = nn.Sequential(nn.Linear(latent_size, hidden_size),nn.ReLU(),nn.Linear(hidden_size, hidden_size),nn.ReLU(),nn.Linear(hidden_size, image_size),nn.Tanh()
)D = D.to(device)
G = G.to(device)loss_fn = nn.BCELoss()
d_optimizer = torch.optim.Adam(D.parameters(), lr=0.0002)
g_optimizer = torch.optim.Adam(G.parameters(), lr=0.0002)#%% md开始训练#%%def reset_grad():d_optimizer.zero_grad()g_optimizer.zero_grad()total_step = len(dataloader)
num_epochs = 200
for epoch in range(num_epochs):for i, (images, _) in enumerate(dataloader):batch_size = images.size(0)images = images.reshape(batch_size, image_size).to(device)real_labels = torch.ones(batch_size, 1).to(device)fake_labels = torch.zeros(batch_size, 1).to(device)outputs = D(images)d_loss_real = loss_fn(outputs, real_labels)real_score = outputs# 开始生成fake imagesz = torch.randn(batch_size, latent_size).to(device)fake_images = G(z)outputs = D(fake_images.detach())d_loss_fake = loss_fn(outputs, fake_labels)fake_score = outputs# 开始优化discriminatord_loss = d_loss_real + d_loss_fakereset_grad()d_loss.backward()d_optimizer.step()# 开始优化generatorz = torch.randn(batch_size, latent_size).to(device)fake_images = G(z)outputs = D(fake_images)g_loss = loss_fn(outputs, real_labels)reset_grad()g_loss.backward()g_optimizer.step()if i % 1000 == 0:print("Epoch [{}/{}], Step [{}/{}], d_loss: {:.4f}, g_loss: {:.4f}, D(x): {:.2f}, D(G(z)): {:.2f}".format(epoch, num_epochs, i, total_step, d_loss.item(), g_loss.item(), real_score.mean().item(), fake_score.mean().item()))#%% mdfake images#%%z = torch.randn(1, latent_size).to(device)
fake_images = G(z).view(28, 28).data.cpu().numpy()
plt.imshow(fake_images)#%% md真实图片#%%plt.imshow(images[0].view(28,28).data.cpu().numpy())#%% md## DCGAN#%% md[UNSUPERVISED REPRESENTATION LEARNING WITH DEEP CONVOLUTIONAL GENERATIVE ADVERSARIAL NETWORKS](https://arxiv.org/pdf/1511.06434.pdf)#%% md[图片下载地址](https://drive.google.com/drive/folders/0B7EVK8r0v71pbWNEUjJKdDQ3dGc)
https://drive.google.com/drive/folders/0B7EVK8r0v71pbWNEUjJKdDQ3dGc#%%import torchvision.utils as vutils#%%# !ls celeba/img_align_celeba/img_align_celeba_png#%%image_size=64
batch_size=128
dataroot="celeba/img_align_celeba"
num_workers = 2
dataset = torchvision.datasets.ImageFolder(root=dataroot, transform=transforms.Compose([transforms.Resize(image_size),transforms.CenterCrop(image_size),transforms.ToTensor(),transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
]))
dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=True, num_workers=num_workers)#%%real_batch=next(iter(dataloader))
plt.figure(figsize=(8,8))
plt.axis=("off")
plt.title("Training Images")
plt.imshow(np.transpose(vutils.make_grid(real_batch[0].to(device)[:64], padding=2, normalize=True).cpu(), (1,2,0)))#%% md我们把模型的所有参数都初始化城mean=0, std=0.2#%%def weights_init(m):classname = m.__class__.__name__if classname.find('Conv') != -1:nn.init.normal_(m.weight.data, 0.0, 0.02)elif classname.find('BatchNorm') != -1:nn.init.normal_(m.weight.data, 1.0, 0.02)nn.init.constant_(m.bias.data, 0)#%% md![dcgan](images/dcgan_generator.png)#%%nz = 100 # latent vector的大小
ngf = 64 # generator feature map size
ndf = 64 # discriminator feature map size
nc = 3 # color channelsclass Generator(nn.Module):def __init__(self):super(Generator, self).__init__()self.main = nn.Sequential(# input is Z, going into a convolution# torch.nn.ConvTranspose2d(in_channels, out_channels, # kernel_size, stride=1, padding=0, output_padding=0, groups=1, bias=True, dilation=1)nn.ConvTranspose2d( nz, ngf * 8, 4, 1, 0, bias=False),nn.BatchNorm2d(ngf * 8),nn.ReLU(True),# state size. (ngf*8) x 4 x 4nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False),nn.BatchNorm2d(ngf * 4),nn.ReLU(True),# state size. (ngf*4) x 8 x 8nn.ConvTranspose2d( ngf * 4, ngf * 2, 4, 2, 1, bias=False),nn.BatchNorm2d(ngf * 2),nn.ReLU(True),# state size. (ngf*2) x 16 x 16nn.ConvTranspose2d( ngf * 2, ngf, 4, 2, 1, bias=False),nn.BatchNorm2d(ngf),nn.ReLU(True),# state size. (ngf) x 32 x 32nn.ConvTranspose2d( ngf, nc, 4, 2, 1, bias=False),nn.Tanh()# state size. (nc) x 64 x 64)def forward(self, input):return self.main(input)#%%# Now, we can instantiate the generator and apply the weights_init function. Check out the printed model to see how the generator object is structured.# Create the generator
netG = Generator().to(device)# Apply the weights_init function to randomly initialize all weights
#  to mean=0, stdev=0.2.
netG.apply(weights_init)# Print the model
print(netG)#%% mdDiscriminator#%%class Discriminator(nn.Module):def __init__(self):super(Discriminator, self).__init__()self.main = nn.Sequential(# input is (nc) x 64 x 64nn.Conv2d(nc, ndf, 4, 2, 1, bias=False),nn.LeakyReLU(0.2, inplace=True),# state size. (ndf) x 32 x 32nn.Conv2d(ndf, ndf * 2, 4, 2, 1, bias=False),nn.BatchNorm2d(ndf * 2),nn.LeakyReLU(0.2, inplace=True),# state size. (ndf*2) x 16 x 16nn.Conv2d(ndf * 2, ndf * 4, 4, 2, 1, bias=False),nn.BatchNorm2d(ndf * 4),nn.LeakyReLU(0.2, inplace=True),# state size. (ndf*4) x 8 x 8nn.Conv2d(ndf * 4, ndf * 8, 4, 2, 1, bias=False),nn.BatchNorm2d(ndf * 8),nn.LeakyReLU(0.2, inplace=True),# state size. (ndf*8) x 4 x 4nn.Conv2d(ndf * 8, 1, 4, 1, 0, bias=False),nn.Sigmoid())def forward(self, input):return self.main(input)#%%# Now, as with the generator, we can create the discriminator, apply the weights_init function, and print the model’s structure.# Create the Discriminator
netD = Discriminator().to(device)# Apply the weights_init function to randomly initialize all weights
#  to mean=0, stdev=0.2.
netD.apply(weights_init)# Print the model
print(netD)#%% md开始训练#%%lr = 0.0002
beta1 = 0.5loss_fn = nn.BCELoss()
fixed_noise = torch.randn(64, nz, 1, 1, device=device)
d_optimizer = torch.optim.Adam(netD.parameters(), lr=lr, betas=(beta1, 0.999))
g_optimizer = torch.optim.Adam(netG.parameters(), lr=lr, betas=(beta1, 0.999))#%%num_epochs = 5
G_losses = []
D_losses = []
for epoch in range(num_epochs):for i, data in enumerate(dataloader):# 训练discriminator, maximize log(D(x)) + log(1-D(G(z)))# 首先训练真实图片netD.zero_grad()real_images = data[0].to(device)b_size = real_images.size(0)label = torch.ones(b_size).to(device)output = netD(real_images).view(-1)real_loss = loss_fn(output, label)real_loss.backward()D_x = output.mean().item()# 然后训练生成的假图片noise = torch.randn(b_size, nz, 1, 1, device=device)fake_images = netG(noise)label.fill_(0)output = netD(fake_images.detach()).view(-1)fake_loss = loss_fn(output, label)fake_loss.backward()D_G_z1 = output.mean().item()loss_D = real_loss + fake_lossd_optimizer.step()# 训练Generator netG.zero_grad()label.fill_(1)output = netD(fake_images).view(-1)loss_G = loss_fn(output, label)loss_G.backward()D_G_z2 = output.mean().item()g_optimizer.step()if i % 50 == 0:print("[{}/{}] [{}/{}] Loss_D: {:.4f} Loss_G {:.4f} D(x): {:.4f} D(G(z)): {:.4f}/{:.4f}".format(epoch, num_epochs, i, len(dataloader), loss_D.item(), loss_G.item(), D_x, D_G_z1, D_G_z2))G_losses.append(loss_G.item())D_losses.append(loss_D.item())#%%with torch.no_grad():fake = netG(fixed_noise).detach().cpu()
# fake#%%real_batch = next(iter(dataloader))# Plot the real images
plt.figure(figsize=(30,30))
plt.subplot(1,2,1)
plt.axis=("off")
plt.title("Real Images")
plt.imshow(np.transpose(vutils.make_grid(real_batch[0].to(device)[:64], padding=5, normalize=True).cpu(),(1,2,0)))# Plot the fake images from the last epoch
plt.subplot(1,2,2)
plt.axis=("off")
plt.title("Fake Images")
plt.imshow(np.transpose(vutils.make_grid(fake, padding=2, normalize=True), (1,2,0)))
plt.show()

第六节 图片风格迁移和GAN相关推荐

  1. 第六课 图片风格迁移和GAN

    一.Neuarl Style Transfer 图片风格迁移 结合一张图片的内容和另一张图片的风格,生成一张新图片 图片表示 代码 %matplotlib inlinefrom __future__ ...

  2. Pytorch入门+实战系列七:图片风格迁移和GAN

    Pytorch官方文档:https://pytorch.org/docs/stable/torch.html? 1. 写在前面 今天开始,兼顾Pytorch学习, 如果刚刚接触深度学习并且想快速搭建神 ...

  3. Pytorch入门(6)-图片风格迁移和GAN

    视频资源:https://www.bilibili.com/video/BV12741177Cu?p=6&spm_id_from=pageDriver from __future__ impo ...

  4. 第十课.图片风格迁移和GAN

    目录 Neural Style Transfer Neural Style Transfer原理 准备工作 定义模型并加载预训练的模型参数 训练target以及结果可视化 生成对抗网络GAN GAN原 ...

  5. 让你的图片更有逼格,用Python实现图片风格迁移

    点击上方 Python知识圈,选择置顶或星标 第一时间关注 Python 技术干货! 文章转自R语言和Python学堂,禁二次转载 阅读文本大概需要 3 分钟. 先来看下效果: 上图是小编在甘南合作的 ...

  6. TensorFlow从1到2(十三)图片风格迁移

    风格迁移 <从锅炉工到AI专家(8)>中我们介绍了一个"图片风格迁移"的例子.因为所引用的作品中使用了TensorFlow 1.x的代码,算法也相对复杂,所以文中没有仔 ...

  7. 使用Tensorflow实现图片风格迁移,圆梦名画

    一,前期基础知识储备 1)Prisma - 图片风格迁移的鼻祖: 照片可以记录生活的瞬间,变成一幅幅的回忆:而 Prisma 则是可以让瞬间的回忆变成永恒的名画!我们平常用手机随意拍出来的照片效果看起 ...

  8. 用Python实现图片风格迁移,让你的图片更加的高逼格!

    先来看下效果: 上图是老王在甘南合作的米拉日巴佛阁外面拍下的一张照片,采用风格迁移技术后的效果为: 一些其它效果图: 下面进入正题. 如果你依然在编程的世界里迷茫,可以加入我们的Python学习扣qu ...

  9. 图片风格迁移:基于实例缓解细节丢失、人脸风格化失败问题

    ​​摘要:本文介绍的基于实例的方法可以很好的缓解细节丢失.人脸风格化失败等问题并得到高质量的风格转换图像. 本文分享自华为云社区 <基于实例的风格迁移>,作者:柠檬柚子茶加冰 . 基于神经 ...

最新文章

  1. Tornado写简易服务器
  2. [Leetcode]100. Same Tree -David_Lin
  3. xlrd,xlwt模塊
  4. ×××S 2008 实用小技巧
  5. vs2010利用属性表自动配置OpenCV(win7的64位系统,opencv版本是2.4.10)
  6. vc设备工程师_4注册公用设备工程师专业基础考试真题.
  7. css 如何让背景图片拉伸填充避免重复显示
  8. 什么是好的API设计? 1
  9. SVN代码回滚命令之---merge的使用
  10. 【Kalman】卡尔曼滤波器工作原理(Link)
  11. led显示屏背景墙设计_西安盛泉广告设计制作||发光字标识,门头广告牌,灯箱,LED显示屏...
  12. 212.单词搜索II
  13. java代码注释规范
  14. 如鹏网.Net三层架构 第四章代码生成器
  15. duboo仿猫眼微服务架构—微服务入门
  16. 谷歌Gmail注册方法
  17. 又有朋友被骗入传销了!
  18. 计算机开机最快设置,w7提高开机速度如何操作_win7电脑怎么开机更快
  19. win7计算机图标排列,win7文件夹内图标取消自动排列,取消自动排列
  20. 杰理之79NRTC 时间【篇】

热门文章

  1. 手机adb 连接不到电脑,但是可以连接到各种助手
  2. 虚拟机上ftp服务器安装与配置文件,Linux下ftp服务器安装与配置实验报告_linuxftp服务器配置实验报告,ftp服务器配置的实验报告...
  3. 天津计算机比赛,我院在华北五省(市、自治区)大学生计算机应用大赛、机器人大赛天津赛区比赛中取得佳绩...
  4. Flutter GridView详解
  5. 用linux如何连接锐捷网络,服务器Ubuntu 16.04下连接锐捷
  6. 数据挖掘中的十个著名算法
  7. python身份证识别仪_C#身份证识别相关技术功能详解
  8. 如何识别笔记本样机(2012版)
  9. 北京:楼盘内部认购禁而不止 业内揭秘花招多
  10. 农业大数据建设的需求、模式与单品种全产业链推进路径