Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
magicwindyyd
mindspore
提交
e9eee16e
M
mindspore
项目概览
magicwindyyd
/
mindspore
与 Fork 源项目一致
Fork自
MindSpore / mindspore
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
mindspore
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
e9eee16e
编写于
8月 18, 2020
作者:
M
mindspore-ci-bot
提交者:
Gitee
8月 18, 2020
浏览文件
操作
浏览文件
下载
差异文件
!4641 [MS][Quant] bug fix for lenet quant
Merge pull request !4641 from chenzhongming/new_master
上级
e60c0b60
e1d6c1ac
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
73 addition
and
8 deletion
+73
-8
mindspore/train/quant/quant.py
mindspore/train/quant/quant.py
+5
-5
model_zoo/official/cv/lenet_quant/src/lenet.py
model_zoo/official/cv/lenet_quant/src/lenet.py
+4
-0
model_zoo/official/cv/lenet_quant/src/lenet_fusion.py
model_zoo/official/cv/lenet_quant/src/lenet_fusion.py
+2
-2
model_zoo/official/cv/lenet_quant/src/lenet_quant.py
model_zoo/official/cv/lenet_quant/src/lenet_quant.py
+61
-0
model_zoo/official/cv/lenet_quant/train_quant.py
model_zoo/official/cv/lenet_quant/train_quant.py
+1
-1
未找到文件。
mindspore/train/quant/quant.py
浏览文件 @
e9eee16e
...
...
@@ -475,8 +475,8 @@ def export(network, *inputs, file_name, mean=127.5, std_dev=127.5, file_format='
def
convert_quant_network
(
network
,
bn_fold
=
Fals
e
,
freeze_bn
=
1
0000
,
bn_fold
=
Tru
e
,
freeze_bn
=
1
e7
,
quant_delay
=
(
0
,
0
),
num_bits
=
(
8
,
8
),
per_channel
=
(
False
,
False
),
...
...
@@ -488,11 +488,11 @@ def convert_quant_network(network,
Args:
network (Cell): Obtain a pipeline through network for saving graph summary.
bn_fold (bool): Flag to used bn fold ops for simulation inference operation. Default:
Fals
e.
freeze_bn (int): Number of steps after which BatchNorm OP parameters used total mean and variance. Default:
0
.
bn_fold (bool): Flag to used bn fold ops for simulation inference operation. Default:
Tru
e.
freeze_bn (int): Number of steps after which BatchNorm OP parameters used total mean and variance. Default:
1e7
.
quant_delay (int, list or tuple): Number of steps after which weights and activations are quantized during
eval. The first element represent weights and second element represent data flow. Default: (0, 0)
num_bits (int, list or tuple): Number of bits to use for quantiz
ing
weights and activations. The first
num_bits (int, list or tuple): Number of bits to use for quantiz
e
weights and activations. The first
element represent weights and second element represent data flow. Default: (8, 8)
per_channel (bool, list or tuple): Quantization granularity based on layer or on channel. If `True`
then base on per channel otherwise base on per layer. The first element represent weights
...
...
model_zoo/official/cv/lenet_quant/src/lenet.py
浏览文件 @
e9eee16e
...
...
@@ -35,7 +35,9 @@ class LeNet5(nn.Cell):
self
.
num_class
=
num_class
self
.
conv1
=
nn
.
Conv2d
(
channel
,
6
,
5
,
pad_mode
=
'valid'
)
self
.
bn1
=
nn
.
BatchNorm2d
(
6
)
self
.
conv2
=
nn
.
Conv2d
(
6
,
16
,
5
,
pad_mode
=
'valid'
)
self
.
bn2
=
nn
.
BatchNorm2d
(
16
)
self
.
fc1
=
nn
.
Dense
(
16
*
5
*
5
,
120
)
self
.
fc2
=
nn
.
Dense
(
120
,
84
)
self
.
fc3
=
nn
.
Dense
(
84
,
self
.
num_class
)
...
...
@@ -46,9 +48,11 @@ class LeNet5(nn.Cell):
def
construct
(
self
,
x
):
x
=
self
.
conv1
(
x
)
x
=
self
.
bn1
(
x
)
x
=
self
.
relu
(
x
)
x
=
self
.
max_pool2d
(
x
)
x
=
self
.
conv2
(
x
)
x
=
self
.
bn2
(
x
)
x
=
self
.
relu
(
x
)
x
=
self
.
max_pool2d
(
x
)
x
=
self
.
flatten
(
x
)
...
...
model_zoo/official/cv/lenet_quant/src/lenet_fusion.py
浏览文件 @
e9eee16e
...
...
@@ -36,8 +36,8 @@ class LeNet5(nn.Cell):
self
.
num_class
=
num_class
# change `nn.Conv2d` to `nn.Conv2dBnAct`
self
.
conv1
=
nn
.
Conv2dBnAct
(
channel
,
6
,
5
,
pad_mode
=
'valid'
,
activation
=
'relu'
)
self
.
conv2
=
nn
.
Conv2dBnAct
(
6
,
16
,
5
,
pad_mode
=
'valid'
,
activation
=
'relu'
)
self
.
conv1
=
nn
.
Conv2dBnAct
(
channel
,
6
,
5
,
pad_mode
=
'valid'
,
has_bn
=
True
,
activation
=
'relu'
)
self
.
conv2
=
nn
.
Conv2dBnAct
(
6
,
16
,
5
,
pad_mode
=
'valid'
,
has_bn
=
True
,
activation
=
'relu'
)
# change `nn.Dense` to `nn.DenseBnAct`
self
.
fc1
=
nn
.
DenseBnAct
(
16
*
5
*
5
,
120
,
activation
=
'relu'
)
self
.
fc2
=
nn
.
DenseBnAct
(
120
,
84
,
activation
=
'relu'
)
...
...
model_zoo/official/cv/lenet_quant/src/lenet_quant.py
0 → 100644
浏览文件 @
e9eee16e
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""Manual construct network for LeNet"""
import
mindspore.nn
as
nn
class
LeNet5
(
nn
.
Cell
):
"""
Lenet network
Args:
num_class (int): Num classes. Default: 10.
Returns:
Tensor, output tensor
Examples:
>>> LeNet(num_class=10)
"""
def
__init__
(
self
,
num_class
=
10
,
channel
=
1
):
super
(
LeNet5
,
self
).
__init__
()
self
.
num_class
=
num_class
self
.
conv1
=
nn
.
Conv2dBnFoldQuant
(
channel
,
6
,
5
,
pad_mode
=
'valid'
,
per_channel
=
True
,
quant_delay
=
900
)
self
.
conv2
=
nn
.
Conv2dBnFoldQuant
(
6
,
16
,
5
,
pad_mode
=
'valid'
,
per_channel
=
True
,
quant_delay
=
900
)
self
.
fc1
=
nn
.
DenseQuant
(
16
*
5
*
5
,
120
,
per_channel
=
True
,
quant_delay
=
900
)
self
.
fc2
=
nn
.
DenseQuant
(
120
,
84
,
per_channel
=
True
,
quant_delay
=
900
)
self
.
fc3
=
nn
.
DenseQuant
(
84
,
self
.
num_class
,
per_channel
=
True
,
quant_delay
=
900
)
self
.
relu
=
nn
.
ActQuant
(
nn
.
ReLU
(),
per_channel
=
False
,
quant_delay
=
900
)
self
.
max_pool2d
=
nn
.
MaxPool2d
(
kernel_size
=
2
,
stride
=
2
)
self
.
flatten
=
nn
.
Flatten
()
def
construct
(
self
,
x
):
x
=
self
.
conv1
(
x
)
x
=
self
.
relu
(
x
)
x
=
self
.
max_pool2d
(
x
)
x
=
self
.
conv2
(
x
)
x
=
self
.
relu
(
x
)
x
=
self
.
max_pool2d
(
x
)
x
=
self
.
flatten
(
x
)
x
=
self
.
fc1
(
x
)
x
=
self
.
relu
(
x
)
x
=
self
.
fc2
(
x
)
x
=
self
.
relu
(
x
)
x
=
self
.
fc3
(
x
)
return
x
model_zoo/official/cv/lenet_quant/train_quant.py
浏览文件 @
e9eee16e
...
...
@@ -57,7 +57,7 @@ if __name__ == "__main__":
load_param_into_net
(
network
,
param_dict
)
# convert fusion network to quantization aware network
network
=
quant
.
convert_quant_network
(
network
,
quant_delay
=
0
,
bn_fold
=
False
,
freeze_bn
=
10000
)
network
=
quant
.
convert_quant_network
(
network
,
quant_delay
=
900
,
per_channel
=
[
True
,
False
],
symmetric
=
[
False
,
False
]
)
# define network loss
net_loss
=
nn
.
SoftmaxCrossEntropyWithLogits
(
is_grad
=
False
,
sparse
=
True
,
reduction
=
"mean"
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录