(五)使用生成对抗网络 (GAN)生成新的时装设计

目录

介绍

预测新时尚形象的力量

构建GAN

初始化GAN参数和加载数据

从头开始构建生成器

从头开始构建鉴别器

初始化GAN的损失和优化器

下一步


介绍

DeepFashion等数据集的可用性为时尚行业开辟了新的可能性。在本系列文章中,我们将展示一个AI驱动的深度学习系统,它可以帮助我们更好地了解客户的需求,从而彻底改变时装设计行业。

在这个项目中,我们将使用:

我们假设您熟悉深度学习的概念,以及Jupyter NotebooksTensorFlow。如果您不熟悉Jupyter Notebook,请从本教程开始。欢迎下载项目代码

上一篇文章中,我们评估并改进了我们的深度网络性能。在本文中,我们将致力于构建、训练和测试生成对抗网络(GAN)——然后我们将使用该网络生成新的服装图像和设计。

预测新时尚形象的力量

人工智能不仅可以帮助我们预测服装项目的类别,还可以创建计算机生成的外观相似项目的图像。这对于努力打造个性化服装或预测更广泛时尚趋势的零售商和时装设计师来说非常方便。

在创建GAN之前,由于图像的大量数据,生成逼真的时尚图像是一项具有挑战性的任务。图像往往是高分辨率的,因此像素很多。另外,每个像素代表三个通道值:红色、蓝色和绿色 (RGB)GAN为研究人员提供了一种可行的方法来生成和验证所有这些数据。

构建GAN

GAN是一种流行的无监督机器学习模型,其中两个神经网络——一个生成器和一个鉴别器——相互交互。生成器的作用是从它作为输入的随机噪声中生成图像。鉴别器的任务是检测这些生成的图像是假的还是真实的(通过将它们与数据集中的图像进行比较)。这个过程会持续几个时期,直到假和真之间的鉴别器损失达到其最小值。随着损失达到最小值,生成器在生成与原始数据集中的图像相似的图像方面变得足够熟练。

构建GAN将包括以下阶段:

  1. 初始化网络参数和加载数据
  2. 构建生成器
  3. 构建鉴别器

我们将使用Pytorch库来构建我们的GAN。这个库速度很快,而且不需要很多计算能力。

Conda上安装带有CUDA10Pytorch

# CUDA 10.0
conda install pytorch==1.2.0 torchvision==0.4.0 cudatoolkit=10.0 -c pytorch

初始化GAN参数和加载数据

构成GAN的两个卷积神经网络 (CNN) 包括用于鉴别器的卷积、批归一化和ReLU层,以及用于生成器的反卷积、批归一化和ReLU层。

在开始构建我们的生成器和鉴别器网络之前,让我们设置一些参数并加载将用于训练和测试网络的时尚图像数据集。

首先,我们导入一些依赖项。

from __future__ import print_function
#%matplotlib inline
import argparse
import os
import random
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torch.utils.data
import torchvision.datasets as dset
import torchvision.transforms as transforms
import torchvision.utils as vutils
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from IPython.display import HTML

接下来,我们设置一些随机种子来实现可重复性:

# Set random seed for reproducibility
manualSeed = 999
#manualSeed = random.randint(1, 10000) # use if you want new results
print("Random Seed: ", manualSeed)
random.seed(manualSeed)
torch.manual_seed(manualSeed)

然后,我们设置了一些重要的参数,例如特征图的数量、输入图像大小、批量大小、时期数和学习率。

# Root directory for dataset
dataroot = r"C:\Users\abdul\Desktop\ContentLab\P2\DeepFashion\Train"

# Number of workers for dataloader
workers = 2

# Batch size during training
batch_size = 128

# 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 = 40

# Learning rate for optimizers
lr = 0.0002

# Beta1 hyperparam for Adam optimizers
beta1 = 0.5

# Number of GPUs available. Use 0 for CPU mode.
ngpu = 1

现在我们可以使用dataloader加载我们的数据并显示该数据的样本。

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)

# Decide which device we want to run on
device = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu > 0) else "cpu")

# Plot some training images
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)))

最后,我们将使用下面的函数来初始化生成器和鉴别器网络的权重。

# custom weights initialization 
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)

从头开始构建生成器

生成器CNN由转置卷积层、批范数层和ReLU激活组成。输入是从标准正态分布中提取的潜在向量z,输出是3 x 64 x 64像素的RGB图像。

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)

现在,我们创建netG生成器并展示其结构。

# Create the generator
netG = Generator(ngpu).to(device)

# Handle multi-gpu if desired
if (device.type == 'cuda') and (ngpu > 1):
    netG = nn.DataParallel(netG, list(range(ngpu)))

# 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)

从头开始构建鉴别器

我们的鉴别器将被称为netD,它将由跨步卷积层、LeakyReLU激活层和批范数层组成。它的输入将是一个3 x 64 x 64的输入图像,它的输出将是输入来自真实数据集的标量概率。

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),
            nn.Sigmoid()
        )

    def forward(self, input):
        return self.main(input)

# Create the Discriminator
netD = Discriminator(ngpu).to(device)

# Handle multi-gpu if desired
if (device.type == 'cuda') and (ngpu > 1):
    netD = nn.DataParallel(netD, list(range(ngpu)))

# 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)

初始化GAN的损失和优化器

在开始训练我们的GAN之前,我们将设置它的损失函数和优化器。在GAN中,我们通常使用二元交叉熵作为损失函数,因为我们在输出中有两个类:Fake (0)Real (1)。我们将使用Adam 优化器,学习率为 0.0002Beta10.5

# Initialize BCELoss function
criterion = nn.BCELoss()

# Create batch of latent vectors that we will use to visualize
#  the progression of the generator
fixed_noise = torch.randn(64, nz, 1, 1, device=device)

# Establish convention for real and fake labels during training
real_label = 1.
fake_label = 0.

# Setup Adam optimizers for both G and D
optimizerD = optim.Adam(netD.parameters(), lr=lr, betas=(beta1, 0.999))
optimizerG = optim.Adam(netG.parameters(), lr=lr, betas=(beta1, 0.999))

下一步

接下来的文章中,我们将告诉你如何培养对时装设计产生的GAN。敬请关注!

https://www.codeproject.com/Articles/5297330/Generate-New-Fashion-Designs-with-a-Generative-Adv

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值