日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人文社科 > 生活经验 >内容正文

生活经验

深度卷积生成对抗网络

發(fā)布時(shí)間:2023/11/28 生活经验 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 深度卷积生成对抗网络 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

深度卷積生成對(duì)抗網(wǎng)絡(luò)

Deep Convolutional Generative Adversarial Networks

GANs如何工作的基本思想。可以從一些簡(jiǎn)單的,易于抽樣的分布,如均勻分布或正態(tài)分布中提取樣本,并將其轉(zhuǎn)換成與某些數(shù)據(jù)集的分布相匹配的樣本。雖然例子匹配一個(gè)二維高斯分布得到了交叉點(diǎn),不是特別令人興奮。

將演示如何使用GANs生成照片級(jí)真實(shí)感圖像。將以深卷積GAN(DCGAN)為基礎(chǔ)建立模型。將借用卷積體系結(jié)構(gòu),已經(jīng)被證明在區(qū)分計(jì)算機(jī)視覺(jué)問(wèn)題上是如此成功,并展示如何通過(guò)GANs來(lái)利用來(lái)生成真實(shí)感圖像。

from mxnet
import gluon, init, np, npx

from mxnet.gluon
import nn

from d2l
import mxnet as d2l

npx.set_np()

  1. The Pokemon Dataset

將使用的數(shù)據(jù)集是從pokemondb獲得的Pokemon精靈集合。首先下載、提取并加載此數(shù)據(jù)集。

#@save

d2l.DATA_HUB[‘pokemon’] = (d2l.DATA_URL

  • ‘pokemon.zip’,

‘c065c0e2593b8b161a2d7873e42418bf6a21106c’)

data_dir = d2l.download_extract(‘pokemon’)

pokemon = gluon.data.vision.datasets.ImageFolderDataset(data_dir)

Downloading …/data/pokemon.zip from

http://d2l-data.s3-accelerate.amazonaws.com/pokemon.zip…

將每個(gè)圖像調(diào)整為64×64。ToTensor變換將像素值投影到[0,1],而生成器將使用tanh函數(shù)來(lái)獲得[-1,1]。因此,用0.5平均值和0.5與值范圍匹配的標(biāo)準(zhǔn)偏差。

batch_size = 256

transformer = gluon.data.vision.transforms.Compose([

gluon.data.vision.transforms.Resize(64),gluon.data.vision.transforms.ToTensor(),gluon.data.vision.transforms.Normalize(0.5, 0.5)])

data_iter = gluon.data.DataLoader(

pokemon.transform_first(transformer), batch_size=batch_size,

shuffle=True, num_workers=d2l.get_dataloader_workers())

讓想象一下前20幅圖像。

d2l.set_figsize((4, 4))

for X, y in data_iter:

imgs = X[0:20,:,:,:].transpose(0, 2, 3, 1)/2+0.5d2l.show_images(imgs, num_rows=4, num_cols=5)break


2. The Generator

生成器Generator需要映射噪聲變量z∈Rd,一個(gè)length-d圖像的寬度和寬度64×64。使用轉(zhuǎn)置卷積層來(lái)擴(kuò)大輸入大小的全卷積網(wǎng)絡(luò)。生成器的基本塊包含一個(gè)轉(zhuǎn)置卷積層,然后是批處理規(guī)范化和ReLU激活。

class G_block(nn.Block):

def __init__(self, channels, kernel_size=4,strides=2, padding=1, **kwargs):super(G_block, self).__init__(**kwargs)self.conv2d_trans = nn.Conv2DTranspose(channels, kernel_size, strides, padding, use_bias=False)self.batch_norm = nn.BatchNorm()self.activation = nn.Activation('relu')def forward(self, X):return self.activation(self.batch_norm(self.conv2d_trans(X)))


x = np.zeros((2, 3, 16, 16))

g_blk = G_block(20)

g_blk.initialize()

g_blk(x).shape

(2, 20, 32, 32)

如果將轉(zhuǎn)置卷積層更改為4×4內(nèi)核,1×1跨步和零填充。輸入大小為1×1輸出寬度和高度分別增加3。

x = np.zeros((2, 3, 1, 1))

g_blk = G_block(20, strides=1, padding=0)

g_blk.initialize()

g_blk(x).shape

(2, 20, 4, 4)

生成器由四個(gè)基本塊組成,將輸入的寬度和高度從1增加到32。同時(shí),首先將潛在變量投影到64×8通道,然后每次將通道減半。最后,利用轉(zhuǎn)置卷積層產(chǎn)生輸出。進(jìn)一步將寬度和高度加倍以匹配所需的64×64形狀,并將通道大小減小到3。tanh激活函數(shù)用于將輸出值投影到(-1,1)范圍。

n_G = 64

net_G = nn.Sequential()

net_G.add(G_block(n_G8, strides=1, padding=0), # output: (648, 4, 4)

      G_block(n_G*4),  # output: (64*4, 8, 8)G_block(n_G*2),  # output: (64*2, 16, 16)G_block(n_G),   # output: (64,

32, 32)

      nn.Conv2DTranspose(3, kernel_size=4, strides=2, padding=1, use_bias=False,activation='tanh'))  # output: (3, 64, 64)

生成一個(gè)100維的潛在變量來(lái)驗(yàn)證生成器的輸出形狀。

x = np.zeros((1, 100, 1, 1))

net_G.initialize()

net_G(x).shape

(1, 3, 64, 64)

  1. Discriminator

