Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Oneflow-Inc
oneflow_convert_tools
提交
bc446342
O
oneflow_convert_tools
项目概览
Oneflow-Inc
/
oneflow_convert_tools
上一次同步 接近 3 年
通知
1
Star
22
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
O
oneflow_convert_tools
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
bc446342
编写于
5月 24, 2021
作者:
M
mosout
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add lenet with qat
上级
3649eac5
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
113 addition
and
0 deletion
+113
-0
examples/oneflow2onnx/models/test_lenet_qat.py
examples/oneflow2onnx/models/test_lenet_qat.py
+113
-0
未找到文件。
examples/oneflow2onnx/models/test_lenet_qat.py
0 → 100644
浏览文件 @
bc446342
import
tempfile
import
numpy
as
np
import
oneflow
as
flow
import
oneflow.typing
as
tp
def
Lenet
(
data
):
initializer
=
flow
.
truncated_normal
(
0.1
)
conv1
=
flow
.
layers
.
conv2d
(
data
,
32
,
5
,
padding
=
"SAME"
,
activation
=
flow
.
nn
.
relu
,
name
=
"conv1"
,
kernel_initializer
=
initializer
,
use_bias
=
False
,
)
pool1
=
flow
.
nn
.
max_pool2d
(
conv1
,
ksize
=
2
,
strides
=
2
,
padding
=
"VALID"
,
name
=
"pool1"
,
data_format
=
"NCHW"
)
conv2
=
flow
.
layers
.
conv2d
(
pool1
,
64
,
5
,
padding
=
"SAME"
,
activation
=
flow
.
nn
.
relu
,
name
=
"conv2"
,
kernel_initializer
=
initializer
,
use_bias
=
False
,
)
pool2
=
flow
.
nn
.
max_pool2d
(
conv2
,
ksize
=
2
,
strides
=
2
,
padding
=
"VALID"
,
name
=
"pool2"
,
data_format
=
"NCHW"
)
reshape
=
flow
.
reshape
(
pool2
,
[
pool2
.
shape
[
0
],
-
1
])
hidden
=
flow
.
layers
.
dense
(
reshape
,
512
,
activation
=
flow
.
nn
.
relu
,
kernel_initializer
=
initializer
,
name
=
"dense1"
,
use_bias
=
False
,
)
return
flow
.
layers
.
dense
(
hidden
,
10
,
kernel_initializer
=
initializer
,
name
=
"dense2"
,
use_bias
=
False
)
def
get_job_function
(
func_type
:
str
=
"train"
,
enable_qat
:
bool
=
True
,
batch_size
:
int
=
100
):
func_config
=
flow
.
FunctionConfig
()
if
enable_qat
:
func_config
.
enable_qat
(
True
)
func_config
.
qat
.
symmetric
(
True
)
func_config
.
qat
.
per_channel_weight_quantization
(
False
)
func_config
.
qat
.
moving_min_max_stop_update_after_iters
(
1000
)
func_config
.
qat
.
target_backend
(
"tensorrt7"
)
if
func_type
==
"train"
:
@
flow
.
global_function
(
type
=
"train"
,
function_config
=
func_config
)
def
train_job
(
images
:
tp
.
Numpy
.
Placeholder
((
batch_size
,
1
,
28
,
28
),
dtype
=
flow
.
float
),
labels
:
tp
.
Numpy
.
Placeholder
((
batch_size
,),
dtype
=
flow
.
int32
),
)
->
tp
.
Numpy
:
with
flow
.
scope
.
placement
(
"gpu"
,
"0:0"
):
logits
=
Lenet
(
images
)
loss
=
flow
.
nn
.
sparse_softmax_cross_entropy_with_logits
(
labels
,
logits
,
name
=
"softmax_loss"
)
lr_scheduler
=
flow
.
optimizer
.
PiecewiseConstantScheduler
([],
[
0.1
])
flow
.
optimizer
.
SGD
(
lr_scheduler
,
momentum
=
0
).
minimize
(
loss
)
return
loss
return
train_job
else
:
@
flow
.
global_function
(
type
=
"predict"
,
function_config
=
func_config
)
def
eval_job
(
images
:
tp
.
Numpy
.
Placeholder
((
batch_size
,
1
,
28
,
28
),
dtype
=
flow
.
float
),
)
->
tp
.
Numpy
:
with
flow
.
scope
.
placement
(
"gpu"
,
"0:0"
):
logits
=
Lenet
(
images
)
return
logits
return
eval_job
if
__name__
==
"__main__"
:
batch_size
=
100
(
train_images
,
train_labels
),
(
test_images
,
test_labels
)
=
flow
.
data
.
load_mnist
(
batch_size
,
batch_size
)
# train
train_job
=
get_job_function
(
"train"
,
batch_size
=
batch_size
)
for
epoch
in
range
(
1
):
for
i
,
(
images
,
labels
)
in
enumerate
(
zip
(
train_images
,
train_labels
)):
loss
=
train_job
(
images
,
labels
)
if
i
%
20
==
0
:
print
(
loss
.
mean
())
flow_weight_dir
=
tempfile
.
TemporaryDirectory
()
flow
.
checkpoint
.
save
(
flow_weight_dir
.
name
)
flow
.
clear_default_session
()
# predict
print
(
flow_weight_dir
.
name
)
eval_job
=
get_job_function
(
"predict"
,
batch_size
=
batch_size
)
flow
.
load_variables
(
flow
.
checkpoint
.
get
(
flow_weight_dir
.
name
))
for
k
,
v
in
flow
.
get_all_variables
().
items
():
print
(
k
)
print
(
v
.
numpy
())
data
=
np
.
ones
((
batch_size
,
1
,
28
,
28
))
print
(
eval_job
(
data
))
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录