Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
冰之2023
Mace
提交
875ed7a5
Mace
项目概览
冰之2023
/
Mace
与 Fork 源项目一致
Fork自
Xiaomi / Mace
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Mace
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
875ed7a5
编写于
3月 28, 2018
作者:
L
liuqi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Caffe converter support depthwise convolution.
上级
a1c6ba92
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
25 addition
and
12 deletion
+25
-12
mace/python/tools/caffe_converter_lib.py
mace/python/tools/caffe_converter_lib.py
+23
-11
mace/python/tools/tf_converter_lib.py
mace/python/tools/tf_converter_lib.py
+2
-1
未找到文件。
mace/python/tools/caffe_converter_lib.py
浏览文件 @
875ed7a5
...
@@ -72,9 +72,9 @@ class Shapes(object):
...
@@ -72,9 +72,9 @@ class Shapes(object):
output_shape
=
np
.
zeros_like
(
input_shape
)
output_shape
=
np
.
zeros_like
(
input_shape
)
output_shape
[
0
]
=
input_shape
[
0
]
output_shape
[
0
]
=
input_shape
[
0
]
output_shape
[
1
]
=
int
(
round_func
((
input_shape
[
1
]
+
paddings
[
0
]
-
filter_shape
[
0
]
output_shape
[
1
]
=
int
(
round_func
((
input_shape
[
1
]
+
paddings
[
0
]
-
filter_shape
[
0
]
-
(
filter_shape
[
0
]
-
1
)
*
(
dilations
[
0
]
-
1
))
/
float
(
strides
[
0
])))
+
1
-
(
filter_shape
[
0
]
-
1
)
*
(
dilations
[
0
]
-
1
))
/
float
(
strides
[
0
])))
+
1
output_shape
[
2
]
=
int
(
round_func
((
input_shape
[
2
]
+
paddings
[
1
]
-
filter_shape
[
1
]
output_shape
[
2
]
=
int
(
round_func
((
input_shape
[
2
]
+
paddings
[
1
]
-
filter_shape
[
1
]
-
(
filter_shape
[
1
]
-
1
)
*
(
dilations
[
1
]
-
1
))
/
float
(
strides
[
1
])))
+
1
-
(
filter_shape
[
1
]
-
1
)
*
(
dilations
[
1
]
-
1
))
/
float
(
strides
[
1
])))
+
1
output_shape
[
3
]
=
filter_shape
[
2
]
output_shape
[
3
]
=
filter_shape
[
2
]
return
output_shape
return
output_shape
...
@@ -333,8 +333,18 @@ class CaffeConverter(object):
...
@@ -333,8 +333,18 @@ class CaffeConverter(object):
return
pad
,
stride
,
kernel
return
pad
,
stride
,
kernel
def
convert_conv2d
(
self
,
op
):
def
convert_conv2d
(
self
,
op
):
op_def
=
self
.
CommonConvert
(
op
,
'Conv2D'
)
param
=
op
.
layer
.
convolution_param
param
=
op
.
layer
.
convolution_param
is_depthwise
=
False
if
param
.
HasField
(
'group'
):
if
param
.
group
==
op
.
data
[
0
].
shape
[
0
]
and
op
.
data
[
0
].
shape
[
1
]
==
1
:
is_depthwise
=
True
else
:
raise
Exception
(
"Mace do not support group convolution yet"
)
if
is_depthwise
:
op_def
=
self
.
CommonConvert
(
op
,
'DepthwiseConv2d'
)
else
:
op_def
=
self
.
CommonConvert
(
op
,
'Conv2D'
)
# Add filter
# Add filter
weight_tensor_name
=
op
.
name
+
'_weight:0'
weight_tensor_name
=
op
.
name
+
'_weight:0'
...
@@ -342,7 +352,7 @@ class CaffeConverter(object):
...
@@ -342,7 +352,7 @@ class CaffeConverter(object):
self
.
add_tensor
(
weight_tensor_name
,
weight_data
)
self
.
add_tensor
(
weight_tensor_name
,
weight_data
)
if
self
.
device
==
'gpu'
:
if
self
.
device
==
'gpu'
:
buffer_type
=
"CONV2D_FILTER"
buffer_type
=
"
DW_CONV2D_FILTER"
if
is_depthwise
else
"
CONV2D_FILTER"
output_name
=
self
.
add_buffer_to_image
(
weight_tensor_name
,
buffer_type
)
output_name
=
self
.
add_buffer_to_image
(
weight_tensor_name
,
buffer_type
)
op_def
.
input
.
extend
([
output_name
])
op_def
.
input
.
extend
([
output_name
])
else
:
else
:
...
@@ -373,15 +383,16 @@ class CaffeConverter(object):
...
@@ -373,15 +383,16 @@ class CaffeConverter(object):
self
.
resolved_ops
.
add
(
op
.
name
)
self
.
resolved_ops
.
add
(
op
.
name
)
output_shape
=
Shapes
.
conv_pool_shape
(
op
.
get_single_parent
().
output_shape_map
[
op
.
layer
.
bottom
[
0
]],
output_shape
=
Shapes
.
conv_pool_shape
(
op
.
get_single_parent
().
output_shape_map
[
op
.
layer
.
bottom
[
0
]],
weight_data
.
shape
,
weight_data
.
shape
,
paddings
,
strides
,
dilations
,
paddings
,
strides
,
dilations
,
math
.
floor
)
math
.
floor
)
op
.
output_shape_map
[
op
.
layer
.
top
[
0
]]
=
output_shape
op
.
output_shape_map
[
op
.
layer
.
top
[
0
]]
=
output_shape
if
len
(
self
.
ops_map
[
final_op
.
name
].
children
)
==
1
\
if
len
(
self
.
ops_map
[
final_op
.
name
].
children
)
==
1
\
and
self
.
ops_map
[
final_op
.
name
].
children
[
0
].
type
in
activation_name_map
:
and
self
.
ops_map
[
final_op
.
name
].
children
[
0
].
type
in
activation_name_map
:
activation_op
=
self
.
ops_map
[
final_op
.
name
].
children
[
0
]
activation_op
=
self
.
ops_map
[
final_op
.
name
].
children
[
0
]
op_def
.
type
=
"FusedConv2D"
if
not
is_depthwise
:
op_def
.
type
=
"FusedConv2D"
fused_act_arg
=
op_def
.
arg
.
add
()
fused_act_arg
=
op_def
.
arg
.
add
()
fused_act_arg
.
name
=
'activation'
fused_act_arg
.
name
=
'activation'
fused_act_arg
.
s
=
activation_name_map
[
activation_op
.
type
]
fused_act_arg
.
s
=
activation_name_map
[
activation_op
.
type
]
...
@@ -412,7 +423,7 @@ class CaffeConverter(object):
...
@@ -412,7 +423,7 @@ class CaffeConverter(object):
width
=
output_shape
[
0
]
*
((
output_shape
[
1
]
+
1
)
/
2
)
*
((
output_shape
[
2
]
+
1
)
/
2
)
width
=
output_shape
[
0
]
*
((
output_shape
[
1
]
+
1
)
/
2
)
*
((
output_shape
[
2
]
+
1
)
/
2
)
return
self
.
winograd
and
self
.
device
==
'gpu'
and
\
return
self
.
winograd
and
self
.
device
==
'gpu'
and
\
filter_shape
[
0
]
==
3
and
(
filter_shape
[
0
]
==
filter_shape
[
1
])
and
\
filter_shape
[
0
]
==
3
and
(
filter_shape
[
0
]
==
filter_shape
[
1
])
and
\
dilations
[
0
]
==
1
and
(
dilations
[
0
]
==
dilations
[
1
])
and
\
dilations
[
0
]
==
1
and
(
dilations
[
0
]
==
dilations
[
1
])
and
\
(
strides
[
0
]
==
1
)
and
(
strides
[
0
]
==
strides
[
1
])
and
\
(
strides
[
0
]
==
1
)
and
(
strides
[
0
]
==
strides
[
1
])
and
\
(
16
*
filter_shape
[
2
]
<
OPENCL_IMAGE_MAX_SIZE
)
and
\
(
16
*
filter_shape
[
2
]
<
OPENCL_IMAGE_MAX_SIZE
)
and
\
(
16
*
filter_shape
[
3
]
<
OPENCL_IMAGE_MAX_SIZE
)
and
\
(
16
*
filter_shape
[
3
]
<
OPENCL_IMAGE_MAX_SIZE
)
and
\
...
@@ -662,7 +673,7 @@ class CaffeConverter(object):
...
@@ -662,7 +673,7 @@ class CaffeConverter(object):
filter_shape
=
[
kernels
[
0
],
kernels
[
1
],
input_shape
[
3
],
input_shape
[
3
]]
filter_shape
=
[
kernels
[
0
],
kernels
[
1
],
input_shape
[
3
],
input_shape
[
3
]]
output_shape
=
Shapes
.
conv_pool_shape
(
input_shape
,
filter_shape
,
output_shape
=
Shapes
.
conv_pool_shape
(
input_shape
,
filter_shape
,
paddings
,
strides
,
[
1
,
1
],
math
.
ceil
)
paddings
,
strides
,
[
1
,
1
],
math
.
ceil
)
op
.
output_shape_map
[
op
.
layer
.
top
[
0
]]
=
output_shape
op
.
output_shape_map
[
op
.
layer
.
top
[
0
]]
=
output_shape
op_def
.
output
.
extend
([
op
.
name
+
':0'
])
op_def
.
output
.
extend
([
op
.
name
+
':0'
])
...
@@ -764,7 +775,7 @@ class CaffeConverter(object):
...
@@ -764,7 +775,7 @@ class CaffeConverter(object):
input_shape
=
op
.
parents
[
0
].
output_shape_map
[
op
.
layer
.
bottom
[
0
]]
input_shape
=
op
.
parents
[
0
].
output_shape_map
[
op
.
layer
.
bottom
[
0
]]
num_outputs
=
len
(
op
.
layer
.
top
)
num_outputs
=
len
(
op
.
layer
.
top
)
if
(
input_shape
[
3
]
%
num_outputs
)
!=
0
or
\
if
(
input_shape
[
3
]
%
num_outputs
)
!=
0
or
\
(
self
.
device
==
'gpu'
and
((
input_shape
[
3
]
/
num_outputs
)
%
4
!=
0
))
:
(
self
.
device
==
'gpu'
and
((
input_shape
[
3
]
/
num_outputs
)
%
4
!=
0
))
:
raise
Exception
(
'Mace do not support slice with input shape '
raise
Exception
(
'Mace do not support slice with input shape '
+
str
(
input_shape
)
+
' and number of output '
+
str
(
num_outputs
))
+
str
(
input_shape
)
+
' and number of output '
+
str
(
num_outputs
))
output_shape
=
Shapes
.
slice_shape
(
input_shape
,
num_outputs
)
output_shape
=
Shapes
.
slice_shape
(
input_shape
,
num_outputs
)
...
@@ -966,3 +977,4 @@ def convert_to_mace_pb(model_file, weight_file, input_node_str, input_shape_str,
...
@@ -966,3 +977,4 @@ def convert_to_mace_pb(model_file, weight_file, input_node_str, input_shape_str,
print
"Memory optimization done."
print
"Memory optimization done."
return
net_def
return
net_def
mace/python/tools/tf_converter_lib.py
浏览文件 @
875ed7a5
...
@@ -362,7 +362,8 @@ class TFConverter(object):
...
@@ -362,7 +362,8 @@ class TFConverter(object):
if
len
(
self
.
tf_graph
.
get
(
final_op
.
name
,
[]))
==
1
\
if
len
(
self
.
tf_graph
.
get
(
final_op
.
name
,
[]))
==
1
\
and
self
.
tf_graph
[
final_op
.
name
][
0
].
type
in
activation_name_map
:
and
self
.
tf_graph
[
final_op
.
name
][
0
].
type
in
activation_name_map
:
activation_op
=
self
.
tf_graph
[
final_op
.
name
][
0
]
activation_op
=
self
.
tf_graph
[
final_op
.
name
][
0
]
op_def
.
type
=
"FusedConv2D"
if
op_def
.
type
==
"Conv2D"
:
op_def
.
type
=
"FusedConv2D"
fused_act_arg
=
op_def
.
arg
.
add
()
fused_act_arg
=
op_def
.
arg
.
add
()
fused_act_arg
.
name
=
'activation'
fused_act_arg
.
name
=
'activation'
fused_act_arg
.
s
=
activation_name_map
[
activation_op
.
type
]
fused_act_arg
.
s
=
activation_name_map
[
activation_op
.
type
]
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录