WGAN自动生成动漫头像PyTorch 代码

时间: 2024-05-04 17:20:29 浏览: 8
以下是使用WGAN生成动漫头像的PyTorch代码,其中使用了DCGAN的结构和WGAN的损失函数。 首先需要导入必要的库: ```python import torch import torch.nn as nn import torch.optim as optim import torchvision.utils as vutils import torchvision.datasets as dset import torchvision.transforms as transforms from torch.utils.data import DataLoader from torch.autograd import Variable import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation from IPython.display import HTML ``` 接下来定义一些超参数: ```python # Root directory for dataset dataroot = "./data" # Number of workers for dataloader workers = 2 # Batch size during training batch_size = 64 # Spatial size of training images. All images will be resized to this # size using a transformer. image_size = 64 # Number of channels in the training images. For color images this is 3 nc = 3 # Size of z latent vector (i.e. size of generator input) nz = 100 # Size of feature maps in generator ngf = 64 # Size of feature maps in discriminator ndf = 64 # Number of training epochs num_epochs = 5 # Learning rate for optimizers lr = 0.00005 # Beta1 hyperparam for Adam optimizers beta1 = 0.5 # Number of GPUs available. Use 0 for CPU mode. ngpu = 0 # Number of critic iterations per generator iteration n_critic = 5 # Clipping parameter for WGAN clip_value = 0.01 # Output directory for generated images output_dir = "./output" ``` 接下来定义数据加载器: ```python # Create the dataset dataset = dset.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)), ])) # Create the dataloader dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=True, num_workers=workers) ``` 接下来定义生成器和判别器的结构: ```python # Generator Code class Generator(nn.Module): def __init__(self, ngpu): super(Generator, self).__init__() self.ngpu = ngpu self.main = nn.Sequential( # input is Z, going into a convolution nn.ConvTranspose2d(nz, ngf * 8, 4, 1, 0, bias=False), nn.BatchNorm2d(ngf * 8), nn.ReLU(True), # state size. (ngf*8) x 4 x 4 nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False), nn.BatchNorm2d(ngf * 4), nn.ReLU(True), # state size. (ngf*4) x 8 x 8 nn.ConvTranspose2d(ngf * 4, ngf * 2, 4, 2, 1, bias=False), nn.BatchNorm2d(ngf * 2), nn.ReLU(True), # state size. (ngf*2) x 16 x 16 nn.ConvTranspose2d(ngf * 2, ngf, 4, 2, 1, bias=False), nn.BatchNorm2d(ngf), nn.ReLU(True), # state size. (ngf) x 32 x 32 nn.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) # Discriminator Code class Discriminator(nn.Module): def __init__(self, ngpu): super(Discriminator, self).__init__() self.ngpu = ngpu self.main = nn.Sequential( # input is (nc) x 64 x 64 nn.Conv2d(nc, ndf, 4, 2, 1, bias=False), nn.LeakyReLU(0.2, inplace=True), # state size. (ndf) x 32 x 32 nn.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 16 nn.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 8 nn.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 4 nn.Conv2d(ndf * 8, 1, 4, 1, 0, bias=False), ) def forward(self, input): return self.main(input).view(-1, 1).squeeze(1) ``` 接下来定义初始化生成器和判别器: ```python # Initialize generator and discriminator netG = Generator(ngpu).cuda() netD = Discriminator(ngpu).cuda() ``` 接下来定义优化器和损失函数: ```python # Initialize optimizer optimizerD = optim.RMSprop(netD.parameters(), lr=lr) optimizerG = optim.RMSprop(netG.parameters(), lr=lr) # Initialize loss functions criterion = nn.BCEWithLogitsLoss() ``` 接下来定义训练过程: ```python # Training Loop # Lists to keep track of progress img_list = [] G_losses = [] D_losses = [] iters = 0 print("Starting Training Loop...") # For each epoch for epoch in range(num_epochs): # For each batch in the dataloader for i, data in enumerate(dataloader, 0): ############################ # (1) Update D network ########################### for n in range(n_critic): # Initialize gradients netD.zero_grad() # Format batch real_cpu = data[0].cuda() b_size = real_cpu.size(0) label = torch.full((b_size,), 1, device=torch.device('cuda')) # Forward pass real batch through D output = netD(real_cpu).view(-1) # Calculate loss on real batch D_loss_real = -output.mean() # Calculate gradients for D in backward pass D_loss_real.backward() # Sample noise as input for G noise = torch.randn(b_size, nz, 1, 1, device=torch.device('cuda')) # Generate fake image batch with G fake = netG(noise) # Classify fake batch with D output = netD(fake.detach()).view(-1) # Calculate loss on fake batch D_loss_fake = output.mean() # Calculate gradients for D in backward pass D_loss_fake.backward() # Compute gradient penalty alpha = torch.rand(b_size, 1, 1, 1).cuda() x_hat = (alpha * real_cpu.data + (1 - alpha) * fake.data).requires_grad_(True) out = netD(x_hat).view(-1) grad = torch.autograd.grad(outputs=out, inputs=x_hat, grad_outputs=torch.ones(out.size()).cuda(), create_graph=True, retain_graph=True, only_inputs=True)[0] gp = ((grad.norm(2, dim=1) - 1) ** 2).mean() * 10 gp.backward() # Add the gradients from the all critic iterations D_loss = D_loss_real + D_loss_fake + gp Wasserstein_D = D_loss_real - D_loss_fake # Update D optimizerD.step() # Clip weights of D for p in netD.parameters(): p.data.clamp_(-clip_value, clip_value) ############################ # (2) Update G network ########################### netG.zero_grad() # Generate a batch of images noise = torch.randn(b_size, nz, 1, 1, device=torch.device('cuda')) fake = netG(noise) # Classify the generated batch with D output = netD(fake).view(-1) # Calculate G's loss based on this output G_loss = -output.mean() # Update G G_loss.backward() optimizerG.step() # Output training stats if i % 50 == 0: print('[%d/%d][%d/%d]\tLoss_D: %.4f\tLoss_G: %.4f\tWasserstein_D: %.4f' % (epoch, num_epochs, i, len(dataloader), D_loss.item(), G_loss.item(), Wasserstein_D.item())) # Save Losses for plotting later G_losses.append(G_loss.item()) D_losses.append(D_loss.item()) # Check how the generator is doing by saving G's output on fixed noise if (iters % 500 == 0) or ((epoch == num_epochs-1) and (i == len(dataloader)-1)): with torch.no_grad(): fake = netG(fixed_noise).detach().cpu() img_list.append(vutils.make_grid(fake, padding=2, normalize=True)) iters += 1 ``` 接下来定义输出结果: ```python # Output generated images fig = plt.figure(figsize=(8, 8)) plt.axis("off") ims = [[plt.imshow(np.transpose(i, (1, 2, 0)), animated=True)] for i in img_list] ani = animation.ArtistAnimation(fig, ims, interval=1000, repeat_delay=1000, blit=True) HTML(ani.to_jshtml()) # Save generated images as GIF file fig = plt.figure(figsize=(8, 8)) plt.axis("off") ims = [[plt.imshow(np.transpose(i, (1, 2, 0)), animated=True)] for i in img_list] ani = animation.ArtistAnimation(fig, ims, interval=1000, repeat_delay=1000, blit=True) ani.save(output_dir + "/anime.gif", writer='pillow', fps=2) ```

