Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
X2Paddle
提交
73830eb2
X
X2Paddle
项目概览
PaddlePaddle
/
X2Paddle
1 年多 前同步成功
通知
328
Star
698
Fork
167
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
26
列表
看板
标记
里程碑
合并请求
4
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
X
X2Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
26
Issue
26
列表
看板
标记
里程碑
合并请求
4
合并请求
4
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
73830eb2
编写于
6月 30, 2020
作者:
J
jiangjiajun
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
modify code format
上级
0edcc783
变更
21
隐藏空白更改
内联
并排
Showing
21 changed file
with
9494 addition
and
9323 deletion
+9494
-9323
tools/merge_params.py
tools/merge_params.py
+9
-7
x2paddle/core/fluid_code.py
x2paddle/core/fluid_code.py
+4
-4
x2paddle/core/op_mapper.py
x2paddle/core/op_mapper.py
+21
-22
x2paddle/decoder/caffe_decoder.py
x2paddle/decoder/caffe_decoder.py
+10
-10
x2paddle/decoder/caffe_pb2.py
x2paddle/decoder/caffe_pb2.py
+9043
-8751
x2paddle/decoder/onnx_decoder.py
x2paddle/decoder/onnx_decoder.py
+4
-6
x2paddle/decoder/tf_decoder.py
x2paddle/decoder/tf_decoder.py
+8
-8
x2paddle/op_mapper/caffe_custom_layer/convolutiondepthwise.py
...ddle/op_mapper/caffe_custom_layer/convolutiondepthwise.py
+18
-16
x2paddle/op_mapper/caffe_custom_layer/detectionoutput.py
x2paddle/op_mapper/caffe_custom_layer/detectionoutput.py
+7
-6
x2paddle/op_mapper/caffe_custom_layer/normalize.py
x2paddle/op_mapper/caffe_custom_layer/normalize.py
+7
-7
x2paddle/op_mapper/caffe_custom_layer/permute.py
x2paddle/op_mapper/caffe_custom_layer/permute.py
+5
-4
x2paddle/op_mapper/caffe_custom_layer/priorbox.py
x2paddle/op_mapper/caffe_custom_layer/priorbox.py
+18
-16
x2paddle/op_mapper/caffe_custom_layer/roipooling.py
x2paddle/op_mapper/caffe_custom_layer/roipooling.py
+11
-9
x2paddle/op_mapper/caffe_custom_layer/select.py
x2paddle/op_mapper/caffe_custom_layer/select.py
+11
-9
x2paddle/op_mapper/caffe_custom_layer/shufflechannel.py
x2paddle/op_mapper/caffe_custom_layer/shufflechannel.py
+5
-4
x2paddle/op_mapper/caffe_op_mapper.py
x2paddle/op_mapper/caffe_op_mapper.py
+145
-182
x2paddle/op_mapper/caffe_shape.py
x2paddle/op_mapper/caffe_shape.py
+4
-4
x2paddle/op_mapper/onnx_custom_layer/InstanceNormalization.py
...ddle/op_mapper/onnx_custom_layer/InstanceNormalization.py
+12
-14
x2paddle/op_mapper/tf_op_mapper.py
x2paddle/op_mapper/tf_op_mapper.py
+120
-216
x2paddle/optimizer/caffe_optimizer.py
x2paddle/optimizer/caffe_optimizer.py
+10
-8
x2paddle/optimizer/tf_optimizer.py
x2paddle/optimizer/tf_optimizer.py
+22
-20
未找到文件。
tools/merge_params.py
浏览文件 @
73830eb2
...
...
@@ -5,12 +5,14 @@ model_dir = sys.argv[1]
new_model_dir
=
sys
.
argv
[
2
]
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
())
[
inference_program
,
feed_target_names
,
fetch_targets
]
=
fluid
.
io
.
load_inference_model
(
dirname
=
model_dir
,
executor
=
exe
)
fetch_targets
]
=
fluid
.
io
.
load_inference_model
(
dirname
=
model_dir
,
executor
=
exe
)
print
(
feed_target_names
)
fluid
.
io
.
save_inference_model
(
dirname
=
new_model_dir
,
feeded_var_names
=
feed_target_names
,
target_vars
=
fetch_targets
,
executor
=
exe
,
main_program
=
inference_program
,
params_filename
=
"__params__"
)
fluid
.
io
.
save_inference_model
(
dirname
=
new_model_dir
,
feeded_var_names
=
feed_target_names
,
target_vars
=
fetch_targets
,
executor
=
exe
,
main_program
=
inference_program
,
params_filename
=
"__params__"
)
x2paddle/core/fluid_code.py
浏览文件 @
73830eb2
...
...
@@ -46,8 +46,8 @@ class Layer(object):
for
input
in
self
.
inputs
:
if
isinstance
(
input
,
GraphNode
):
if
hasattr
(
input
,
"index"
):
in_list
+=
(
input
.
layer_name
+
"[{}]"
.
format
(
input
.
index
)
+
", "
)
in_list
+=
(
input
.
layer_name
+
"[{}]"
.
format
(
input
.
index
)
+
", "
)
else
:
in_list
+=
(
input
.
layer_name
+
", "
)
elif
isinstance
(
input
,
six
.
string_types
):
...
...
@@ -71,8 +71,8 @@ class Layer(object):
layer_code
=
layer_code
+
key
+
"={}, "
.
format
(
input
)
elif
isinstance
(
self
.
inputs
,
GraphNode
):
if
hasattr
(
self
.
inputs
,
"index"
):
layer_code
+=
(
self
.
inputs
.
layer_name
+
"[{}]"
.
format
(
self
.
inputs
.
index
))
layer_code
+=
(
self
.
inputs
.
layer_name
+
"[{}]"
.
format
(
self
.
inputs
.
index
))
else
:
layer_code
+=
(
self
.
inputs
.
layer_name
)
if
self
.
op
!=
"="
:
...
...
x2paddle/core/op_mapper.py
浏览文件 @
73830eb2
...
...
@@ -64,10 +64,8 @@ def run_net(param_dir="./"):
b
=
os
.
path
.
exists
(
os
.
path
.
join
(
param_dir
,
var
.
name
))
return
b
fluid
.
io
.
load_vars
(
exe
,
param_dir
,
fluid
.
default_main_program
(),
predicate
=
if_exist
)
fluid
.
io
.
load_vars
(
exe
,
param_dir
,
fluid
.
default_main_program
(),
predicate
=
if_exist
)
class
OpMapper
(
object
):
...
...
@@ -98,8 +96,8 @@ class OpMapper(object):
def
add_codes
(
self
,
codes
,
indent
=
0
):
if
isinstance
(
codes
,
list
):
for
code
in
codes
:
self
.
paddle_codes
+=
(
self
.
tab
*
indent
+
code
.
strip
(
'
\n
'
)
+
'
\n
'
)
self
.
paddle_codes
+=
(
self
.
tab
*
indent
+
code
.
strip
(
'
\n
'
)
+
'
\n
'
)
elif
isinstance
(
codes
,
str
):
self
.
paddle_codes
+=
(
self
.
tab
*
indent
+
codes
.
strip
(
'
\n
'
)
+
'
\n
'
)
else
:
...
...
@@ -135,24 +133,25 @@ class OpMapper(object):
os
.
path
.
join
(
os
.
path
.
join
(
py_code_dir
,
var
.
name
)))
return
b
fluid
.
io
.
load_vars
(
exe
,
py_code_dir
,
fluid
.
default_main_program
(),
predicate
=
if_exist
)
fluid
.
io
.
load_vars
(
exe
,
py_code_dir
,
fluid
.
default_main_program
(),
predicate
=
if_exist
)
if
params_merge
:
fluid
.
io
.
save_inference_model
(
dirname
=
os
.
path
.
join
(
save_dir
,
"inference_model"
),
feeded_var_names
=
input_names
,
target_vars
=
outputs
,
executor
=
exe
,
params_filename
=
"__params__"
)
fluid
.
io
.
save_inference_model
(
dirname
=
os
.
path
.
join
(
save_dir
,
"inference_model"
),
feeded_var_names
=
input_names
,
target_vars
=
outputs
,
executor
=
exe
,
params_filename
=
"__params__"
)
else
:
fluid
.
io
.
save_inference_model
(
dirname
=
os
.
path
.
join
(
save_dir
,
"inference_model"
),
feeded_var_names
=
input_names
,
target_vars
=
outputs
,
executor
=
exe
,
params_filename
=
None
)
fluid
.
io
.
save_inference_model
(
dirname
=
os
.
path
.
join
(
save_dir
,
"inference_model"
),
feeded_var_names
=
input_names
,
target_vars
=
outputs
,
executor
=
exe
,
params_filename
=
None
)
except
:
raise
Exception
(
"Paddle code was saved in {}/model.py, but seems there's wrong exist, please check model.py manually."
...
...
x2paddle/decoder/caffe_decoder.py
浏览文件 @
73830eb2
...
...
@@ -34,8 +34,8 @@ class CaffeResolver(object):
if
not
os
.
path
.
isfile
(
self
.
caffe_proto
):
raise
Exception
(
"The .py file compiled by caffe.proto is not exist."
)
(
filepath
,
tempfilename
)
=
os
.
path
.
split
(
os
.
path
.
abspath
(
self
.
caffe_proto
))
(
filepath
,
tempfilename
)
=
os
.
path
.
split
(
os
.
path
.
abspath
(
self
.
caffe_proto
))
(
filename
,
extension
)
=
os
.
path
.
splitext
(
tempfilename
)
sys
.
path
.
append
(
filepath
)
out
=
__import__
(
filename
)
...
...
@@ -49,13 +49,13 @@ class CaffeResolver(object):
class
CaffeGraphNode
(
GraphNode
):
def
__init__
(
self
,
layer
,
type_str
,
layer_name
=
None
):
if
layer_name
is
None
:
super
(
CaffeGraphNode
,
self
).
__init__
(
layer
,
layer
.
name
.
replace
(
'/'
,
'_'
).
replace
(
'-'
,
'_'
))
super
(
CaffeGraphNode
,
self
).
__init__
(
layer
,
layer
.
name
.
replace
(
'/'
,
'_'
).
replace
(
'-'
,
'_'
))
else
:
super
(
CaffeGraphNode
,
self
).
__init__
(
layer
,
layer_name
.
replace
(
'/'
,
'_'
).
replace
(
'-'
,
'_'
))
super
(
CaffeGraphNode
,
self
).
__init__
(
layer
,
layer_name
.
replace
(
'/'
,
'_'
).
replace
(
'-'
,
'_'
))
self
.
layer_type
=
type_str
self
.
fluid_code
=
FluidCode
()
self
.
data
=
None
...
...
@@ -268,8 +268,8 @@ class CaffeDecoder(object):
c_i
=
blob
.
channels
h
=
blob
.
height
w
=
blob
.
width
data
=
np
.
asarray
(
list
(
blob
.
data
),
dtype
=
np
.
float32
).
reshape
(
c_o
,
c_i
,
h
,
w
)
data
=
np
.
asarray
(
list
(
blob
.
data
),
dtype
=
np
.
float32
).
reshape
(
c_o
,
c_i
,
h
,
w
)
transformed
.
append
(
data
)
return
transformed
x2paddle/decoder/caffe_pb2.py
浏览文件 @
73830eb2
因为 它太大了无法显示 source diff 。你可以改为
查看blob
。
x2paddle/decoder/onnx_decoder.py
浏览文件 @
73830eb2
...
...
@@ -71,9 +71,8 @@ class ONNXGraphNode(GraphNode):
if
attr
.
type
==
onnx
.
AttributeProto
.
TENSOR
:
dtype
=
np
.
dtype
(
TENSOR_TYPE_TO_NP_TYPE
[
attr
.
t
.
data_type
])
data
=
attr
.
t
.
raw_data
value
=
np
.
frombuffer
(
data
,
dtype
=
dtype
,
count
=
(
len
(
data
)
//
dtype
.
itemsize
))
value
=
np
.
frombuffer
(
data
,
dtype
=
dtype
,
count
=
(
len
(
data
)
//
dtype
.
itemsize
))
elif
attr
.
type
==
onnx
.
AttributeProto
.
STRING
:
value
=
attr
.
s
value
=
value
.
decode
()
if
isinstance
(
value
,
bytes
)
else
value
...
...
@@ -205,9 +204,8 @@ class ONNXGraph(Graph):
self
.
node_map
[
name
].
weight
=
weight
self
.
node_map
[
name
].
embeded_as
=
[]
else
:
self
.
node_map
[
name
]
=
ONNXGraphDataNode
(
initializer
,
layer_name
=
name
,
is_global_input
=
False
)
self
.
node_map
[
name
]
=
ONNXGraphDataNode
(
initializer
,
layer_name
=
name
,
is_global_input
=
False
)
self
.
node_map
[
name
].
weight
=
weight
self
.
node_map
[
name
].
embeded_as
=
[]
...
...
x2paddle/decoder/tf_decoder.py
浏览文件 @
73830eb2
...
...
@@ -120,13 +120,13 @@ class TFGraph(Graph):
def
build
(
self
):
for
layer
in
self
.
model
.
node
:
self
.
node_map
[
layer
.
name
.
replace
(
'/'
,
'_'
).
replace
(
'-'
,
'_'
)]
=
TFGraphNode
(
layer
,
data_format
=
self
.
tf_data_format
)
'-'
,
'_'
)]
=
TFGraphNode
(
layer
,
data_format
=
self
.
tf_data_format
)
for
layer_name
,
node
in
self
.
node_map
.
items
():
for
in_node
in
node
.
layer
.
input
:
in_node
=
in_node
.
replace
(
'/'
,
'_'
).
replace
(
'-'
,
'_'
).
replace
(
'^'
,
''
)
in_node
=
in_node
.
replace
(
'/'
,
'_'
).
replace
(
'-'
,
'_'
).
replace
(
'^'
,
''
)
if
in_node
not
in
self
.
node_map
:
if
in_node
.
strip
().
split
(
':'
)[
0
]
in
self
.
node_map
:
self
.
connect
(
in_node
.
strip
().
split
(
':'
)[
0
],
layer_name
)
...
...
@@ -390,10 +390,10 @@ class TFDecoder(object):
shape
=
shape
,
name
=
"x2paddle_{}"
.
format
(
layer
.
name
))
except
:
x2paddle_input
=
tf
.
placeholder
(
dtype
=
dtype
,
shape
=
sha
pe
,
name
=
"x2paddle_{}"
.
format
(
layer
.
name
))
x2paddle_input
=
tf
.
placeholder
(
dtype
=
dty
pe
,
shape
=
shape
,
name
=
"x2paddle_{}"
.
format
(
layer
.
name
))
input_map
[
"{}:0"
.
format
(
layer
.
name
)]
=
x2paddle_input
if
shape
.
count
(
None
)
>
0
:
...
...
x2paddle/op_mapper/caffe_custom_layer/convolutiondepthwise.py
浏览文件 @
73830eb2
...
...
@@ -120,18 +120,19 @@ def convolutiondepthwise_layer(inputs,
dila_len
)
c_in
=
input_shape
[
0
][
1
]
c_out
=
num_output
if
num_output
is
not
None
else
input_shape
[
0
][
1
]
group
=
int
(
c_in
/
(
c_in
/
c_out
))
if
c_in
>
c_out
else
int
(
c_in
/
(
c_out
/
c_in
))
out
=
fluid
.
layers
.
conv2d
(
input
,
dilation
=
[
dila_h
,
dila_w
],
filter_size
=
[
k_h
,
k_w
],
stride
=
[
s_h
,
s_w
],
padding
=
[
p_h
,
p_w
],
groups
=
group
,
num_filters
=
c_out
,
param_attr
=
name
+
'_weights'
,
bias_attr
=
name
+
'_bias'
,
name
=
name
)
group
=
int
(
c_in
/
(
c_in
/
c_out
))
if
c_in
>
c_out
else
int
(
c_in
/
(
c_out
/
c_in
))
out
=
fluid
.
layers
.
conv2d
(
input
,
dilation
=
[
dila_h
,
dila_w
],
filter_size
=
[
k_h
,
k_w
],
stride
=
[
s_h
,
s_w
],
padding
=
[
p_h
,
p_w
],
groups
=
group
,
num_filters
=
c_out
,
param_attr
=
name
+
'_weights'
,
bias_attr
=
name
+
'_bias'
,
name
=
name
)
return
out
...
...
@@ -142,7 +143,8 @@ def convolutiondepthwise_weights(name, data=None):
return
weights_name
register
(
kind
=
'ConvolutionDepthwise'
,
shape
=
convolutiondepthwise_shape
,
layer
=
convolutiondepthwise_layer
,
weights
=
convolutiondepthwise_weights
)
register
(
kind
=
'ConvolutionDepthwise'
,
shape
=
convolutiondepthwise_shape
,
layer
=
convolutiondepthwise_layer
,
weights
=
convolutiondepthwise_weights
)
x2paddle/op_mapper/caffe_custom_layer/detectionoutput.py
浏览文件 @
73830eb2
...
...
@@ -37,8 +37,8 @@ def detectionoutput_layer(inputs,
pbv
=
fluid
.
layers
.
reshape
(
x
=
pbv
,
shape
=
[
-
1
,
4
])
mbox_loc
=
inputs
[
0
]
mbox_loc
=
fluid
.
layers
.
reshape
(
x
=
mbox_loc
,
shape
=
[
-
1
,
pb
.
shape
[
0
],
4
])
mbox_conf_flatten
=
fluid
.
layers
.
reshape
(
x
=
mbox_conf_flatten
,
shape
=
[
0
,
pb
.
shape
[
0
],
-
1
])
mbox_conf_flatten
=
fluid
.
layers
.
reshape
(
x
=
mbox_conf_flatten
,
shape
=
[
0
,
pb
.
shape
[
0
],
-
1
])
default
=
{
"nms_threshold"
:
0.3
,
"top_k"
:
10
,
"eta"
:
1.0
}
fields
=
[
'eta'
,
'top_k'
,
'nms_threshold'
]
...
...
@@ -64,7 +64,8 @@ def detectionoutput_weights(name, data=None):
return
weights_name
register
(
kind
=
'DetectionOutput'
,
shape
=
detectionoutput_shape
,
layer
=
detectionoutput_layer
,
weights
=
detectionoutput_weights
)
register
(
kind
=
'DetectionOutput'
,
shape
=
detectionoutput_shape
,
layer
=
detectionoutput_layer
,
weights
=
detectionoutput_weights
)
x2paddle/op_mapper/caffe_custom_layer/normalize.py
浏览文件 @
73830eb2
...
...
@@ -20,9 +20,8 @@ def normalize_layer(inputs,
attr
=
name
+
'_scale'
)
scale_param
=
fluid
.
layers
.
reshape
(
x
=
scale_param
,
\
shape
=
[
1
]
if
channel_shared
else
[
input_shape
[
0
][
1
]])
out
=
fluid
.
layers
.
elementwise_mul
(
x
=
l2_norm
,
y
=
scale_param
,
axis
=-
1
if
channel_shared
else
1
)
out
=
fluid
.
layers
.
elementwise_mul
(
x
=
l2_norm
,
y
=
scale_param
,
axis
=-
1
if
channel_shared
else
1
)
return
out
...
...
@@ -31,7 +30,8 @@ def normalize_weights(name, data=None):
return
weights_name
register
(
kind
=
'Normalize'
,
shape
=
normalize_shape
,
layer
=
normalize_layer
,
weights
=
normalize_weights
)
register
(
kind
=
'Normalize'
,
shape
=
normalize_shape
,
layer
=
normalize_layer
,
weights
=
normalize_weights
)
x2paddle/op_mapper/caffe_custom_layer/permute.py
浏览文件 @
73830eb2
...
...
@@ -23,7 +23,8 @@ def permute_weights(name, data=None):
return
weights_name
register
(
kind
=
'Permute'
,
shape
=
permute_shape
,
layer
=
permute_layer
,
weights
=
permute_weights
)
register
(
kind
=
'Permute'
,
shape
=
permute_shape
,
layer
=
permute_layer
,
weights
=
permute_weights
)
x2paddle/op_mapper/caffe_custom_layer/priorbox.py
浏览文件 @
73830eb2
...
...
@@ -30,18 +30,19 @@ def priorbox_layer(inputs,
steps
=
tuple
(
step
)
if
type
(
step
)
is
list
or
type
(
step
)
is
tuple
else
(
step
,
step
)
box
,
variance_
=
fluid
.
layers
.
prior_box
(
input
,
image
,
min_sizes
=
min_size
,
max_sizes
=
max_size
,
aspect_ratios
=
aspect_ratio
,
variance
=
variance
,
flip
=
flip
,
clip
=
clip
,
steps
=
steps
,
offset
=
offset
,
name
=
name
,
min_max_aspect_ratios_order
=
True
)
box
,
variance_
=
fluid
.
layers
.
prior_box
(
input
,
image
,
min_sizes
=
min_size
,
max_sizes
=
max_size
,
aspect_ratios
=
aspect_ratio
,
variance
=
variance
,
flip
=
flip
,
clip
=
clip
,
steps
=
steps
,
offset
=
offset
,
name
=
name
,
min_max_aspect_ratios_order
=
True
)
box
=
fluid
.
layers
.
reshape
(
box
,
[
1
,
1
,
-
1
])
variance_
=
fluid
.
layers
.
reshape
(
variance_
,
[
1
,
1
,
-
1
])
out
=
fluid
.
layers
.
concat
([
box
,
variance_
],
axis
=
1
)
...
...
@@ -53,7 +54,8 @@ def priorbox_weights(name, data=None):
return
weights_name
register
(
kind
=
'PriorBox'
,
shape
=
priorbox_shape
,
layer
=
priorbox_layer
,
weights
=
priorbox_weights
)
register
(
kind
=
'PriorBox'
,
shape
=
priorbox_shape
,
layer
=
priorbox_layer
,
weights
=
priorbox_weights
)
x2paddle/op_mapper/caffe_custom_layer/roipooling.py
浏览文件 @
73830eb2
...
...
@@ -21,11 +21,12 @@ def roipooling_layer(inputs,
input
=
inputs
[
0
]
roi
=
inputs
[
1
]
roi
=
fluid
.
layers
.
slice
(
roi
,
axes
=
[
1
],
starts
=
[
1
],
ends
=
[
5
])
out
=
fluid
.
layers
.
roi_pool
(
input
,
roi
,
pooled_height
=
pooled_h
,
pooled_width
=
pooled_w
,
spatial_scale
=
spatial_scale
)
out
=
fluid
.
layers
.
roi_pool
(
input
,
roi
,
pooled_height
=
pooled_h
,
pooled_width
=
pooled_w
,
spatial_scale
=
spatial_scale
)
return
out
...
...
@@ -34,7 +35,8 @@ def roipooling_weights(name, data=None):
return
weights_name
register
(
kind
=
'ROIPooling'
,
shape
=
roipooling_shape
,
layer
=
roipooling_layer
,
weights
=
roipooling_weights
)
register
(
kind
=
'ROIPooling'
,
shape
=
roipooling_shape
,
layer
=
roipooling_layer
,
weights
=
roipooling_weights
)
x2paddle/op_mapper/caffe_custom_layer/select.py
浏览文件 @
73830eb2
...
...
@@ -30,11 +30,12 @@ def select_layer(inputs,
out
=
[]
for
i
in
range
(
len
(
slice_point
)):
out
.
append
(
fluid
.
layers
.
slice
(
input
,
axes
=
[
axis
],
starts
=
[
slice_point
[
i
]],
ends
=
[
slice_point
[
i
+
1
]],
name
=
name
+
'_'
+
str
(
i
)))
fluid
.
layers
.
slice
(
input
,
axes
=
[
axis
],
starts
=
[
slice_point
[
i
]],
ends
=
[
slice_point
[
i
+
1
]],
name
=
name
+
'_'
+
str
(
i
)))
if
i
==
len
(
slice_point
)
-
2
:
break
return
out
...
...
@@ -45,7 +46,8 @@ def select_weights(name, data=None):
return
weights_name
register
(
kind
=
'Select'
,
shape
=
select_shape
,
layer
=
select_layer
,
weights
=
select_weights
)
register
(
kind
=
'Select'
,
shape
=
select_shape
,
layer
=
select_layer
,
weights
=
select_weights
)
x2paddle/op_mapper/caffe_custom_layer/shufflechannel.py
浏览文件 @
73830eb2
...
...
@@ -17,7 +17,8 @@ def shufflechannel_weights(name, data=None):
return
weights_name
register
(
kind
=
'ShuffleChannel'
,
shape
=
shufflechannel_shape
,
layer
=
shufflechannel_layer
,
weights
=
shufflechannel_weights
)
register
(
kind
=
'ShuffleChannel'
,
shape
=
shufflechannel_shape
,
layer
=
shufflechannel_layer
,
weights
=
shufflechannel_weights
)
x2paddle/op_mapper/caffe_op_mapper.py
浏览文件 @
73830eb2
...
...
@@ -195,10 +195,8 @@ class CaffeOpMapper(OpMapper):
'shape'
:
shape
,
'name'
:
string
(
node
.
layer_name
)
}
node
.
fluid_code
.
add_layer
(
"data"
,
inputs
=
None
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"data"
,
inputs
=
None
,
output
=
node
,
param_attr
=
attr
)
def
MemoryData
(
self
,
node
):
# TODO(syf): Paddlepaddle can't fully support
...
...
@@ -209,10 +207,8 @@ class CaffeOpMapper(OpMapper):
'shape'
:
shape
,
'name'
:
string
(
node
.
layer_name
)
}
node
.
fluid_code
.
add_layer
(
"data"
,
inputs
=
None
,
output
=
node
.
layer_name
+
'0'
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"data"
,
inputs
=
None
,
output
=
node
.
layer_name
+
'0'
,
param_attr
=
attr
)
node
.
fluid_code
.
add_note
(
'{} = [{}]'
.
format
(
node
.
layer_name
,
node
.
layer_name
+
'0'
))
...
...
@@ -263,10 +259,8 @@ class CaffeOpMapper(OpMapper):
'bias_attr'
:
False
if
len
(
data
)
==
1
else
string
(
node
.
layer_name
+
'_bias'
),
}
node
.
fluid_code
.
add_layer
(
"conv2d"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"conv2d"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
Deconvolution
(
self
,
node
):
data
=
node
.
data
...
...
@@ -316,10 +310,8 @@ class CaffeOpMapper(OpMapper):
'bias_attr'
:
False
if
len
(
data
)
==
1
else
string
(
node
.
layer_name
+
'_bias'
)
}
node
.
fluid_code
.
add_layer
(
"conv2d_transpose"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"conv2d_transpose"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
Pooling
(
self
,
node
):
params
=
node
.
layer
.
pooling_param
...
...
@@ -345,10 +337,8 @@ class CaffeOpMapper(OpMapper):
'global_pooling'
:
global_pool
,
'name'
:
string
(
node
.
layer_name
)
}
node
.
fluid_code
.
add_layer
(
"pool2d"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"pool2d"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
LRN
(
self
,
node
):
assert
len
(
node
.
inputs
)
==
1
,
'The count of LRN node
\'
s input is not 1.'
...
...
@@ -368,10 +358,8 @@ class CaffeOpMapper(OpMapper):
'beta'
:
params
.
beta
,
'name'
:
string
(
node
.
layer_name
)
}
node
.
fluid_code
.
add_layer
(
"lrn"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"lrn"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
InnerProduct
(
self
,
node
):
data
=
node
.
data
...
...
@@ -420,10 +408,8 @@ class CaffeOpMapper(OpMapper):
'bias_attr'
:
False
if
len
(
data
)
==
1
else
string
(
node
.
layer_name
+
'_bias'
)
}
node
.
fluid_code
.
add_layer
(
"fc"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"fc"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
Softmax
(
self
,
node
):
assert
len
(
...
...
@@ -435,10 +421,8 @@ class CaffeOpMapper(OpMapper):
dims
=
len
(
shape
)
axis
=
axis
+
dims
if
axis
<
0
else
axis
attr
=
{
'axis'
:
axis
,
'name'
:
string
(
node
.
layer_name
+
'_softmax'
)}
node
.
fluid_code
.
add_layer
(
"softmax"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"softmax"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
Slice
(
self
,
node
):
assert
len
(
...
...
@@ -459,10 +443,8 @@ class CaffeOpMapper(OpMapper):
'dim'
:
axis
,
'name'
:
string
(
node
.
layer_name
)
}
node
.
fluid_code
.
add_layer
(
"split"
,
inputs
=
input
,
output
=
node
.
layer_name
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"split"
,
inputs
=
input
,
output
=
node
.
layer_name
,
param_attr
=
attr
)
def
Concat
(
self
,
node
):
assert
len
(
...
...
@@ -475,10 +457,8 @@ class CaffeOpMapper(OpMapper):
params
=
node
.
layer
.
concat_param
axis
=
params
.
axis
attr
=
{
'axis'
:
axis
,
'name'
:
string
(
node
.
layer_name
)}
node
.
fluid_code
.
add_layer
(
"concat"
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"concat"
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
attr
)
def
PReLU
(
self
,
node
):
assert
len
(
...
...
@@ -499,10 +479,8 @@ class CaffeOpMapper(OpMapper):
'param_attr'
:
string
(
node
.
layer_name
+
'_weights'
),
'name'
:
string
(
node
.
layer_name
)
}
node
.
fluid_code
.
add_layer
(
"prelu"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"prelu"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
Accuracy
(
self
,
node
):
assert
len
(
...
...
@@ -526,10 +504,8 @@ class CaffeOpMapper(OpMapper):
assert
axis
==
1
,
'PaddlePaddle can not support the situation when the axis is not 1.'
assert
not
ignore_label
>=
0
,
'PaddlePaddle can not support the situation when the model has ignore label.'
attr
=
{
'k'
:
top_k
}
node
.
fluid_code
.
add_layer
(
"accuracy"
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"accuracy"
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
attr
)
def
Eltwise
(
self
,
node
):
assert
len
(
...
...
@@ -546,10 +522,11 @@ class CaffeOpMapper(OpMapper):
inputs_dict
[
'x'
]
=
inputs
[
0
]
inputs_dict
[
'y'
]
=
inputs
[
1
]
attr
=
{
'act'
:
None
,
'name'
:
string
(
node
.
layer_name
)}
node
.
fluid_code
.
add_layer
(
"elementwise_mul"
,
inputs
=
inputs_dict
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"elementwise_mul"
,
inputs
=
inputs_dict
,
output
=
node
,
param_attr
=
attr
)
elif
mode
==
1
:
if
hasattr
(
params
,
'coeff'
)
and
len
(
params
.
coeff
)
==
2
:
coeff
=
params
.
coeff
...
...
@@ -559,57 +536,62 @@ class CaffeOpMapper(OpMapper):
'value'
:
coeff
[
0
],
'dtype'
:
'{}.dtype'
.
format
(
input1_name
)
}
node
.
fluid_code
.
add_layer
(
"fill_constant"
,
inputs
=
None
,
output
=
node
.
layer_name
+
'_const1'
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"fill_constant"
,
inputs
=
None
,
output
=
node
.
layer_name
+
'_const1'
,
param_attr
=
attr
)
attr
=
{
'act'
:
None
,
'name'
:
string
(
node
.
layer_name
+
'_mul1'
)}
node
.
fluid_code
.
add_layer
(
"elementwise_mul"
,
inputs
=
input1_name
+
', '
+
node
.
layer_name
+
'_const1'
,
output
=
node
.
layer_name
+
'_mul1'
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"elementwise_mul"
,
inputs
=
input1_name
+
', '
+
node
.
layer_name
+
'_const1'
,
output
=
node
.
layer_name
+
'_mul1'
,
param_attr
=
attr
)
input2_name
=
self
.
get_input_name
(
inputs
[
1
])
attr
=
{
'shape'
:
[
1
],
'value'
:
coeff
[
1
],
'dtype'
:
'{}.dtype'
.
format
(
input2_name
)
}
node
.
fluid_code
.
add_layer
(
"fill_constant"
,
inputs
=
None
,
output
=
node
.
layer_name
+
'_const2'
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"fill_constant"
,
inputs
=
None
,
output
=
node
.
layer_name
+
'_const2'
,
param_attr
=
attr
)
attr
=
{
'act'
:
None
,
'name'
:
string
(
node
.
layer_name
+
'_mul2'
)}
node
.
fluid_code
.
add_layer
(
"elementwise_mul"
,
inputs
=
input2_name
+
', '
+
node
.
layer_name
+
'_const2'
,
output
=
node
.
layer_name
+
'_mul2'
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"elementwise_mul"
,
inputs
=
input2_name
+
', '
+
node
.
layer_name
+
'_const2'
,
output
=
node
.
layer_name
+
'_mul2'
,
param_attr
=
attr
)
attr
=
{
'act'
:
None
,
'name'
:
string
(
node
.
layer_name
)}
node
.
fluid_code
.
add_layer
(
"elementwise_add"
,
inputs
=
'{}_mul1, {}_mul2'
.
format
(
node
.
layer_name
,
node
.
layer_name
),
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"elementwise_add"
,
inputs
=
'{}_mul1, {}_mul2'
.
format
(
node
.
layer_name
,
node
.
layer_name
),
output
=
node
,
param_attr
=
attr
)
else
:
inputs_dict
=
{}
inputs_dict
[
'x'
]
=
inputs
[
0
]
inputs_dict
[
'y'
]
=
inputs
[
1
]
attr
=
{
'act'
:
None
,
'name'
:
string
(
node
.
layer_name
)}
node
.
fluid_code
.
add_layer
(
"elementwise_add"
,
inputs
=
inputs_dict
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"elementwise_add"
,
inputs
=
inputs_dict
,
output
=
node
,
param_attr
=
attr
)
else
:
inputs_dict
=
{}
inputs_dict
[
'x'
]
=
inputs
[
0
]
inputs_dict
[
'y'
]
=
inputs
[
1
]
attr
=
{
'act'
:
None
,
'name'
:
string
(
node
.
layer_name
)}
node
.
fluid_code
.
add_layer
(
"elementwise_max"
,
inputs
=
inputs_dict
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"elementwise_max"
,
inputs
=
inputs_dict
,
output
=
node
,
param_attr
=
attr
)
def
BatchNorm
(
self
,
node
):
assert
len
(
...
...
@@ -651,10 +633,8 @@ class CaffeOpMapper(OpMapper):
'epsilon'
:
eps
,
'name'
:
string
(
node
.
layer_name
)
}
node
.
fluid_code
.
add_layer
(
"batch_norm"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"batch_norm"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
Scale
(
self
,
node
):
if
node
.
data
is
None
:
...
...
@@ -687,10 +667,11 @@ class CaffeOpMapper(OpMapper):
inputs_dict
[
'x'
]
=
input0
inputs_dict
[
'y'
]
=
input1
attr
=
{
'axis'
:
axis
,
'name'
:
string
(
node
.
layer_name
+
'_mul'
)}
node
.
fluid_code
.
add_layer
(
"elementwise_mul"
,
inputs
=
inputs_dict
,
output
=
node
.
layer_name
+
'_mul'
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"elementwise_mul"
,
inputs
=
inputs_dict
,
output
=
node
.
layer_name
+
'_mul'
,
param_attr
=
attr
)
else
:
bias_shape
=
node
.
input_shape
[
0
][
axis
:
axis
+
num_axes
]
input0
=
self
.
graph
.
get_bottom_node
(
node
,
idx
=
0
,
copy
=
True
)
...
...
@@ -703,18 +684,17 @@ class CaffeOpMapper(OpMapper):
'is_bias'
:
True
,
'default_initializer'
:
'Constant(value=1.0)'
}
node
.
fluid_code
.
add_layer
(
"create_parameter"
,
inputs
=
None
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"create_parameter"
,
inputs
=
None
,
output
=
node
,
param_attr
=
attr
)
inputs_dict
=
{}
inputs_dict
[
'x'
]
=
input0
inputs_dict
[
'y'
]
=
node
attr
=
{
'axis'
:
axis
,
'name'
:
string
(
node
.
layer_name
+
'_mul'
)}
node
.
fluid_code
.
add_layer
(
"elementwise_mul"
,
inputs
=
inputs_dict
,
output
=
node
.
layer_name
+
'_mul'
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"elementwise_mul"
,
inputs
=
inputs_dict
,
output
=
node
.
layer_name
+
'_mul'
,
param_attr
=
attr
)
scale_shape
=
bias_shape
input0_name
=
self
.
get_input_name
(
input0
)
attr
=
{
...
...
@@ -725,16 +705,18 @@ class CaffeOpMapper(OpMapper):
'is_bias'
:
True
,
'default_initializer'
:
'Constant(value=1.0)'
}
node
.
fluid_code
.
add_layer
(
"create_parameter"
,
inputs
=
None
,
output
=
node
.
layer_name
+
'_offset_param'
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"create_parameter"
,
inputs
=
None
,
output
=
node
.
layer_name
+
'_offset_param'
,
param_attr
=
attr
)
attr
=
{
'axis'
:
axis
,
'name'
:
string
(
node
.
layer_name
+
'_add'
)}
node
.
fluid_code
.
add_layer
(
"elementwise_add"
,
inputs
=
'{}_mul, {}_offset_param'
.
format
(
node
.
layer_name
,
node
.
layer_name
),
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"elementwise_add"
,
inputs
=
'{}_mul, {}_offset_param'
.
format
(
node
.
layer_name
,
node
.
layer_name
),
output
=
node
,
param_attr
=
attr
)
def
Reshape
(
self
,
node
):
input
=
self
.
graph
.
get_bottom_node
(
node
,
idx
=
0
,
copy
=
True
)
...
...
@@ -747,10 +729,8 @@ class CaffeOpMapper(OpMapper):
'act'
:
None
,
'name'
:
string
(
node
.
layer_name
)
}
node
.
fluid_code
.
add_layer
(
"reshape"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"reshape"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
ArgMax
(
self
,
node
):
assert
len
(
node
.
inputs
)
==
1
and
len
(
...
...
@@ -767,11 +747,12 @@ class CaffeOpMapper(OpMapper):
axis
+=
len
(
input_shape
)
if
out_max_val
is
True
:
attr
=
{
'k'
:
top_k
,
'name'
:
string
(
node
.
layer_name
+
'_topk'
)}
node
.
fluid_code
.
add_layer
(
"topk"
,
inputs
=
input
,
output
=
'{}_topk_var, {}_index_var'
.
format
(
node
.
layer_name
,
node
.
layer_name
),
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"topk"
,
inputs
=
input
,
output
=
'{}_topk_var, {}_index_var'
.
format
(
node
.
layer_name
,
node
.
layer_name
),
param_attr
=
attr
)
attr
=
{
'dtype'
:
'{}_topk_var.dtype'
.
format
(
node
.
layer_name
)}
node
.
fluid_code
.
add_layer
(
"cast"
,
...
...
@@ -779,17 +760,19 @@ class CaffeOpMapper(OpMapper):
output
=
'{}_index_var'
.
format
(
node
.
layer_name
),
param_attr
=
attr
)
attr
=
{
'axis'
:
axis
,
'name'
:
string
(
node
.
layer_name
)}
node
.
fluid_code
.
add_layer
(
"concat"
,
inputs
=
'{}_topk_var, {}_index_var'
.
format
(
node
.
layer_name
,
node
.
layer_name
),
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"concat"
,
inputs
=
'{}_topk_var, {}_index_var'
.
format
(
node
.
layer_name
,
node
.
layer_name
),
output
=
node
,
param_attr
=
attr
)
else
:
attr
=
{
'k'
:
top_k
,
'name'
:
string
(
node
.
layer_name
)}
node
.
fluid_code
.
add_layer
(
"topk"
,
inputs
=
input
,
output
=
'_, {}'
.
format
(
node
.
layer_name
),
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"topk"
,
inputs
=
input
,
output
=
'_, {}'
.
format
(
node
.
layer_name
),
param_attr
=
attr
)
def
Crop
(
self
,
node
):
assert
len
(
...
...
@@ -808,13 +791,14 @@ class CaffeOpMapper(OpMapper):
offset
),
"invalid offset[%s] in crop layer"
%
(
str
(
offset
))
offset_real
=
[
0
]
*
axis
+
offset
attr
=
{
'offsets'
:
list
(
offset_real
),
'name'
:
string
(
node
.
layer_name
)}
node
.
fluid_code
.
add_layer
(
"crop"
,
inputs
=
{
'x'
:
input
,
'shape'
:
node
.
input_shape
[
1
]
},
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"crop"
,
inputs
=
{
'x'
:
input
,
'shape'
:
node
.
input_shape
[
1
]
},
output
=
node
,
param_attr
=
attr
)
def
Flatten
(
self
,
node
):
assert
len
(
...
...
@@ -823,10 +807,8 @@ class CaffeOpMapper(OpMapper):
input
=
self
.
graph
.
get_bottom_node
(
node
,
idx
=
0
,
copy
=
True
)
shape
=
node
.
output_shape
[
0
]
attr
=
{
'shape'
:
shape
,
'name'
:
string
(
node
.
layer_name
)}
node
.
fluid_code
.
add_layer
(
"reshape"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"reshape"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
Power
(
self
,
node
):
assert
len
(
...
...
@@ -842,15 +824,11 @@ class CaffeOpMapper(OpMapper):
'bias_after_scale'
:
True
,
'name'
:
string
(
node
.
layer_name
+
'_scale'
)
}
node
.
fluid_code
.
add_layer
(
"scale"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"scale"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
attr
=
{
'factor'
:
power
,
'name'
:
string
(
node
.
layer_name
)}
node
.
fluid_code
.
add_layer
(
"pow"
,
inputs
=
node
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"pow"
,
inputs
=
node
,
output
=
node
,
param_attr
=
attr
)
def
Reduction
(
self
,
node
):
assert
len
(
...
...
@@ -872,55 +850,41 @@ class CaffeOpMapper(OpMapper):
'keep_dim'
:
False
,
'name'
:
string
(
node
.
layer_name
)
}
node
.
fluid_code
.
add_layer
(
"reduce_sum"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"reduce_sum"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
elif
operation
==
2
:
## operation = ASUM
attr
=
{
'name'
:
string
(
node
.
layer_name
+
'_abs'
)}
node
.
fluid_code
.
add_layer
(
"abs"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"abs"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
attr
=
{
'dim'
:
dim
[
axis
:],
'keep_dim'
:
False
,
'name'
:
string
(
node
.
layer_name
)
}
node
.
fluid_code
.
add_layer
(
"reduce_sum"
,
inputs
=
node
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"reduce_sum"
,
inputs
=
node
,
output
=
node
,
param_attr
=
attr
)
elif
operation
==
3
:
## operation = SUMSQ
attr
=
{
'factor'
:
2.0
,
'name'
:
string
(
node
.
layer_name
+
'_pow'
)}
node
.
fluid_code
.
add_layer
(
"pow"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"pow"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
attr
=
{
'dim'
:
dim
[
axis
:],
'keep_dim'
:
False
,
'name'
:
string
(
node
.
layer_name
)
}
node
.
fluid_code
.
add_layer
(
"reduce_sum"
,
inputs
=
node
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"reduce_sum"
,
inputs
=
node
,
output
=
node
,
param_attr
=
attr
)
else
:
## operation = MEAN
attr
=
{
'dim'
:
dim
[
axis
:],
'keep_dim'
:
False
,
'name'
:
string
(
node
.
layer_name
)
}
node
.
fluid_code
.
add_layer
(
"reduce_mean"
,
inputs
=
node
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"reduce_mean"
,
inputs
=
node
,
output
=
node
,
param_attr
=
attr
)
attr
=
{
'scale'
:
coeff
}
node
.
fluid_code
.
add_layer
(
"scale"
,
inputs
=
node
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"scale"
,
inputs
=
node
,
output
=
node
,
param_attr
=
attr
)
def
deal_custom_layer
(
self
,
node
):
op
=
node
.
layer_type
...
...
@@ -947,11 +911,12 @@ class CaffeOpMapper(OpMapper):
assert
input
is
not
None
,
'This kind of DetectionOutput is not supported!'
input
=
self
.
graph
.
get_bottom_node
(
input
,
idx
=
0
,
copy
=
True
)
inputs_node
.
append
(
input
)
node
.
fluid_code
.
add_layer
(
func
.
__code__
.
co_name
,
inputs
=
inputs_node
,
output
=
node
,
param_attr
=
kwargs
,
is_custom_layer
=
True
)
node
.
fluid_code
.
add_layer
(
func
.
__code__
.
co_name
,
inputs
=
inputs_node
,
output
=
node
,
param_attr
=
kwargs
,
is_custom_layer
=
True
)
if
op
not
in
self
.
used_custom_layers
:
self
.
used_custom_layers
[
op
]
=
custom_code
...
...
@@ -960,7 +925,5 @@ class CaffeOpMapper(OpMapper):
op_info
=
self
.
directly_map_ops
[
node
.
layer_type
]
input
=
self
.
graph
.
get_bottom_node
(
node
,
idx
=
0
,
copy
=
True
)
attr
=
{
'name'
:
string
(
node
.
layer_name
)}
node
.
fluid_code
.
add_layer
(
op_info
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
op_info
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
x2paddle/op_mapper/caffe_shape.py
浏览文件 @
73830eb2
...
...
@@ -67,10 +67,10 @@ def get_strided_kernel_output_shape(params, input_shape, round_func):
i_w
=
input_shape
[
3
]
dila_h
,
dila_w
,
pad_h
,
pad_w
,
kernel_h
,
kernel_w
,
stride_h
,
stride_w
=
get_kernel_parameters
(
params
)
o_h
=
(
i_h
+
2
*
pad_h
-
(
dila_h
*
(
kernel_h
-
1
)
+
1
))
/
float
(
stride_h
)
+
1
o_w
=
(
i_w
+
2
*
pad_w
-
(
dila_w
*
(
kernel_w
-
1
)
+
1
))
/
float
(
stride_w
)
+
1
o_h
=
(
i_h
+
2
*
pad_h
-
(
dila_h
*
(
kernel_h
-
1
)
+
1
))
/
float
(
stride_h
)
+
1
o_w
=
(
i_w
+
2
*
pad_w
-
(
dila_w
*
(
kernel_w
-
1
)
+
1
))
/
float
(
stride_w
)
+
1
o_h
=
int
(
round_func
(
o_h
))
o_w
=
int
(
round_func
(
o_w
))
has_c_o
=
hasattr
(
params
,
'num_output'
)
...
...
x2paddle/op_mapper/onnx_custom_layer/InstanceNormalization.py
浏览文件 @
73830eb2
...
...
@@ -24,21 +24,18 @@ def InstanceNormalization_layer(inputs, name=None):
epsilon
=
1e-5
input_
=
inputs
[
0
]
mean
=
fluid
.
layers
.
reduce_mean
(
input_
,
dim
=
[
2
,
3
],
keep_dim
=
True
)
var
=
fluid
.
layers
.
reduce_mean
(
fluid
.
layers
.
square
(
input_
-
mean
),
dim
=
[
2
,
3
],
keep_dim
=
True
)
var
=
fluid
.
layers
.
reduce_mean
(
fluid
.
layers
.
square
(
input_
-
mean
),
dim
=
[
2
,
3
],
keep_dim
=
True
)
if
name
is
not
None
:
scale_name
=
name
+
"_scale"
offset_name
=
name
+
"_offset"
scale_param
=
inputs
[
1
]
offset_param
=
inputs
[
2
]
scale
=
fluid
.
layers
.
create_parameter
(
name
=
scale_param
.
name
,
shape
=
input_
.
shape
[
1
:
2
],
dtype
=
"float32"
)
offset
=
fluid
.
layers
.
create_parameter
(
name
=
offset_param
.
name
,
shape
=
input_
.
shape
[
1
:
2
],
dtype
=
"float32"
)
scale
=
fluid
.
layers
.
create_parameter
(
name
=
scale_param
.
name
,
shape
=
input_
.
shape
[
1
:
2
],
dtype
=
"float32"
)
offset
=
fluid
.
layers
.
create_parameter
(
name
=
offset_param
.
name
,
shape
=
input_
.
shape
[
1
:
2
],
dtype
=
"float32"
)
tmp
=
fluid
.
layers
.
elementwise_mul
(
x
=
(
input_
-
mean
),
y
=
scale
,
axis
=
1
)
tmp
=
tmp
/
fluid
.
layers
.
sqrt
(
var
+
epsilon
)
...
...
@@ -51,8 +48,9 @@ def InstanceNormalization_weights(name, data=None):
return
weights_name
register
(
kind
=
'InstanceNormalization'
,
shape
=
InstanceNormalization_shape
,
layer
=
InstanceNormalization_layer
,
child_func
=
None
,
weights
=
InstanceNormalization_weights
)
register
(
kind
=
'InstanceNormalization'
,
shape
=
InstanceNormalization_shape
,
layer
=
InstanceNormalization_layer
,
child_func
=
None
,
weights
=
InstanceNormalization_weights
)
x2paddle/op_mapper/tf_op_mapper.py
浏览文件 @
73830eb2
...
...
@@ -141,10 +141,8 @@ class TFOpMapper(OpMapper):
pd_param_name
=
list
(
param
.
values
())[
0
]
tf_param
=
node
.
get_attr
(
tf_param_name
)
attr
[
pd_param_name
]
=
tf_param
node
.
fluid_code
.
add_layer
(
op_info
[
0
],
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
op_info
[
0
],
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
elementwise_map
(
self
,
node
):
assert
node
.
layer_type
in
self
.
elementwise_ops
...
...
@@ -179,21 +177,21 @@ class TFOpMapper(OpMapper):
0
]
==
y_shape
[
-
1
]
and
y_shape
.
count
(
-
1
)
<
1
:
shape
=
[
1
,
x_shape
[
0
],
1
,
1
]
attr
=
{
"shape"
:
shape
}
node
.
fluid_code
.
add_layer
(
"reshape"
,
inputs
=
x_input
,
output
=
"reshape_x"
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"reshape"
,
inputs
=
x_input
,
output
=
"reshape_x"
,
param_attr
=
attr
)
if
y_shape
[
0
]
!=
1
:
attr
=
{
"expand_times"
:
[
y_shape
[
0
],
1
,
1
,
1
]}
node
.
fluid_code
.
add_layer
(
"expand"
,
inputs
=
"reshape_x"
,
output
=
"reshape_x"
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"expand"
,
inputs
=
"reshape_x"
,
output
=
"reshape_x"
,
param_attr
=
attr
)
inputs
=
{
"x"
:
"reshape_x"
,
"y"
:
y_input
}
node
.
fluid_code
.
add_layer
(
op_type
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
None
)
node
.
fluid_code
.
add_layer
(
op_type
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
None
)
return
else
:
raise
Exception
(
"Unexpected situation happend"
)
...
...
@@ -205,10 +203,8 @@ class TFOpMapper(OpMapper):
axis
=
-
1
attr
=
{
"axis"
:
axis
}
inputs
=
{
"x"
:
x_input
,
"y"
:
y_input
}
node
.
fluid_code
.
add_layer
(
op_type
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
op_type
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
attr
)
return
is_sub_seq
=
True
...
...
@@ -242,10 +238,8 @@ class TFOpMapper(OpMapper):
if
len
(
x_expand_times
)
==
4
and
x
.
tf_data_format
==
"NHWC"
:
x_expand_times
=
[
x_expand_times
[
i
]
for
i
in
[
0
,
3
,
1
,
2
]]
attr
=
{
"expand_times"
:
x_expand_times
}
node
.
fluid_code
.
add_layer
(
"expand"
,
inputs
=
x_input
,
output
=
"x_tmp"
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"expand"
,
inputs
=
x_input
,
output
=
"x_tmp"
,
param_attr
=
attr
)
x_input
=
"x_tmp"
if
y_need_expand
:
if
len
(
y_expand_times
)
==
3
and
y
.
tf_data_format
==
"NHWC"
:
...
...
@@ -253,16 +247,12 @@ class TFOpMapper(OpMapper):
if
len
(
y_expand_times
)
==
4
and
y
.
tf_data_format
==
"NHWC"
:
y_expand_times
=
[
y_expand_times
[
i
]
for
i
in
[
0
,
3
,
1
,
2
]]
attr
=
{
"expand_times"
:
y_expand_times
}
node
.
fluid_code
.
add_layer
(
"expand"
,
inputs
=
y_input
,
output
=
"y_tmp"
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"expand"
,
inputs
=
y_input
,
output
=
"y_tmp"
,
param_attr
=
attr
)
y_input
=
"y_tmp"
inputs
=
{
"x"
:
x_input
,
"y"
:
y_input
}
node
.
fluid_code
.
add_layer
(
op_type
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
None
)
node
.
fluid_code
.
add_layer
(
op_type
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
None
)
def
Placeholder
(
self
,
node
):
shape
=
node
.
out_shapes
[
0
]
...
...
@@ -283,10 +273,8 @@ class TFOpMapper(OpMapper):
if
shape
[
0
]
<
0
:
self
.
batch_node
=
node
node
.
fluid_code
.
add_layer
(
"data"
,
inputs
=
None
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"data"
,
inputs
=
None
,
output
=
node
,
param_attr
=
attr
)
def
OneShotIterator
(
self
,
node
):
return
self
.
Placeholder
(
node
)
...
...
@@ -320,10 +308,8 @@ class TFOpMapper(OpMapper):
'name'
:
string
(
node
.
layer_name
),
'default_initializer'
:
initializer
}
node
.
fluid_code
.
add_layer
(
"create_parameter"
,
inputs
=
None
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"create_parameter"
,
inputs
=
None
,
output
=
node
,
param_attr
=
attr
)
def
Transpose
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
...
...
@@ -362,16 +348,12 @@ class TFOpMapper(OpMapper):
node
.
tf_data_format
=
[
tf_data_format
[
i
]
for
i
in
perm
]
node
.
pd_data_format
=
[
pd_data_format
[
i
]
for
i
in
perm
]
attr
=
{
'perm'
:
new_perm
}
node
.
fluid_code
.
add_layer
(
"transpose"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"transpose"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
elif
len
(
node
.
out_shapes
[
0
])
!=
4
:
attr
=
{
'perm'
:
perm
}
node
.
fluid_code
.
add_layer
(
"transpose"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"transpose"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
else
:
raise
Exception
(
"Unexpected situation happend in Transpose OP"
)
...
...
@@ -401,10 +383,8 @@ class TFOpMapper(OpMapper):
"pool_padding"
:
string
(
pad_mode
),
"pool_stride"
:
strides
[
2
:
4
]
}
node
.
fluid_code
.
add_layer
(
"pool2d"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"pool2d"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
Conv2D
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
...
...
@@ -444,10 +424,8 @@ class TFOpMapper(OpMapper):
"dilation"
:
dilations
[
2
:
4
],
"padding"
:
string
(
pad_mode
)
}
node
.
fluid_code
.
add_layer
(
"conv2d"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"conv2d"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
BiasAdd
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
...
...
@@ -457,10 +435,8 @@ class TFOpMapper(OpMapper):
axis
=
1
inputs
=
{
"x"
:
input
,
"y"
:
bias
}
attr
=
{
"axis"
:
axis
}
node
.
fluid_code
.
add_layer
(
"elementwise_add"
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"elementwise_add"
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
attr
)
def
FusedBatchNorm
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
...
...
@@ -491,10 +467,8 @@ class TFOpMapper(OpMapper):
"is_test"
:
True
}
node
.
fluid_code
.
add_layer
(
"batch_norm"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"batch_norm"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
FusedBatchNormV3
(
self
,
node
):
return
self
.
FusedBatchNorm
(
node
)
...
...
@@ -539,10 +513,8 @@ class TFOpMapper(OpMapper):
"use_cudnn"
:
False
,
"padding"
:
string
(
pad_mode
)
}
node
.
fluid_code
.
add_layer
(
"conv2d"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"conv2d"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
Reshape
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
...
...
@@ -565,15 +537,14 @@ class TFOpMapper(OpMapper):
assert
len
(
param
.
out_shapes
[
0
]
)
==
1
,
"Unexpected situation of shape parameter"
attr
=
{
"shape"
:
[
-
1
]}
node
.
fluid_code
.
add_layer
(
"reshape"
,
inputs
=
param
,
output
=
"shape_param"
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"reshape"
,
inputs
=
param
,
output
=
"shape_param"
,
param_attr
=
attr
)
attr
=
{
"num_or_sections"
:
param
.
out_shapes
[
0
][
0
],
"dim"
:
0
}
node
.
fluid_code
.
add_layer
(
"split"
,
inputs
=
"shape_param"
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"split"
,
inputs
=
"shape_param"
,
output
=
node
,
param_attr
=
attr
)
new_param
=
"["
for
i
in
range
(
param
.
out_shapes
[
0
][
0
]):
new_param
+=
(
node
.
layer_name
+
"[{}]"
.
format
(
i
)
+
", "
)
...
...
@@ -601,14 +572,10 @@ class TFOpMapper(OpMapper):
if
len
(
input
.
out_shapes
[
0
])
==
4
and
node
.
tf_data_format
==
"NHWC"
:
if
len
(
attr
[
"shape"
])
<
3
:
perm
=
{
"perm"
:
[
0
,
2
,
3
,
1
]}
node
.
fluid_code
.
add_layer
(
"transpose"
,
inputs
=
input
,
output
=
node
,
param_attr
=
perm
)
node
.
fluid_code
.
add_layer
(
"reshape"
,
inputs
=
node
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"transpose"
,
inputs
=
input
,
output
=
node
,
param_attr
=
perm
)
node
.
fluid_code
.
add_layer
(
"reshape"
,
inputs
=
node
,
output
=
node
,
param_attr
=
attr
)
return
if
len
(
attr
[
"shape"
])
==
4
and
node
.
tf_data_format
==
"NHWC"
:
...
...
@@ -617,27 +584,19 @@ class TFOpMapper(OpMapper):
attr
[
"shape"
]
=
[
attr
[
"shape"
][
i
]
for
i
in
[
0
,
3
,
1
,
2
]]
else
:
perm
=
{
"perm"
:
[
0
,
2
,
3
,
1
]}
node
.
fluid_code
.
add_layer
(
"transpose"
,
inputs
=
input
,
output
=
node
,
param_attr
=
perm
)
node
.
fluid_code
.
add_layer
(
"reshape"
,
inputs
=
node
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"transpose"
,
inputs
=
input
,
output
=
node
,
param_attr
=
perm
)
node
.
fluid_code
.
add_layer
(
"reshape"
,
inputs
=
node
,
output
=
node
,
param_attr
=
attr
)
perm
=
{
"perm"
:
[
0
,
3
,
1
,
2
]}
node
.
fluid_code
.
add_layer
(
"transpose"
,
inputs
=
node
,
output
=
node
,
param_attr
=
perm
)
node
.
fluid_code
.
add_layer
(
"transpose"
,
inputs
=
node
,
output
=
node
,
param_attr
=
perm
)
return
if
len
(
attr
[
"shape"
])
==
5
:
attr
[
"shape"
]
=
[
attr
[
"shape"
][
i
]
for
i
in
[
0
,
1
,
4
,
2
,
3
]]
node
.
fluid_code
.
add_layer
(
"reshape"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"reshape"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
AvgPool
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
...
...
@@ -665,10 +624,8 @@ class TFOpMapper(OpMapper):
"pool_stride"
:
strides
[
2
:
4
],
"pool_padding"
:
string
(
pad_mode
)
}
node
.
fluid_code
.
add_layer
(
"pool2d"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"pool2d"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
SplitV
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
...
...
@@ -685,10 +642,8 @@ class TFOpMapper(OpMapper):
"num_or_sections"
:
num_sections
.
value
.
tolist
(),
"dim"
:
dim
.
value
}
node
.
fluid_code
.
add_layer
(
"split"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"split"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
ConcatV2
(
self
,
node
):
inputs
=
[
...
...
@@ -703,10 +658,8 @@ class TFOpMapper(OpMapper):
inputs
[
0
].
out_shapes
[
0
])
==
4
:
axis
=
nhwc_dim_to_nchw
(
inputs
[
0
],
axis
)
attr
=
{
"axis"
:
axis
}
node
.
fluid_code
.
add_layer
(
"concat"
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"concat"
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
attr
)
def
Tile
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
...
...
@@ -726,10 +679,8 @@ class TFOpMapper(OpMapper):
expand_times
[
i
]
=
1
attr
=
{
"expand_times"
:
expand_times
}
node
.
fluid_code
.
add_layer
(
"expand"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"expand"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
Pack
(
self
,
node
):
inputs
=
[
...
...
@@ -747,10 +698,8 @@ class TFOpMapper(OpMapper):
node
.
pd_data_format
=
""
.
join
(
pd_data_format
)
attr
=
{
"axis"
:
axis
}
node
.
fluid_code
.
add_layer
(
"stack"
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"stack"
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
attr
)
def
Pad
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
...
...
@@ -767,10 +716,8 @@ class TFOpMapper(OpMapper):
paddings
=
paddings
[
4
:]
pad_op
=
"pad2d"
attr
=
{
"paddings"
:
paddings
}
node
.
fluid_code
.
add_layer
(
pad_op
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
pad_op
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
MirrorPad
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
...
...
@@ -789,10 +736,8 @@ class TFOpMapper(OpMapper):
paddings
=
paddings
[
4
:]
pad_op
=
"pad2d"
attr
=
{
"paddings"
:
paddings
,
"mode"
:
string
(
"reflect"
)}
node
.
fluid_code
.
add_layer
(
pad_op
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
pad_op
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
Range
(
self
,
node
):
start
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
...
...
@@ -816,10 +761,8 @@ class TFOpMapper(OpMapper):
inputs
=
{
"start"
:
start
,
"end"
:
limit
,
"step"
:
delta
}
attr
=
{
"dtype"
:
string
(
node
.
dtype
)}
node
.
fluid_code
.
add_layer
(
"range"
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"range"
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
attr
)
def
Mean
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
...
...
@@ -833,10 +776,8 @@ class TFOpMapper(OpMapper):
dims
[
i
]
=
nhwc_dim_to_nchw
(
input
,
dims
[
i
])
attr
=
{
"dim"
:
dims
,
"keep_dim"
:
keep_dims
}
node
.
fluid_code
.
add_layer
(
"reduce_mean"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"reduce_mean"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
MatMul
(
self
,
node
):
x
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
...
...
@@ -850,15 +791,11 @@ class TFOpMapper(OpMapper):
shape
=
x
.
out_shapes
[
0
]
shape
[
-
1
]
=
y
.
out_shapes
[
0
][
0
]
attr
=
{
"shape"
:
shape
}
node
.
fluid_code
.
add_layer
(
"reshape"
,
inputs
=
x
,
output
=
x
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"reshape"
,
inputs
=
x
,
output
=
x
,
param_attr
=
attr
)
attr
=
{
"transpose_x"
:
transpose_a
,
"transpose_y"
:
transpose_b
}
node
.
fluid_code
.
add_layer
(
"matmul"
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"matmul"
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
attr
)
def
ArgMax
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
...
...
@@ -869,10 +806,8 @@ class TFOpMapper(OpMapper):
if
input
.
tf_data_format
==
"NHWC"
and
len
(
input
.
out_shapes
[
0
])
==
4
:
axis
=
nhwc_dim_to_nchw
(
input
,
axis
)
attr
=
{
"axis"
:
axis
}
node
.
fluid_code
.
add_layer
(
"argmax"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"argmax"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
StridedSlice
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
...
...
@@ -910,16 +845,12 @@ class TFOpMapper(OpMapper):
x
=
shrink_axis_mask
>>
i
&
1
if
x
==
1
:
squeeze_dims
.
append
(
i
)
node
.
fluid_code
.
add_layer
(
"slice"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"slice"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
if
shrink_axis_mask
>
0
and
len
(
input
.
out_shapes
[
0
])
==
5
:
attr
=
{
"axes"
:
squeeze_dims
}
node
.
fluid_code
.
add_layer
(
"squeeze"
,
inputs
=
node
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"squeeze"
,
inputs
=
node
,
output
=
node
,
param_attr
=
attr
)
def
Slice
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
...
...
@@ -951,10 +882,8 @@ class TFOpMapper(OpMapper):
"starts"
:
begin
,
"ends"
:
size
}
node
.
fluid_code
.
add_layer
(
"slice"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"slice"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
Conv2DBackpropInput
(
self
,
node
):
out_shape
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
...
...
@@ -1004,10 +933,8 @@ class TFOpMapper(OpMapper):
"padding"
:
string
(
pad_mode
),
"output_size"
:
out_shape
[
1
:
3
]
}
node
.
fluid_code
.
add_layer
(
"conv2d_transpose"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"conv2d_transpose"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
Max
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
...
...
@@ -1019,10 +946,8 @@ class TFOpMapper(OpMapper):
dim
=
nhwc_dim_to_nchw
(
input
,
dim
)
attr
=
{
"dim"
:
dim
,
"keep_dim"
:
keep_dims
}
node
.
fluid_code
.
add_layer
(
"reduce_max"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"reduce_max"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
Sum
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
...
...
@@ -1034,19 +959,15 @@ class TFOpMapper(OpMapper):
dim
=
nhwc_dim_to_nchw
(
input
,
dim
)
attr
=
{
"dim"
:
dim
,
"keep_dim"
:
keep_dims
}
node
.
fluid_code
.
add_layer
(
"reduce_sum"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"reduce_sum"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
Cast
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
dtype
=
node
.
dtype_map
[
node
.
get_attr
(
'DstT'
)]
attr
=
{
"dtype"
:
string
(
dtype
)}
node
.
fluid_code
.
add_layer
(
"cast"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"cast"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
Split
(
self
,
node
):
dim
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
...
...
@@ -1058,10 +979,8 @@ class TFOpMapper(OpMapper):
dim
=
nhwc_dim_to_nchw
(
input
,
dim
)
attr
=
{
"num_or_sections"
:
num_split
,
"dim"
:
dim
}
node
.
fluid_code
.
add_layer
(
"split"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"split"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
Squeeze
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
...
...
@@ -1070,10 +989,8 @@ class TFOpMapper(OpMapper):
for
i
in
range
(
len
(
squeeze_dims
)):
squeeze_dims
[
i
]
=
nhwc_dim_to_nchw
(
input
,
squeeze_dims
[
i
])
attr
=
{
"axes"
:
squeeze_dims
}
node
.
fluid_code
.
add_layer
(
"squeeze"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"squeeze"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
Softmax
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
...
...
@@ -1083,10 +1000,8 @@ class TFOpMapper(OpMapper):
if
input
.
tf_data_format
==
"NHWC"
and
len
(
input
.
out_shapes
[
0
])
==
4
:
axis
=
nhwc_dim_to_nchw
(
input
,
axis
)
attr
=
{
"axis"
:
axis
}
node
.
fluid_code
.
add_layer
(
"softmax"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"softmax"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
ResizeNearestNeighbor
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
...
...
@@ -1099,10 +1014,8 @@ class TFOpMapper(OpMapper):
resize_shape
,
node
.
out_shapes
[
0
])
align_corners
=
node
.
get_attr
(
"align_corners"
)
attr
=
{
"align_corners"
:
align_corners
,
"out_shape"
:
resize_shape
}
node
.
fluid_code
.
add_layer
(
"resize_nearest"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"resize_nearest"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
ResizeBilinear
(
self
,
node
):
input
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
...
...
@@ -1119,19 +1032,15 @@ class TFOpMapper(OpMapper):
"out_shape"
:
resize_shape
,
"align_mode"
:
1
}
node
.
fluid_code
.
add_layer
(
"resize_bilinear"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"resize_bilinear"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
def
GreaterEqual
(
self
,
node
):
x
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
y
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
],
copy
=
True
)
inputs
=
{
"x"
:
x
,
"y"
:
y
}
node
.
fluid_code
.
add_layer
(
"greater_equal"
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
None
)
node
.
fluid_code
.
add_layer
(
"greater_equal"
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
None
)
def
RandomUniform
(
self
,
node
):
shape
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
...
...
@@ -1145,26 +1054,21 @@ class TFOpMapper(OpMapper):
attr
=
{
"shape"
:
shape
,
"min"
:
0.0
,
"max"
:
0.9999
}
if
shape
[
0
]
<
0
:
input
=
self
.
batch_node
node
.
fluid_code
.
add_layer
(
"uniform_random_batch_size_like"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"uniform_random_batch_size_like"
,
inputs
=
input
,
output
=
node
,
param_attr
=
attr
)
else
:
node
.
fluid_code
.
add_layer
(
"uniform_random"
,
inputs
=
None
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"uniform_random"
,
inputs
=
None
,
output
=
node
,
param_attr
=
attr
)
def
SquaredDifference
(
self
,
node
):
x
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
0
],
copy
=
True
)
y
=
self
.
graph
.
get_node
(
node
.
layer
.
input
[
1
],
copy
=
True
)
inputs
=
{
"x"
:
x
,
"y"
:
y
}
node
.
fluid_code
.
add_layer
(
"elementwise_sub"
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
None
)
node
.
fluid_code
.
add_layer
(
"elementwise_sub"
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
None
)
inputs
=
{
"x"
:
node
,
"y"
:
node
}
node
.
fluid_code
.
add_layer
(
"elementwise_mul"
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
None
)
node
.
fluid_code
.
add_layer
(
"elementwise_mul"
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
None
)
x2paddle/optimizer/caffe_optimizer.py
浏览文件 @
73830eb2
...
...
@@ -41,10 +41,11 @@ class CaffeOptimizer(object):
if
is_delete_node
:
parent_node
.
fluid_code
.
clear
()
node
.
fluid_code
.
clear
()
node
.
fluid_code
.
add_layer
(
"batch_norm"
,
inputs
=
input
,
output
=
node
,
param_attr
=
parent_param_attr
)
node
.
fluid_code
.
add_layer
(
"batch_norm"
,
inputs
=
input
,
output
=
node
,
param_attr
=
parent_param_attr
)
def
merge_op_activation
(
self
):
for
node_name
in
self
.
graph
.
topo_sort
:
...
...
@@ -62,7 +63,8 @@ class CaffeOptimizer(object):
if
is_delete_node
:
parent_node
.
fluid_code
.
clear
()
node
.
fluid_code
.
clear
()
node
.
fluid_code
.
add_layer
(
op
,
inputs
=
input
,
output
=
node
,
param_attr
=
parent_param_attr
)
node
.
fluid_code
.
add_layer
(
op
,
inputs
=
input
,
output
=
node
,
param_attr
=
parent_param_attr
)
x2paddle/optimizer/tf_optimizer.py
浏览文件 @
73830eb2
...
...
@@ -554,10 +554,11 @@ class TFOptimizer(object):
node
.
fluid_code
.
layers
[
0
].
param_attr
[
"shape"
]
=
shape
node
.
fluid_code
.
layers
[
0
].
output
=
"nhwc_"
+
name
attr
=
{
"perm"
:
[
0
,
2
,
3
,
1
]}
node
.
fluid_code
.
add_layer
(
"transpose"
,
inputs
=
"nhwc_"
+
name
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"transpose"
,
inputs
=
"nhwc_"
+
name
,
output
=
node
,
param_attr
=
attr
)
self
.
graph
.
input_nodes
[
i
]
=
"nhwc_"
+
name
for
i
,
name
in
enumerate
(
self
.
graph
.
output_nodes
):
node
=
self
.
graph
.
get_node
(
name
)
...
...
@@ -972,10 +973,8 @@ class TFOptimizer(object):
"bias_after_scale"
:
True
,
"act"
:
act
}
node
.
fluid_code
.
add_layer
(
"scale"
,
inputs
=
in_node
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"scale"
,
inputs
=
in_node
,
output
=
node
,
param_attr
=
attr
)
del
self
.
graph
.
node_map
[
in_nodes0
[
0
].
layer_name
]
del
self
.
graph
.
node_map
[
in_nodes0
[
1
].
layer_name
]
...
...
@@ -1055,29 +1054,32 @@ class TFOptimizer(object):
"shape"
:
[
channel
],
"name"
:
string
(
node
.
layer_name
+
"_scale"
)
}
node
.
fluid_code
.
add_layer
(
"create_parameter"
,
inputs
=
None
,
output
=
node
.
layer_name
+
"_scale"
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"create_parameter"
,
inputs
=
None
,
output
=
node
.
layer_name
+
"_scale"
,
param_attr
=
attr
)
attr
=
{
"dtype"
:
string
(
scale
.
dtype
),
"shape"
:
[
channel
],
"name"
:
string
(
node
.
layer_name
+
"_bias"
)
}
node
.
fluid_code
.
add_layer
(
"create_parameter"
,
inputs
=
None
,
output
=
node
.
layer_name
+
"_bias"
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"create_parameter"
,
inputs
=
None
,
output
=
node
.
layer_name
+
"_bias"
,
param_attr
=
attr
)
inputs
=
{
"x"
:
in_node
,
"scale"
:
node
.
layer_name
+
"_scale"
,
"bias"
:
node
.
layer_name
+
"_bias"
}
attr
=
{
"act"
:
act
}
node
.
fluid_code
.
add_layer
(
"affine_channel"
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
attr
)
node
.
fluid_code
.
add_layer
(
"affine_channel"
,
inputs
=
inputs
,
output
=
node
,
param_attr
=
attr
)
del
self
.
graph
.
node_map
[
in_nodes0
[
0
].
layer_name
]
del
self
.
graph
.
node_map
[
in_nodes0
[
1
].
layer_name
]
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录