基于PaddlePaddle的StarGAN,AttGAN,STGAN算法

简介

生成对抗网络(Generative Adversarial Network[1], 简称GAN) 是一种非监督学习的方式,通过让两个神经网络相互博弈的方法进行学习,该方法由lan Goodfellow等人在2014年提出。生成对抗网络由一个生成网络和一个判别网络组成,生成网络从潜在的空间(latent space)中随机采样作为输入,其输出结果需要尽量模仿训练集中的真实样本。判别网络的输入为真实样本或生成网络的输出,其目的是将生成网络的输出从真实样本中尽可能的分辨出来。而生成网络则尽可能的欺骗判别网络,两个网络相互对抗,不断调整参数。 生成对抗网络常用于生成以假乱真的图片。此外,该方法还被用于生成影片,三维物体模型等。

下载安装命令

## CPU版本安装命令
pip install -f https://paddlepaddle.org.cn/pip/oschina/cpu paddlepaddle

## GPU版本安装命令
pip install -f https://paddlepaddle.org.cn/pip/oschina/gpu paddlepaddle-gpu

本项目采用celeba数据集,关于celeba数据集的介绍,详见https://zhuanlan.zhihu.com/p/35975956

In[1]

#解压数据集,加载数据集需要10秒左右的时间
!cd /home/aistudio/data/data11404 && unzip -qo celeba.zip

In[2]

#安装imageio ,scipy
!pip install -q imageio
!pip install -q scipy==1.2.1
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='mirrors.ustc.edu.cn', port=443): Read timed out. (read timeout=15)",)': /pypi/web/simple/imageio/
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='pypi.mirrors.ustc.edu.cn', port=443): Read timed out. (read timeout=15)",)': /simple/scipy/

In[
 ]

# #代码结构 
# ├── data_reader.py 数据预处理
# │  
# ├── train.py 模型的训练入口
# │  
# ├── infer.py 模型的预测入口
# │  
# ├── trainer 不同模型的训练脚本
# │   ├── StarGAN.py Conditional GAN的训练脚本
# │   ├── ...
# │   ├── STGAN.py STGAN的训练脚本
# │  
# ├── network 不同模型的网络结构
# │   ├── base_network.py GAN模型需要的公共基础网络结构
# │   ├── ...
# │   ├── STGAN_network.py STGAN的网络结构
# │  
# ├── util 网络的基础配置和公共模块
# │   ├── config.py 网络公用的基础配置
# │   ├── utility.py 保存模型等网络公用的模块
# │  

 

注意: StarGAN,AttGAN和STGAN由于梯度惩罚所需的操作目前只支持GPU,需使用GPU训练。

 

Pix2Pix模型解决了有Pair对数据的图像翻译问题;CycleGAN解决了Unpaired数据下的图像翻译问题。但无论是Pix2Pix还是CycleGAN,都是解决了一对一的问题,即一个领域到另一个领域的转换。当有很多领域要转换了,对于每一个领域转换,都需要重新训练一个模型去解决。这样的行为太低效了。**本文所介绍的StarGAN就是将多领域转换用统一框架实现的算法。传统的GAN多领域转换结构(左)和starGAN多领域转换结构(右)结构如下图所示: 基于PaddlePaddle的StarGAN,AttGAN,STGAN算法

StarGAN中生成网络的编码部分主要由convolution-instance norm-ReLU组成,解码部分主要由transpose convolution-norm-ReLU组成,判别网络主要由convolution-leaky_ReLU组成,详细网络结构可以查看network/StarGAN_network.py文件。生成网络的损失函数是由WGAN的损失函数,重构损失和分类损失组成,判别网络的损失函数由预测损失,分类损失和梯度惩罚损失组成。

基于PaddlePaddle的StarGAN,AttGAN,STGAN算法

stargan论文地址https://arxiv.org/abs/1711.09020

优秀解读博客https://blog.csdn.net/stdcoutzyx/article/details/78829232

In[6]

#模型训练,训练结果保存在star_output文件夹中。
!python PaddleGAN/train.py   --model_net StarGAN /
                             --dataset celeba /
                             --data_dir ./data/data11404 /
                             --crop_size 178 /
                             --image_size 128 /
                             --train_list ./data/data11404/celeba/list_attr_celeba.txt /
                             --test_list ./data/data11404/celeba/test_list_attr_celeba.txt  /
                             --batch_size 8 /
                             --epoch 5 /
                             --print_freq 350 /
                             --output ./PaddleGAN/freeze_model_stargan /
                             --use_gpu True
