未验证 提交 b1673312 编写于 作者: L lvmengsi 提交者: GitHub

Sync 1.6 to develop (#3797)

* sync 1.6

* sync 1.6

* update readme
上级 1c7527ed
......@@ -62,14 +62,14 @@
```
同时推荐用户参考[ IPython Notebook demo](https://aistudio.baidu.com/aistudio/projectDetail/122272)
同时推荐用户参考[ IPython Notebook demo](https://aistudio.baidu.com/aistudio/projectDetail/122272)
## 快速开始
### 安装说明
**安装[PaddlePaddle](https://github.com/PaddlePaddle/Paddle):**
在当前目录下运行样例代码需要PadddlePaddle Fluid的v.1.5或以上的版本。如果你的运行环境中的PaddlePaddle低于此版本,请根据[安装文档](https://www.paddlepaddle.org.cn/documentation/docs/zh/1.5/beginners_guide/install/index_cn.html)中的说明来更新PaddlePaddle。
在当前目录下运行样例代码需要PadddlePaddle Fluid的v.1.6或以上的版本。如果你的运行环境中的PaddlePaddle低于此版本,请根据[安装文档](https://www.paddlepaddle.org.cn/documentation/docs/zh/1.5/beginners_guide/install/index_cn.html)中的说明来更新PaddlePaddle。
其他依赖包:
1. `pip install imageio` 或者 `pip install -r requirements.txt` 安装imageio包(保存图片代码中所依赖的包)
......@@ -114,7 +114,7 @@ SPADE使用的[cityscapes](https://www.cityscapes-dataset.com)数据集可以自
- 每个GAN都给出了一份运行示例,放在scripts文件夹内,用户可以直接运行训练脚本快速开始训练。
- 用户可以通过设置`--model_net`参数来选择想要训练的模型,通过设置`--dataset`参数来选择训练所需要的数据集。
- SPADE模型的训练需要在主目录下新建一个VGG19_pretrained目录,从[该链接](https://paddle-imagenet-models-name.bj.bcebos.com/VGG19_pretrained.tar)下载在ImageNet上预训练好的VGG19模型,解压之后把VGG19模型的参数名改成`vgg19_`开头的参数名
- SPADE模型的训练需要在主目录下新建一个VGG19_pretrained目录,从[该链接](https://paddle-gan-models.bj.bcebos.com/vgg19_spade.tar.gz)下载并解压在ImageNet上预训练好的VGG19模型,该模型是从分类模型页面下载的VGG19的预训练模型并为参数添加`vgg19_`的前缀用来区分和生成网络中的参数
### 模型测试
模型测试是利用训练完成的生成模型进行图像生成。infer.py是主要的执行程序,调用示例如下:
......@@ -185,7 +185,7 @@ STGAN的效果图(图片属性分别为:original image, Bald, Bangs, Black Hai
| StarGAN | [StarGAN的预训练模型](https://paddle-gan-models.bj.bcebos.com/stargan_G.tar.gz) |
| AttGAN | [AttGAN的预训练模型](https://paddle-gan-models.bj.bcebos.com/attgan_G.tar.gz) |
| STGAN | [STGAN的预训练模型](https://paddle-gan-models.bj.bcebos.com/stgan_G.tar.gz) |
| SPADE | [SPADE的预训练模型]() ([SPADE需要的vgg预训练模型]())
| SPADE | [SPADE的预训练模型](https://paddle-gan-models.bj.bcebos.com/spade_G.tar.gz) ([SPADE需要的vgg19预训练模型](https://paddle-gan-models.bj.bcebos.com/vgg19_spade.tar.gz))
## 进阶使用
......
......@@ -27,7 +27,7 @@ import imageio
import glob
from util.config import add_arguments, print_arguments
from data_reader import celeba_reader_creator, reader_creator, triplex_reader_creator
from util.utility import check_attribute_conflict, check_gpu, save_batch_image
from util.utility import check_attribute_conflict, check_gpu, save_batch_image, check_version
from util import utility
import copy
......@@ -84,11 +84,12 @@ def infer(args):
model_name = 'net_G'
if args.model_net == 'CycleGAN':
py_reader = fluid.io.PyReader(
loader = fluid.io.DataLoader.from_generator(
feed_list=[input, image_name],
capacity=4, ## batch_size * 4
iterable=True,
use_double_buffer=True)
from network.CycleGAN_network import CycleGAN_model
model = CycleGAN_model()
if args.input_style == "A":
......@@ -98,7 +99,7 @@ def infer(args):
else:
raise "Input with style [%s] is not supported." % args.input_style
elif args.model_net == 'Pix2pix':
py_reader = fluid.io.PyReader(
loader = fluid.io.DataLoader.from_generator(
feed_list=[input, image_name],
capacity=4, ## batch_size * 4
iterable=True,
......@@ -169,13 +170,13 @@ def infer(args):
model = DCGAN_model(args.n_samples)
fake = model.network_G(noise, name="G")
elif args.model_net == 'SPADE':
label_shape = [-1, args.label_nc, args.crop_height, args.crop_width]
spade_data_shape = [-1, 1, args.crop_height, args.crop_width]
label_shape = [None, args.label_nc, args.crop_height, args.crop_width]
spade_data_shape = [None, 1, args.crop_height, args.crop_width]
from network.SPADE_network import SPADE_model
model = SPADE_model()
input_label = fluid.layers.data(
input_label = fluid.data(
name='input_label', shape=label_shape, dtype='float32')
input_ins = fluid.layers.data(
input_ins = fluid.data(
name='input_ins', shape=spade_data_shape, dtype='float32')
input_ = fluid.layers.concat([input_label, input_ins], 1)
fake = model.network_G(input_, "generator", cfg=args, is_test=True)
......@@ -298,11 +299,11 @@ def infer(args):
batch_size=args.n_samples,
mode="VAL")
reader_test = test_reader.make_reader(args, return_name=True)
py_reader.decorate_batch_generator(
loader.set_batch_generator(
reader_test,
places=fluid.cuda_places() if args.use_gpu else fluid.cpu_places())
id2name = test_reader.id2name
for data in py_reader():
for data in loader():
real_img, image_name = data[0]['input'], data[0]['image_name']
image_name = id2name[np.array(image_name).astype('int32')[0]]
print("read: ", image_name)
......@@ -387,4 +388,5 @@ if __name__ == "__main__":
args = parser.parse_args()
print_arguments(args)
check_gpu(args.use_gpu)
check_version()
infer(args)
......@@ -66,6 +66,7 @@ if __name__ == "__main__":
cfg = config.parse_args()
config.print_arguments(cfg)
utility.check_gpu(cfg.use_gpu)
utility.check_version()
if cfg.profile:
if cfg.use_gpu:
with fluid.profiler.profiler('All', 'total',
......
......@@ -270,18 +270,18 @@ class AttGAN(object):
self.id2name = id2name
def build_model(self):
data_shape = [-1, 3, self.cfg.image_size, self.cfg.image_size]
data_shape = [None, 3, self.cfg.image_size, self.cfg.image_size]
image_real = fluid.layers.data(
image_real = fluid.data(
name='image_real', shape=data_shape, dtype='float32')
label_org = fluid.layers.data(
name='label_org', shape=[self.cfg.c_dim], dtype='float32')
label_trg = fluid.layers.data(
name='label_trg', shape=[self.cfg.c_dim], dtype='float32')
label_org_ = fluid.layers.data(
name='label_org_', shape=[self.cfg.c_dim], dtype='float32')
label_trg_ = fluid.layers.data(
name='label_trg_', shape=[self.cfg.c_dim], dtype='float32')
label_org = fluid.data(
name='label_org', shape=[None, self.cfg.c_dim], dtype='float32')
label_trg = fluid.data(
name='label_trg', shape=[None, self.cfg.c_dim], dtype='float32')
label_org_ = fluid.data(
name='label_org_', shape=[None, self.cfg.c_dim], dtype='float32')
label_trg_ = fluid.data(
name='label_trg_', shape=[None, self.cfg.c_dim], dtype='float32')
py_reader = fluid.io.PyReader(
feed_list=[image_real, label_org, label_trg],
......@@ -369,9 +369,9 @@ class AttGAN(object):
batch_id += 1
if self.cfg.run_test:
image_name = fluid.layers.data(
image_name = fluid.data(
name='image_name',
shape=[self.cfg.n_samples],
shape=[None, self.cfg.n_samples],
dtype='int32')
test_py_reader = fluid.io.PyReader(
feed_list=[image_real, label_org, label_trg, image_name],
......
......@@ -88,12 +88,12 @@ class CGAN(object):
def build_model(self):
img = fluid.layers.data(name='img', shape=[784], dtype='float32')
condition = fluid.layers.data(
name='condition', shape=[1], dtype='float32')
noise = fluid.layers.data(
name='noise', shape=[self.cfg.noise_size], dtype='float32')
label = fluid.layers.data(name='label', shape=[1], dtype='float32')
img = fluid.data(name='img', shape=[None, 784], dtype='float32')
condition = fluid.data(
name='condition', shape=[None, 1], dtype='float32')
noise = fluid.data(
name='noise', shape=[None, self.cfg.noise_size], dtype='float32')
label = fluid.data(name='label', shape=[None, 1], dtype='float32')
g_trainer = GTrainer(noise, condition, self.cfg)
d_trainer = DTrainer(img, condition, label, self.cfg)
......
......@@ -229,15 +229,13 @@ class CycleGAN(object):
self.B_id2name = B_id2name
def build_model(self):
data_shape = [-1, 3, self.cfg.crop_size, self.cfg.crop_size]
data_shape = [None, 3, self.cfg.crop_size, self.cfg.crop_size]
input_A = fluid.layers.data(
name='input_A', shape=data_shape, dtype='float32')
input_B = fluid.layers.data(
name='input_B', shape=data_shape, dtype='float32')
fake_pool_A = fluid.layers.data(
input_A = fluid.data(name='input_A', shape=data_shape, dtype='float32')
input_B = fluid.data(name='input_B', shape=data_shape, dtype='float32')
fake_pool_A = fluid.data(
name='fake_pool_A', shape=data_shape, dtype='float32')
fake_pool_B = fluid.layers.data(
fake_pool_B = fluid.data(
name='fake_pool_B', shape=data_shape, dtype='float32')
A_py_reader = fluid.io.PyReader(
......@@ -348,10 +346,10 @@ class CycleGAN(object):
batch_id += 1
if self.cfg.run_test:
A_image_name = fluid.layers.data(
name='A_image_name', shape=[1], dtype='int32')
B_image_name = fluid.layers.data(
name='B_image_name', shape=[1], dtype='int32')
A_image_name = fluid.data(
name='A_image_name', shape=[None, 1], dtype='int32')
B_image_name = fluid.data(
name='B_image_name', shape=[None, 1], dtype='int32')
A_test_py_reader = fluid.io.PyReader(
feed_list=[input_A, A_image_name],
capacity=4,
......
......@@ -86,10 +86,10 @@ class DCGAN(object):
self.train_reader = train_reader
def build_model(self):
img = fluid.layers.data(name='img', shape=[784], dtype='float32')
noise = fluid.layers.data(
name='noise', shape=[self.cfg.noise_size], dtype='float32')
label = fluid.layers.data(name='label', shape=[1], dtype='float32')
img = fluid.data(name='img', shape=[None, 784], dtype='float32')
noise = fluid.data(
name='noise', shape=[None, self.cfg.noise_size], dtype='float32')
label = fluid.data(name='label', shape=[None, 1], dtype='float32')
g_trainer = GTrainer(noise, label, self.cfg)
d_trainer = DTrainer(img, label, self.cfg)
......
......@@ -211,18 +211,16 @@ class Pix2pix(object):
self.id2name = id2name
def build_model(self):
data_shape = [-1, 3, self.cfg.crop_size, self.cfg.crop_size]
data_shape = [None, 3, self.cfg.crop_size, self.cfg.crop_size]
input_A = fluid.layers.data(
name='input_A', shape=data_shape, dtype='float32')
input_B = fluid.layers.data(
name='input_B', shape=data_shape, dtype='float32')
input_fake = fluid.layers.data(
input_A = fluid.data(name='input_A', shape=data_shape, dtype='float32')
input_B = fluid.data(name='input_B', shape=data_shape, dtype='float32')
input_fake = fluid.data(
name='input_fake', shape=data_shape, dtype='float32')
py_reader = fluid.io.PyReader(
loader = fluid.io.DataLoader.from_generator(
feed_list=[input_A, input_B],
capacity=4, ## batch_size * 4
capacity=4,
iterable=True,
use_double_buffer=True)
......@@ -232,7 +230,7 @@ class Pix2pix(object):
# prepare environment
place = fluid.CUDAPlace(0) if self.cfg.use_gpu else fluid.CPUPlace()
py_reader.decorate_batch_generator(
loader.set_batch_generator(
self.train_reader,
places=fluid.cuda_places()
if self.cfg.use_gpu else fluid.cpu_places())
......@@ -259,7 +257,7 @@ class Pix2pix(object):
for epoch_id in range(self.cfg.epoch):
batch_id = 0
for tensor in py_reader():
for tensor in loader():
s_time = time.time()
tensor_A, tensor_B = tensor[0]['input_A'], tensor[0]['input_B']
......@@ -298,16 +296,16 @@ class Pix2pix(object):
batch_id += 1
if self.cfg.run_test:
image_name = fluid.layers.data(
image_name = fluid.data(
name='image_name',
shape=[self.cfg.batch_size],
shape=[None, self.cfg.batch_size],
dtype="int32")
test_py_reader = fluid.io.PyReader(
test_loader = fluid.io.DataLoader.from_generator(
feed_list=[input_A, input_B, image_name],
capacity=4, ## batch_size * 4
capacity=4,
iterable=True,
use_double_buffer=True)
test_py_reader.decorate_batch_generator(
test_loader.set_batch_generator(
self.test_reader,
places=fluid.cuda_places()
if self.cfg.use_gpu else fluid.cpu_places())
......@@ -319,7 +317,7 @@ class Pix2pix(object):
place,
test_program,
gen_trainer,
test_py_reader,
test_loader,
A_id2name=self.id2name)
if self.cfg.save_checkpoints:
......
......@@ -284,19 +284,19 @@ class SPADE(object):
self.id2name = id2name
def build_model(self):
data_shape = [-1, 3, self.cfg.crop_height, self.cfg.crop_width]
data_shape = [None, 3, self.cfg.crop_height, self.cfg.crop_width]
label_shape = [
-1, self.cfg.label_nc, self.cfg.crop_height, self.cfg.crop_width
None, self.cfg.label_nc, self.cfg.crop_height, self.cfg.crop_width
]
edge_shape = [-1, 1, self.cfg.crop_height, self.cfg.crop_width]
edge_shape = [None, 1, self.cfg.crop_height, self.cfg.crop_width]
input_A = fluid.layers.data(
input_A = fluid.data(
name='input_label', shape=label_shape, dtype='float32')
input_B = fluid.layers.data(
input_B = fluid.data(
name='input_img', shape=data_shape, dtype='float32')
input_C = fluid.layers.data(
input_C = fluid.data(
name='input_ins', shape=edge_shape, dtype='float32')
input_fake = fluid.layers.data(
input_fake = fluid.data(
name='input_fake', shape=data_shape, dtype='float32')
gen_trainer = GTrainer(input_A, input_B, input_C, self.cfg,
......@@ -394,9 +394,9 @@ class SPADE(object):
if self.cfg.run_test:
test_program = gen_trainer.infer_program
image_name = fluid.layers.data(
image_name = fluid.data(
name='image_name',
shape=[self.cfg.batch_size],
shape=[None, self.cfg.batch_size],
dtype="int32")
test_py_reader = fluid.io.PyReader(
feed_list=[input_A, input_B, input_C, image_name],
......
......@@ -282,18 +282,18 @@ class STGAN(object):
self.batch_num = batch_num
def build_model(self):
data_shape = [-1, 3, self.cfg.image_size, self.cfg.image_size]
data_shape = [None, 3, self.cfg.image_size, self.cfg.image_size]
image_real = fluid.layers.data(
image_real = fluid.data(
name='image_real', shape=data_shape, dtype='float32')
label_org = fluid.layers.data(
name='label_org', shape=[self.cfg.c_dim], dtype='float32')
label_trg = fluid.layers.data(
name='label_trg', shape=[self.cfg.c_dim], dtype='float32')
label_org_ = fluid.layers.data(
name='label_org_', shape=[self.cfg.c_dim], dtype='float32')
label_trg_ = fluid.layers.data(
name='label_trg_', shape=[self.cfg.c_dim], dtype='float32')
label_org = fluid.data(
name='label_org', shape=[None, self.cfg.c_dim], dtype='float32')
label_trg = fluid.data(
name='label_trg', shape=[None, self.cfg.c_dim], dtype='float32')
label_org_ = fluid.data(
name='label_org_', shape=[None, self.cfg.c_dim], dtype='float32')
label_trg_ = fluid.data(
name='label_trg_', shape=[None, self.cfg.c_dim], dtype='float32')
test_gen_trainer = GTrainer(image_real, label_org, label_org_,
label_trg, label_trg_, self.cfg,
......@@ -378,9 +378,9 @@ class STGAN(object):
batch_id += 1
if self.cfg.run_test:
image_name = fluid.layers.data(
image_name = fluid.data(
name='image_name',
shape=[self.cfg.n_samples],
shape=[None, self.cfg.n_samples],
dtype='int32')
test_py_reader = fluid.io.PyReader(
feed_list=[image_real, label_org, label_trg, image_name],
......
......@@ -259,14 +259,14 @@ class StarGAN(object):
self.batch_num = batch_num
def build_model(self):
data_shape = [-1, 3, self.cfg.image_size, self.cfg.image_size]
data_shape = [None, 3, self.cfg.image_size, self.cfg.image_size]
image_real = fluid.layers.data(
image_real = fluid.data(
name='image_real', shape=data_shape, dtype='float32')
label_org = fluid.layers.data(
name='label_org', shape=[self.cfg.c_dim], dtype='float32')
label_trg = fluid.layers.data(
name='label_trg', shape=[self.cfg.c_dim], dtype='float32')
label_org = fluid.data(
name='label_org', shape=[None, self.cfg.c_dim], dtype='float32')
label_trg = fluid.data(
name='label_trg', shape=[None, self.cfg.c_dim], dtype='float32')
py_reader = fluid.io.PyReader(
feed_list=[image_real, label_org, label_trg],
......@@ -346,9 +346,9 @@ class StarGAN(object):
batch_id += 1
if self.cfg.run_test:
image_name = fluid.layers.data(
image_name = fluid.data(
name='image_name',
shape=[self.cfg.n_samples],
shape=[None, self.cfg.n_samples],
dtype='int32')
test_py_reader = fluid.io.PyReader(
feed_list=[image_real, label_org, label_trg, image_name],
......
......@@ -409,3 +409,19 @@ def check_gpu(use_gpu):
sys.exit(1)
except Exception as e:
pass
def check_version():
"""
Log error and exit when the installed version of paddlepaddle is
not satisfied.
"""
err = "PaddlePaddle version 1.6 or higher is required, " \
"or a suitable develop version is satisfied as well. \n" \
"Please make sure the version is good with your code." \
try:
fluid.require_version('1.6.0')
except Exception as e:
print(err)
sys.exit(1)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册