Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
c7e09860
M
models
项目概览
PaddlePaddle
/
models
1 年多 前同步成功
通知
222
Star
6828
Fork
2962
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
602
列表
看板
标记
里程碑
合并请求
255
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
models
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
602
Issue
602
列表
看板
标记
里程碑
合并请求
255
合并请求
255
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
c7e09860
编写于
6月 11, 2020
作者:
Z
Zhen Wang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
compare to dygraph quant.
上级
a95d4932
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
155 addition
and
100 deletion
+155
-100
PaddleSlim/quant_low_level_api/quant.py
PaddleSlim/quant_low_level_api/quant.py
+147
-93
PaddleSlim/quant_low_level_api/run_quant.sh
PaddleSlim/quant_low_level_api/run_quant.sh
+6
-5
PaddleSlim/reader.py
PaddleSlim/reader.py
+2
-2
未找到文件。
PaddleSlim/quant_low_level_api/quant.py
浏览文件 @
c7e09860
...
@@ -158,7 +158,13 @@ def build_program(is_train, main_prog, startup_prog, args):
...
@@ -158,7 +158,13 @@ def build_program(is_train, main_prog, startup_prog, args):
dtypes
=
[
"float32"
,
"int64"
],
dtypes
=
[
"float32"
,
"int64"
],
use_double_buffer
=
True
)
use_double_buffer
=
True
)
with
fluid
.
unique_name
.
guard
():
with
fluid
.
unique_name
.
guard
():
image
,
label
=
fluid
.
layers
.
read_file
(
py_reader
)
image
=
fluid
.
data
(
name
=
"image"
,
shape
=
[
-
1
]
+
image_shape
,
dtype
=
'float32'
)
label
=
fluid
.
data
(
name
=
"label"
,
shape
=
[
-
1
,
1
],
dtype
=
"int64"
)
# if is_train:
# image = fluid.data(name="image", shape=[-1] + image_shape, dtype='float32')
# label = fluid.data(name="label", shape=[-1, 1], dtype="int64")
# else:
# image, label = fluid.layers.read_file(py_reader)
out
,
avg_cost
,
acc_top1
,
acc_top5
=
net_config
(
image
,
label
,
model
,
args
)
out
,
avg_cost
,
acc_top1
,
acc_top5
=
net_config
(
image
,
label
,
model
,
args
)
avg_cost
.
persistable
=
True
avg_cost
.
persistable
=
True
acc_top1
.
persistable
=
True
acc_top1
.
persistable
=
True
...
@@ -171,11 +177,13 @@ def build_program(is_train, main_prog, startup_prog, args):
...
@@ -171,11 +177,13 @@ def build_program(is_train, main_prog, startup_prog, args):
params
[
"learning_strategy"
][
"batch_size"
]
=
args
.
batch_size
params
[
"learning_strategy"
][
"batch_size"
]
=
args
.
batch_size
params
[
"learning_strategy"
][
"name"
]
=
args
.
lr_strategy
params
[
"learning_strategy"
][
"name"
]
=
args
.
lr_strategy
optimizer
=
optimizer_setting
(
params
)
# optimizer = optimizer_setting(params)
optimizer
=
fluid
.
optimizer
.
SGD
(
learning_rate
=
0.1
)
optimizer
.
minimize
(
avg_cost
)
optimizer
.
minimize
(
avg_cost
)
global_lr
=
optimizer
.
_global_learning_rate
()
global_lr
=
optimizer
.
_global_learning_rate
()
if
is_train
:
if
is_train
:
return
image
,
out
,
py_reader
,
avg_cost
,
acc_top1
,
acc_top5
,
global_lr
return
image
,
label
,
out
,
py_reader
,
avg_cost
,
acc_top1
,
acc_top5
,
global_lr
else
:
else
:
return
image
,
out
,
py_reader
,
avg_cost
,
acc_top1
,
acc_top5
return
image
,
out
,
py_reader
,
avg_cost
,
acc_top1
,
acc_top5
...
@@ -195,12 +203,12 @@ def train(args):
...
@@ -195,12 +203,12 @@ def train(args):
train_prog
=
fluid
.
Program
()
train_prog
=
fluid
.
Program
()
test_prog
=
fluid
.
Program
()
test_prog
=
fluid
.
Program
()
_
,
_
,
train_py_reader
,
train_cost
,
train_acc1
,
train_acc5
,
global_lr
=
build_program
(
image
,
label
,
out
,
train_py_reader
,
train_cost
,
train_acc1
,
train_acc5
,
global_lr
=
build_program
(
is_train
=
True
,
is_train
=
True
,
main_prog
=
train_prog
,
main_prog
=
train_prog
,
startup_prog
=
startup_prog
,
startup_prog
=
startup_prog
,
args
=
args
)
args
=
args
)
image
,
out
,
test_py_reader
,
test_cost
,
test_acc1
,
test_acc5
=
build_program
(
_
,
_
,
test_py_reader
,
test_cost
,
test_acc1
,
test_acc5
=
build_program
(
is_train
=
False
,
is_train
=
False
,
main_prog
=
test_prog
,
main_prog
=
test_prog
,
startup_prog
=
startup_prog
,
startup_prog
=
startup_prog
,
...
@@ -233,12 +241,13 @@ def train(args):
...
@@ -233,12 +241,13 @@ def train(args):
test_batch_size
=
1
if
activation_quant_type
==
'abs_max'
else
8
test_batch_size
=
1
if
activation_quant_type
==
'abs_max'
else
8
train_reader
=
paddle
.
batch
(
train_reader
=
paddle
.
batch
(
reader
.
train
(
data_dir
=
data_dir
),
batch_size
=
train_batch_size
,
drop_last
=
True
)
reader
.
train
(
data_dir
=
data_dir
),
batch_size
=
train_batch_size
,
drop_last
=
True
)
test_reader
=
paddle
.
batch
(
reader
.
val
(
data_dir
=
data_dir
),
batch_size
=
test_batch_size
)
test_reader
=
paddle
.
batch
(
reader
.
val
(
data_dir
=
data_dir
),
batch_size
=
128
)
train_py_reader
.
decorate_paddle_reader
(
train_reader
)
# train_py_reader.decorate_paddle_reader(train_reader)
feeder_train
=
fluid
.
DataFeeder
(
feed_list
=
[
image
,
label
],
place
=
place
)
test_py_reader
.
decorate_paddle_reader
(
test_reader
)
test_py_reader
.
decorate_paddle_reader
(
test_reader
)
train_fetch_list
=
[
train_cost
.
name
,
train_acc1
.
name
,
train_acc5
.
name
,
global_lr
.
name
]
test_fetch_list
=
[
test_cost
.
name
,
test_acc1
.
name
,
test_acc5
.
name
]
test_fetch_list
=
[
test_cost
.
name
,
test_acc1
.
name
,
test_acc5
.
name
]
# 1. Make some quantization transforms in the graph before training and testing.
# 1. Make some quantization transforms in the graph before training and testing.
...
@@ -261,77 +270,122 @@ def train(args):
...
@@ -261,77 +270,122 @@ def train(args):
loss_name
=
train_cost
.
name
,
build_strategy
=
build_strategy
)
loss_name
=
train_cost
.
name
,
build_strategy
=
build_strategy
)
test_prog
=
test_graph
.
to_program
()
test_prog
=
test_graph
.
to_program
()
params
=
models
.
__dict__
[
args
.
model
]().
params
params
=
models
.
__dict__
[
args
.
model
]().
params
fetch_var_name
=
'conv1_weights'
train_fetch_list
=
[
fetch_var_name
,
train_cost
.
name
,
train_acc1
.
name
,
train_acc5
.
name
,
global_lr
.
name
]
for
pass_id
in
range
(
params
[
"num_epochs"
]):
for
pass_id
in
range
(
params
[
"num_epochs"
]):
train_py_reader
.
start
()
train_info
=
[[],
[],
[]]
train_info
=
[[],
[],
[]]
test_info
=
[[],
[],
[]]
test_info
=
[[],
[],
[]]
train_time
=
[]
train_time
=
[]
batch_id
=
0
batch_id
=
0
try
:
while
True
:
for
batch_id
,
data
in
enumerate
(
test_reader
()):
t1
=
time
.
time
()
t1
=
time
.
time
()
loss
,
acc1
,
acc5
,
lr
=
exe
.
run
(
binary
,
fetch_list
=
train_fetch_list
)
# print(np.array(data)[0][0][0][0][:10])
t2
=
time
.
time
()
x_data
=
np
.
array
(
period
=
t2
-
t1
[
x
[
0
].
reshape
(
3
,
224
,
224
)
for
x
in
data
]).
astype
(
'float32'
)
loss
=
np
.
mean
(
np
.
array
(
loss
))
# x_data = np.ones_like(np.array(x_data)) * batch_id
acc1
=
np
.
mean
(
np
.
array
(
acc1
))
# print(x_data[0][0][0][:10])
acc5
=
np
.
mean
(
np
.
array
(
acc5
))
y_data
=
np
.
array
([
x
[
1
]
for
x
in
data
]).
astype
(
'int64'
).
reshape
(
-
1
,
1
)
train_info
[
0
].
append
(
loss
)
w_v
=
np
.
array
(
fluid
.
global_scope
().
find_var
(
'conv1_weights'
).
get_tensor
())
train_info
[
1
].
append
(
acc1
)
# print("weight check----------------", w_v[0][0][0][:10])
train_info
[
2
].
append
(
acc5
)
# img_data, loss, acc1, acc5, lr = exe.run(binary, feed=feeder_train.feed(data), fetch_list=train_fetch_list)
lr
=
np
.
mean
(
np
.
array
(
lr
))
target_var
,
loss
,
acc1
,
acc5
,
lr
=
exe
.
run
(
binary
,
feed
=
{
image
.
name
:
x_data
,
label
.
name
:
y_data
},
fetch_list
=
train_fetch_list
)
train_time
.
append
(
period
)
# print("var check(name: {})----------------".format(fetch_var_name), np.array(target_var)[0][0][0][:10])
if
batch_id
%
10
==
0
:
t2
=
time
.
time
()
print
(
"Pass {0}, trainbatch {1}, loss {2},
\
period
=
t2
-
t1
acc1 {3}, acc5 {4}, lr {5}, time {6}"
loss
=
np
.
mean
(
np
.
array
(
loss
))
.
format
(
pass_id
,
batch_id
,
loss
,
acc1
,
acc5
,
"%.6f"
%
acc1
=
np
.
mean
(
np
.
array
(
acc1
))
lr
,
"%2.2f sec"
%
period
))
acc5
=
np
.
mean
(
np
.
array
(
acc5
))
sys
.
stdout
.
flush
()
train_info
[
0
].
append
(
loss
)
batch_id
+=
1
train_info
[
1
].
append
(
acc1
)
except
fluid
.
core
.
EOFException
:
train_info
[
2
].
append
(
acc5
)
train_py_reader
.
reset
()
lr
=
np
.
mean
(
np
.
array
(
lr
))
train_time
.
append
(
period
)
if
batch_id
%
1
==
0
:
print
(
"Pass {0}, trainbatch {1}, loss {2},
\
acc1 {3}, acc5 {4}, lr {5}, time {6}"
.
format
(
pass_id
,
batch_id
,
loss
,
acc1
,
acc5
,
"%.6f"
%
lr
,
"%2.2f sec"
%
period
))
sys
.
stdout
.
flush
()
batch_id
+=
1
train_loss
=
np
.
array
(
train_info
[
0
]).
mean
()
train_loss
=
np
.
array
(
train_info
[
0
]).
mean
()
train_acc1
=
np
.
array
(
train_info
[
1
]).
mean
()
train_acc1
=
np
.
array
(
train_info
[
1
]).
mean
()
train_acc5
=
np
.
array
(
train_info
[
2
]).
mean
()
train_acc5
=
np
.
array
(
train_info
[
2
]).
mean
()
test_py_reader
.
start
()
# for pass_id in range(params["num_epochs"]):
test_batch_id
=
0
try
:
# train_py_reader.start()
while
True
:
t1
=
time
.
time
()
# train_info = [[], [], []]
loss
,
acc1
,
acc5
=
exe
.
run
(
program
=
test_prog
,
# test_info = [[], [], []]
fetch_list
=
test_fetch_list
)
# train_time = []
t2
=
time
.
time
()
# batch_id = 0
period
=
t2
-
t1
# try:
loss
=
np
.
mean
(
loss
)
# while True:
acc1
=
np
.
mean
(
acc1
)
# t1 = time.time()
acc5
=
np
.
mean
(
acc5
)
# img_data, loss, acc1, acc5, lr = exe.run(binary, fetch_list=train_fetch_list)
test_info
[
0
].
append
(
loss
)
# print(np.array(img_data)[0][0][0][:10])
test_info
[
1
].
append
(
acc1
)
# t2 = time.time()
test_info
[
2
].
append
(
acc5
)
# period = t2 - t1
if
test_batch_id
%
10
==
0
:
# loss = np.mean(np.array(loss))
print
(
"Pass {0},testbatch {1},loss {2},
\
# acc1 = np.mean(np.array(acc1))
acc1 {3},acc5 {4},time {5}"
# acc5 = np.mean(np.array(acc5))
.
format
(
pass_id
,
test_batch_id
,
loss
,
acc1
,
acc5
,
# train_info[0].append(loss)
"%2.2f sec"
%
period
))
# train_info[1].append(acc1)
sys
.
stdout
.
flush
()
# train_info[2].append(acc5)
test_batch_id
+=
1
# lr = np.mean(np.array(lr))
except
fluid
.
core
.
EOFException
:
# train_time.append(period)
test_py_reader
.
reset
()
# if batch_id % 10 == 0:
# print("Pass {0}, trainbatch {1}, loss {2}, \
test_loss
=
np
.
array
(
test_info
[
0
]).
mean
()
# acc1 {3}, acc5 {4}, lr {5}, time {6}"
test_acc1
=
np
.
array
(
test_info
[
1
]).
mean
()
# .format(pass_id, batch_id, loss, acc1, acc5, "%.6f" %
test_acc5
=
np
.
array
(
test_info
[
2
]).
mean
()
# lr, "%2.2f sec" % period))
# sys.stdout.flush()
print
(
"End pass {0}, train_loss {1}, train_acc1 {2}, train_acc5 {3}, "
# batch_id += 1
"test_loss {4}, test_acc1 {5}, test_acc5 {6}"
.
format
(
# except fluid.core.EOFException:
pass_id
,
train_loss
,
train_acc1
,
train_acc5
,
test_loss
,
# train_py_reader.reset()
test_acc1
,
test_acc5
))
sys
.
stdout
.
flush
()
# train_loss = np.array(train_info[0]).mean()
# train_acc1 = np.array(train_info[1]).mean()
# train_acc5 = np.array(train_info[2]).mean()
# test_py_reader.start()
# test_batch_id = 0
# try:
# while True:
# t1 = time.time()
# loss, acc1, acc5 = exe.run(program=test_prog,
# fetch_list=test_fetch_list)
# t2 = time.time()
# period = t2 - t1
# loss = np.mean(loss)
# acc1 = np.mean(acc1)
# acc5 = np.mean(acc5)
# test_info[0].append(loss)
# test_info[1].append(acc1)
# test_info[2].append(acc5)
# if test_batch_id % 10 == 0:
# print("Pass {0},testbatch {1},loss {2}, \
# acc1 {3},acc5 {4},time {5}"
# .format(pass_id, test_batch_id, loss, acc1, acc5,
# "%2.2f sec" % period))
# sys.stdout.flush()
# test_batch_id += 1
# except fluid.core.EOFException:
# test_py_reader.reset()
# test_loss = np.array(test_info[0]).mean()
# test_acc1 = np.array(test_info[1]).mean()
# test_acc5 = np.array(test_info[2]).mean()
# print("End pass {0}, train_loss {1}, train_acc1 {2}, train_acc5 {3}, "
# "test_loss {4}, test_acc1 {5}, test_acc5 {6}".format(
# pass_id, train_loss, train_acc1, train_acc5, test_loss,
# test_acc1, test_acc5))
# sys.stdout.flush()
save_checkpoint_path
=
os
.
path
.
join
(
model_save_dir
,
model_name
,
str
(
pass_id
))
save_checkpoint_path
=
os
.
path
.
join
(
model_save_dir
,
model_name
,
str
(
pass_id
))
if
not
os
.
path
.
isdir
(
save_checkpoint_path
):
if
not
os
.
path
.
isdir
(
save_checkpoint_path
):
...
@@ -347,39 +401,39 @@ def train(args):
...
@@ -347,39 +401,39 @@ def train(args):
# 2. Freeze the graph after training by adjusting the quantize
# 2. Freeze the graph after training by adjusting the quantize
# operators' order for the inference.
# operators' order for the inference.
freeze_pass
=
QuantizationFreezePass
(
#
freeze_pass = QuantizationFreezePass(
scope
=
fluid
.
global_scope
(),
#
scope=fluid.global_scope(),
place
=
place
,
#
place=place,
weight_quantize_type
=
weight_quant_type
)
#
weight_quantize_type=weight_quant_type)
freeze_pass
.
apply
(
test_graph
)
#
freeze_pass.apply(test_graph)
server_program
=
test_graph
.
to_program
()
#
server_program = test_graph.to_program()
fluid
.
io
.
save_inference_model
(
#
fluid.io.save_inference_model(
dirname
=
float_path
,
#
dirname=float_path,
feeded_var_names
=
[
image
.
name
],
#
feeded_var_names=[image.name],
target_vars
=
[
out
],
executor
=
exe
,
#
target_vars=[out], executor=exe,
main_program
=
server_program
)
#
main_program=server_program)
# 3. Convert the weights into int8_t type.
# 3. Convert the weights into int8_t type.
# (This step is optional.)
# (This step is optional.)
convert_int8_pass
=
ConvertToInt8Pass
(
scope
=
fluid
.
global_scope
(),
place
=
place
)
#
convert_int8_pass = ConvertToInt8Pass(scope=fluid.global_scope(), place=place)
convert_int8_pass
.
apply
(
test_graph
)
#
convert_int8_pass.apply(test_graph)
server_int8_program
=
test_graph
.
to_program
()
#
server_int8_program = test_graph.to_program()
fluid
.
io
.
save_inference_model
(
#
fluid.io.save_inference_model(
dirname
=
int8_path
,
#
dirname=int8_path,
feeded_var_names
=
[
image
.
name
],
#
feeded_var_names=[image.name],
target_vars
=
[
out
],
executor
=
exe
,
#
target_vars=[out], executor=exe,
main_program
=
server_int8_program
)
#
main_program=server_int8_program)
# 4. Convert the freezed graph for paddle-mobile execution.
# 4. Convert the freezed graph for paddle-mobile execution.
# (This step is optional.)
# (This step is optional.)
mobile_pass
=
TransformForMobilePass
()
#
mobile_pass = TransformForMobilePass()
mobile_pass
.
apply
(
test_graph
)
#
mobile_pass.apply(test_graph)
mobile_program
=
test_graph
.
to_program
()
#
mobile_program = test_graph.to_program()
fluid
.
io
.
save_inference_model
(
fluid
.
io
.
save_inference_model
(
dirname
=
mobile_path
,
dirname
=
"quant_infer"
,
feeded_var_names
=
[
image
.
name
],
feeded_var_names
=
[
image
.
name
],
target_vars
=
[
out
],
executor
=
exe
,
target_vars
=
[
out
],
executor
=
exe
,
main_program
=
mobile_program
)
main_program
=
test_graph
.
to_program
()
)
def
main
():
def
main
():
args
=
parser
.
parse_args
()
args
=
parser
.
parse_args
()
...
...
PaddleSlim/quant_low_level_api/run_quant.sh
浏览文件 @
c7e09860
...
@@ -5,7 +5,7 @@ root_url="https://paddle-inference-dist.bj.bcebos.com/int8/pretrain"
...
@@ -5,7 +5,7 @@ root_url="https://paddle-inference-dist.bj.bcebos.com/int8/pretrain"
MobileNetV1
=
"MobileNetV1_pretrained.zip"
MobileNetV1
=
"MobileNetV1_pretrained.zip"
ResNet50
=
"ResNet50_pretrained.zip"
ResNet50
=
"ResNet50_pretrained.zip"
GoogleNet
=
"GoogleNet_pretrained.tar"
GoogleNet
=
"GoogleNet_pretrained.tar"
data_dir
=
'
Your image dataset path, e.g. ILSVRC2012
'
data_dir
=
'
/work/datasets/ILSVRC2012/
'
pretrain_dir
=
'../pretrain'
pretrain_dir
=
'../pretrain'
if
[
!
-d
${
pretrain_dir
}
]
;
then
if
[
!
-d
${
pretrain_dir
}
]
;
then
...
@@ -32,15 +32,16 @@ fi
...
@@ -32,15 +32,16 @@ fi
cd
-
cd
-
export
CUDA_VISIBLE_DEVICES
=
0,1,2,3
export
CUDA_VISIBLE_DEVICES
=
1
export
FLAGS_cudnn_deterministic
=
1
#MobileNet v1:
#MobileNet v1:
python quant.py
\
python
-u
quant.py
\
--model
=
MobileNet
\
--model
=
MobileNet
\
--pretrained_fp32_model
=
${
pretrain_dir
}
/MobileNetV1_pretrained
\
--pretrained_fp32_model
=
${
pretrain_dir
}
/MobileNetV1_pretrained
\
--use_gpu
=
True
\
--use_gpu
=
True
\
--data_dir
=
${
data_dir
}
\
--data_dir
=
${
data_dir
}
\
--batch_size
=
256
\
--batch_size
=
128
\
--total_images
=
1281167
\
--total_images
=
1281167
\
--class_dim
=
1000
\
--class_dim
=
1000
\
--image_shape
=
3,224,224
\
--image_shape
=
3,224,224
\
...
@@ -48,7 +49,7 @@ python quant.py \
...
@@ -48,7 +49,7 @@ python quant.py \
--lr_strategy
=
piecewise_decay
\
--lr_strategy
=
piecewise_decay
\
--num_epochs
=
20
\
--num_epochs
=
20
\
--lr
=
0.0001
\
--lr
=
0.0001
\
--act_quant_type
=
abs_max
\
--act_quant_type
=
moving_average_
abs_max
\
--wt_quant_type
=
abs_max
--wt_quant_type
=
abs_max
...
...
PaddleSlim/reader.py
浏览文件 @
c7e09860
...
@@ -11,7 +11,7 @@ np.random.seed(0)
...
@@ -11,7 +11,7 @@ np.random.seed(0)
DATA_DIM
=
224
DATA_DIM
=
224
THREAD
=
1
6
THREAD
=
1
BUF_SIZE
=
10240
BUF_SIZE
=
10240
DATA_DIR
=
'data/ILSVRC2012'
DATA_DIR
=
'data/ILSVRC2012'
...
@@ -175,7 +175,7 @@ def train(data_dir=DATA_DIR):
...
@@ -175,7 +175,7 @@ def train(data_dir=DATA_DIR):
return
_reader_creator
(
return
_reader_creator
(
file_list
,
file_list
,
'train'
,
'train'
,
shuffle
=
Tru
e
,
shuffle
=
Fals
e
,
color_jitter
=
False
,
color_jitter
=
False
,
rotate
=
False
,
rotate
=
False
,
data_dir
=
data_dir
)
data_dir
=
data_dir
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录