-----------  Configuration Arguments -----------
batch_size: 8
c_dim: 5
crop_size: 178
crop_type: Centor
d_base_dims: 64
d_lr: 0.0001
d_repeat_num: 6
data_dir: ./data/data11404
dataset: celeba
drop_last: False
dropout: False
epoch: 5
g_base_dims: 64
g_lr: 0.0001
g_repeat_num: 6
image_size: 128
init_model: None
lambda_L1: 100.0
lambda_cls: 1.0
lambda_gp: 10.0
lambda_rec: 10.0
learning_rate: 0.0002
load_size: 286
lr_update_step: 1000
model_net: StarGAN
n_critic: 100
n_samples: 1
norm_type: batch_norm
num_discriminator_time: 1
num_generator_time: 1
num_iters: 200000
num_iters_decay: 100000
output: ./PaddleGAN/freeze_model_stargan
print_freq: 350
profile: False
run_test: True
save_checkpoints: True
selected_attrs: Black_Hair,Blond_Hair,Brown_Hair,Male,Young
shuffle: True
test_list: ./data/data11404/celeba/test_list_attr_celeba.txt
train_list: ./data/data11404/celeba/list_attr_celeba.txt
use_gpu: 1
------------------------------------------------
./data/data11404/celeba ./data/data11404/celeba/list_attr_celeba.txt
./data/data11404/celeba ./data/data11404/celeba/test_list_attr_celeba.txt
W0906 15:00:40.942924   531 device_context.cc:259] Please NOTE: device: 0, CUDA Capability: 70, Driver API Version: 9.2, Runtime API Version: 9.0
W0906 15:00:40.947310   531 device_context.cc:267] device: 0, cuDNN Version: 7.3.
WARNING:root:
     You can try our memory optimize feature to save your memory usage:
         # create a build_strategy variable to set memory optimize option
         build_strategy = compiler.BuildStrategy()
         build_strategy.enable_inplace = True
         build_strategy.memory_optimize = True
         
         # pass the build_strategy to with_data_parallel API
         compiled_prog = compiler.CompiledProgram(main).with_data_parallel(
             loss_name=loss.name, build_strategy=build_strategy)
      
     !!! Memory optimize is our experimental feature !!!
         some variables may be removed/reused internal to save memory usage, 
         in order to fetch the right value of the fetch_list, please set the 
         persistable property to true for each variable in fetch_list

         # Sample
         conv1 = fluid.layers.conv2d(data, 4, 5, 1, act=None) 
         # if you need to fetch conv1, then:
         conv1.persistable = True

                 
WARNING:root:
     You can try our memory optimize feature to save your memory usage:
         # create a build_strategy variable to set memory optimize option
         build_strategy = compiler.BuildStrategy()
         build_strategy.enable_inplace = True
         build_strategy.memory_optimize = True
         
         # pass the build_strategy to with_data_parallel API
         compiled_prog = compiler.CompiledProgram(main).with_data_parallel(
             loss_name=loss.name, build_strategy=build_strategy)
      
     !!! Memory optimize is our experimental feature !!!
         some variables may be removed/reused internal to save memory usage, 
         in order to fetch the right value of the fetch_list, please set the 
         persistable property to true for each variable in fetch_list

         # Sample
         conv1 = fluid.layers.conv2d(data, 4, 5, 1, act=None) 
         # if you need to fetch conv1, then:
         conv1.persistable = True

                 
I0906 15:00:41.401780   531 parallel_executor.cc:329] The number of CUDAPlace, which is used in ParallelExecutor, is 1. And the Program will be copied 1 copies
I0906 15:00:41.414952   531 build_strategy.cc:340] SeqOnlyAllReduceOps:0, num_trainers:1
epoch0: batch0: 
                         d_loss_real: -0.00020998765830881894; d_loss_fake: 2.1925799956079572e-05; d_loss_cls: 3.466519355773926; d_loss_gp: 0.9953457117080688 
                         Batch_time_cost: 1.42
I0906 15:00:53.946933   531 parallel_executor.cc:329] The number of CUDAPlace, which is used in ParallelExecutor, is 1. And the Program will be copied 1 copies
I0906 15:00:53.989949   531 build_strategy.cc:340] SeqOnlyAllReduceOps:0, num_trainers:1
epoch0: batch99: 
                         g_loss_fake: 19.995725631713867; g_loss_rec: 0.6063364744186401; g_loss_cls: 8.140337944030762

In[7]

