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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人工智能 > pytorch >内容正文

pytorch

深度学习总结:cycleGAN原理,实现图片风格切换,可以和之前的伪DL方式对比一下,pytoch实现

發(fā)布時間:2024/9/15 pytorch 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 深度学习总结:cycleGAN原理,实现图片风格切换,可以和之前的伪DL方式对比一下,pytoch实现 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

  • cycleGAN原理
    • 一般的unsupervised conditional generation的處理辦法
    • cycleGAN處理unsupervised conditional generation的辦法:
      • 比較正常的思路:
      • cycleGAN的思路:
  • cycleGAN實現(xiàn):
    • Discriminator的結(jié)構(gòu):
    • Discriminator的Loss:
    • Generator的結(jié)構(gòu):
      • Generator的結(jié)構(gòu)圖:
      • Residual Block:
      • Autoencoder實現(xiàn):
    • Generator的Loss:
    • 整體結(jié)構(gòu):
    • 優(yōu)化器:
    • 訓(xùn)練過程:

cycleGAN原理

一般的unsupervised conditional generation的處理辦法

參考一下,信息量很大

cycleGAN處理unsupervised conditional generation的辦法:

比較正常的思路:

給一個domian X的image_x, 通過NN后變成domian Y的image_x,問題就解決了,但是NN學(xué)習(xí)需要目標(biāo)啊,目標(biāo)就是image_x是不是domian Y,還有就是和image_x像不像,也就是上圖中的real or not , match or not,這個實際上可以假如GAN的framework,在保證像的同時,讓domain 越來越match。

前面提到過一種思路:使用vgg的features來保證圖片像,使用features內(nèi)部之間的內(nèi)積和表示圖片風(fēng)格,一種偽DL的實現(xiàn)方法:https://blog.csdn.net/weixin_40759186/article/details/87804316

我們一般的GAN的Discriminator都是一個目標(biāo),怎么實現(xiàn)real or not && match or not?real or not通過reconstruct error實現(xiàn),match or not通過訓(xùn)練一個domian classifier實現(xiàn)。

cycleGAN的思路:

real or not通過reconstruct error實現(xiàn),match or not通過訓(xùn)練一個domian classifier實現(xiàn)。

match or not通過訓(xùn)練一個domian classifier實現(xiàn):generator 生成的fake data和樣本的real data訓(xùn)練domian classifier,可以得到兩個Discriminator: D_x和D_y。

real or not通過reconstruct error實現(xiàn):x_domainx —>generator 生成的fake data: x_domainy—>generator 生成的reconstruct x^ _domianx,比較x^_domianx和x_domainx。

cycleGAN實現(xiàn):

Discriminator的結(jié)構(gòu):

class Discriminator(nn.Module):def __init__(self, conv_dim=64):super(Discriminator, self).__init__()# Define all convolutional layers# Should accept an RGB image as input and output a single value# Convolutional layers, increasing in depth# first layer has *no* batchnormself.conv1 = conv(3, conv_dim, 4, batch_norm=False) # x, y = 64, depth 64self.conv2 = conv(conv_dim, conv_dim*2, 4) # (32, 32, 128)self.conv3 = conv(conv_dim*2, conv_dim*4, 4) # (16, 16, 256)self.conv4 = conv(conv_dim*4, conv_dim*8, 4) # (8, 8, 512)# Classification layerself.conv5 = conv(conv_dim*8, 1, 4, stride=1, batch_norm=False)def forward(self, x):# relu applied to all conv layers but lastout = F.relu(self.conv1(x))out = F.relu(self.conv2(out))out = F.relu(self.conv3(out))out = F.relu(self.conv4(out))# last, classification layerout = self.conv5(out)return out

Discriminator的Loss:

這個就是平常的GAN的Discriminator,讓real loss底,fake loss高

def real_mse_loss(D_out):# how close is the produced output from being "real"?return torch.mean((D_out-1)**2)def fake_mse_loss(D_out):# how close is the produced output from being "false"?return torch.mean(D_out**2)## First: D_X, real and fake loss components ### Train with real imagesd_x_optimizer.zero_grad()# 1. Compute the discriminator losses on real imagesout_x = D_X(images_X)D_X_real_loss = real_mse_loss(out_x)# Train with fake images# 2. Generate fake images that look like domain X based on real images in domain Yfake_X = G_YtoX(images_Y)# 3. Compute the fake loss for D_Xout_x = D_X(fake_X)D_X_fake_loss = fake_mse_loss(out_x)# 4. Compute the total loss and perform backpropd_x_loss = D_X_real_loss + D_X_fake_loss

Generator的結(jié)構(gòu):

Generator的結(jié)構(gòu)圖:

這個一個典型的autoencoder結(jié)構(gòu)

