Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
冰之2023
Mace
提交
4eb498a6
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,发现更多精彩内容 >>
提交
4eb498a6
编写于
12月 06, 2017
作者:
L
Liangliang He
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'convert-gcn' into 'master'
Add gcn validation tools. See merge request !149
上级
1635046e
a66e0e35
变更
7
展开全部
隐藏空白更改
内联
并排
Showing
7 changed file
with
503 addition
and
262 deletion
+503
-262
mace/examples/mace_run.cc
mace/examples/mace_run.cc
+17
-11
mace/kernels/opencl/fused_conv_2d_opencl.cc
mace/kernels/opencl/fused_conv_2d_opencl.cc
+17
-16
mace/ops/fused_conv_2d_test.cc
mace/ops/fused_conv_2d_test.cc
+78
-0
mace/python/tools/tf_converter.py
mace/python/tools/tf_converter.py
+6
-1
mace/python/tools/tf_converter_lib.py
mace/python/tools/tf_converter_lib.py
+268
-212
tools/validate.py
tools/validate.py
+37
-22
tools/validate_gcn.sh
tools/validate_gcn.sh
+80
-0
未找到文件。
mace/examples/mace_run.cc
浏览文件 @
4eb498a6
...
...
@@ -129,15 +129,21 @@ int main(int argc, char **argv) {
// save output
const
Tensor
*
output
=
ws
.
GetTensor
(
output_node
+
":0"
);
Tensor
::
MappingGuard
output_guard
(
output
);
ofstream
out_file
(
output_file
,
ios
::
binary
);
out_file
.
write
((
const
char
*
)(
output
->
data
<
float
>
()),
output
->
size
()
*
sizeof
(
float
));
out_file
.
flush
();
out_file
.
close
();
VLOG
(
0
)
<<
"Output shape: ["
<<
output
->
dim
(
0
)
<<
", "
<<
output
->
dim
(
1
)
<<
", "
<<
output
->
dim
(
2
)
<<
", "
<<
output
->
dim
(
3
)
<<
"]"
;
std
::
remove
(
output_file
.
c_str
());
if
(
output
!=
nullptr
)
{
Tensor
::
MappingGuard
output_guard
(
output
);
ofstream
out_file
(
output_file
,
ios
::
binary
);
out_file
.
write
((
const
char
*
)(
output
->
data
<
float
>
()),
output
->
size
()
*
sizeof
(
float
));
out_file
.
flush
();
out_file
.
close
();
stringstream
ss
;
ss
<<
"Output shape: ["
;
for
(
int
i
=
0
;
i
<
output
->
dim_size
();
++
i
)
{
ss
<<
output
->
dim
(
i
)
<<
", "
;
}
ss
<<
"]"
;
VLOG
(
0
)
<<
ss
.
str
();
}
}
\ No newline at end of file
mace/kernels/opencl/fused_conv_2d_opencl.cc
浏览文件 @
4eb498a6
...
...
@@ -28,6 +28,11 @@ extern void Conv2dOpenclK3x3S2(const Tensor *input, const Tensor *filter,
const
int
*
padding
,
const
DataType
dt
,
Tensor
*
output
);
extern
void
Conv2dOpencl
(
const
Tensor
*
input
,
const
Tensor
*
filter
,
const
Tensor
*
bias
,
const
bool
fused_relu
,
const
uint32_t
stride
,
const
int
*
padding
,
const
DataType
dt
,
Tensor
*
output
);
template
<
typename
T
>
void
FusedConv2dFunctor
<
DeviceType
::
OPENCL
,
T
>::
operator
()(
const
Tensor
*
input
,
const
Tensor
*
filter
,
...
...
@@ -44,20 +49,15 @@ void FusedConv2dFunctor<DeviceType::OPENCL, T>::operator()(const Tensor *input,
{
Conv2dOpenclK3x3S1
,
Conv2dOpenclK3x3S2
},
{
nullptr
,
nullptr
},
{
nullptr
,
nullptr
}};
index_t
kernel_h
=
filter
->
dim
(
0
);
index_t
kernel_w
=
filter
->
dim
(
1
);
if
(
kernel_h
!=
kernel_w
||
kernel_h
>
5
||
strides_
[
0
]
!=
strides_
[
1
]
||
strides_
[
0
]
>
2
||
dilations_
[
0
]
!=
1
||
dilations_
[
1
]
!=
1
||
selector
[
kernel_h
-
1
][
strides_
[
0
]
-
1
]
==
nullptr
)
{
if
(
!
input
->
is_image
()
||
strides_
[
0
]
!=
strides_
[
1
]
||
strides_
[
0
]
>
2
||
dilations_
[
0
]
!=
1
||
dilations_
[
1
]
!=
1
)
{
LOG
(
WARNING
)
<<
"OpenCL conv2d kernel with "
<<
"filter"
<<
kernel_h
<<
"x"
<<
kernel_w
<<
","
<<
" stride "
<<
strides_
[
0
]
<<
"x"
<<
strides_
[
1
]
<<
" is not implemented yet, using slow version"
;
// TODO(heliangliang) The CPU/NEON kernel should map the buffer
FusedConv2dFunctor
<
DeviceType
::
CPU
,
T
>
(
strides_
,
paddings_
,
dilations_
)(
input
,
filter
,
bias
,
output
);
return
;
MACE_NOT_IMPLEMENTED
;
}
std
::
vector
<
index_t
>
output_shape
(
4
);
...
...
@@ -66,16 +66,17 @@ void FusedConv2dFunctor<DeviceType::OPENCL, T>::operator()(const Tensor *input,
input
->
shape
().
data
(),
filter
->
shape
().
data
(),
dilations_
,
strides_
,
paddings_
,
output_shape
.
data
(),
paddings
.
data
());
if
(
input
->
is_image
())
{
std
::
vector
<
size_t
>
output_image_shape
;
CalImage2DShape
(
output_shape
,
BufferType
::
IN_OUT
,
output_image_shape
);
output
->
ResizeImage
(
output_shape
,
output_image_shape
);
std
::
vector
<
size_t
>
output_image_shape
;
CalImage2DShape
(
output_shape
,
BufferType
::
IN_OUT
,
output_image_shape
);
output
->
ResizeImage
(
output_shape
,
output_image_shape
);
if
(
kernel_h
==
kernel_w
&&
kernel_h
<=
5
&&
selector
[
kernel_h
-
1
][
strides_
[
0
]
-
1
]
!=
nullptr
)
{
auto
conv2d_func
=
selector
[
kernel_h
-
1
][
strides_
[
0
]
-
1
];
conv2d_func
(
input
,
filter
,
bias
,
true
,
paddings
.
data
(),
DataTypeToEnum
<
T
>::
value
,
output
);
}
else
{
output
->
Resize
(
output_shape
);
Conv2dOpencl
(
input
,
filter
,
bias
,
true
,
strides_
[
0
],
paddings
.
data
(),
DataTypeToEnum
<
T
>::
value
,
output
);
}
auto
conv2d_func
=
selector
[
kernel_h
-
1
][
strides_
[
0
]
-
1
];
conv2d_func
(
input
,
filter
,
bias
,
true
,
paddings
.
data
(),
DataTypeToEnum
<
T
>::
value
,
output
);
}
template
...
...
mace/ops/fused_conv_2d_test.cc
浏览文件 @
4eb498a6
...
...
@@ -408,3 +408,81 @@ TEST_F(FusedConv2dOpTest, OPENCLHalfAlignedConvNxNS12) {
TestHalfComplexConvNxNS12
<
DeviceType
::
OPENCL
>
({
32
,
32
,
32
,
64
});
}
template
<
DeviceType
D
,
typename
T
>
static
void
TestGeneralConvNxNS12
(
const
std
::
vector
<
index_t
>
&
image_shape
,
const
std
::
vector
<
index_t
>
&
filter_shape
)
{
testing
::
internal
::
LogToStderr
();
auto
func
=
[
&
](
int
stride_h
,
int
stride_w
,
Padding
type
)
{
srand
(
time
(
NULL
));
// generate random input
index_t
batch
=
1
;
index_t
height
=
image_shape
[
0
];
index_t
width
=
image_shape
[
1
];
index_t
input_channels
=
filter_shape
[
2
];
index_t
output_channels
=
filter_shape
[
3
];
index_t
kernel_h
=
filter_shape
[
0
];
index_t
kernel_w
=
filter_shape
[
1
];
// Construct graph
OpsTestNet
net
;
OpDefBuilder
(
"FusedConv2D"
,
"FusedConv2dTest"
)
.
Input
(
"Input"
)
.
Input
(
"Filter"
)
.
Input
(
"Bias"
)
.
Output
(
"Output"
)
.
AddIntsArg
(
"strides"
,
{
stride_h
,
stride_w
})
.
AddIntArg
(
"padding"
,
type
)
.
AddIntsArg
(
"dilations"
,
{
1
,
1
})
.
AddIntArg
(
"T"
,
static_cast
<
int
>
(
DataTypeToEnum
<
T
>::
value
))
.
Finalize
(
net
.
NewOperatorDef
());
// Add input data
net
.
AddRandomInput
<
D
,
T
>
(
"Input"
,
{
batch
,
height
,
width
,
input_channels
});
net
.
AddRandomInput
<
D
,
T
>
(
"Filter"
,
{
kernel_h
,
kernel_w
,
input_channels
,
output_channels
});
net
.
AddRandomInput
<
D
,
T
>
(
"Bias"
,
{
output_channels
});
// run on cpu
net
.
RunOp
();
// Check
Tensor
expected
;
expected
.
Copy
(
*
net
.
GetOutput
(
"Output"
));
// run on gpu
BufferToImage
<
D
,
T
>
(
net
,
"Input"
,
"InputImage"
,
kernels
::
BufferType
::
IN_OUT
);
BufferToImage
<
D
,
T
>
(
net
,
"Filter"
,
"FilterImage"
,
kernels
::
BufferType
::
FILTER
);
BufferToImage
<
D
,
T
>
(
net
,
"Bias"
,
"BiasImage"
,
kernels
::
BufferType
::
ARGUMENT
);
OpDefBuilder
(
"FusedConv2D"
,
"FusedConv2dTest"
)
.
Input
(
"InputImage"
)
.
Input
(
"FilterImage"
)
.
Input
(
"BiasImage"
)
.
Output
(
"OutputImage"
)
.
AddIntsArg
(
"strides"
,
{
stride_h
,
stride_w
})
.
AddIntArg
(
"padding"
,
type
)
.
AddIntsArg
(
"dilations"
,
{
1
,
1
})
.
AddIntArg
(
"T"
,
static_cast
<
int
>
(
DataTypeToEnum
<
T
>::
value
))
.
Finalize
(
net
.
NewOperatorDef
());
// Run on device
net
.
RunOp
(
D
);
ImageToBuffer
<
D
,
T
>
(
net
,
"OutputImage"
,
"OPENCLOutput"
,
kernels
::
BufferType
::
IN_OUT
);
ExpectTensorNear
<
float
>
(
expected
,
*
net
.
GetOutput
(
"OPENCLOutput"
),
0.001
);
};
for
(
int
stride
:
{
1
,
2
})
{
func
(
stride
,
stride
,
VALID
);
func
(
stride
,
stride
,
SAME
);
}
}
TEST_F
(
FusedConv2dOpTest
,
OPENCL7X7ConvNxNS12
)
{
TestGeneralConvNxNS12
<
DeviceType
::
OPENCL
,
float
>
({
32
,
32
},
{
7
,
7
,
3
,
64
});
}
TEST_F
(
FusedConv2dOpTest
,
OPENCL15X1ConvNxNS12
)
{
TestGeneralConvNxNS12
<
DeviceType
::
OPENCL
,
float
>
({
40
,
40
},
{
15
,
1
,
32
,
64
});
}
mace/python/tools/tf_converter.py
浏览文件 @
4eb498a6
...
...
@@ -24,7 +24,7 @@ def main(unused_args):
input_graph_def
,
FLAGS
.
input_node
,
FLAGS
.
output_node
,
FLAGS
.
prequantize
)
else
:
output_graph_def
=
tf_converter_lib
.
convert_to_mace_pb
(
input_graph_def
,
FLAGS
.
runtime
)
input_graph_def
,
FLAGS
.
input_node
,
FLAGS
.
output_node
,
FLAGS
.
data_type
,
FLAGS
.
runtime
)
with
gfile
.
GFile
(
FLAGS
.
output
,
"wb"
)
as
f
:
f
.
write
(
output_graph_def
.
SerializeToString
())
...
...
@@ -67,6 +67,11 @@ def parse_args():
type
=
bool
,
default
=
False
,
help
=
"e.g., False"
)
parser
.
add_argument
(
"--data_type"
,
type
=
str
,
default
=
'DT_FLOAT'
,
help
=
"e.g., DT_HALF/DT_FLOAT"
)
return
parser
.
parse_known_args
()
...
...
mace/python/tools/tf_converter_lib.py
浏览文件 @
4eb498a6
此差异已折叠。
点击以展开。
tools/validate
_icnet
.py
→
tools/validate.py
浏览文件 @
4eb498a6
import
argparse
import
sys
import
os
import
os.path
import
tensorflow
as
tf
import
numpy
as
np
...
...
@@ -13,28 +15,35 @@ from tensorflow import gfile
# 3. adb pull the result.
# 4. Compare output data of mace and tf
# python validate_icnet.py --model_file opt_icnet.pb \
# --
tf_
input_file input_file \
# --input_file input_file \
# --mace_out_file icnet.out
def
generate_data
(
shape
):
np
.
random
.
seed
(
FLAGS
.
random_seed
)
data
=
np
.
random
.
random
(
shape
)
print
FLAGS
.
tf_input_file
data
.
astype
(
np
.
float32
).
tofile
(
FLAGS
.
tf_input_file
)
mace_data
=
np
.
transpose
(
data
,
axes
=
(
2
,
0
,
1
))
mace_data
.
astype
(
np
.
float32
).
tofile
(
FLAGS
.
mace_input_file
)
print
FLAGS
.
input_file
data
.
astype
(
np
.
float32
).
tofile
(
FLAGS
.
input_file
)
print
"Generate input file done."
def
load_data
(
file
):
return
np
.
fromfile
(
file
=
file
,
dtype
=
np
.
float32
)
if
os
.
path
.
isfile
(
file
):
return
np
.
fromfile
(
file
=
file
,
dtype
=
np
.
float32
)
else
:
return
np
.
empty
([
0
])
def
valid_output
(
out_shape
,
mace_out_file
,
tf_out_value
):
mace_out_value
=
load_data
(
mace_out_file
)
mace_out_value
=
mace_out_value
.
reshape
(
out_shape
)
tf_out_data_t
=
np
.
transpose
(
tf_out_value
,
axes
=
(
0
,
3
,
1
,
2
))
res
=
np
.
allclose
(
mace_out_value
,
tf_out_data_t
,
rtol
=
0
,
atol
=
1e-5
)
print
'Passed! Haha'
if
res
else
'Failed! Oops'
if
mace_out_value
.
size
!=
0
:
mace_out_value
=
mace_out_value
.
reshape
(
out_shape
)
np
.
testing
.
assert_allclose
(
tf_out_value
,
mace_out_value
,
rtol
=
0
,
atol
=
1e-3
)
res
=
np
.
allclose
(
tf_out_value
,
mace_out_value
,
rtol
=
0
,
atol
=
1e-3
)
if
res
:
print
'=======================Passed! Haha======================'
else
:
print
'=======================Failed! Oops======================'
else
:
print
'=======================Skip empty node==================='
def
run_model
(
input_shape
):
...
...
@@ -51,13 +60,14 @@ def run_model(input_shape):
with
tf
.
Session
()
as
session
:
with
session
.
graph
.
as_default
()
as
graph
:
tf
.
import_graph_def
(
input_graph_def
,
name
=
""
)
input_node
=
graph
.
get_tensor_by_name
(
'input_node
:0'
)
output_node
=
graph
.
get_tensor_by_name
(
'output_node
:0'
)
input_node
=
graph
.
get_tensor_by_name
(
FLAGS
.
input_node
+
'
:0'
)
output_node
=
graph
.
get_tensor_by_name
(
FLAGS
.
output_node
+
'
:0'
)
input_value
=
load_data
(
FLAGS
.
tf_
input_file
)
input_value
=
load_data
(
FLAGS
.
input_file
)
input_value
=
input_value
.
reshape
(
input_shape
)
output_value
=
session
.
run
(
output_node
,
feed_dict
=
{
input_node
:
[
input_value
]})
# output_value.astype(np.float32).tofile( os.path.dirname(FLAGS.input_file) + '/tf_weight')
return
output_value
def
main
(
unused_args
):
...
...
@@ -80,15 +90,10 @@ def parse_args():
default
=
""
,
help
=
"TensorFlow
\'
GraphDef
\'
file to load."
)
parser
.
add_argument
(
"--tf_input_file"
,
type
=
str
,
default
=
""
,
help
=
"tensorflow input data to load."
)
parser
.
add_argument
(
"--mace_input_file"
,
"--input_file"
,
type
=
str
,
default
=
""
,
help
=
"
mace input data to load
."
)
help
=
"
input file
."
)
parser
.
add_argument
(
"--mace_out_file"
,
type
=
str
,
...
...
@@ -97,13 +102,23 @@ def parse_args():
parser
.
add_argument
(
"--input_shape"
,
type
=
str
,
default
=
"
480,480
,3"
,
default
=
"
512,512
,3"
,
help
=
"input shape."
)
parser
.
add_argument
(
"--output_shape"
,
type
=
str
,
default
=
"1,
2,480,480
"
,
default
=
"1,
512,512,2
"
,
help
=
"output shape."
)
parser
.
add_argument
(
"--input_node"
,
type
=
str
,
default
=
"input_node"
,
help
=
"input node"
)
parser
.
add_argument
(
"--output_node"
,
type
=
str
,
default
=
"output_node"
,
help
=
"output node"
)
parser
.
add_argument
(
"--generate_data"
,
type
=
'bool'
,
...
...
tools/validate_gcn.sh
0 → 100644
浏览文件 @
4eb498a6
#!/bin/bash
# Must run at root dir of mace project.
Usage
()
{
echo
'Usage: bash tools/validate_gcn.sh tf_model_file'
}
if
[
$#
!=
1
]
;
then
Usage
exit
-1
fi
TF_MODEL_FILE_PATH
=
$1
MODEL_DIR
=
$(
dirname
${
TF_MODEL_FILE_PATH
}
)
MACE_MODEL_NAME
=
'mace_model.pb'
INPUT_FILE_NAME
=
'model_input'
OUTPUT_FILE_NAME
=
'gcn.out'
OUTPUT_LIST_FILE
=
'gcn.list'
PHONE_DATA_DIR
=
"/data/local/tmp/
${
MACE_MODEL_NAME
}
"
KERNEL_DIR
=
"
${
PHONE_DATA_DIR
}
/cl/"
# Step 1: Generate input data
echo
"Step 1: Generate input data"
python tools/validate.py
--generate_data
true
--random_seed
1
\
--input_file
=
${
MODEL_DIR
}
/
${
INPUT_FILE_NAME
}
\
--input_shape
=
512,512,3
# Step 2: convert tf model to mace model
echo
"Step 2: convert tf model to mace model"
bazel build //mace/python/tools:tf_converter
bazel-bin/mace/python/tools/tf_converter
--input
=
${
TF_MODEL_FILE_PATH
}
\
--output
=
${
MODEL_DIR
}
/
${
MACE_MODEL_NAME
}
\
--input_node
=
input
\
--output_node
=
GCN/br_result_2/fcn_br
\
--data_type
=
DT_FLOAT
\
--runtime
=
gpu
# Step 3: Run model on the phone
echo
"Step 3: Run model on the phone"
bazel build
-c
opt
--strip
always mace/examples:mace_run
\
--crosstool_top
=
//external:android/crosstool
\
--host_crosstool_top
=
@bazel_tools//tools/cpp:toolchain
\
--cpu
=
arm64-v8a
adb shell
"mkdir -p
${
PHONE_DATA_DIR
}
"
adb shell
"mkdir -p
${
KERNEL_DIR
}
"
adb push mace/kernels/opencl/cl/
*
${
KERNEL_DIR
}
adb push
${
MODEL_DIR
}
/
${
MACE_MODEL_NAME
}
${
PHONE_DATA_DIR
}
adb push
${
MODEL_DIR
}
/
${
INPUT_FILE_NAME
}
${
PHONE_DATA_DIR
}
adb push bazel-bin/mace/examples/mace_run
${
PHONE_DATA_DIR
}
num_threads
=
${
1
:-
1
}
adb </dev/null shell
MACE_RUN_PARAMETER_PATH
=
${
PHONE_DATA_DIR
}
/mace_run.config
\
MACE_KERNEL_PATH
=
$KERNEL_DIR
\
OMP_NUM_THREADS
=
$num_threads
\
${
PHONE_DATA_DIR
}
/mace_run
\
--model
=
${
PHONE_DATA_DIR
}
/
${
MACE_MODEL_NAME
}
\
--input
=
mace_input_node
\
--output
=
mace_output_node
\
--input_shape
=
1,512,512,3
\
--input_file
=
${
PHONE_DATA_DIR
}
/
${
INPUT_FILE_NAME
}
\
--output_file
=
${
PHONE_DATA_DIR
}
/
${
OUTPUT_FILE_NAME
}
\
--device
=
OPENCL
# Step 4: pull the mace run result.
echo
"Step 4: pull the mace run result."
rm
-rf
${
MODEL_DIR
}
/
${
OUTPUT_FILE_NAME
}
adb </dev/null pull
${
PHONE_DATA_DIR
}
/
${
OUTPUT_FILE_NAME
}
${
MODEL_DIR
}
# Step 5: validate the result
echo
"Step 5: validate the result"
python tools/validate.py
--model_file
${
TF_MODEL_FILE_PATH
}
\
--input_file
${
MODEL_DIR
}
/
${
INPUT_FILE_NAME
}
\
--mace_out_file
${
MODEL_DIR
}
/
${
OUTPUT_FILE_NAME
}
\
--input_node
input
\
--output_node
GCN/br_result_2/fcn_br
\
--output_shape
1,512,512,2
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录