#模型测试,预测结果保存在star_infer_result
!python PaddleGAN/infer_stargan.py --c_dim 5 /
                                  --model_path 'PaddleGAN/freeze_model_stargan' /
                                  --selected_attrs "Black_Hair,Blond_Hair,Brown_Hair,Male,Young" /
                                  --test_list ./data/data11404/celeba/test_list_attr_celeba.txt /
                                  --output ./PaddleGAN/star_infer_result
                                  
#预测结果之一可视化
%matplotlib inline
import matplotlib.pyplot as plt  
import numpy as np
import cv2

img= cv2.imread('PaddleGAN/star_infer_result/fake_img_000017.jpg')

plt.imshow(img)
plt.show()
-----------  Configuration Arguments -----------
batch_size: 16
c_dim: 5
crop_size: 178
dataset_dir: ./data/data11404/celeba
image_size: 128
model_path: PaddleGAN/freeze_model_stargan
output: ./PaddleGAN/star_infer_result
selected_attrs: Black_Hair,Blond_Hair,Brown_Hair,Male,Young
test_list: ./data/data11404/celeba/test_list_attr_celeba.txt
use_gpu: True
------------------------------------------------
W0906 13:28:57.981617   263 device_context.cc:259] Please NOTE: device: 0, CUDA Capability: 70, Driver API Version: 9.2, Runtime API Version: 9.0
W0906 13:28:57.985031   263 device_context.cc:267] device: 0, cuDNN Version: 7.3.
./data/data11404/celeba ./data/data11404/celeba/test_list_attr_celeba.txt
read ['000001.jpg', '000002.jpg', '000003.jpg', '000004.jpg', '000005.jpg', '000006.jpg', '000007.jpg', '000008.jpg', '000009.jpg', '000010.jpg', '000011.jpg', '000012.jpg', '000013.jpg', '000014.jpg', '000015.jpg', '000016.jpg']
read ['000017.jpg', '000018.jpg', '000019.jpg', '000020.jpg', '000021.jpg', '000022.jpg', '000023.jpg', '000024.jpg', '000025.jpg', '000026.jpg', '000027.jpg', '000028.jpg', '000029.jpg', '000030.jpg', '000031.jpg', '000032.jpg']
read ['000033.jpg', '000034.jpg', '000035.jpg', '000036.jpg', '000037.jpg', '000038.jpg', '000039.jpg', '000040.jpg', '000041.jpg', '000042.jpg', '000043.jpg', '000044.jpg', '000045.jpg', '000046.jpg', '000047.jpg', '000048.jpg']
read ['000049.jpg', '000050.jpg', '000051.jpg', '000052.jpg', '000053.jpg', '000054.jpg', '000055.jpg', '000056.jpg', '000057.jpg', '000058.jpg', '000059.jpg', '000060.jpg', '000061.jpg', '000062.jpg', '000063.jpg', '000064.jpg']
read ['000065.jpg', '000066.jpg', '000067.jpg', '000068.jpg', '000069.jpg', '000070.jpg', '000071.jpg', '000072.jpg', '000073.jpg', '000074.jpg', '000075.jpg', '000076.jpg', '000077.jpg', '000078.jpg', '000079.jpg', '000080.jpg']
read ['000081.jpg', '000082.jpg', '000083.jpg', '000084.jpg', '000085.jpg', '000086.jpg', '000087.jpg', '000088.jpg', '000089.jpg', '000090.jpg', '000091.jpg', '000092.jpg', '000093.jpg', '000094.jpg', '000095.jpg', '000096.jpg']
read ['000097.jpg', '000098.jpg', '000099.jpg', '000100.jpg', '000101.jpg', '000102.jpg', '000103.jpg', '000104.jpg', '000105.jpg', '000106.jpg', '000107.jpg', '000108.jpg', '000109.jpg', '000110.jpg', '000111.jpg', '000112.jpg']
read ['000113.jpg', '000114.jpg', '000115.jpg', '000116.jpg', '000117.jpg', '000118.jpg', '000119.jpg', '000120.jpg']

基于PaddlePaddle的StarGAN,AttGAN,STGAN算法

 

AttGAN使用一个统一的框架进行人脸属性的迁移, 通过操作单个或多个感兴趣的属性(如头发颜色、表情、胡须和年龄)来编辑面部图像。通过将latent representation与属性结合(将目标属性和encode输出结合),这样将属性和latent representation分开可以使迁移后的属性更加准确。同时采用了属性分类误差,重构误差,对抗学习误差,与starGAN一样,多领域的转换只需要一个网络模型。

