Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
慢慢CG
Mace
提交
3d903175
Mace
项目概览
慢慢CG
/
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看板
提交
3d903175
编写于
1月 18, 2018
作者:
Y
yejianwu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
move distribute tools from mace
上级
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
290 addition
and
0 deletion
+290
-0
model.config
model.config
+4
-0
validate.py
validate.py
+133
-0
validate_model.sh
validate_model.sh
+153
-0
未找到文件。
model.config
0 → 100644
浏览文件 @
3d903175
TF_INPUT_NODE
=
input
TF_OUTPUT_NODE
=
softmax
/
Reshape_1
TF_OUTPUT_BR_NODE
=
GCN
/
br_result_2
/
fcn_br
\ No newline at end of file
validate.py
0 → 100644
浏览文件 @
3d903175
import
argparse
import
sys
import
os
import
os.path
import
tensorflow
as
tf
import
numpy
as
np
from
scipy
import
spatial
from
tensorflow
import
gfile
# Validation Flow:
# 1. Generate input data
# python validate_icnet.py --generate_data 1
#
# 2. Use mace_run to run icnet on phone.
# 3. adb pull the result.
# 4. Compare output data of mace and tf
# python validate_icnet.py --model_file opt_icnet.pb \
# --input_file input_file \
# --mace_out_file icnet.out
def
generate_data
(
shape
):
np
.
random
.
seed
()
data
=
np
.
random
.
random
(
shape
)
*
2
-
1
print
FLAGS
.
input_file
data
.
astype
(
np
.
float32
).
tofile
(
FLAGS
.
input_file
)
print
"Generate input file done."
def
load_data
(
file
):
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
)
if
mace_out_value
.
size
!=
0
:
similarity
=
(
1
-
spatial
.
distance
.
cosine
(
tf_out_value
.
flat
,
mace_out_value
))
print
'MACE VS TF similarity: '
,
similarity
if
similarity
>
0.995
:
print
'=======================Similarity Test Passed======================'
else
:
print
'=======================Similarity Test Failed======================'
else
:
print
'=======================Skip empty node==================='
def
run_model
(
input_shape
):
if
not
gfile
.
Exists
(
FLAGS
.
model_file
):
print
(
"Input graph file '"
+
FLAGS
.
model_file
+
"' does not exist!"
)
return
-
1
input_graph_def
=
tf
.
GraphDef
()
with
gfile
.
Open
(
FLAGS
.
model_file
,
"rb"
)
as
f
:
data
=
f
.
read
()
input_graph_def
.
ParseFromString
(
data
)
tf
.
import_graph_def
(
input_graph_def
,
name
=
""
)
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
(
FLAGS
.
input_node
+
':0'
)
output_node
=
graph
.
get_tensor_by_name
(
FLAGS
.
output_node
+
':0'
)
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_out'
)
return
output_value
def
main
(
unused_args
):
input_shape
=
[
int
(
x
)
for
x
in
FLAGS
.
input_shape
.
split
(
','
)]
output_shape
=
[
int
(
x
)
for
x
in
FLAGS
.
output_shape
.
split
(
','
)]
if
FLAGS
.
generate_data
:
generate_data
(
input_shape
)
else
:
output_value
=
run_model
(
input_shape
)
valid_output
(
output_shape
,
FLAGS
.
mace_out_file
,
output_value
)
def
parse_args
():
"""Parses command line arguments."""
parser
=
argparse
.
ArgumentParser
()
parser
.
register
(
"type"
,
"bool"
,
lambda
v
:
v
.
lower
()
==
"true"
)
parser
.
add_argument
(
"--model_file"
,
type
=
str
,
default
=
""
,
help
=
"TensorFlow
\'
GraphDef
\'
file to load."
)
parser
.
add_argument
(
"--input_file"
,
type
=
str
,
default
=
""
,
help
=
"input file."
)
parser
.
add_argument
(
"--mace_out_file"
,
type
=
str
,
default
=
""
,
help
=
"mace output file to load."
)
parser
.
add_argument
(
"--input_shape"
,
type
=
str
,
default
=
"512,512,3"
,
help
=
"input shape."
)
parser
.
add_argument
(
"--output_shape"
,
type
=
str
,
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'
,
default
=
"false"
,
help
=
"Generate data or not."
)
return
parser
.
parse_known_args
()
if
__name__
==
'__main__'
:
FLAGS
,
unparsed
=
parse_args
()
main
(
unused_args
=
[
sys
.
argv
[
0
]]
+
unparsed
)
validate_model.sh
0 → 100755
浏览文件 @
3d903175
#!/bin/bash
# Must run at root dir of mace project.
set
+x
Usage
()
{
echo
'Usage: bash tools/validate_gcn.sh tools/gcn.config tf_model_path model_tag image_size runtime[gpu/dsp] [tuning]'
}
if
[
$#
-lt
5
]
;
then
Usage
exit
-1
fi
source
$1
TF_MODEL_FILE_PATH
=
$2
MODEL_TAG
=
$3
IMAGE_SIZE
=
$4
RUNTIME
=
$5
TUNING_OR_NOT
=
${
6
:-
0
}
if
[
x
"
$RUNTIME
"
=
x
"dsp"
]
;
then
DATA_TYPE
=
"DT_UINT8"
DEVICE_TYPE
=
"HEXAGON"
TF_OUTPUT_NODE
=
${
TF_OUTPUT_BR_NODE
}
else
DATA_TYPE
=
"DT_HALF"
DEVICE_TYPE
=
"OPENCL"
fi
VLOG_LEVEL
=
0
MODEL_DIR
=
$(
dirname
${
TF_MODEL_FILE_PATH
}
)
LIBMACE_SOURCE_DIR
=
`
/bin/pwd
`
INPUT_FILE_NAME
=
'model_input'
OUTPUT_FILE_NAME
=
'model.out'
OUTPUT_LIST_FILE
=
'model.list'
PHONE_DATA_DIR
=
"/data/local/tmp/mace_run"
KERNEL_DIR
=
"
${
PHONE_DATA_DIR
}
/cl/"
CODEGEN_DIR
=
${
LIBMACE_SOURCE_DIR
}
/codegen
MODEL_CODEGEN_DIR
=
${
CODEGEN_DIR
}
/models/
${
MODEL_TAG
}
CL_CODEGEN_DIR
=
${
CODEGEN_DIR
}
/opencl
CL_BIN_DIR
=
${
CODEGEN_DIR
}
/opencl_bin
TUNING_CODEGEN_DIR
=
${
CODEGEN_DIR
}
/tuning
VERSION_SOURCE_PATH
=
${
CODEGEN_DIR
}
/version
build_and_run
()
{
PRODUCTION_MODE
=
$1
if
[
"
$PRODUCTION_MODE
"
=
true
]
;
then
PRODUCTION_MODE_BUILD_FLAGS
=
"--define production=true"
fi
if
[[
"
${
TUNING_OR_NOT
}
"
!=
"0"
&&
"
$PRODUCTION_MODE
"
!=
true
]]
;
then
tuning_flag
=
1
round
=
0
# only warm up
else
tuning_flag
=
0
round
=
2
fi
if
[
x
"
$RUNTIME
"
=
x
"dsp"
]
;
then
HEXAGON_MODE_BUILD_FLAGS
=
"--define hexagon=true"
fi
bazel build
--verbose_failures
-c
opt
--strip
always examples:mace_run
\
--crosstool_top
=
//external:android/crosstool
\
--host_crosstool_top
=
@bazel_tools//tools/cpp:toolchain
\
--cpu
=
armeabi-v7a
\
--copt
=
"-std=c++11"
\
--copt
=
"-D_GLIBCXX_USE_C99_MATH_TR1"
\
--copt
=
"-Werror=return-type"
\
--copt
=
"-DMACE_MODEL_TAG=
${
MODEL_TAG
}
"
\
--copt
=
"-DMACE_OBFUSCATE_LITERALS"
\
$PRODUCTION_MODE_BUILD_FLAGS
\
$HEXAGON_MODE_BUILD_FLAGS
||
exit
-1
adb shell
"mkdir -p
${
PHONE_DATA_DIR
}
"
||
exit
-1
if
[
"
$PRODUCTION_MODE
"
=
false
]
;
then
adb shell
"mkdir -p
${
KERNEL_DIR
}
"
||
exit
-1
fi
adb push
${
MODEL_DIR
}
/
${
INPUT_FILE_NAME
}
${
PHONE_DATA_DIR
}
||
exit
-1
adb push bazel-bin/examples/mace_run
${
PHONE_DATA_DIR
}
||
exit
-1
adb push lib/hexagon/libhexagon_controller.so
${
PHONE_DATA_DIR
}
||
exit
0
adb </dev/null shell
\
LD_LIBRARY_PATH
=
${
PHONE_DATA_DIR
}
\
MACE_TUNING
=
${
tuning_flag
}
\
MACE_CPP_MIN_VLOG_LEVEL
=
$VLOG_LEVEL
\
MACE_RUN_PARAMETER_PATH
=
${
PHONE_DATA_DIR
}
/mace_run.config
\
MACE_KERNEL_PATH
=
$KERNEL_DIR
\
${
PHONE_DATA_DIR
}
/mace_run
\
--input_shape
=
"1,
${
IMAGE_SIZE
}
,
${
IMAGE_SIZE
}
,3"
\
--output_shape
=
"1,
${
IMAGE_SIZE
}
,
${
IMAGE_SIZE
}
,2"
\
--input_file
=
${
PHONE_DATA_DIR
}
/
${
INPUT_FILE_NAME
}
\
--output_file
=
${
PHONE_DATA_DIR
}
/
${
OUTPUT_FILE_NAME
}
\
--device
=
${
DEVICE_TYPE
}
\
--round
=
$round
||
exit
-1
}
echo
"Step 1: Generate input data"
rm
-rf
${
MODEL_DIR
}
/
${
INPUT_FILE_NAME
}
python tools/validate.py
--generate_data
true
\
--input_file
=
${
MODEL_DIR
}
/
${
INPUT_FILE_NAME
}
\
--input_shape
=
"
${
IMAGE_SIZE
}
,
${
IMAGE_SIZE
}
,3"
||
exit
-1
echo
"Step 2: Convert tf model to mace model and optimize memory"
bazel build //lib/python/tools:tf_converter
||
exit
-1
rm
-rf
${
MODEL_CODEGEN_DIR
}
mkdir
-p
${
MODEL_CODEGEN_DIR
}
bazel-bin/lib/python/tools/tf_converter
--input
=
${
TF_MODEL_FILE_PATH
}
\
--output
=
${
MODEL_CODEGEN_DIR
}
/model.cc
\
--input_node
=
${
TF_INPUT_NODE
}
\
--output_node
=
${
TF_OUTPUT_NODE
}
\
--data_type
=
${
DATA_TYPE
}
\
--runtime
=
${
RUNTIME
}
\
--output_type
=
source
\
--template
=
${
LIBMACE_SOURCE_DIR
}
/lib/python/tools/model.template
\
--model_tag
=
${
MODEL_TAG
}
\
--obfuscate
=
True
||
exit
-1
echo
"Step 3: Run model on the phone with files"
build_and_run
false
echo
"Step 4: Generate OpenCL binary program and config code"
rm
-rf
${
CL_BIN_DIR
}
rm
-rf
${
CL_CODEGEN_DIR
}
mkdir
-p
${
CL_BIN_DIR
}
mkdir
-p
${
CL_CODEGEN_DIR
}
adb pull
${
KERNEL_DIR
}
${
CL_BIN_DIR
}
python lib/python/tools/opencl_codegen.py
\
--cl_binary_dir
=
${
CL_BIN_DIR
}
--output_path
=
${
CL_CODEGEN_DIR
}
/opencl_compiled_program.cc
echo
"Step 5: Generate tuning source file"
adb pull
${
PHONE_DATA_DIR
}
/mace_run.config
${
CL_BIN_DIR
}
rm
-rf
${
TUNING_CODEGEN_DIR
}
mkdir
-p
${
TUNING_CODEGEN_DIR
}
python lib/python/tools/binary_codegen.py
\
--binary_file
=
${
CL_BIN_DIR
}
/mace_run.config
--output_path
=
${
TUNING_CODEGEN_DIR
}
/tuning_params.cc
echo
"Step 6: Run model on the phone using binary"
build_and_run
true
echo
"Step 7: Pull the mace run result."
rm
-rf
${
MODEL_DIR
}
/
${
OUTPUT_FILE_NAME
}
adb </dev/null pull
${
PHONE_DATA_DIR
}
/
${
OUTPUT_FILE_NAME
}
${
MODEL_DIR
}
echo
"Step 8: 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
${
TF_INPUT_NODE
}
\
--output_node
${
TF_OUTPUT_NODE
}
\
--input_shape
"
${
IMAGE_SIZE
}
,
${
IMAGE_SIZE
}
,3"
\
--output_shape
"1,
${
IMAGE_SIZE
}
,
${
IMAGE_SIZE
}
,2"
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录