自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

生成對抗網(wǎng)絡(luò),AI將圖片轉(zhuǎn)成漫畫風(fēng)格

人工智能
今天分享的這個項目是用 GAN 生成對抗網(wǎng)絡(luò)實現(xiàn)的,關(guān)于GAN的原理和實戰(zhàn)我們之前分享過很多文章,想了解的朋友可以去翻歷史文章。

哈嘍,大家好。

最近大家都在玩 AI 繪畫,我在 GitHub 上找了一個開源項目,給大家分享一下。

圖片

今天分享的這個項目是用 GAN? 生成對抗網(wǎng)絡(luò)實現(xiàn)的,關(guān)于GAN的原理和實戰(zhàn)我們之前分享過很多文章,想了解的朋友可以去翻歷史文章。

源碼和數(shù)據(jù)集文末獲取,下面分享如何訓(xùn)練、運行項目。

1. 準(zhǔn)備環(huán)境

安裝 tensorflow-gpu 1.15.0?,GPU顯卡使用2080Ti?,cuda版本10.0。

git下載項目AnimeGANv2源碼。

搭建好環(huán)境后,還需要準(zhǔn)備數(shù)據(jù)集和vgg19。

圖片

下載dataset.zip?壓縮文件,里面包含 6k 張真實圖片和2k張漫畫圖片,用于GAN的訓(xùn)練。

圖片

vgg19是用來計算損失的,下面會有詳細(xì)介紹。

2. 網(wǎng)絡(luò)模型

生成對抗網(wǎng)絡(luò)需要定義兩個模型,一個是生成器,一個是判別器。

生成器網(wǎng)絡(luò)定義如下:

with tf.variable_scope('A'):
inputs = Conv2DNormLReLU(inputs, 32, 7)
inputs = Conv2DNormLReLU(inputs, 64, strides=2)
inputs = Conv2DNormLReLU(inputs, 64)

with tf.variable_scope('B'):
inputs = Conv2DNormLReLU(inputs, 128, strides=2)
inputs = Conv2DNormLReLU(inputs, 128)

with tf.variable_scope('C'):
inputs = Conv2DNormLReLU(inputs, 128)
inputs = self.InvertedRes_block(inputs, 2, 256, 1, 'r1')
inputs = self.InvertedRes_block(inputs, 2, 256, 1, 'r2')
inputs = self.InvertedRes_block(inputs, 2, 256, 1, 'r3')
inputs = self.InvertedRes_block(inputs, 2, 256, 1, 'r4')
inputs = Conv2DNormLReLU(inputs, 128)

with tf.variable_scope('D'):
inputs = Unsample(inputs, 128)
inputs = Conv2DNormLReLU(inputs, 128)

with tf.variable_scope('E'):
inputs = Unsample(inputs,64)
inputs = Conv2DNormLReLU(inputs, 64)
inputs = Conv2DNormLReLU(inputs, 32, 7)
with tf.variable_scope('out_layer'):
out = Conv2D(inputs, filters =3, kernel_size=1, strides=1)
self.fake = tf.tanh(out)

生成器中主要的模塊是反向殘差塊

圖片

殘差結(jié)構(gòu)(a)和反向殘差塊(b)

判別器網(wǎng)絡(luò)結(jié)構(gòu)如下:

def D_net(x_init,ch, n_dis,sn, scope, reuse):
channel = ch // 2
with tf.variable_scope(scope, reuse=reuse):
x = conv(x_init, channel, kernel=3, stride=1, pad=1, use_bias=False, sn=sn, scope='conv_0')
x = lrelu(x, 0.2)

for i in range(1, n_dis):
x = conv(x, channel * 2, kernel=3, stride=2, pad=1, use_bias=False, sn=sn, scope='conv_s2_' + str(i))
x = lrelu(x, 0.2)

x = conv(x, channel * 4, kernel=3, stride=1, pad=1, use_bias=False, sn=sn, scope='conv_s1_' + str(i))
x = layer_norm(x, scope='1_norm_' + str(i))
x = lrelu(x, 0.2)

channel = channel * 2

x = conv(x, channel * 2, kernel=3, stride=1, pad=1, use_bias=False, sn=sn, scope='last_conv')
x = layer_norm(x, scope='2_ins_norm')
x = lrelu(x, 0.2)