AttGAN中生成网络的编码部分主要由convolution-instance norm-ReLU组成,解码部分由transpose convolution-norm-ReLU组成,判别网络主要由convolution-leaky_ReLU组成,详细网络结构可以查看network/AttGAN_network.py文件。生成网络的损失函数是由WGAN的损失函数,重构损失和分类损失组成,判别网络的损失函数由预测损失,分类损失和梯度惩罚损失组成。

基于PaddlePaddle的StarGAN,AttGAN,STGAN算法

论文地址https://arxiv.org/abs/1711.10678

优秀解读博客https://blog.csdn.net/Invokar/article/details/98469672

In[5]

#模型训练,训练结果保存在att_output文件夹中
!python PaddleGAN/train.py --model_net AttGAN /
                                 --dataset celeba /
                                 --data_dir ./data/data11404 /
                                 --crop_size 170 /
                                 --image_size 128 /
                                 --train_list ./data/data11404/celeba/list_attr_celeba.txt /
                                 --test_list ./data/data11404/celeba/test_list_attr_celeba.txt /
                                 --batch_size 8 /
                                 --print_freq 100 /
                                 --num_discriminator_time 5 /
                                 --epoch 5 /
                                 --output ./PaddleGAN/freeze_model_attgan /
                                 --use_gpu True
-----------  Configuration Arguments -----------
batch_size: 8
c_dim: 13
crop_size: 170
crop_type: Centor
d_base_dims: 64
d_fc_dim: 1024
d_lr: 0.0002
data_dir: ./data/data11404
dataset: celeba
dis_norm: None
drop_last: False
dropout: False
epoch: 5
g_base_dims: 64
g_lr: 0.0002
image_size: 128
init_model: None
lambda_L1: 100.0
lambda_cls: 10.0
lambda_gp: 10.0
lambda_rec: 100.0
learning_rate: 0.0002
load_size: 286
model_net: AttGAN
n_layers: 5
n_samples: 16
norm_type: batch_norm
num_discriminator_time: 5
num_generator_time: 1
output: ./PaddleGAN/freeze_model_attgan
print_freq: 100
profile: False
run_test: True
save_checkpoints: True
selected_attrs: Bald,Bangs,Black_Hair,Blond_Hair,Brown_Hair,Bushy_Eyebrows,Eyeglasses,Male,Mouth_Slightly_Open,Mustache,No_Beard,Pale_Skin,Young
shuffle: True
test_list: ./data/data11404/celeba/test_list_attr_celeba.txt
thres_int: 0.5
train_list: ./data/data11404/celeba/list_attr_celeba.txt
use_gpu: 1
------------------------------------------------
./data/data11404/celeba ./data/data11404/celeba/list_attr_celeba.txt
./data/data11404/celeba ./data/data11404/celeba/test_list_attr_celeba.txt
W0906 15:14:09.025193   363 device_context.cc:259] Please NOTE: device: 0, CUDA Capability: 70, Driver API Version: 9.2, Runtime API Version: 9.0
W0906 15:14:09.029356   363 device_context.cc:267] device: 0, cuDNN Version: 7.3.
WARNING:root:
     You can try our memory optimize feature to save your memory usage:
         # create a build_strategy variable to set memory optimize option
         build_strategy = compiler.BuildStrategy()
         build_strategy.enable_inplace = True
         build_strategy.memory_optimize = True
         
         # pass the build_strategy to with_data_parallel API
         compiled_prog = compiler.CompiledProgram(main).with_data_parallel(
             loss_name=loss.name, build_strategy=build_strategy)
      
     !!! Memory optimize is our experimental feature !!!
         some variables may be removed/reused internal to save memory usage, 
         in order to fetch the right value of the fetch_list, please set the 
         persistable property to true for each variable in fetch_list

         # Sample
         conv1 = fluid.layers.conv2d(data, 4, 5, 1, act=None) 
         # if you need to fetch conv1, then:
         conv1.persistable = True

                 
WARNING:root:
     You can try our memory optimize feature to save your memory usage:
         # create a build_strategy variable to set memory optimize option
         build_strategy = compiler.BuildStrategy()
         build_strategy.enable_inplace = True
         build_strategy.memory_optimize = True
         
         # pass the build_strategy to with_data_parallel API
         compiled_prog = compiler.CompiledProgram(main).with_data_parallel(
             loss_name=loss.name, build_strategy=build_strategy)
      
     !!! Memory optimize is our experimental feature !!!
         some variables may be removed/reused internal to save memory usage, 
         in order to fetch the right value of the fetch_list, please set the 
         persistable property to true for each variable in fetch_list

         # Sample
         conv1 = fluid.layers.conv2d(data, 4, 5, 1, act=None) 
         # if you need to fetch conv1, then:
         conv1.persistable = True

                 
