Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
8875bb25
M
models
项目概览
PaddlePaddle
/
models
大约 1 年 前同步成功
通知
222
Star
6828
Fork
2962
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
602
列表
看板
标记
里程碑
合并请求
255
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
models
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
602
Issue
602
列表
看板
标记
里程碑
合并请求
255
合并请求
255
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
8875bb25
编写于
3月 05, 2019
作者:
W
whs
提交者:
GitHub
3月 05, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix cycle gan: (#1829) (#1835)
1. use_cudnn=False 2. fix saving checkponint 3. using compiled program
上级
a8976f5c
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
42 addition
and
22 deletion
+42
-22
fluid/PaddleCV/gan/cycle_gan/README.md
fluid/PaddleCV/gan/cycle_gan/README.md
+2
-2
fluid/PaddleCV/gan/cycle_gan/infer.py
fluid/PaddleCV/gan/cycle_gan/infer.py
+3
-1
fluid/PaddleCV/gan/cycle_gan/layers.py
fluid/PaddleCV/gan/cycle_gan/layers.py
+5
-1
fluid/PaddleCV/gan/cycle_gan/train.py
fluid/PaddleCV/gan/cycle_gan/train.py
+32
-18
未找到文件。
fluid/PaddleCV/gan/cycle_gan/README.md
浏览文件 @
8875bb25
...
...
@@ -74,8 +74,8 @@ env CUDA_VISIBLE_DEVICES=0 python train.py
```
env CUDA_VISIBLE_DEVICE=0 python infer.py \
--init_model="
model
s/1" --input="./data/inputA/*" \
--output="./output"
--init_model="
checkpoint
s/1" --input="./data/inputA/*" \
--
input_style A --
output="./output"
```
训练150轮的模型预测效果如图2和图3所示:
...
...
fluid/PaddleCV/gan/cycle_gan/infer.py
浏览文件 @
8875bb25
...
...
@@ -26,8 +26,10 @@ def infer(args):
data_shape
=
[
-
1
,
3
,
256
,
256
]
input
=
fluid
.
layers
.
data
(
name
=
'input'
,
shape
=
data_shape
,
dtype
=
'float32'
)
if
args
.
input_style
==
"A"
:
model_name
=
'g_a'
fake
=
build_generator_resnet_9blocks
(
input
,
name
=
"g_A"
)
elif
args
.
input_style
==
"B"
:
model_name
=
'g_b'
fake
=
build_generator_resnet_9blocks
(
input
,
name
=
"g_B"
)
else
:
raise
"Input with style [%s] is not supported."
%
args
.
input_style
...
...
@@ -37,7 +39,7 @@ def infer(args):
place
=
fluid
.
CUDAPlace
(
0
)
exe
=
fluid
.
Executor
(
place
)
exe
.
run
(
fluid
.
default_startup_program
())
fluid
.
io
.
load_persistables
(
exe
,
args
.
init_model
)
fluid
.
io
.
load_persistables
(
exe
,
args
.
init_model
+
"/"
+
model_name
)
if
not
os
.
path
.
exists
(
args
.
output
):
os
.
makedirs
(
args
.
output
)
...
...
fluid/PaddleCV/gan/cycle_gan/layers.py
浏览文件 @
8875bb25
...
...
@@ -3,10 +3,12 @@ import paddle.fluid as fluid
import
numpy
as
np
import
os
use_cudnn
=
True
# cudnn is not better when batch size is 1.
use_cudnn
=
False
if
'ce_mode'
in
os
.
environ
:
use_cudnn
=
False
def
cal_padding
(
img_size
,
stride
,
filter_size
,
dilation
=
1
):
"""Calculate padding size."""
valid_filter_size
=
dilation
*
(
filter_size
-
1
)
+
1
...
...
@@ -18,6 +20,8 @@ def cal_padding(img_size, stride, filter_size, dilation=1):
def
instance_norm
(
input
,
name
=
None
):
# TODO(lvmengsi@baidu.com): Check the accuracy when using fluid.layers.layer_norm.
# return fluid.layers.layer_norm(input, begin_norm_axis=2)
helper
=
fluid
.
layer_helper
.
LayerHelper
(
"instance_norm"
,
**
locals
())
dtype
=
helper
.
input_dtype
()
epsilon
=
1e-5
...
...
fluid/PaddleCV/gan/cycle_gan/train.py
浏览文件 @
8875bb25
...
...
@@ -17,7 +17,6 @@ import data_reader
from
utility
import
add_arguments
,
print_arguments
,
ImagePool
from
trainer
import
*
parser
=
argparse
.
ArgumentParser
(
description
=
__doc__
)
add_arg
=
functools
.
partial
(
add_arguments
,
argparser
=
parser
)
# yapf: disable
...
...
@@ -36,7 +35,7 @@ add_arg('run_ce', bool, False, "Whether to run for model ce.")
def
train
(
args
):
max_images_num
=
data_reader
.
max_images_num
()
shuffle
=
True
shuffle
=
True
if
args
.
run_ce
:
np
.
random
.
seed
(
10
)
fluid
.
default_startup_program
().
random_seed
=
90
...
...
@@ -67,8 +66,10 @@ def train(args):
A_pool
=
ImagePool
()
B_pool
=
ImagePool
()
A_reader
=
paddle
.
batch
(
data_reader
.
a_reader
(
shuffle
=
shuffle
),
args
.
batch_size
)()
B_reader
=
paddle
.
batch
(
data_reader
.
b_reader
(
shuffle
=
shuffle
),
args
.
batch_size
)()
A_reader
=
paddle
.
batch
(
data_reader
.
a_reader
(
shuffle
=
shuffle
),
args
.
batch_size
)()
B_reader
=
paddle
.
batch
(
data_reader
.
b_reader
(
shuffle
=
shuffle
),
args
.
batch_size
)()
if
not
args
.
run_ce
:
A_test_reader
=
data_reader
.
a_test_reader
()
B_test_reader
=
data_reader
.
b_test_reader
()
...
...
@@ -119,13 +120,13 @@ def train(args):
if
not
os
.
path
.
exists
(
out_path
):
os
.
makedirs
(
out_path
)
fluid
.
io
.
save_persistables
(
exe
,
out_path
+
"/g_a"
,
main_program
=
g_A_trainer
.
program
,
filename
=
"params"
)
exe
,
out_path
+
"/g_a"
,
main_program
=
g_A_trainer
.
program
)
fluid
.
io
.
save_persistables
(
exe
,
out_path
+
"/g_b"
,
main_program
=
g_B_trainer
.
program
,
filename
=
"params"
)
exe
,
out_path
+
"/g_b"
,
main_program
=
g_B_trainer
.
program
)
fluid
.
io
.
save_persistables
(
exe
,
out_path
+
"/d_a"
,
main_program
=
d_A_trainer
.
program
,
filename
=
"params"
)
exe
,
out_path
+
"/d_a"
,
main_program
=
d_A_trainer
.
program
)
fluid
.
io
.
save_persistables
(
exe
,
out_path
+
"/d_b"
,
main_program
=
d_B_trainer
.
program
,
filename
=
"params"
)
exe
,
out_path
+
"/d_b"
,
main_program
=
d_B_trainer
.
program
)
print
(
"saved checkpoint to {}"
.
format
(
out_path
))
sys
.
stdout
.
flush
()
...
...
@@ -144,8 +145,21 @@ def train(args):
if
args
.
init_model
:
init_model
()
losses
=
[[],
[]]
losses
=
[[],
[]]
t_time
=
0
g_A_trainer_program
=
fluid
.
CompiledProgram
(
g_A_trainer
.
program
).
with_data_parallel
(
loss_name
=
g_A_trainer
.
g_loss_A
.
name
)
g_B_trainer_program
=
fluid
.
CompiledProgram
(
g_B_trainer
.
program
).
with_data_parallel
(
loss_name
=
g_B_trainer
.
g_loss_B
.
name
)
d_B_trainer_program
=
fluid
.
CompiledProgram
(
d_B_trainer
.
program
).
with_data_parallel
(
loss_name
=
d_B_trainer
.
d_loss_B
.
name
)
d_A_trainer_program
=
fluid
.
CompiledProgram
(
d_A_trainer
.
program
).
with_data_parallel
(
loss_name
=
d_A_trainer
.
d_loss_A
.
name
)
for
epoch
in
range
(
args
.
epoch
):
batch_id
=
0
for
i
in
range
(
max_images_num
):
...
...
@@ -158,7 +172,7 @@ def train(args):
s_time
=
time
.
time
()
# optimize the g_A network
g_A_loss
,
fake_B_tmp
=
exe
.
run
(
g_A_trainer
.
program
,
g_A_trainer
_
program
,
fetch_list
=
[
g_A_trainer
.
g_loss_A
,
g_A_trainer
.
fake_B
],
feed
=
{
"input_A"
:
tensor_A
,
"input_B"
:
tensor_B
})
...
...
@@ -167,14 +181,14 @@ def train(args):
# optimize the d_B network
d_B_loss
=
exe
.
run
(
d_B_trainer
.
program
,
d_B_trainer
_
program
,
fetch_list
=
[
d_B_trainer
.
d_loss_B
],
feed
=
{
"input_B"
:
tensor_B
,
"fake_pool_B"
:
fake_pool_B
})[
0
]
# optimize the g_B network
g_B_loss
,
fake_A_tmp
=
exe
.
run
(
g_B_trainer
.
program
,
g_B_trainer
_
program
,
fetch_list
=
[
g_B_trainer
.
g_loss_B
,
g_B_trainer
.
fake_A
],
feed
=
{
"input_A"
:
tensor_A
,
"input_B"
:
tensor_B
})
...
...
@@ -183,16 +197,16 @@ def train(args):
# optimize the d_A network
d_A_loss
=
exe
.
run
(
d_A_trainer
.
program
,
d_A_trainer
_
program
,
fetch_list
=
[
d_A_trainer
.
d_loss_A
],
feed
=
{
"input_A"
:
tensor_A
,
"fake_pool_A"
:
fake_pool_A
})[
0
]
batch_time
=
time
.
time
()
-
s_time
t_time
+=
batch_time
print
(
"epoch{}; batch{}; g_A_loss: {}; d_B_loss: {}; g_B_loss: {}; d_A_loss: {}; "
"Batch_time_cost: {:.2f}"
.
format
(
epoch
,
batch_id
,
g_A_loss
[
0
],
d_B_loss
[
0
],
g_B_loss
[
0
],
d_A_loss
[
0
],
batch_time
))
print
(
"epoch{}; batch{}; g_A_loss: {}; d_B_loss: {}; g_B_loss: {}; d_A_loss: {}; "
"Batch_time_cost: {:.2f}"
.
format
(
epoch
,
batch_id
,
g_A_loss
[
0
],
d_B_loss
[
0
],
g_B_loss
[
0
],
d_A_loss
[
0
],
batch_time
))
losses
[
0
].
append
(
g_A_loss
[
0
])
losses
[
1
].
append
(
d_A_loss
[
0
])
sys
.
stdout
.
flush
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录