Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSlim
提交
b1e04ba7
P
PaddleSlim
项目概览
PaddlePaddle
/
PaddleSlim
大约 2 年 前同步成功
通知
51
Star
1434
Fork
344
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
53
列表
看板
标记
里程碑
合并请求
16
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleSlim
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
53
Issue
53
列表
看板
标记
里程碑
合并请求
16
合并请求
16
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
b1e04ba7
编写于
11月 11, 2021
作者:
W
wanghaoshuang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Modify quant model
上级
c7ec8584
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
112 addition
and
24 deletion
+112
-24
demo/dygraph/quant/train.py
demo/dygraph/quant/train.py
+45
-6
demo/models/dygraph/mobilenet_v3.py
demo/models/dygraph/mobilenet_v3.py
+2
-1
demo/models/mobilenet_v3.py
demo/models/mobilenet_v3.py
+3
-0
demo/quant/pact_quant_aware/train.py
demo/quant/pact_quant_aware/train.py
+10
-0
demo/quant/quant_aware/train.py
demo/quant/quant_aware/train.py
+42
-7
paddleslim/dygraph/quant/ptq.py
paddleslim/dygraph/quant/ptq.py
+8
-8
paddleslim/dygraph/quant/qat.py
paddleslim/dygraph/quant/qat.py
+2
-2
未找到文件。
demo/dygraph/quant/train.py
浏览文件 @
b1e04ba7
...
...
@@ -36,6 +36,8 @@ sys.path.append(os.path.join(os.path.dirname("__file__")))
from
optimizer
import
create_optimizer
sys
.
path
.
append
(
os
.
path
.
join
(
os
.
path
.
dirname
(
"__file__"
),
os
.
path
.
pardir
,
os
.
path
.
pardir
))
print
(
f
"sys.path:
{
sys
.
path
}
"
)
from
utility
import
add_arguments
,
print_arguments
from
models.dygraph.mobilenet_v3
import
MobileNetV3_large_x1_0
...
...
@@ -74,8 +76,8 @@ def load_dygraph_pretrain(model, path=None, load_static_weights=False):
for
key
in
model_dict
.
keys
():
weight_name
=
model_dict
[
key
].
name
if
weight_name
in
pre_state_dict
.
keys
():
print
(
'Load weight: {}, shape: {}'
.
format
(
weight_name
,
pre_state_dict
[
weight_name
].
shape
))
#
print('Load weight: {}, shape: {}'.format(
#
weight_name, pre_state_dict[weight_name].shape))
param_state_dict
[
key
]
=
pre_state_dict
[
weight_name
]
else
:
param_state_dict
[
key
]
=
model_dict
[
key
]
...
...
@@ -125,7 +127,7 @@ def compress(args):
else
:
raise
ValueError
(
"{} is not supported."
.
format
(
args
.
model
))
_logger
.
info
(
"Origin model summary:"
)
paddle
.
summary
(
net
,
(
1
,
3
,
224
,
224
))
#
paddle.summary(net, (1, 3, 224, 224))
############################################################################################################
# 1. quantization configs
...
...
@@ -163,7 +165,18 @@ def compress(args):
quanter
=
QAT
(
config
=
quant_config
)
quanter
.
quantize
(
net
)
_logger
.
info
(
"QAT model summary:"
)
path
=
os
.
path
.
join
(
args
.
model_save_dir
,
"inference_model"
,
'qat_model'
)
quanter
.
save_quantized_model
(
net
,
path
,
input_spec
=
[
paddle
.
static
.
InputSpec
(
shape
=
[
None
,
3
,
224
,
224
],
dtype
=
'float32'
)
])
return
# _logger.info("QAT model summary:")
paddle
.
summary
(
net
,
(
1
,
3
,
224
,
224
))
opt
,
lr
=
create_optimizer
(
net
,
trainer_num
,
args
)
...
...
@@ -178,7 +191,7 @@ def compress(args):
batch_sampler
=
train_batch_sampler
,
places
=
place
,
return_list
=
True
,
num_workers
=
4
)
num_workers
=
1
)
valid_loader
=
paddle
.
io
.
DataLoader
(
val_dataset
,
...
...
@@ -187,7 +200,14 @@ def compress(args):
shuffle
=
False
,
drop_last
=
False
,
return_list
=
True
,
num_workers
=
4
)
num_workers
=
1
)
# model_prefix = os.path.join(args.model_save_dir, "step_" + str(773))
# net.set_dict(paddle.load(model_prefix + ".pdparams"))
# opt.set_dict(paddle.load(model_prefix + ".pdopt"))
@
paddle
.
no_grad
()
def
test
(
epoch
,
net
):
...
...
@@ -304,6 +324,25 @@ def compress(args):
train_reader_cost
=
0.0
train_run_cost
=
0.0
total_samples
=
0
# sys.exit(0)
# if batch_id > 0 and batch_id < 2:
# model_prefix = os.path.join(args.model_save_dir, "step_" + str(batch_id))
# paddle.save(net.state_dict(), model_prefix + ".pdparams")
# paddle.save(opt.state_dict(), model_prefix + ".pdopt")
# path = os.path.join(args.model_save_dir, "inference_model", str(batch_id))
# #model_prefix = os.path.join(args.model_save_dir, "inference_model", str(batch_id), f"{batch_id}.pdparams")
# #paddle.save(net.state_dict(), model_prefix)
# quanter.save_quantized_model(
# net,
# path,
# input_spec=[
# paddle.static.InputSpec(
# shape=[None, 3, 224, 224], dtype='float32')
# ])
# if batch_id > 10:
# sys.exit(0)
batch_id
+=
1
reader_start
=
time
.
time
()
...
...
demo/models/dygraph/mobilenet_v3.py
浏览文件 @
b1e04ba7
...
...
@@ -159,9 +159,10 @@ class MobileNetV3(nn.Layer):
def
forward
(
self
,
inputs
,
label
=
None
):
x
=
self
.
conv1
(
inputs
)
i
=
0
for
block
in
self
.
block_list
:
x
=
block
(
x
)
i
+=
1
x
=
self
.
last_second_conv
(
x
)
x
=
self
.
pool
(
x
)
...
...
demo/models/mobilenet_v3.py
浏览文件 @
b1e04ba7
...
...
@@ -15,6 +15,7 @@ __all__ = [
class
MobileNetV3
():
def
__init__
(
self
,
scale
=
1.0
,
model_name
=
'small'
):
print
(
"init mobilenetv3------------------------"
)
self
.
scale
=
scale
self
.
inplanes
=
16
if
model_name
==
"large"
:
...
...
@@ -59,6 +60,7 @@ class MobileNetV3():
raise
NotImplementedError
def
net
(
self
,
input
,
class_dim
=
1000
):
print
(
"mobilenetv3 net-------------------------"
)
scale
=
self
.
scale
inplanes
=
self
.
inplanes
cfg
=
self
.
cfg
...
...
@@ -77,6 +79,7 @@ class MobileNetV3():
if_act
=
True
,
act
=
'hard_swish'
,
name
=
'conv1'
)
i
=
0
for
layer_cfg
in
cfg
:
conv
=
self
.
residual_unit
(
...
...
demo/quant/pact_quant_aware/train.py
浏览文件 @
b1e04ba7
...
...
@@ -285,6 +285,16 @@ def compress(args):
optimizer_func
=
optimizer_func
,
executor
=
executor
,
for_test
=
True
)
for
param
in
val_program
.
global_block
().
all_parameters
():
print
(
f
"param:
{
param
}
"
)
print
(
f
"program type:
{
type
(
val_program
)
}
"
)
paddle
.
static
.
save
(
val_program
,
"./temp"
)
return
compiled_train_prog
=
quant_aware
(
train_prog
,
place
,
...
...
demo/quant/quant_aware/train.py
浏览文件 @
b1e04ba7
...
...
@@ -143,21 +143,55 @@ def compress(args):
# According to the weight and activation quantization type, the graph will be added
# some fake quantize operators and fake dequantize operators.
############################################################################################################
val_program
=
quant_aware
(
quant_
val_program
=
quant_aware
(
val_program
,
place
,
quant_config
,
scope
=
None
,
for_test
=
True
)
compiled_train_prog
=
quant_aware
(
train_prog
,
place
,
quant_config
,
scope
=
None
,
for_test
=
False
)
train_prog
=
quant_aware
(
train_prog
,
place
,
quant_config
,
scope
=
None
,
for_test
=
False
,
return_program
=
True
)
opt
=
create_optimizer
(
args
)
opt
.
minimize
(
avg_cost
)
exe
=
paddle
.
static
.
Executor
(
place
)
exe
.
run
(
paddle
.
static
.
default_startup_program
())
assert
os
.
path
.
exists
(
args
.
pretrained_model
),
"pretrained_model doesn't exist"
float_path
=
"./outputs"
paddle
.
fluid
.
io
.
save_inference_model
(
dirname
=
"./outpus"
,
feeded_var_names
=
[
image
.
name
],
target_vars
=
[
out
],
executor
=
exe
,
main_program
=
quant_val_program
,
model_filename
=
float_path
+
'/before_convert.pdmodel'
,
params_filename
=
float_path
+
'/params'
)
float_program
,
int8_program
=
convert
(
quant_val_program
,
place
,
quant_config
,
\
scope
=
None
,
\
save_int8
=
True
)
paddle
.
fluid
.
io
.
save_inference_model
(
dirname
=
"./outpus"
,
feeded_var_names
=
[
image
.
name
],
target_vars
=
[
out
],
executor
=
exe
,
main_program
=
float_program
,
model_filename
=
float_path
+
'/after_convert.pdmodel'
,
params_filename
=
float_path
+
'/params'
)
sys
.
exit
(
0
)
path_state_dict
=
'temp/model.pdparams'
paddle
.
static
.
save
(
val_program
,
path_state_dict
,
protocol
=
4
)
# paddle.save(train_prog.state_dict("param"), path_state_dict)
# fluid.io.save_persistables(exe, "checkpoint", compiled_train_prog)
# assert os.path.exists(
# args.pretrained_model), "pretrained_model doesn't exist"
if
args
.
pretrained_model
:
paddle
.
static
.
load
(
train_prog
,
args
.
pretrained_model
,
exe
)
#
if args.pretrained_model:
#
paddle.static.load(train_prog, args.pretrained_model, exe)
places
=
paddle
.
static
.
cuda_places
(
)
if
args
.
use_gpu
else
paddle
.
static
.
cpu_places
()
...
...
@@ -232,6 +266,7 @@ def compress(args):
build_strategy
.
fuse_all_reduce_ops
=
False
build_strategy
.
sync_batch_norm
=
False
exec_strategy
=
paddle
.
static
.
ExecutionStrategy
()
compiled_train_prog
=
paddle
.
static
.
CompiledProgram
(
train_prog
)
compiled_train_prog
=
compiled_train_prog
.
with_data_parallel
(
loss_name
=
avg_cost
.
name
,
build_strategy
=
build_strategy
,
...
...
paddleslim/dygraph/quant/ptq.py
浏览文件 @
b1e04ba7
...
...
@@ -18,20 +18,20 @@ import logging
import
paddle
import
paddle.nn
as
nn
import
paddle.fluid.contrib.slim.quantization
as
Q
from
paddle.fluid.contrib.slim.quantization
import
AbsmaxQuantizer
from
paddle.fluid.contrib.slim.quantization
import
HistQuantizer
from
paddle.fluid.contrib.slim.quantization
import
KLQuantizer
from
paddle.fluid.contrib.slim.quantization
import
PerChannelAbsmaxQuantizer
#
from paddle.fluid.contrib.slim.quantization import AbsmaxQuantizer
#
from paddle.fluid.contrib.slim.quantization import HistQuantizer
#
from paddle.fluid.contrib.slim.quantization import KLQuantizer
#
from paddle.fluid.contrib.slim.quantization import PerChannelAbsmaxQuantizer
from
...common
import
get_logger
_logger
=
get_logger
(
__name__
,
level
=
logging
.
INFO
)
__all__
=
[
'PTQ'
,
'AbsmaxQuantizer'
,
'HistQuantiz
er'
,
'KLQuantizer'
,
'PerChannelAbsmaxQuantizer'
,
#
'AbsmaxQuantizer',
# 'HistQuantizgg
er',
#
'KLQuantizer',
#
'PerChannelAbsmaxQuantizer',
]
...
...
paddleslim/dygraph/quant/qat.py
浏览文件 @
b1e04ba7
...
...
@@ -16,7 +16,7 @@ import copy
import
logging
import
paddle
from
paddle.fluid.contrib.slim.quantization
import
ImperativeQuantAware
from
paddle.fluid.contrib.slim.quantization
import
ImperativeQuantAware
V2
,
ImperativeQuantAware
from
...common
import
get_logger
_logger
=
get_logger
(
__name__
,
level
=
logging
.
INFO
)
...
...
@@ -195,7 +195,7 @@ class QAT(object):
self
.
weight_quantize
=
weight_quantize
self
.
act_quantize
=
act_quantize
self
.
imperative_qat
=
ImperativeQuantAware
(
self
.
imperative_qat
=
ImperativeQuantAware
V2
(
weight_bits
=
self
.
config
[
'weight_bits'
],
activation_bits
=
self
.
config
[
'activation_bits'
],
weight_quantize_type
=
self
.
config
[
'weight_quantize_type'
],
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录