I0906 15:14:09.143767   363 parallel_executor.cc:329] The number of CUDAPlace, which is used in ParallelExecutor, is 1. And the Program will be copied 1 copies
I0906 15:14:09.155784   363 build_strategy.cc:340] SeqOnlyAllReduceOps:0, num_trainers:1
epoch0: batch0:  
                             d_loss: 10.647875785827637; d_loss_real: 0.029214587062597275; d_loss_fake: -0.029463771730661392; d_loss_cls: 0.6928870677947998; d_loss_gp: 0.9955238103866577 
                             Batch_time_cost: 1.38
I0906 15:14:10.893005   363 parallel_executor.cc:329] The number of CUDAPlace, which is used in ParallelExecutor, is 1. And the Program will be copied 1 copies
I0906 15:14:10.903579   363 build_strategy.cc:340] SeqOnlyAllReduceOps:0, num_trainers:1
epoch0: batch100:  
                             d_loss: -50.821163177490234; d_loss_real: 67.7457275390625; d_loss_fake: -159.93515014648438; d_loss_cls: 0.6928127408027649; d_loss_gp: 4.067544937133789 
                             Batch_time_cost: 0.05
epoch0: batch200:  
                             d_loss: -24.396202087402344; d_loss_real: 29.23967742919922; d_loss_fake: -59.72174072265625; d_loss_cls: 1.0565842390060425; d_loss_gp: 0.5029278993606567 
                             Batch_time_cost: 0.05

In[9]

#利用固化后的模型进行预测,预测结果保存在PaddleGAN/att_infer_result
!python PaddleGAN/infer_attgan.py --dataset_dir "data/data11404/celeba/" /
                                        --model_path 'PaddleGAN/freeze_model_attgan' /
                                        --test_list "./data/data11404/celeba/test_list_attr_celeba.txt" /
                                        --image_size 128 /
                                        --use_gpu True /
                                        --output ./PaddleGAN/att_infer_result
                                        
#预测结果之一可视化
%matplotlib inline
import matplotlib.pyplot as plt  
import numpy as np
import cv2

img= cv2.imread('PaddleGAN/att_infer_result/fake_img_000033.jpg')

plt.imshow(img)
plt.show()
-----------  Configuration Arguments -----------
batch_size: 16
c_dim: 5
crop_size: 178
dataset_dir: data/data11404/celeba/
image_size: 128
model_path: PaddleGAN/freeze_model_attgan
output: ./PaddleGAN/att_infer_result
selected_attrs: Bald,Bangs,Black_Hair,Blond_Hair,Brown_Hair,Bushy_Eyebrows,Eyeglasses,Male,Mouth_Slightly_Open,Mustache,No_Beard,Pale_Skin,Young
test_list: ./data/data11404/celeba/test_list_attr_celeba.txt
use_gpu: 1
------------------------------------------------
W0906 14:56:53.088762   439 device_context.cc:259] Please NOTE: device: 0, CUDA Capability: 70, Driver API Version: 9.2, Runtime API Version: 9.0
W0906 14:56:53.092612   439 device_context.cc:267] device: 0, cuDNN Version: 7.3.
data/data11404/celeba/ ./data/data11404/celeba/test_list_attr_celeba.txt
read ['000001.jpg', '000002.jpg', '000003.jpg', '000004.jpg', '000005.jpg', '000006.jpg', '000007.jpg', '000008.jpg', '000009.jpg', '000010.jpg', '000011.jpg', '000012.jpg', '000013.jpg', '000014.jpg', '000015.jpg', '000016.jpg']
read ['000017.jpg', '000018.jpg', '000019.jpg', '000020.jpg', '000021.jpg', '000022.jpg', '000023.jpg', '000024.jpg', '000025.jpg', '000026.jpg', '000027.jpg', '000028.jpg', '000029.jpg', '000030.jpg', '000031.jpg', '000032.jpg']
read ['000033.jpg', '000034.jpg', '000035.jpg', '000036.jpg', '000037.jpg', '000038.jpg', '000039.jpg', '000040.jpg', '000041.jpg', '000042.jpg', '000043.jpg', '000044.jpg', '000045.jpg', '000046.jpg', '000047.jpg', '000048.jpg']
read ['000049.jpg', '000050.jpg', '000051.jpg', '000052.jpg', '000053.jpg', '000054.jpg', '000055.jpg', '000056.jpg', '000057.jpg', '000058.jpg', '000059.jpg', '000060.jpg', '000061.jpg', '000062.jpg', '000063.jpg', '000064.jpg']
read ['000065.jpg', '000066.jpg', '000067.jpg', '000068.jpg', '000069.jpg', '000070.jpg', '000071.jpg', '000072.jpg', '000073.jpg', '000074.jpg', '000075.jpg', '000076.jpg', '000077.jpg', '000078.jpg', '000079.jpg', '000080.jpg']
read ['000081.jpg', '000082.jpg', '000083.jpg', '000084.jpg', '000085.jpg', '000086.jpg', '000087.jpg', '000088.jpg', '000089.jpg', '000090.jpg', '000091.jpg', '000092.jpg', '000093.jpg', '000094.jpg', '000095.jpg', '000096.jpg']
read ['000097.jpg', '000098.jpg', '000099.jpg', '000100.jpg', '000101.jpg', '000102.jpg', '000103.jpg', '000104.jpg', '000105.jpg', '000106.jpg', '000107.jpg', '000108.jpg', '000109.jpg', '000110.jpg', '000111.jpg', '000112.jpg']
read ['000113.jpg', '000114.jpg', '000115.jpg', '000116.jpg', '000117.jpg', '000118.jpg', '000119.jpg', '000120.jpg']

