Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
3ebf6aaf
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
3ebf6aaf
编写于
11月 07, 2016
作者:
W
wangyang59
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fixed a gpu bug in trainer API to train gan using GPU
上级
c159e4dd
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
92 addition
and
101 deletion
+92
-101
demo/gan/.gitignore
demo/gan/.gitignore
+1
-1
demo/gan/gan_conf_image.py
demo/gan/gan_conf_image.py
+59
-73
demo/gan/gan_trainer.py
demo/gan/gan_trainer.py
+9
-9
demo/gan/gan_trainer_image.py
demo/gan/gan_trainer_image.py
+22
-16
paddle/api/Paddle.swig
paddle/api/Paddle.swig
+1
-2
未找到文件。
demo/gan/.gitignore
浏览文件 @
3ebf6aaf
...
...
@@ -2,5 +2,5 @@ output/
*.png
.pydevproject
.project
train
Log.txt
train
.log
data/raw_data/
demo/gan/gan_conf_image.py
浏览文件 @
3ebf6aaf
...
...
@@ -41,39 +41,9 @@ settings(
learning_method
=
AdamOptimizer
()
)
def
convTrans_bn
(
input
,
channels
,
output_x
,
num_filters
,
imgSize
,
stride
,
name
,
param_attr
,
bias_attr
,
param_attr_bn
):
tmp
=
imgSize
-
(
output_x
-
1
)
*
stride
if
tmp
<=
1
or
tmp
>
5
:
raise
ValueError
(
"convTrans input-output dimension does not fit"
)
elif
tmp
<=
3
:
filter_size
=
tmp
+
2
padding
=
1
else
:
filter_size
=
tmp
padding
=
0
convTrans
=
img_conv_layer
(
input
,
filter_size
=
filter_size
,
num_filters
=
num_filters
,
name
=
name
+
"_convt"
,
num_channels
=
channels
,
act
=
LinearActivation
(),
groups
=
1
,
stride
=
stride
,
padding
=
padding
,
bias_attr
=
bias_attr
,
param_attr
=
param_attr
,
shared_biases
=
True
,
layer_attr
=
None
,
filter_size_y
=
None
,
stride_y
=
None
,
padding_y
=
None
,
trans
=
True
)
convTrans_bn
=
batch_norm_layer
(
convTrans
,
act
=
ReluActivation
(),
name
=
name
+
"_convt_bn"
,
bias_attr
=
bias_attr
,
param_attr
=
param_attr_bn
,
use_global_stats
=
False
)
return
convTrans_bn
def
conv_bn
(
input
,
channels
,
imgSize
,
num_filters
,
output_x
,
stride
,
name
,
param_attr
,
bias_attr
,
param_attr_bn
,
bn
):
param_attr
,
bias_attr
,
param_attr_bn
,
bn
,
trans
=
False
,
act
=
ReluActivation
()):
tmp
=
imgSize
-
(
output_x
-
1
)
*
stride
if
tmp
<=
1
or
tmp
>
5
:
raise
ValueError
(
"conv input-output dimension does not fit"
)
...
...
@@ -86,18 +56,24 @@ def conv_bn(input, channels, imgSize, num_filters, output_x, stride, name,
print
(
imgSize
,
output_x
,
stride
,
filter_size
,
padding
)
if
trans
:
nameApx
=
"_conv"
else
:
nameApx
=
"_convt"
if
bn
:
conv
=
img_conv_layer
(
input
,
filter_size
=
filter_size
,
num_filters
=
num_filters
,
name
=
name
+
"_conv"
,
num_channels
=
channels
,
name
=
name
+
nameApx
,
num_channels
=
channels
,
act
=
LinearActivation
(),
groups
=
1
,
stride
=
stride
,
padding
=
padding
,
bias_attr
=
bias_attr
,
param_attr
=
param_attr
,
shared_biases
=
True
,
layer_attr
=
None
,
filter_size_y
=
None
,
stride_y
=
None
,
padding_y
=
None
)
filter_size_y
=
None
,
stride_y
=
None
,
padding_y
=
None
,
trans
=
trans
)
conv_bn
=
batch_norm_layer
(
conv
,
act
=
ReluActivation
()
,
name
=
name
+
"_conv
_bn"
,
act
=
act
,
name
=
name
+
nameApx
+
"
_bn"
,
bias_attr
=
bias_attr
,
param_attr
=
param_attr_bn
,
use_global_stats
=
False
)
...
...
@@ -106,11 +82,12 @@ def conv_bn(input, channels, imgSize, num_filters, output_x, stride, name,
else
:
conv
=
img_conv_layer
(
input
,
filter_size
=
filter_size
,
num_filters
=
num_filters
,
name
=
name
+
"_conv"
,
num_channels
=
channels
,
act
=
ReluActivation
()
,
groups
=
1
,
stride
=
stride
,
name
=
name
+
nameApx
,
num_channels
=
channels
,
act
=
act
,
groups
=
1
,
stride
=
stride
,
padding
=
padding
,
bias_attr
=
bias_attr
,
param_attr
=
param_attr
,
shared_biases
=
True
,
layer_attr
=
None
,
filter_size_y
=
None
,
stride_y
=
None
,
padding_y
=
None
)
filter_size_y
=
None
,
stride_y
=
None
,
padding_y
=
None
,
trans
=
trans
)
return
conv
def
generator
(
noise
):
...
...
@@ -143,7 +120,7 @@ def generator(noise):
param_attr
=
param_attr_bn
,
use_global_stats
=
False
)
h2_bn
=
conv
Trans
_bn
(
h1_bn
,
h2_bn
=
conv_bn
(
h1_bn
,
channels
=
gf_dim
*
4
,
output_x
=
s8
,
num_filters
=
gf_dim
*
2
,
...
...
@@ -152,9 +129,11 @@ def generator(noise):
name
=
"gen_layer_h2"
,
param_attr
=
param_attr
,
bias_attr
=
bias_attr
,
param_attr_bn
=
param_attr_bn
)
param_attr_bn
=
param_attr_bn
,
bn
=
True
,
trans
=
True
)
h3_bn
=
conv
Trans
_bn
(
h2_bn
,
h3_bn
=
conv_bn
(
h2_bn
,
channels
=
gf_dim
*
2
,
output_x
=
s4
,
num_filters
=
gf_dim
,
...
...
@@ -163,10 +142,12 @@ def generator(noise):
name
=
"gen_layer_h3"
,
param_attr
=
param_attr
,
bias_attr
=
bias_attr
,
param_attr_bn
=
param_attr_bn
)
param_attr_bn
=
param_attr_bn
,
bn
=
True
,
trans
=
True
)
return
conv
Trans
_bn
(
h3_bn
,
return
conv_bn
(
h3_bn
,
channels
=
gf_dim
,
output_x
=
s2
,
num_filters
=
c_dim
,
...
...
@@ -175,7 +156,10 @@ def generator(noise):
name
=
"gen_layer_h4"
,
param_attr
=
param_attr
,
bias_attr
=
bias_attr
,
param_attr_bn
=
param_attr_bn
)
param_attr_bn
=
param_attr_bn
,
bn
=
False
,
trans
=
True
,
act
=
TanhActivation
())
def
discriminator
(
sample
):
...
...
@@ -186,10 +170,12 @@ def discriminator(sample):
of the sample is from generator and dimension 1 is the probabblity
of the sample is from real data.
"""
param_attr
=
ParamAttr
(
is_static
=
is_generator_training
)
param_attr
=
ParamAttr
(
is_static
=
is_generator_training
,
initial_mean
=
0.0
,
initial_std
=
0.02
)
bias_attr
=
ParamAttr
(
is_static
=
is_generator_training
,
initial_mean
=
1
.0
,
initial_std
=
0
)
initial_mean
=
0
.0
,
initial_std
=
0
.0
)
param_attr_bn
=
ParamAttr
(
is_static
=
is_generator_training
,
initial_mean
=
1.0
,
...
...
demo/gan/gan_trainer.py
浏览文件 @
3ebf6aaf
...
...
@@ -97,32 +97,32 @@ def prepare_discriminator_data_batch(
(
numpy
.
zeros
(
batch_size
/
2
,
dtype
=
'int32'
),
numpy
.
ones
(
batch_size
/
2
,
dtype
=
'int32'
)),
0
)
inputs
=
api
.
Arguments
.
createArguments
(
2
)
inputs
.
setSlotValue
(
0
,
api
.
Matrix
.
create
C
puDenseFromNumpy
(
all_samples
))
inputs
.
setSlotIds
(
1
,
api
.
IVector
.
create
CpuVectorFromNump
y
(
all_labels
))
inputs
.
setSlotValue
(
0
,
api
.
Matrix
.
create
G
puDenseFromNumpy
(
all_samples
))
inputs
.
setSlotIds
(
1
,
api
.
IVector
.
create
GpuVectorFromNum
y
(
all_labels
))
return
inputs
def
prepare_discriminator_data_batch_pos
(
batch_size
,
noise_dim
,
sample_dim
):
real_samples
=
get_real_samples
(
batch_size
,
sample_dim
)
labels
=
numpy
.
ones
(
batch_size
,
dtype
=
'int32'
)
inputs
=
api
.
Arguments
.
createArguments
(
2
)
inputs
.
setSlotValue
(
0
,
api
.
Matrix
.
create
C
puDenseFromNumpy
(
real_samples
))
inputs
.
setSlotIds
(
1
,
api
.
IVector
.
create
CpuVectorFromNump
y
(
labels
))
inputs
.
setSlotValue
(
0
,
api
.
Matrix
.
create
G
puDenseFromNumpy
(
real_samples
))
inputs
.
setSlotIds
(
1
,
api
.
IVector
.
create
GpuVectorFromNum
y
(
labels
))
return
inputs
def
prepare_discriminator_data_batch_neg
(
generator_machine
,
batch_size
,
noise_dim
,
sample_dim
):
fake_samples
=
get_fake_samples
(
generator_machine
,
batch_size
,
noise_dim
,
sample_dim
)
labels
=
numpy
.
zeros
(
batch_size
,
dtype
=
'int32'
)
inputs
=
api
.
Arguments
.
createArguments
(
2
)
inputs
.
setSlotValue
(
0
,
api
.
Matrix
.
create
C
puDenseFromNumpy
(
fake_samples
))
inputs
.
setSlotIds
(
1
,
api
.
IVector
.
create
CpuVectorFromNump
y
(
labels
))
inputs
.
setSlotValue
(
0
,
api
.
Matrix
.
create
G
puDenseFromNumpy
(
fake_samples
))
inputs
.
setSlotIds
(
1
,
api
.
IVector
.
create
GpuVectorFromNum
y
(
labels
))
return
inputs
def
prepare_generator_data_batch
(
batch_size
,
dim
):
noise
=
numpy
.
random
.
normal
(
size
=
(
batch_size
,
dim
)).
astype
(
'float32'
)
label
=
numpy
.
ones
(
batch_size
,
dtype
=
'int32'
)
inputs
=
api
.
Arguments
.
createArguments
(
2
)
inputs
.
setSlotValue
(
0
,
api
.
Matrix
.
create
C
puDenseFromNumpy
(
noise
))
inputs
.
setSlotIds
(
1
,
api
.
IVector
.
create
CpuVectorFromNump
y
(
label
))
inputs
.
setSlotValue
(
0
,
api
.
Matrix
.
create
G
puDenseFromNumpy
(
noise
))
inputs
.
setSlotIds
(
1
,
api
.
IVector
.
create
GpuVectorFromNum
y
(
label
))
return
inputs
...
...
@@ -140,7 +140,7 @@ def get_layer_size(model_conf, layer_name):
def
main
():
api
.
initPaddle
(
'--use_gpu=
0
'
,
'--dot_period=100'
,
'--log_period=10000'
)
api
.
initPaddle
(
'--use_gpu=
1
'
,
'--dot_period=100'
,
'--log_period=10000'
)
gen_conf
=
parse_config
(
"gan_conf.py"
,
"mode=generator_training"
)
dis_conf
=
parse_config
(
"gan_conf.py"
,
"mode=discriminator_training"
)
generator_conf
=
parse_config
(
"gan_conf.py"
,
"mode=generator"
)
...
...
demo/gan/gan_trainer_image.py
浏览文件 @
3ebf6aaf
...
...
@@ -16,7 +16,7 @@ import argparse
import
itertools
import
random
import
numpy
import
sys
,
os
import
sys
,
os
,
gc
from
PIL
import
Image
from
paddle.trainer.config_parser
import
parse_config
...
...
@@ -94,10 +94,19 @@ def load_mnist_data(imageFile):
f
.
close
()
return
data
def
merge
(
images
,
size
):
h
,
w
=
28
,
28
img
=
numpy
.
zeros
((
h
*
size
[
0
],
w
*
size
[
1
]))
for
idx
in
xrange
(
size
[
0
]
*
size
[
1
]):
i
=
idx
%
size
[
1
]
j
=
idx
//
size
[
1
]
img
[
j
*
h
:
j
*
h
+
h
,
i
*
w
:
i
*
w
+
w
]
=
(
images
[
idx
,
:].
reshape
((
h
,
w
))
+
1.0
)
/
2.0
*
255.0
return
img
def
saveImages
(
images
,
path
):
for
i
in
xrange
(
10
):
im
=
Image
.
fromarray
(
images
[
i
,
:].
reshape
((
28
,
28
))
*
255.0
).
convert
(
'RGB'
)
im
.
save
(
path
+
"/image_"
+
str
(
i
)
+
".png"
)
merged_img
=
merge
(
images
,
[
8
,
8
])
im
=
Image
.
fromarray
(
merged_img
).
convert
(
'RGB'
)
im
.
save
(
path
)
def
get_real_samples
(
batch_size
,
data_np
):
return
data_np
[
numpy
.
random
.
choice
(
data_np
.
shape
[
0
],
batch_size
,
...
...
@@ -124,8 +133,8 @@ def prepare_discriminator_data_batch_pos(batch_size, data_np):
real_samples
=
get_real_samples
(
batch_size
,
data_np
)
labels
=
numpy
.
ones
(
batch_size
,
dtype
=
'int32'
)
inputs
=
api
.
Arguments
.
createArguments
(
2
)
inputs
.
setSlotValue
(
0
,
api
.
Matrix
.
create
C
puDenseFromNumpy
(
real_samples
))
inputs
.
setSlotIds
(
1
,
api
.
IVector
.
create
C
puVectorFromNumpy
(
labels
))
inputs
.
setSlotValue
(
0
,
api
.
Matrix
.
create
G
puDenseFromNumpy
(
real_samples
))
inputs
.
setSlotIds
(
1
,
api
.
IVector
.
create
G
puVectorFromNumpy
(
labels
))
return
inputs
def
prepare_discriminator_data_batch_neg
(
generator_machine
,
batch_size
,
noise
):
...
...
@@ -133,16 +142,16 @@ def prepare_discriminator_data_batch_neg(generator_machine, batch_size, noise):
#print fake_samples.shape
labels
=
numpy
.
zeros
(
batch_size
,
dtype
=
'int32'
)
inputs
=
api
.
Arguments
.
createArguments
(
2
)
inputs
.
setSlotValue
(
0
,
api
.
Matrix
.
create
C
puDenseFromNumpy
(
fake_samples
))
inputs
.
setSlotIds
(
1
,
api
.
IVector
.
create
C
puVectorFromNumpy
(
labels
))
inputs
.
setSlotValue
(
0
,
api
.
Matrix
.
create
G
puDenseFromNumpy
(
fake_samples
))
inputs
.
setSlotIds
(
1
,
api
.
IVector
.
create
G
puVectorFromNumpy
(
labels
))
return
inputs
def
prepare_generator_data_batch
(
batch_size
,
noise
):
label
=
numpy
.
ones
(
batch_size
,
dtype
=
'int32'
)
#label = numpy.zeros(batch_size, dtype='int32')
inputs
=
api
.
Arguments
.
createArguments
(
2
)
inputs
.
setSlotValue
(
0
,
api
.
Matrix
.
create
C
puDenseFromNumpy
(
noise
))
inputs
.
setSlotIds
(
1
,
api
.
IVector
.
create
C
puVectorFromNumpy
(
label
))
inputs
.
setSlotValue
(
0
,
api
.
Matrix
.
create
G
puDenseFromNumpy
(
noise
))
inputs
.
setSlotIds
(
1
,
api
.
IVector
.
create
G
puVectorFromNumpy
(
label
))
return
inputs
...
...
@@ -160,7 +169,7 @@ def get_layer_size(model_conf, layer_name):
def
main
():
api
.
initPaddle
(
'--use_gpu=
0
'
,
'--dot_period=10'
,
'--log_period=100'
)
api
.
initPaddle
(
'--use_gpu=
1
'
,
'--dot_period=10'
,
'--log_period=100'
)
gen_conf
=
parse_config
(
"gan_conf_image.py"
,
"mode=generator_training"
)
dis_conf
=
parse_config
(
"gan_conf_image.py"
,
"mode=discriminator_training"
)
generator_conf
=
parse_config
(
"gan_conf_image.py"
,
"mode=generator"
)
...
...
@@ -252,10 +261,7 @@ def main():
fake_samples
=
get_fake_samples
(
generator_machine
,
batch_size
,
noise
)
save_dir
=
"./pass_"
+
str
(
train_pass
)
if
not
os
.
path
.
exists
(
save_dir
):
os
.
makedirs
(
save_dir
)
saveImages
(
fake_samples
,
save_dir
)
saveImages
(
fake_samples
,
"train_pass%s.png"
%
train_pass
)
dis_trainer
.
finishTrain
()
gen_trainer
.
finishTrain
()
...
...
paddle/api/Paddle.swig
浏览文件 @
3ebf6aaf
...
...
@@ -194,4 +194,3 @@ namespace std {
%ignore ParameterTraverseCallbackPrivate;
%include "utils/GlobalConstants.h"
%include "api/PaddleAPI.h"
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录