x = conv(x, channels=1, kernel=3, stride=1, pad=1, use_bias=False, sn=sn, scope='D_logit')

return x

3. 損失

計算損失之前先用VGG19?網(wǎng)路將圖片向量化。這個過程有點像NLP?中的Embedding操作。

Eembedding?是講詞轉(zhuǎn)化成向量,VGG19是講圖片轉(zhuǎn)化成向量。

圖片

VGG19定義

計算損失部分邏輯如下:

def con_sty_loss(vgg, real, anime, fake):

# 真實圖片向量化
vgg.build(real)
real_feature_map = vgg.conv4_4_no_activation

# 生成圖片向量化
vgg.build(fake)
fake_feature_map = vgg.conv4_4_no_activation

# 漫畫風(fēng)格向量化
vgg.build(anime[:fake_feature_map.shape[0]])
anime_feature_map = vgg.conv4_4_no_activation

# 真實圖片與生成圖片的損失
c_loss = L1_loss(real_feature_map, fake_feature_map)
# 漫畫風(fēng)格與生成圖片的損失
s_loss = style_loss(anime_feature_map, fake_feature_map)

return c_loss, s_loss

這里使用vgg19?分別計算真實圖片(參數(shù)real)與生成的圖片(參數(shù)fake)?的損失,生成的圖片(參數(shù)fake)與漫畫風(fēng)格(參數(shù)anime)的損失。

c_loss, s_loss = con_sty_loss(self.vgg, self.real, self.anime_gray, self.generated)
t_loss = self.con_weight * c_loss + self.sty_weight * s_loss + color_loss(self.real,self.generated) * self.color_weight + tv_loss

最終給這兩個損失不同的權(quán)重,這樣是的生成器生成的圖片,既保留了真實圖片的樣子,又向漫畫風(fēng)格進行遷移

4. 訓(xùn)練

在項目目錄下執(zhí)行以下命令開始訓(xùn)練

python train.py --dataset Hayao --epoch 101 --init_epoch 10

運行成功后,可以看到一下數(shù)據(jù)。

圖片

同時,也可以看到損失在不斷下降。

源碼和數(shù)據(jù)集都已經(jīng)打包好了,需要的朋友評論區(qū)留言即可。

如果大家覺得本文對你有用就點個 在看 鼓勵一下吧,后續(xù)我會持續(xù)分享優(yōu)秀的 Python+AI 項目。

責(zé)任編輯:武曉燕 來源: 渡碼
相關(guān)推薦

2022-09-20 08:00:00

暗數(shù)據(jù)機器學(xué)習(xí)數(shù)據(jù)

2020-05-28 10:45:36

機器學(xué)習(xí)人工智能 PyTorch

2023-10-31 10:33:35

對抗網(wǎng)絡(luò)人工智能

2024-04-01 08:00:00

2021-03-12 10:40:46

CycleGAN網(wǎng)絡(luò)圖像深度學(xué)習(xí)

2017-05-10 14:32:31

神經(jīng)網(wǎng)絡(luò)網(wǎng)絡(luò)架構(gòu)模型

2023-07-04 09:49:50

人工智能GAN

2017-10-23 06:36:27

生成對抗網(wǎng)絡(luò)架構(gòu)訓(xùn)練技巧

2018-07-11 10:46:05

人工智能計算機視覺面部屬性

2020-05-21 14:05:59

人工智能機器學(xué)習(xí)技術(shù)

2018-07-04 09:10:54

人工智能機器人側(cè)臉

2017-10-31 10:17:06

阿里

2025-01-13 00:00:00

AI模型訓(xùn)練

2017-09-11 17:16:35

2020-10-22 17:56:40

神經(jīng)網(wǎng)絡(luò)生成式對抗網(wǎng)絡(luò)

2022-01-05 07:53:03

訪問控制網(wǎng)絡(luò)犯罪網(wǎng)絡(luò)安全

2022-08-02 07:25:48

對抗網(wǎng)絡(luò)數(shù)據(jù)生成Python

2015-05-25 10:05:16

2017-08-24 15:09:13

GAN神經(jīng)網(wǎng)絡(luò)無監(jiān)督學(xué)習(xí)

2017-05-08 22:40:55

深度學(xué)習(xí)自編碼器對抗網(wǎng)絡(luò)
點贊
收藏

51CTO技術(shù)棧公眾號