基于PaddlePaddle的StarGAN,AttGAN,STGAN算法

 

AttGAN 和 StarGAN 在人脸属性编辑上取得了很大的成功,但当人脸属性之间相互交集或者目标人脸属性比较复杂时,这两种方式对于控制属性的标签上的精细化就显示了些许的不足。STGAN 是一个建立在 AttGAN 基础上的人脸属性编辑模型,通过差分属性标签下选择性传输单元的跳跃连接实现了人脸高精度属性的编辑。

STGAN 的优势在于:将差异属性向量作为输入,以增强属性的灵活转换并简化训练过程;设计选择性传输单元并与编码器 – 解码器结合,以同时提高属性操作能力和图像质量;

STGAN中生成网络再编码器和解码器之间加入Selective Transfer Units(STU),有选择的转换编码网络,从而更好的适配解码网络。生成网络中的编码网络主要由convolution-instance norm-ReLU组成,解码网络主要由transpose convolution-norm-leaky_ReLU组成,判别网络主要由convolution-leaky_ReLU组成,详细网络结构可以查看network/STGAN_network.py文件。生成网络的损失函数是由WGAN的损失函数,重构损失和分类损失组成,判别网络的损失函数由预测损失,分类损失和梯度惩罚损失组成。

基于PaddlePaddle的StarGAN,AttGAN,STGAN算法

论文地址http://openaccess.thecvf.com/content_cvpr_2018/papers/Lin_ST-GAN_Spatial_Transformer_CVPR_2018_paper.pdf

优秀解读博客https://blog.csdn.net/tommorrow12/article/details/89289078

In[26]

#模型训练,训练结果保存在st_output文件夹中
!python PaddleGAN/train.py --model_net STGAN /
                                 --dataset celeba /
                                 --data_dir ./data/data11404 /
                                 --crop_size 170 /
                                 --image_size 128 /
                                 --train_list ./data/data11404/celeba/list_attr_celeba.txt /
                                 --test_list ./data/data11404/celeba/test_list_attr_celeba.txt  /
                                 --batch_size 32 /
                                 --print_freq 100 /
                                 --num_discriminator_time 5 /
                                 --epoch 5 /
                                 --use_gpu True /
                                 --output ./PaddleGAN/freeze_model_stgan