該鑒別器Discriminator是一個(gè)普通的卷積網(wǎng)絡(luò),但使用泄漏ReLU作為其激活函數(shù)。鑒于α∈[0,1]。R ReLU if α=0, and an identity function if α=1.
For α∈(0,1), leaky ReLU。可以看出,如果α=0,以及如果α=1. 為α∈(0,1),leaky ReLU是一個(gè)非線性函數(shù),對(duì)負(fù)輸入給出非零輸出。目的是解決“dying ReLU”問(wèn)題,即神經(jīng)元可能總是輸出一個(gè)負(fù)值,因此由于ReLU的梯度為0,因此無(wú)法取得任何進(jìn)展。

alphas = [0, 0.2, 0.4, .6, .8, 1]

x = np.arange(-2, 1, 0.1)

Y = [nn.LeakyReLU(alpha)(x).asnumpy() for alpha in alphas]

d2l.plot(x.asnumpy(), Y, ‘x’, ‘y’, alphas)

鑒別器的基本模塊是卷積層,然后是批處理規(guī)范化層和泄漏ReLU激活。卷積層的超參數(shù)類似于生成塊中的轉(zhuǎn)置卷積層。

class D_block(nn.Block):

def __init__(self, channels, kernel_size=4, strides=2,padding=1, alpha=0.2, **kwargs):super(D_block, self).__init__(**kwargs)self.conv2d = nn.Conv2D(channels, kernel_size, strides, padding, use_bias=False)self.batch_norm = nn.BatchNorm()self.activation = nn.LeakyReLU(alpha)def forward(self, X):return self.activation(self.batch_norm(self.conv2d(X)))


x = np.zeros((2, 3, 16, 16))

d_blk = D_block(20)

d_blk.initialize()

d_blk(x).shape

(2, 20, 8, 8)

鑒別器是發(fā)生器的鏡像。

n_D = 64

net_D = nn.Sequential()

net_D.add(D_block(n_D), # output: (64,
32, 32)

      D_block(n_D*2),  # output: (64*2, 16, 16)D_block(n_D*4),  # output: (64*4, 8, 8)D_block(n_D*8),  # output: (64*8, 4, 4)nn.Conv2D(1, kernel_size=4, use_bias=False)) 

# output: (1, 1, 1)

使用帶輸出通道的卷積層1作為最后一層獲得單個(gè)預(yù)測(cè)值。

x = np.zeros((1, 3, 64, 64))

net_D.initialize()

net_D(x).shape

(1, 1, 1, 1)

  1. Training

對(duì)生成器和鑒別器使用學(xué)習(xí)速率,改變?chǔ)?在亞當(dāng)中0.9到0.5。降低了動(dòng)量的平滑度,即過(guò)去梯度的指數(shù)加權(quán)移動(dòng)平均值,以處理快速變化的梯度,因?yàn)樯善骱丸b別器相互競(jìng)爭(zhēng)。另外,用隨機(jī)產(chǎn)生的隨機(jī)噪聲來(lái)加速計(jì)算。

def train(net_D, net_G, data_iter, num_epochs, lr, latent_dim,

      ctx=d2l.try_gpu()):loss = gluon.loss.SigmoidBCELoss()net_D.initialize(init=init.Normal(0.02), force_reinit=True, ctx=ctx)net_G.initialize(init=init.Normal(0.02), force_reinit=True, ctx=ctx)trainer_hp = {'learning_rate': lr, 'beta1': 0.5}trainer_D = gluon.Trainer(net_D.collect_params(), 'adam', trainer_hp)trainer_G = gluon.Trainer(net_G.collect_params(), 'adam', trainer_hp)animator = d2l.Animator(xlabel='epoch', ylabel='loss',xlim=[1, num_epochs], nrows=2, figsize=(5, 5),legend=['discriminator', 'generator'])animator.fig.subplots_adjust(hspace=0.3)for epoch in range(1, num_epochs + 1):# Train one epochtimer = d2l.Timer()metric = d2l.Accumulator(3)  # loss_D, loss_G, num_examplesfor X, _ in data_iter:batch_size = X.shape[0]Z = np.random.normal(0, 1, size=(batch_size, latent_dim, 1, 1))X, Z = X.as_in_ctx(ctx), Z.as_in_ctx(ctx),metric.add(d2l.update_D(X, Z, net_D, net_G, loss, trainer_D),d2l.update_G(Z, net_D, net_G, loss, trainer_G),batch_size)# Show generated examplesZ = np.random.normal(0, 1, size=(21, latent_dim, 1, 1), ctx=ctx)# Normalize the synthetic data to N(0, 1)fake_x = net_G(Z).transpose(0, 2, 3, 1) / 2 + 0.5imgs = np.concatenate([np.concatenate([fake_x[i * 7 + j] for j in range(7)], axis=1)for i in range(len(fake_x)//7)], axis=0)animator.axes[1].cla()animator.axes[1].imshow(imgs.asnumpy())# Show the lossesloss_D, loss_G = metric[0] / metric[2], metric[1] / metric[2]animator.add(epoch, (loss_D, loss_G))print('loss_D %.3f, loss_G %.3f, %d examples/sec on %s' % (loss_D, loss_G, metric[2]/timer.stop(), ctx))

現(xiàn)在訓(xùn)練模型

latent_dim, lr, num_epochs = 100, 0.005, 40

train(net_D, net_G, data_iter, num_epochs, lr, latent_dim)

loss_D 0.011, loss_G 7.465, 2663 examples/sec on gpu(0)

5. Summary

· DCGAN architecture has four convolutional layers for the Discriminator and four “fractionally-strided” convolutional layers for the Generator.

· The Discriminator is a 4-layer strided convolutions with batch normalization (except its input layer) and leaky ReLU activations.

· Leaky ReLU is a nonlinear function that give a non-zero output for a negative input. It aims to fix the “dying ReLU” problem and helps the gradients flow easier through the architecture.

總結(jié)

以上是生活随笔為你收集整理的深度卷积生成对抗网络的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。