相关推荐

最新推荐

recommend-type

GAN、WGAN、WGAN-GP5.docx

基于PyTorch实现生成对抗网络 拟合给定分布 要求可视化训练过程 实验报告 对比GAN、WGAN、WGAN-GP(稳定性、性能) 对比不同优化器的影响
recommend-type

毕业设计基于STC12C5A、SIM800C、GPS的汽车防盗报警系统源码.zip

STC12C5A通过GPS模块获取当前定位信息,如果车辆发生异常震动或车主打来电话(主动请求定位),将通过GSM发送一条定位短信到车主手机,车主点击链接默认打开网页版定位,如果有安装高德地图APP将在APP中打开并展示汽车当前位置 GPS模块可以使用多家的GPS模块,需要注意的是,当前程序对应的是GPS北斗双模芯片,故只解析 GNRMC数据,如果你使用GPS芯片则应改为GPRMC数据即可。 系统在初始化的时候会持续短鸣,每初始化成功一部分后将长鸣一声,如果持续短鸣很久(超过20分钟),建议通过串口助手查看系统输出的调试信息,系统串口默认输出从初始化开始的所有运行状态信息。 不过更建议你使用SIM868模块,集成GPS.GSM.GPRS,使用更加方便
recommend-type

基于tensorflow2.x卷积神经网络字符型验证码识别.zip