-----------  Configuration Arguments -----------
batch_size: 32
c_dim: 13
crop_size: 170
crop_type: Centor
d_base_dims: 64
d_fc_dim: 1024
d_lr: 0.0002
data_dir: ./data/data11404
dataset: celeba
dis_norm: None
drop_last: False
dropout: False
epoch: 5
g_base_dims: 64
g_lr: 0.0002
gru_n_layers: 4
image_size: 128
init_model: None
lambda_L1: 100.0
lambda_cls: 10.0
lambda_gp: 10.0
lambda_rec: 100.0
learning_rate: 0.0002
load_size: 286
model_net: STGAN
n_layers: 5
n_samples: 16
norm_type: batch_norm
num_discriminator_time: 5
num_generator_time: 1
output: ./PaddleGAN/freeze_model_stgan
print_freq: 100
profile: False
run_test: True
save_checkpoints: True
selected_attrs: Bald,Bangs,Black_Hair,Blond_Hair,Brown_Hair,Bushy_Eyebrows,Eyeglasses,Male,Mouth_Slightly_Open,Mustache,No_Beard,Pale_Skin,Young
shuffle: True
test_list: ./data/data11404/celeba/test_list_attr_celeba.txt
thres_int: 0.5
train_list: ./data/data11404/celeba/list_attr_celeba.txt
use_gpu: 1
use_gru: True
------------------------------------------------
./data/data11404/celeba ./data/data11404/celeba/list_attr_celeba.txt
./data/data11404/celeba ./data/data11404/celeba/test_list_attr_celeba.txt
W0906 15:47:49.227231   100 device_context.cc:259] Please NOTE: device: 0, CUDA Capability: 70, Driver API Version: 9.2, Runtime API Version: 9.0
W0906 15:47:49.231148   100 device_context.cc:267] device: 0, cuDNN Version: 7.3.
WARNING:root:
     You can try our memory optimize feature to save your memory usage:
         # create a build_strategy variable to set memory optimize option
         build_strategy = compiler.BuildStrategy()
         build_strategy.enable_inplace = True
         build_strategy.memory_optimize = True
         
         # pass the build_strategy to with_data_parallel API
         compiled_prog = compiler.CompiledProgram(main).with_data_parallel(
             loss_name=loss.name, build_strategy=build_strategy)
      
     !!! Memory optimize is our experimental feature !!!
         some variables may be removed/reused internal to save memory usage, 
         in order to fetch the right value of the fetch_list, please set the 
         persistable property to true for each variable in fetch_list

         # Sample
         conv1 = fluid.layers.conv2d(data, 4, 5, 1, act=None) 
         # if you need to fetch conv1, then:
         conv1.persistable = True

                 
WARNING:root:
     You can try our memory optimize feature to save your memory usage:
         # create a build_strategy variable to set memory optimize option
         build_strategy = compiler.BuildStrategy()
         build_strategy.enable_inplace = True
         build_strategy.memory_optimize = True
         
         # pass the build_strategy to with_data_parallel API
         compiled_prog = compiler.CompiledProgram(main).with_data_parallel(
             loss_name=loss.name, build_strategy=build_strategy)
      
     !!! Memory optimize is our experimental feature !!!
         some variables may be removed/reused internal to save memory usage, 
         in order to fetch the right value of the fetch_list, please set the 
         persistable property to true for each variable in fetch_list

         # Sample
         conv1 = fluid.layers.conv2d(data, 4, 5, 1, act=None) 
         # if you need to fetch conv1, then:
         conv1.persistable = True

                 
I0906 15:47:49.441879   100 parallel_executor.cc:329] The number of CUDAPlace, which is used in ParallelExecutor, is 1. And the Program will be copied 1 copies
I0906 15:47:49.465831   100 build_strategy.cc:340] SeqOnlyAllReduceOps:0, num_trainers:1
epoch0: batch0:  
                             d_loss: 10.643710136413574; d_loss_real: -0.01927444152534008; d_loss_fake: 0.021249720826745033; d_loss_cls: 0.694129467010498; d_loss_gp: 0.9947605133056641 
                             Batch_time_cost: 1.59
I0906 15:47:52.212213   100 parallel_executor.cc:329] The number of CUDAPlace, which is used in ParallelExecutor, is 1. And the Program will be copied 1 copies
I0906 15:47:52.244446   100 build_strategy.cc:340] SeqOnlyAllReduceOps:0, num_trainers:1
epoch0: batch100:  
                             d_loss: -12.276394844055176; d_loss_real: 43.6812629699707; d_loss_fake: -57.956871032714844; d_loss_cls: 0.6155716180801392; d_loss_gp: 0.13836410641670227 
                             Batch_time_cost: 0.24
epoch0: batch200:  
                             d_loss: 8.267728805541992; d_loss_real: -13.713830947875977; d_loss_fake: 19.90814971923828; d_loss_cls: 0.469912588596344; d_loss_gp: 0.16034969687461853 
                             Batch_time_cost: 0.24

In[20]

#利用固化模型进行预测,预测结果保存在PaddleGAN/st_infer_result
!python PaddleGAN/infer_stgan.py --dataset_dir "data/data11404/celeba/" /
                                        --model_path 'PaddleGAN/freeze_model_stgan' /
                                        --test_list "./data/data11404/celeba/test_list_attr_celeba.txt" /
                                        --image_size 128 /
                                        --output ./PaddleGAN/st_infer_result
                                        