Residual Block:

# residual block class class ResidualBlock(nn.Module):"""Defines a residual block.This adds an input x to a convolutional layer (applied to x) with the same size input and output.These blocks allow a model to learn an effective transformation from one domain to another."""def __init__(self, conv_dim):super(ResidualBlock, self).__init__()# conv_dim = number of inputs# define two convolutional layers + batch normalization that will act as our residual function, F(x)# layers should have the same shape input as output; I suggest a kernel_size of 3self.conv_layer1 = conv(in_channels=conv_dim, out_channels=conv_dim, kernel_size=3, stride=1, padding=1, batch_norm=True)self.conv_layer2 = conv(in_channels=conv_dim, out_channels=conv_dim, kernel_size=3, stride=1, padding=1, batch_norm=True)def forward(self, x):# apply a ReLu activation the outputs of the first layer# return a summed output, x + resnet_block(x)out_1 = F.relu(self.conv_layer1(x))out_2 = x + self.conv_layer2(out_1)return out_2

Autoencoder實現(xiàn):

class CycleGenerator(nn.Module):def __init__(self, conv_dim=64, n_res_blocks=6):super(CycleGenerator, self).__init__()# 1. Define the encoder part of the generator# initial convolutional layer given, belowself.conv1 = conv(3, conv_dim, 4)self.conv2 = conv(conv_dim, conv_dim*2, 4)self.conv3 = conv(conv_dim*2, conv_dim*4, 4)# 2. Define the resnet part of the generator# Residual blocksres_layers = []for layer in range(n_res_blocks):res_layers.append(ResidualBlock(conv_dim*4))# use sequential to create these layersself.res_blocks = nn.Sequential(*res_layers)# 3. Define the decoder part of the generator# two transpose convolutional layers and a third that looks a lot like the initial conv layerself.deconv1 = deconv(conv_dim*4, conv_dim*2, 4)self.deconv2 = deconv(conv_dim*2, conv_dim, 4)# no batch norm on last layerself.deconv3 = deconv(conv_dim, 3, 4, batch_norm=False)def forward(self, x):"""Given an image x, returns a transformed image."""# define feedforward behavior, applying activations as necessaryout = F.relu(self.conv1(x))out = F.relu(self.conv2(out))out = F.relu(self.conv3(out))out = self.res_blocks(out)out = F.relu(self.deconv1(out))out = F.relu(self.deconv2(out))# tanh applied to last layerout = F.tanh(self.deconv3(out))return out# helper deconv function def deconv(in_channels, out_channels, kernel_size, stride=2, padding=1, batch_norm=True):"""Creates a transpose convolutional layer, with optional batch normalization."""layers = []# append transpose conv layerlayers.append(nn.ConvTranspose2d(in_channels, out_channels, kernel_size, stride, padding, bias=False))# optional batch norm layerif batch_norm:layers.append(nn.BatchNorm2d(out_channels))return nn.Sequential(*layers)

Generator的Loss:

注意這里是一個cycle之后再去更新G1和G2的梯度

def real_mse_loss(D_out):# how close is the produced output from being "real"?return torch.mean((D_out-1)**2)def fake_mse_loss(D_out):# how close is the produced output from being "false"?return torch.mean(D_out**2)def cycle_consistency_loss(real_im, reconstructed_im, lambda_weight):# calculate reconstruction loss # as absolute value difference between the real and reconstructed imagesreconstr_loss = torch.mean(torch.abs(real_im - reconstructed_im))# return weighted lossreturn lambda_weight*reconstr_loss# =========================================# TRAIN THE GENERATORS# =========================================## First: generate fake X images and reconstructed Y images ##g_optimizer.zero_grad()# 1. Generate fake images that look like domain X based on real images in domain Yfake_X = G_YtoX(images_Y)# 2. Compute the generator loss based on domain Xout_x = D_X(fake_X)g_YtoX_loss = real_mse_loss(out_x)# 3. Create a reconstructed y# 4. Compute the cycle consistency loss (the reconstruction loss)reconstructed_Y = G_XtoY(fake_X)reconstructed_y_loss = cycle_consistency_loss(images_Y, reconstructed_Y, lambda_weight=10)## Second: generate fake Y images and reconstructed X images ### 1. Generate fake images that look like domain Y based on real images in domain Xfake_Y = G_XtoY(images_X)# 2. Compute the generator loss based on domain Yout_y = D_Y(fake_Y)g_XtoY_loss = real_mse_loss(out_y)# 3. Create a reconstructed x# 4. Compute the cycle consistency loss (the reconstruction loss)reconstructed_X = G_YtoX(fake_Y)reconstructed_x_loss = cycle_consistency_loss(images_X, reconstructed_X, lambda_weight=10)# 5. Add up all generator and reconstructed losses and perform backpropg_total_loss = g_YtoX_loss + g_XtoY_loss + reconstructed_y_loss + reconstructed_x_loss