基于tensorflow2.x卷积神经网络字符型验证码识别 卷积神经网络(Convolutional Neural Networks, CNNs 或 ConvNets)是一类深度神经网络,特别擅长处理图像相关的机器学习和深度学习任务。它们的名称来源于网络中使用了一种叫做卷积的数学运算。以下是卷积神经网络的一些关键组件和特性: 卷积层(Convolutional Layer): 卷积层是CNN的核心组件。它们通过一组可学习的滤波器(或称为卷积核、卷积器)在输入图像(或上一层的输出特征图)上滑动来工作。 滤波器和图像之间的卷积操作生成输出特征图,该特征图反映了滤波器所捕捉的局部图像特性(如边缘、角点等)。 通过使用多个滤波器,卷积层可以提取输入图像中的多种特征。 激活函数(Activation Function): 在卷积操作之后,通常会应用一个激活函数(如ReLU、Sigmoid或tanh)来增加网络的非线性。 池化层(Pooling Layer): 池化层通常位于卷积层之后,用于降低特征图的维度(空间尺寸),减少计算量和参数数量,同时保持特征的空间层次结构。 常见的池化操作包括最大池化(Max Pooling)和平均池化(Average Pooling)。 全连接层(Fully Connected Layer): 在CNN的末端,通常会有几层全连接层(也称为密集层或线性层)。这些层中的每个神经元都与前一层的所有神经元连接。 全连接层通常用于对提取的特征进行分类或回归。 训练过程: CNN的训练过程与其他深度学习模型类似,通过反向传播算法和梯度下降(或其变种)来优化网络参数(如滤波器权重和偏置)。 训练数据通常被分为多个批次(mini-batches),并在每个批次上迭代更新网络参数。 应用: CNN在计算机视觉领域有着广泛的应用,包括图像分类、目标检测、图像分割、人脸识别等。 它们也已被扩展到处理其他类型的数据,如文本(通过卷积一维序列)和音频(通过卷积时间序列)。 随着深度学习技术的发展,卷积神经网络的结构和设计也在不断演变,出现了许多新的变体和改进,如残差网络(ResNet)、深度卷积生成对抗网络(DCGAN)等。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

解答下列问题:S—>S;T|T;T—>a 构造任意项目集规范族,构造LR(0)分析表,并分析a;a

对于这个文法,我们可以构造以下项目集规范族: I0: S -> .S S -> .T T -> .a I1: S -> S. [$ T -> T. [$ I2: S -> T. I3: S -> S.;S S -> S.;T T -> T.;a 其中,点(.)表示已经被扫描过的符号,;$表示输入串的结束符号。 根据项目集规范族,我们可以构造出LR(0)分析表: 状态 | a | $ ---- | - | - I0 | s3| I1 | |acc I2 | | 其中s3表示移进到状态3,acc表示接受。在分析字符串a;a时,我们可以按照以下步骤进行
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

实现实时监控告警系统:Kafka与Grafana整合

![实现实时监控告警系统:Kafka与Grafana整合](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9tbWJpei5xcGljLmNuL21tYml6X2pwZy9BVldpY3ladXVDbEZpY1pLWmw2bUVaWXFUcEdLT1VDdkxRSmQxZXB5R1lxaWNlUjA2c0hFek5Qc3FyRktudFF1VDMxQVl3QTRXV2lhSWFRMEFRc0I1cW1ZOGcvNjQw?x-oss-process=image/format,png) # 1.1 Kafka集群架构 Kafka集群由多个称为代理的服务器组成,这