#预测结果之一可视化
%matplotlib inline
import matplotlib.pyplot as plt  
import numpy as np
import cv2

img= cv2.imread('PaddleGAN/st_infer_result/fake_img_000113.jpg')

plt.imshow(img)
plt.show()
-----------  Configuration Arguments -----------
batch_size: 16
c_dim: 5
crop_size: 178
dataset_dir: data/data11404/celeba/
image_size: 128
model_path: PaddleGAN/freeze_model_stgan/0
output: ./PaddleGAN/st_infer_result
selected_attrs: Bald,Bangs,Black_Hair,Blond_Hair,Brown_Hair,Bushy_Eyebrows,Eyeglasses,Male,Mouth_Slightly_Open,Mustache,No_Beard,Pale_Skin,Young
test_list: ./data/data11404/celeba/test_list_attr_celeba.txt
use_gpu: True
------------------------------------------------
W0819 13:24:52.262744  1632 device_context.cc:259] Please NOTE: device: 0, CUDA Capability: 70, Driver API Version: 9.2, Runtime API Version: 9.0
W0819 13:24:52.266196  1632 device_context.cc:267] device: 0, cuDNN Version: 7.3.
data/data11404/celeba/ ./data/data11404/celeba/test_list_attr_celeba.txt
read ['000001.jpg', '000002.jpg', '000003.jpg', '000004.jpg', '000005.jpg', '000006.jpg', '000007.jpg', '000008.jpg', '000009.jpg', '000010.jpg', '000011.jpg', '000012.jpg', '000013.jpg', '000014.jpg', '000015.jpg', '000016.jpg']
read ['000017.jpg', '000018.jpg', '000019.jpg', '000020.jpg', '000021.jpg', '000022.jpg', '000023.jpg', '000024.jpg', '000025.jpg', '000026.jpg', '000027.jpg', '000028.jpg', '000029.jpg', '000030.jpg', '000031.jpg', '000032.jpg']
read ['000033.jpg', '000034.jpg', '000035.jpg', '000036.jpg', '000037.jpg', '000038.jpg', '000039.jpg', '000040.jpg', '000041.jpg', '000042.jpg', '000043.jpg', '000044.jpg', '000045.jpg', '000046.jpg', '000047.jpg', '000048.jpg']
read ['000049.jpg', '000050.jpg', '000051.jpg', '000052.jpg', '000053.jpg', '000054.jpg', '000055.jpg', '000056.jpg', '000057.jpg', '000058.jpg', '000059.jpg', '000060.jpg', '000061.jpg', '000062.jpg', '000063.jpg', '000064.jpg']
read ['000065.jpg', '000066.jpg', '000067.jpg', '000068.jpg', '000069.jpg', '000070.jpg', '000071.jpg', '000072.jpg', '000073.jpg', '000074.jpg', '000075.jpg', '000076.jpg', '000077.jpg', '000078.jpg', '000079.jpg', '000080.jpg']
read ['000081.jpg', '000082.jpg', '000083.jpg', '000084.jpg', '000085.jpg', '000086.jpg', '000087.jpg', '000088.jpg', '000089.jpg', '000090.jpg', '000091.jpg', '000092.jpg', '000093.jpg', '000094.jpg', '000095.jpg', '000096.jpg']
read ['000097.jpg', '000098.jpg', '000099.jpg', '000100.jpg', '000101.jpg', '000102.jpg', '000103.jpg', '000104.jpg', '000105.jpg', '000106.jpg', '000107.jpg', '000108.jpg', '000109.jpg', '000110.jpg', '000111.jpg', '000112.jpg']
read ['000113.jpg', '000114.jpg', '000115.jpg', '000116.jpg', '000117.jpg', '000118.jpg', '000119.jpg', '000120.jpg']

基于PaddlePaddle的StarGAN,AttGAN,STGAN算法
    点击链接,使用AI Studio一键上手实践项目吧:
https://aistudio.baidu.com/aistudio/projectdetail/169439

下载安装命令

## CPU版本安装命令
pip install -f https://paddlepaddle.org.cn/pip/oschina/cpu paddlepaddle

## GPU版本安装命令
pip install -f https://paddlepaddle.org.cn/pip/oschina/gpu paddlepaddle-gpu

>> 访问 PaddlePaddle 官网,了解更多相关内容

{{o.name}}


{{m.name}}

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/72268.html

(0)
上一篇 2021年8月11日 21:18
下一篇 2021年8月11日 21:18

相关推荐

发表回复

登录后才能评论