整體結(jié)構(gòu):

需要訓(xùn)練2個Discriminators和2個Generators:
有人可能要問訓(xùn)練一個Generator可不可以? X->G->Y, Y->G->X,真把Generator當(dāng)作萬能的?

def create_model(g_conv_dim=64, d_conv_dim=64, n_res_blocks=6):"""Builds the generators and discriminators."""# Instantiate generatorsG_XtoY = CycleGenerator(conv_dim=g_conv_dim, n_res_blocks=n_res_blocks)G_YtoX = CycleGenerator(conv_dim=g_conv_dim, n_res_blocks=n_res_blocks)# Instantiate discriminatorsD_X = Discriminator(conv_dim=d_conv_dim)D_Y = Discriminator(conv_dim=d_conv_dim)# move models to GPU, if availableif torch.cuda.is_available():device = torch.device("cuda:0")G_XtoY.to(device)G_YtoX.to(device)D_X.to(device)D_Y.to(device)print('Models moved to GPU.')else:print('Only CPU available.')return G_XtoY, G_YtoX, D_X, D_Y

優(yōu)化器:

看到?jīng)]G_XtoY和G_YtoX是在一個cycle更新的

import torch.optim as optim# hyperparams for Adam optimizer lr=0.0002 beta1=0.5 beta2=0.999 # default valueg_params = list(G_XtoY.parameters()) + list(G_YtoX.parameters()) # Get generator parameters# Create optimizers for the generators and discriminators g_optimizer = optim.Adam(g_params, lr, [beta1, beta2]) d_x_optimizer = optim.Adam(D_X.parameters(), lr, [beta1, beta2]) d_y_optimizer = optim.Adam(D_Y.parameters(), lr, [beta1, beta2])

訓(xùn)練過程:

先訓(xùn)練D_X,先訓(xùn)練D_Y,再訓(xùn)練G_XtoY和G_YtoX

def training_loop(dataloader_X, dataloader_Y, test_dataloader_X, test_dataloader_Y, n_epochs=1000):print_every=10# keep track of losses over timelosses = []test_iter_X = iter(test_dataloader_X)test_iter_Y = iter(test_dataloader_Y)# Get some fixed data from domains X and Y for sampling. These are images that are held# constant throughout training, that allow us to inspect the model's performance.fixed_X = test_iter_X.next()[0]fixed_Y = test_iter_Y.next()[0]fixed_X = scale(fixed_X) # make sure to scale to a range -1 to 1fixed_Y = scale(fixed_Y)# batches per epochiter_X = iter(dataloader_X)iter_Y = iter(dataloader_Y)batches_per_epoch = min(len(iter_X), len(iter_Y))for epoch in range(1, n_epochs+1):# Reset iterators for each epochif epoch % batches_per_epoch == 0:iter_X = iter(dataloader_X)iter_Y = iter(dataloader_Y)images_X, _ = iter_X.next()images_X = scale(images_X) # make sure to scale to a range -1 to 1images_Y, _ = iter_Y.next()images_Y = scale(images_Y)# move images to GPU if available (otherwise stay on CPU)device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")images_X = images_X.to(device)images_Y = images_Y.to(device)# ============================================# TRAIN THE DISCRIMINATORS# ============================================## First: D_X, real and fake loss components ### Train with real imagesd_x_optimizer.zero_grad()# 1. Compute the discriminator losses on real imagesout_x = D_X(images_X)D_X_real_loss = real_mse_loss(out_x)# Train with fake images# 2. Generate fake images that look like domain X based on real images in domain Yfake_X = G_YtoX(images_Y)# 3. Compute the fake loss for D_Xout_x = D_X(fake_X)D_X_fake_loss = fake_mse_loss(out_x)# 4. Compute the total loss and perform backpropd_x_loss = D_X_real_loss + D_X_fake_lossd_x_loss.backward()d_x_optimizer.step()## Second: D_Y, real and fake loss components ### Train with real imagesd_y_optimizer.zero_grad()# 1. Compute the discriminator losses on real imagesout_y = D_Y(images_Y)D_Y_real_loss = real_mse_loss(out_y)# Train with fake images# 2. Generate fake images that look like domain Y based on real images in domain Xfake_Y = G_XtoY(images_X)# 3. Compute the fake loss for D_Yout_y = D_Y(fake_Y)D_Y_fake_loss = fake_mse_loss(out_y)# 4. Compute the total loss and perform backpropd_y_loss = D_Y_real_loss + D_Y_fake_lossd_y_loss.backward()d_y_optimizer.step()# =========================================# TRAIN THE GENERATORS# =========================================## First: generate fake X images and reconstructed Y images ##g_optimizer.zero_grad()# 1. Generate fake images that look like domain X based on real images in domain Yfake_X = G_YtoX(images_Y)# 2. Compute the generator loss based on domain Xout_x = D_X(fake_X)g_YtoX_loss = real_mse_loss(out_x)# 3. Create a reconstructed y# 4. Compute the cycle consistency loss (the reconstruction loss)reconstructed_Y = G_XtoY(fake_X)reconstructed_y_loss = cycle_consistency_loss(images_Y, reconstructed_Y, lambda_weight=10)## Second: generate fake Y images and reconstructed X images ### 1. Generate fake images that look like domain Y based on real images in domain Xfake_Y = G_XtoY(images_X)# 2. Compute the generator loss based on domain Yout_y = D_Y(fake_Y)g_XtoY_loss = real_mse_loss(out_y)# 3. Create a reconstructed x# 4. Compute the cycle consistency loss (the reconstruction loss)reconstructed_X = G_YtoX(fake_Y)reconstructed_x_loss = cycle_consistency_loss(images_X, reconstructed_X, lambda_weight=10)# 5. Add up all generator and reconstructed losses and perform backpropg_total_loss = g_YtoX_loss + g_XtoY_loss + reconstructed_y_loss + reconstructed_x_lossg_total_loss.backward()g_optimizer.step()

