Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Oneflow-Inc
OneFlow-Benchmark
提交
9c0c2dff
O
OneFlow-Benchmark
项目概览
Oneflow-Inc
/
OneFlow-Benchmark
上一次同步 2 年多
通知
1
Star
92
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
O
OneFlow-Benchmark
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
9c0c2dff
编写于
7月 13, 2020
作者:
L
Lyon
提交者:
GitHub
7月 13, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #32 from Oneflow-Inc/of_develop_py3_luyang
Of develop py3 luyang
上级
3ea36036
96e057f7
变更
3
展开全部
隐藏空白更改
内联
并排
Showing
3 changed file
with
316 addition
and
77 deletion
+316
-77
Classification/cnns/README.md
Classification/cnns/README.md
+201
-77
Classification/cnns/docs/resnet50_lr_schedule.png
Classification/cnns/docs/resnet50_lr_schedule.png
+0
-0
Classification/cnns/resnet_to_onnx.py
Classification/cnns/resnet_to_onnx.py
+115
-0
未找到文件。
Classification/cnns/README.md
浏览文件 @
9c0c2dff
此差异已折叠。
点击以展开。
Classification/cnns/docs/resnet50_lr_schedule.png
0 → 100644
浏览文件 @
9c0c2dff
22.2 KB
Classification/cnns/resnet_to_onnx.py
0 → 100644
浏览文件 @
9c0c2dff
# from __future__ import absolute_import
# from __future__ import division
# from __future__ import print_function
import
os
import
oneflow
as
flow
import
onnxruntime
as
ort
import
numpy
as
np
import
time
from
PIL
import
Image
from
collections
import
OrderedDict
from
resnet_model
import
resnet50
from
imagenet1000_clsidx_to_labels
import
clsidx_2_labels
def
load_image
(
image_path
):
rgb_mean
=
[
123.68
,
116.779
,
103.939
]
rgb_std
=
[
58.393
,
57.12
,
57.375
]
print
(
image_path
)
im
=
Image
.
open
(
image_path
)
im
=
im
.
resize
((
224
,
224
))
im
=
im
.
convert
(
'RGB'
)
# 有的图像是单通道的,不加转换会报错
im
=
np
.
array
(
im
).
astype
(
'float32'
)
im
=
(
im
-
rgb_mean
)
/
rgb_std
im
=
np
.
transpose
(
im
,
(
2
,
0
,
1
))
im
=
np
.
expand_dims
(
im
,
axis
=
0
)
return
np
.
ascontiguousarray
(
im
,
'float32'
)
def
func_config
():
func_config
=
flow
.
FunctionConfig
()
func_config
.
default_data_type
(
flow
.
float
)
return
func_config
@
flow
.
global_function
(
func_config
())
def
InferenceNet
(
images
=
flow
.
FixedTensorDef
((
1
,
3
,
224
,
224
))):
logits
=
resnet50
(
images
,
training
=
False
)
predictions
=
flow
.
nn
.
softmax
(
logits
)
return
predictions
def
onnx_inference
(
image_path
,
onnx_model_path
,
ort_optimize
=
True
):
"""
test onnx model with onnx runtime
:param image_path: input image path
:param onnx_model_path: path of model.onnx
:param ort_optimize:
:return:
"""
assert
os
.
path
.
isfile
(
image_path
)
and
os
.
path
.
isfile
(
onnx_model_path
)
ort_sess_opt
=
ort
.
SessionOptions
()
ort_sess_opt
.
graph_optimization_level
=
\
ort
.
GraphOptimizationLevel
.
ORT_ENABLE_EXTENDED
if
ort_optimize
else
\
ort
.
GraphOptimizationLevel
.
ORT_DISABLE_ALL
sess
=
ort
.
InferenceSession
(
onnx_model_path
,
sess_options
=
ort_sess_opt
)
assert
len
(
sess
.
get_outputs
())
==
1
and
len
(
sess
.
get_inputs
())
<=
1
ipt_dict
=
OrderedDict
()
for
ipt
in
sess
.
get_inputs
():
ipt_dict
[
ipt
.
name
]
=
load_image
(
image_path
)
start
=
time
.
time
()
onnx_res
=
sess
.
run
([],
ipt_dict
)[
0
]
print
(
'Cost: %.4f s'
%
(
time
.
time
()
-
start
))
clsidx_onnx
=
onnx_res
.
argmax
()
print
(
'Onnx >> '
,
onnx_res
.
max
(),
clsidx_2_labels
[
clsidx_onnx
])
def
oneflow_to_onnx
(
job_func
,
flow_model_path
,
onnx_model_dir
,
external_data
=
False
):
"""
convert oneflow model to onnx model
:param job_func: inference function in oneflow
:param flow_model_path: input oneflow model path
:param onnx_model_dir: output dir path to save model.onnx
:param external_data:
:return: ture or false
"""
if
not
os
.
path
.
exists
(
onnx_model_dir
):
os
.
makedirs
(
onnx_model_dir
)
assert
os
.
path
.
exists
(
flow_model_path
)
and
os
.
path
.
isdir
(
onnx_model_dir
)
check_point
=
flow
.
train
.
CheckPoint
()
# it is a trick to keep check_point.save() from hanging when there is no variable
@
flow
.
global_function
(
flow
.
FunctionConfig
())
def
add_var
():
return
flow
.
get_variable
(
name
=
"trick"
,
shape
=
(
1
,),
dtype
=
flow
.
float
,
initializer
=
flow
.
random_uniform_initializer
(),
)
check_point
.
init
()
onnx_model_path
=
os
.
path
.
join
(
onnx_model_dir
,
os
.
path
.
basename
(
flow_model_path
)
+
'.onnx'
)
flow
.
onnx
.
export
(
job_func
,
flow_model_path
,
onnx_model_path
,
opset
=
11
,
external_data
=
external_data
)
print
(
'Convert to onnx success! >> '
,
onnx_model_path
)
return
onnx_model_path
if
__name__
==
"__main__"
:
# path = 'tiger.jpg'
path
=
'test_img/ILSVRC2012_val_00020287.JPEG'
flow_model_path
=
'/your/oneflow/model/path'
onnx_model_dir
=
'onnx/model'
# conver oneflow to onnx
onnx_model_path
=
oneflow_to_onnx
(
InferenceNet
,
flow_model_path
,
onnx_model_dir
,
external_data
=
False
)
# inference
onnx_inference
(
path
,
onnx_model_path
)
# Output:
# ILSVRC2012_val_00020287.JPEG
# Cost: 0.0319s
# Onnx >> 0.9924272 hay
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录