總結(jié)

以上是生活随笔為你收集整理的深度学习总结:cycleGAN原理,实现图片风格切换,可以和之前的伪DL方式对比一下,pytoch实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 久久精品一本 | 性色在线| 激情黄色小说视频 | 中文字幕永久在线播放 | www在线看片 | 少妇精品久久久一区二区三区 | 蜜臀av性久久久久蜜臀aⅴ流畅 | 欧美人与动牲交a欧美精品 欧美三级在线看 | 蜜桃啪啪 | 亚洲一卡二卡三卡四卡 | 午夜精品小视频 | 四虎首页| 夜夜嗨av一区二区三区 | 伊人国产精品 | 日本视频免费在线播放 | 加勒比精品在线 | 欧美日韩视频在线播放 | 天天摸天天操天天射 | 99热这里是精品 | 一级免费看片 | 日本免费色视频 | 一起射导航 | 色婷婷精品久久二区二区密 | 亚洲男人天堂 | 色哟哟网站 | 伊人久久精品 | 国内精久久久久久久久久人 | 成人天堂av | 欧美日韩精品一区二区三区四区 | a v视频在线播放 | 中文字幕日韩一区 | 亚洲 欧美 激情 另类 | 呦呦视频在线观看 | 极品美女高潮出白浆 | 亚洲综合免费 | 57pao国产成永久免费视频 | 国产91精品一区二区绿帽 | 用舌头去添高潮无码视频 | 天天夜夜操 | 亚洲综合视频网 | 韩国三级免费 | 日日网站 | 打美女白嫩屁屁网站 | 亚洲网站免费 | 亚洲国产成人精品久久久 | 一级黄色片在线看 | 国产精品一区二区三区免费看 | 热久久久久久久 | 西西4444www大胆无视频 | 中文字幕日韩欧美 | 欧美人与性动交xxⅹxx | 欧美成人免费网站 | www.黄色片网站 | 69er小视频 | 邪恶久久| 黄色一级片免费 | 蜜桃精品视频在线 | 色a视频| 人妻无码一区二区三区免费 | 高清乱码免费 | 亚洲一区偷拍 | 哪里可以免费看毛片 | 欧美高清视频一区二区 | 国产精品扒开腿做爽爽爽视频 | 青青草国产在线观看 | 亚洲国产无线乱码在线观看 | 国产18在线| 国产做爰高潮呻吟视频 | 天天综合网在线观看 | 午夜影院免费视频 | 亚洲av永久无码精品一区二区国产 | 亚洲一区二区三区在线观看视频 | 北条麻妃av在线播放 | 少妇精品久久久久www | 大香伊人中文字幕精品 | 欧美性受xxxx黑人xyx性 | 黄色成人免费观看 | 8x8x最新网址 | 双性尿奴穿贞c带憋尿 | 在线视频欧美亚洲 | 特大黑人娇小亚洲女 | 黄色在线小视频 | 中文字幕一区二区av | 久草播放 | 99色热| 美女免费黄色 | 日本黄网站在线观看 | 亚洲第七页 | 37p粉嫩大胆色噜噜噜 | 国产xxx | 欧美日韩加勒比 | 亚洲精品综合 | 国产成人一区二区三区免费看 | 不卡视频在线观看免费 | 国产欧美在线看 | 偷拍视频一区 | 在线播放av片| 欧美影视一区二区三区 | 妞妞影视 |