未验证 提交 bed39157 编写于 作者: B Bai Yifan 提交者: GitHub

migrate quant code to paddle2.0 (#488)

* migrate quant code to paddle2.0

* fix quant unittest

* fix demo
上级 3a33f1a4
......@@ -9,7 +9,6 @@ import time
import numpy as np
from collections import defaultdict
import paddle.fluid as fluid
sys.path.append(os.path.dirname("__file__"))
sys.path.append(
os.path.join(os.path.dirname("__file__"), os.path.pardir, os.path.pardir))
......@@ -21,8 +20,6 @@ from utility import add_arguments, print_arguments
from paddle.fluid.layer_helper import LayerHelper
quantization_model_save_dir = './quantization_models/'
from paddle.fluid.contrib.slim.quantization import AddQuantDequantPass
_logger = get_logger(__name__, level=logging.INFO)
parser = argparse.ArgumentParser(description=__doc__)
......@@ -74,29 +71,32 @@ model_list = [m for m in dir(models) if "__" not in m]
def piecewise_decay(args):
places = fluid.cuda_places() if args.use_gpu else fluid.cpu_places()
places = paddle.static.cuda_places(
) if args.use_gpu else paddle.static.cpu_places()
step = int(
math.ceil(float(args.total_images) / (args.batch_size * len(places))))
bd = [step * e for e in args.step_epochs]
lr = [args.lr * (0.1**i) for i in range(len(bd) + 1)]
learning_rate = fluid.layers.piecewise_decay(boundaries=bd, values=lr)
optimizer = fluid.optimizer.Momentum(
learning_rate = paddle.optimizer.lr.PiecewiseDecay(
boundaries=bd, values=lr, verbose=False)
optimizer = paddle.optimizer.Momentum(
learning_rate=learning_rate,
momentum=args.momentum_rate,
regularization=fluid.regularizer.L2Decay(args.l2_decay))
weight_decay=paddle.regularizer.L2Decay(args.l2_decay))
return learning_rate, optimizer
def cosine_decay(args):
places = fluid.cuda_places() if args.use_gpu else fluid.cpu_places()
places = paddle.static.cuda_places(
) if args.use_gpu else paddle.static.cpu_places()
step = int(
math.ceil(float(args.total_images) / (args.batch_size * len(places))))
learning_rate = fluid.layers.cosine_decay(
learning_rate=args.lr, step_each_epoch=step, epochs=args.num_epochs)
optimizer = fluid.optimizer.Momentum(
learning_rate = paddle.optimizer.lr.CosineAnnealingDecay(
learning_rate=args.lr, T_max=step * args.num_epochs, verbose=False)
optimizer = paddle.optimizer.Momentum(
learning_rate=learning_rate,
momentum=args.momentum_rate,
regularization=fluid.regularizer.L2Decay(args.l2_decay))
weight_decay=paddle.regularizer.L2Decay(args.l2_decay))
return learning_rate, optimizer
......@@ -127,41 +127,43 @@ def compress(args):
image_shape = [int(m) for m in image_shape.split(",")]
assert args.model in model_list, "{} is not in lists: {}".format(args.model,
model_list)
image = fluid.layers.data(name='image', shape=image_shape, dtype='float32')
image = paddle.static.data(
name='image', shape=[None] + image_shape, dtype='float32')
if args.use_pact:
image.stop_gradient = False
label = fluid.layers.data(name='label', shape=[1], dtype='int64')
label = paddle.static.data(name='label', shape=[None, 1], dtype='int64')
# model definition
model = models.__dict__[args.model]()
out = model.net(input=image, class_dim=class_dim)
cost = fluid.layers.cross_entropy(input=out, label=label)
avg_cost = fluid.layers.mean(x=cost)
acc_top1 = fluid.layers.accuracy(input=out, label=label, k=1)
acc_top5 = fluid.layers.accuracy(input=out, label=label, k=5)
cost = paddle.nn.functional.loss.cross_entropy(input=out, label=label)
avg_cost = paddle.mean(x=cost)
acc_top1 = paddle.metric.accuracy(input=out, label=label, k=1)
acc_top5 = paddle.metric.accuracy(input=out, label=label, k=5)
train_prog = fluid.default_main_program()
val_program = fluid.default_main_program().clone(for_test=True)
train_prog = paddle.static.default_main_program()
val_program = paddle.static.default_main_program().clone(for_test=True)
if not args.analysis:
learning_rate, opt = create_optimizer(args)
opt.minimize(avg_cost)
place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
place = paddle.CUDAPlace(0) if args.use_gpu else paddle.CPUPlace()
exe = paddle.static.Executor(place)
exe.run(paddle.static.default_startup_program())
train_reader = paddle.fluid.io.batch(
train_reader = paddle.batch(
train_reader, batch_size=args.batch_size, drop_last=True)
train_loader = fluid.io.DataLoader.from_generator(
train_loader = paddle.io.DataLoader.from_generator(
feed_list=[image, label],
capacity=512,
use_double_buffer=True,
iterable=True)
places = fluid.cuda_places() if args.use_gpu else fluid.cpu_places()
places = paddle.static.cuda_places(
) if args.use_gpu else paddle.static.cpu_places()
train_loader.set_sample_list_generator(train_reader, places)
val_reader = paddle.fluid.io.batch(val_reader, batch_size=args.batch_size)
valid_loader = fluid.io.DataLoader.from_generator(
val_reader = paddle.batch(val_reader, batch_size=args.batch_size)
valid_loader = paddle.io.DataLoader.from_generator(
feed_list=[image, label],
capacity=512,
use_double_buffer=True,
......@@ -246,21 +248,21 @@ def compress(args):
helper = LayerHelper("pact", **locals())
dtype = 'float32'
init_thres = values[x.name.split('_tmp_input')[0]]
u_param_attr = fluid.ParamAttr(
u_param_attr = paddle.ParamAttr(
name=x.name + '_pact',
initializer=fluid.initializer.ConstantInitializer(value=init_thres),
regularizer=fluid.regularizer.L2Decay(0.0001),
initializer=paddle.nn.initializer.Constant(value=init_thres),
regularizer=paddle.regularizer.L2Decay(0.0001),
learning_rate=1)
u_param = helper.create_parameter(
attr=u_param_attr, shape=[1], dtype=dtype)
part_a = fluid.layers.relu(fluid.layers.elementwise_sub(x, u_param))
part_b = fluid.layers.relu(fluid.layers.elementwise_sub(-u_param, x))
part_a = paddle.nn.functional.relu(x - u_param)
part_b = paddle.nn.functional.relu(-u_param - x)
x = x - part_a + part_b
return x
def get_optimizer():
return fluid.optimizer.MomentumOptimizer(args.lr, 0.9)
return paddle.optimizer.Momentum(args.lr, 0.9)
if args.use_pact:
act_preprocess_func = pact
......@@ -294,11 +296,7 @@ def compress(args):
args.pretrained_model), "pretrained_model doesn't exist"
if args.pretrained_model:
def if_exist(var):
return os.path.exists(os.path.join(args.pretrained_model, var.name))
fluid.io.load_vars(exe, args.pretrained_model, predicate=if_exist)
paddle.static.load(train_prog, args.pretrained_model, exe)
def test(epoch, program):
batch_id = 0
......@@ -325,44 +323,42 @@ def compress(args):
np.mean(np.array(acc_top1_ns)), np.mean(np.array(acc_top5_ns))))
return np.mean(np.array(acc_top1_ns))
def train(epoch, compiled_train_prog):
def train(epoch, compiled_train_prog, lr):
batch_id = 0
for data in train_loader():
start_time = time.time()
lr_n, loss_n, acc_top1_n, acc_top5_n = exe.run(
loss_n, acc_top1_n, acc_top5_n = exe.run(
compiled_train_prog,
feed=data,
fetch_list=[
learning_rate.name, avg_cost.name, acc_top1.name,
acc_top5.name
])
fetch_list=[avg_cost.name, acc_top1.name, acc_top5.name])
end_time = time.time()
lr_n = np.mean(lr_n)
loss_n = np.mean(loss_n)
acc_top1_n = np.mean(acc_top1_n)
acc_top5_n = np.mean(acc_top5_n)
if batch_id % args.log_period == 0:
_logger.info(
"epoch[{}]-batch[{}] lr: {:.6f} - loss: {:.6f}; acc_top1: {:.6f}; acc_top5: {:.6f}; time: {:.3f}".
format(epoch, batch_id, lr_n, loss_n, acc_top1_n,
format(epoch, batch_id,
learning_rate.get_lr(), loss_n, acc_top1_n,
acc_top5_n, end_time - start_time))
if args.use_pact and batch_id % 1000 == 0:
threshold = {}
for var in val_program.list_vars():
if 'pact' in var.name:
array = np.array(fluid.global_scope().find_var(var.name)
.get_tensor())
array = np.array(paddle.static.global_scope().find_var(
var.name).get_tensor())
threshold[var.name] = array[0]
_logger.info(threshold)
batch_id += 1
lr.step()
build_strategy = fluid.BuildStrategy()
build_strategy = paddle.static.BuildStrategy()
build_strategy.enable_inplace = False
build_strategy.fuse_all_reduce_ops = False
exec_strategy = fluid.ExecutionStrategy()
exec_strategy = paddle.static.ExecutionStrategy()
compiled_train_prog = compiled_train_prog.with_data_parallel(
loss_name=avg_cost.name,
build_strategy=build_strategy,
......@@ -377,37 +373,38 @@ def compress(args):
ckpt_path = args.checkpoint_dir
assert args.checkpoint_epoch is not None, "checkpoint_epoch must be set"
start_epoch = args.checkpoint_epoch
fluid.io.load_persistables(
paddle.static.load_vars(
exe, dirname=args.checkpoint_dir, main_program=val_program)
start_step = start_epoch * int(
math.ceil(float(args.total_images) / args.batch_size))
v = fluid.global_scope().find_var('@LR_DECAY_COUNTER@').get_tensor()
v = paddle.static.global_scope().find_var(
'@LR_DECAY_COUNTER@').get_tensor()
v.set(np.array([start_step]).astype(np.float32), place)
best_eval_acc1 = 0
best_acc1_epoch = 0
for i in range(start_epoch, args.num_epochs):
train(i, compiled_train_prog)
train(i, compiled_train_prog, learning_rate)
acc1 = test(i, val_program)
if acc1 > best_eval_acc1:
best_eval_acc1 = acc1
best_acc1_epoch = i
_logger.info("Best Validation Acc1: {:.6f}, at epoch {}".format(
best_eval_acc1, best_acc1_epoch))
fluid.io.save_persistables(
paddle.static.save(
exe,
dirname=os.path.join(args.output_dir, str(i)),
main_program=val_program)
if acc1 > best_acc1:
best_acc1 = acc1
best_epoch = i
fluid.io.save_persistables(
paddle.static.save(
exe,
dirname=os.path.join(args.output_dir, 'best_model'),
main_program=val_program)
if os.path.exists(os.path.join(args.output_dir, 'best_model')):
fluid.io.load_persistables(
paddle.static.load(
exe,
dirname=os.path.join(args.output_dir, 'best_model'),
main_program=val_program)
......@@ -430,7 +427,7 @@ def compress(args):
if not os.path.isdir(model_path):
os.makedirs(model_path)
fluid.io.save_inference_model(
paddle.static.save_inference_model(
dirname=float_path,
feeded_var_names=[image.name],
target_vars=[out],
......
......@@ -44,25 +44,32 @@ model_list = [m for m in dir(models) if "__" not in m]
def piecewise_decay(args):
step = int(math.ceil(float(args.total_images) / args.batch_size))
places = paddle.static.cuda_places(
) if args.use_gpu else paddle.static.cpu_places()
step = int(
math.ceil(float(args.total_images) / (args.batch_size * len(places))))
bd = [step * e for e in args.step_epochs]
lr = [args.lr * (0.1**i) for i in range(len(bd) + 1)]
learning_rate = fluid.layers.piecewise_decay(boundaries=bd, values=lr)
optimizer = fluid.optimizer.Momentum(
learning_rate = paddle.optimizer.lr.PiecewiseDecay(
boundaries=bd, values=lr, verbose=False)
optimizer = paddle.optimizer.Momentum(
learning_rate=learning_rate,
momentum=args.momentum_rate,
regularization=fluid.regularizer.L2Decay(args.l2_decay))
weight_decay=paddle.regularizer.L2Decay(args.l2_decay))
return optimizer
def cosine_decay(args):
step = int(math.ceil(float(args.total_images) / args.batch_size))
learning_rate = fluid.layers.cosine_decay(
learning_rate=args.lr, step_each_epoch=step, epochs=args.num_epochs)
optimizer = fluid.optimizer.Momentum(
places = paddle.static.cuda_places(
) if args.use_gpu else paddle.static.cpu_places()
step = int(
math.ceil(float(args.total_images) / (args.batch_size * len(places))))
learning_rate = paddle.optimizer.lr.CosineAnnealingDecay(
learning_rate=args.lr, T_max=step * args.num_epochs, verbose=False)
optimizer = paddle.optimizer.Momentum(
learning_rate=learning_rate,
momentum=args.momentum_rate,
regularization=fluid.regularizer.L2Decay(args.l2_decay))
weight_decay=paddle.regularizer.L2Decay(args.l2_decay))
return optimizer
......@@ -118,20 +125,21 @@ def compress(args):
image_shape = [int(m) for m in image_shape.split(",")]
assert args.model in model_list, "{} is not in lists: {}".format(args.model,
model_list)
image = fluid.layers.data(name='image', shape=image_shape, dtype='float32')
label = fluid.layers.data(name='label', shape=[1], dtype='int64')
image = paddle.static.data(
name='image', shape=[None] + image_shape, dtype='float32')
label = paddle.static.data(name='label', shape=[None, 1], dtype='int64')
# model definition
model = models.__dict__[args.model]()
out = model.net(input=image, class_dim=class_dim)
cost = fluid.layers.cross_entropy(input=out, label=label)
avg_cost = fluid.layers.mean(x=cost)
acc_top1 = fluid.layers.accuracy(input=out, label=label, k=1)
acc_top5 = fluid.layers.accuracy(input=out, label=label, k=5)
cost = paddle.nn.functional.loss.cross_entropy(input=out, label=label)
avg_cost = paddle.mean(x=cost)
acc_top1 = paddle.metric.accuracy(input=out, label=label, k=1)
acc_top5 = paddle.metric.accuracy(input=out, label=label, k=5)
train_prog = fluid.default_main_program()
val_program = fluid.default_main_program().clone(for_test=True)
train_prog = paddle.static.default_main_program()
val_program = paddle.static.default_main_program().clone(for_test=True)
place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace()
place = paddle.CUDAPlace(0) if args.use_gpu else paddle.CPUPlace()
############################################################################################################
# 2. quantization transform programs (training aware)
# Make some quantization transforms in the graph before training and testing.
......@@ -145,37 +153,42 @@ def compress(args):
opt = create_optimizer(args)
opt.minimize(avg_cost)
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
exe = paddle.static.Executor(place)
exe.run(paddle.static.default_startup_program())
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)
def if_exist(var):
return os.path.exists(os.path.join(args.pretrained_model, var.name))
fluid.io.load_vars(exe, args.pretrained_model, predicate=if_exist)
val_reader = paddle.fluid.io.batch(val_reader, batch_size=args.batch_size)
train_reader = paddle.fluid.io.batch(
val_reader = paddle.batch(val_reader, batch_size=args.batch_size)
train_reader = paddle.batch(
train_reader, batch_size=args.batch_size, drop_last=True)
train_feeder = feeder = fluid.DataFeeder([image, label], place)
val_feeder = feeder = fluid.DataFeeder(
[image, label], place, program=val_program)
places = paddle.static.cuda_places(
) if args.use_gpu else paddle.static.cpu_places()
train_loader = paddle.io.DataLoader.from_generator(
feed_list=[image, label],
capacity=512,
use_double_buffer=True,
iterable=True)
valid_loader = paddle.io.DataLoader.from_generator(
feed_list=[image, label],
capacity=512,
use_double_buffer=True,
iterable=True)
train_loader.set_sample_list_generator(train_reader, places)
valid_loader.set_sample_list_generator(val_reader, places[0])
def test(epoch, program):
batch_id = 0
acc_top1_ns = []
acc_top5_ns = []
for data in val_reader():
for data in valid_loader():
start_time = time.time()
acc_top1_n, acc_top5_n = exe.run(
program,
feed=train_feeder.feed(data),
fetch_list=[acc_top1.name, acc_top5.name])
program, feed=data, fetch_list=[acc_top1.name, acc_top5.name])
end_time = time.time()
if batch_id % args.log_period == 0:
_logger.info(
......@@ -195,11 +208,11 @@ def compress(args):
def train(epoch, compiled_train_prog):
batch_id = 0
for data in train_reader():
for data in train_loader():
start_time = time.time()
loss_n, acc_top1_n, acc_top5_n = exe.run(
compiled_train_prog,
feed=train_feeder.feed(data),
feed=data,
fetch_list=[avg_cost.name, acc_top1.name, acc_top5.name])
end_time = time.time()
loss_n = np.mean(loss_n)
......@@ -212,12 +225,12 @@ def compress(args):
end_time - start_time))
batch_id += 1
build_strategy = fluid.BuildStrategy()
build_strategy = paddle.static.BuildStrategy()
build_strategy.memory_optimize = False
build_strategy.enable_inplace = False
build_strategy.fuse_all_reduce_ops = False
build_strategy.sync_batch_norm = False
exec_strategy = fluid.ExecutionStrategy()
exec_strategy = paddle.static.ExecutionStrategy()
compiled_train_prog = compiled_train_prog.with_data_parallel(
loss_name=avg_cost.name,
build_strategy=build_strategy,
......@@ -231,19 +244,17 @@ def compress(args):
for i in range(args.num_epochs):
train(i, compiled_train_prog)
acc1 = test(i, val_program)
fluid.io.save_persistables(
exe,
dirname=os.path.join(args.checkpoint_dir, str(i)),
main_program=val_program)
paddle.static.save(
program=val_program,
model_path=os.path.join(args.checkpoint_dir, str(i)))
if acc1 > best_acc1:
best_acc1 = acc1
best_epoch = i
fluid.io.save_persistables(
exe,
dirname=os.path.join(args.checkpoint_dir, 'best_model'),
main_program=val_program)
paddle.static.save(
program=val_program,
model_path=os.path.join(args.checkpoint_dir, 'best_model'))
if os.path.exists(os.path.join(args.checkpoint_dir, 'best_model')):
fluid.io.load_persistables(
paddle.static.load(
exe,
dirname=os.path.join(args.checkpoint_dir, 'best_model'),
main_program=val_program)
......@@ -264,11 +275,10 @@ def compress(args):
'act_' + quant_config['activation_quantize_type']
+ '_w_' + quant_config['weight_quantize_type'])
float_path = os.path.join(model_path, 'float')
int8_path = os.path.join(model_path, 'int8')
if not os.path.isdir(model_path):
os.makedirs(model_path)
fluid.io.save_inference_model(
paddle.static.save_inference_model(
dirname=float_path,
feeded_var_names=[image.name],
target_vars=[out],
......@@ -277,15 +287,6 @@ def compress(args):
model_filename=float_path + '/model',
params_filename=float_path + '/params')
fluid.io.save_inference_model(
dirname=int8_path,
feeded_var_names=[image.name],
target_vars=[out],
executor=exe,
main_program=int8_program,
model_filename=int8_path + '/model',
params_filename=int8_path + '/params')
def main():
paddle.enable_static()
......
......@@ -19,7 +19,6 @@ import argparse
import functools
import paddle
import paddle.fluid as fluid
sys.path[0] = os.path.join(
os.path.dirname("__file__"), os.path.pardir, os.path.pardir)
import imagenet_reader as reader
......@@ -38,29 +37,33 @@ add_arg('params_name', str, None, "params filename for inference model")
def eval(args):
# parameters from arguments
place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace()
exe = fluid.Executor(place)
place = paddle.CUDAPlace(0) if args.use_gpu else paddle.CPUPlace()
exe = paddle.static.Executor(place)
val_program, feed_target_names, fetch_targets = fluid.io.load_inference_model(
val_program, feed_target_names, fetch_targets = paddle.static.load_inference_model(
args.model_path,
exe,
model_filename=args.model_name,
params_filename=args.params_name)
val_reader = paddle.fluid.io.batch(reader.val(), batch_size=128)
feeder = fluid.DataFeeder(
place=place, feed_list=feed_target_names, program=val_program)
val_reader = paddle.batch(reader.val(), batch_size=1)
image = paddle.static.data(
name='image', shape=[None, 3, 224, 224], dtype='float32')
label = paddle.static.data(name='label', shape=[None, 1], dtype='int64')
valid_loader = paddle.io.DataLoader.from_generator(
feed_list=[image], capacity=512, use_double_buffer=True, iterable=True)
valid_loader.set_sample_list_generator(val_reader, place)
results = []
for batch_id, data in enumerate(val_reader()):
# top1_acc, top5_acc
if len(feed_target_names) == 1:
# eval "infer model", which input is image, output is classification probability
image = [[d[0]] for d in data]
image = data[0][0].reshape((1, 3, 224, 224))
label = [[d[1]] for d in data]
feed_data = feeder.feed(image)
pred = exe.run(val_program,
feed=feed_data,
feed={feed_target_names[0]: image},
fetch_list=fetch_targets)
pred = np.array(pred[0])
label = np.array(label)
......@@ -76,8 +79,13 @@ def eval(args):
results.append([top_1, top_5])
else:
# eval "eval model", which inputs are image and label, output is top1 and top5 accuracy
image = data[0][0].reshape((1, 3, 224, 224))
label = [[d[1]] for d in data]
result = exe.run(val_program,
feed=feeder.feed(data),
feed={
feed_target_names[0]: image,
feed_target_names[1]: label
},
fetch_list=fetch_targets)
result = [np.mean(r) for r in result]
results.append(result)
......
......@@ -7,7 +7,6 @@ import functools
import math
import time
import numpy as np
import paddle.fluid as fluid
sys.path[0] = os.path.join(
os.path.dirname("__file__"), os.path.pardir, os.path.pardir)
from paddleslim.common import get_logger
......@@ -46,28 +45,24 @@ def export_model(args):
raise ValueError("{} is not supported.".format(args.data))
image_shape = [int(m) for m in image_shape.split(",")]
image = fluid.data(
image = paddle.static.data(
name='image', shape=[None] + image_shape, dtype='float32')
assert args.model in model_list, "{} is not in lists: {}".format(args.model,
model_list)
# model definition
model = models.__dict__[args.model]()
out = model.net(input=image, class_dim=class_dim)
val_program = fluid.default_main_program().clone(for_test=True)
place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
val_program = paddle.static.default_main_program().clone(for_test=True)
place = paddle.CUDAPlace(0) if args.use_gpu else paddle.CPUPlace()
exe = paddle.static.Executor(place)
exe.run(paddle.static.default_startup_program())
if args.pretrained_model:
def if_exist(var):
return os.path.exists(os.path.join(args.pretrained_model, var.name))
fluid.io.load_vars(exe, args.pretrained_model, predicate=if_exist)
paddle.static.load(val_program, args.pretrained_model, exe)
else:
assert False, "args.pretrained_model must set"
fluid.io.save_inference_model(
paddle.static.save_inference_model(
'./inference_model/' + args.model,
feeded_var_names=[image.name],
target_vars=[out],
......
......@@ -7,7 +7,6 @@ import functools
import math
import time
import numpy as np
import paddle.fluid as fluid
sys.path[0] = os.path.join(
os.path.dirname("__file__"), os.path.pardir, os.path.pardir)
......@@ -33,12 +32,12 @@ add_arg('params_filename', str, None, "params file name")
def quantize(args):
val_reader = reader.train()
place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace()
place = paddle.CUDAPlace(0) if args.use_gpu else paddle.CPUPlace()
assert os.path.exists(args.model_path), "args.model_path doesn't exist"
assert os.path.isdir(args.model_path), "args.model_path must be a dir"
exe = fluid.Executor(place)
exe = paddle.static.Executor(place)
quant_post(
executor=exe,
model_dir=args.model_path,
......
......@@ -21,7 +21,7 @@ import numpy as np
import math
from multiprocessing.dummy import Pool as ThreadPool
import paddle.fluid as fluid
import paddle
from paddle.fluid.framework import IrGraph
from paddle.fluid import core
......@@ -102,7 +102,7 @@ def _get_var_tensor(scope, var_name):
"""
get tensor array by name.
Args:
scope(fluid.Scope): scope to get var
scope(paddle.static.Scope): scope to get var
var_name(str): vatiable name
Return:
np.array
......@@ -158,8 +158,8 @@ def _quant_embedding_abs_max(graph, scope, place, config, var_name,
Args:
graph(IrGraph): graph that includes lookup_table op
scope(fluid.Scope): scope
place(fluid.CPUPlace or flud.CUDAPlace): place
scope(paddle.static.Scope): scope
place(paddle.CPUPlace or paddle.CUDAPlace): place
config(dict): config to quant
"""
......@@ -250,15 +250,14 @@ def _quant_embedding_abs_max(graph, scope, place, config, var_name,
graph.safe_remove_nodes(embedding_node)
def _quant_embedding_log(graph, scope, place, config, var_name,
embedding_node):
def _quant_embedding_log(graph, scope, place, config, var_name, embedding_node):
"""
quantize embedding using log
Args:
graph(IrGraph): graph that includes Embedding Parameter
scope(fluid.Scope): scope
place(fluid.CPUPlace or flud.CUDAPlace): place to run program
scope(paddle.static.Scope): scope
place(paddle.CPUPlace or paddle.CUDAPlace): place to run program
config(dict): config to quant Embedding
"""
......@@ -424,9 +423,9 @@ def quant_embedding(program, place, config=None, scope=None):
"""quantize lookup_table op parameters
Args:
program(fluid.Program): infer program
scope(fluid.Scope, optional): Scope records the mapping between variable names and variables, similar to brackets in programming languages. Usually users can use `fluid.global_scope() <https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api_cn/executor_cn/global_scope_cn.html>`_ . When ``None`` will use `fluid.global_scope() <https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api_cn/executor_cn/global_scope_cn.html>`_. Default : ``None``.
place(fluid.CPUPlace or fluid.CUDAPlace): This parameter represents the executor run on which device.
program(paddle.static.Program): infer program
scope(paddle.static.Scope, optional): Scope records the mapping between variable names and variables, similar to brackets in programming languages. Usually users can use `paddle.static.global_scope() <https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api_cn/executor_cn/global_scope_cn.html>`_ . When ``None`` will use `paddle.static.global_scope() <https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api_cn/executor_cn/global_scope_cn.html>`_. Default : ``None``.
place(paddle.CPUPlace or paddle.CUDAPlace): This parameter represents the executor run on which device.
config(dict, optional): config to quantize. The keys are 'quantize_op_types'. For op in quantize_op_types, you can define 'quantize_type', \
'quantize_bits', 'dtype', 'threshold'. \
``quantize_type`` is quantize type, supported types are ['abs_max'], default is "abs_max".
......@@ -440,7 +439,7 @@ def quant_embedding(program, place, config=None, scope=None):
"""
config = config or {}
config = _merge_config(copy.deepcopy(_default_config), config)
scope = fluid.global_scope() if scope is None else scope
scope = paddle.static.global_scope() if scope is None else scope
graph = IrGraph(core.Graph(program.desc), for_test=True)
quantize_params_map = {}
......
......@@ -18,7 +18,6 @@ import json
import logging
import paddle
import paddle.fluid as fluid
from paddle.fluid.framework import IrGraph
from paddle.fluid.contrib.slim.quantization import QuantizationTransformPass
from paddle.fluid.contrib.slim.quantization import QuantizationFreezePass
......@@ -191,14 +190,14 @@ def quant_aware(program,
for quantization training or testing.
Args:
program(fluid.Program): training or testing ``program``.
place(fluid.CPUPlace or fluid.CUDAPlace): This parameter represents
program(paddle.static.Program): training or testing ``program``.
place(paddle.CPUPlace or paddle.CUDAPlace): This parameter represents
the executor run on which device.
config(dict, optional): configs for quantization. if None, will use default config.
Default: None.
scope(fluid.Scope): Scope records the mapping between variable names and variables,
scope(paddle.static.Scope): Scope records the mapping between variable names and variables,
similar to brackets in programming languages. Usually users can use
`fluid.global_scope <https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api_cn/executor_cn/global_scope_cn.html>`_. When ``None`` will use `fluid.global_scope() <https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api_cn/executor_cn/global_scope_cn.html>`_ . Default: ``None``.
`paddle.static.global_scope <https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api_cn/executor_cn/global_scope_cn.html>`_. When ``None`` will use `paddle.static.global_scope() <https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api_cn/executor_cn/global_scope_cn.html>`_ . Default: ``None``.
for_test(bool): If the 'program' parameter is a test program, this parameter should be set to ``True``.
Otherwise, set to ``False``.Default: False
weight_quantize_func(function): Function that defines how to quantize weight. Using this
......@@ -225,15 +224,15 @@ def quant_aware(program,
Default is None.
optimizer_func(function): Fuction return a optimizer. When 'is_test' is False and user want to use self-defined
quantization function and preprocess function, this function must be set. Default is None.
exe(Fluid.Executor): If user want to use self-defined quantization function and preprocess function, exe must be set for
exe(paddle.static.Executor): If user want to use self-defined quantization function and preprocess function, exe must be set for
initialization. Default is None.
return_program(bool): If user want return value is a Program rather than Compiled Program, This argument should be set True.
Default is False.
Returns:
fluid.CompiledProgram | fluid.Program: Program with quantization and dequantization ``operators``
paddle.static.CompiledProgram | paddle.static.Program: Program with quantization and dequantization ``operators``
"""
scope = fluid.global_scope() if not scope else scope
scope = paddle.static.global_scope() if not scope else scope
if config is None:
config = _quant_config_default
else:
......@@ -297,7 +296,7 @@ def quant_aware(program,
if for_test or return_program:
quant_program = main_graph.to_program()
else:
quant_program = fluid.CompiledProgram(main_graph.graph)
quant_program = paddle.static.CompiledProgram(main_graph.graph)
return quant_program
......@@ -331,13 +330,13 @@ def quant_post_static(
and dequantization operators to obtain the quantized model.
Args:
executor(fluid.Executor): The executor to load, run and save the
executor(paddle.static.Executor): The executor to load, run and save the
quantized model.
model_dir(str): The path of fp32 model that will be quantized, and
the model and params that saved by ``fluid.io.save_inference_model``
the model and params that saved by ``paddle.static.io.save_inference_model``
are under the path.
quantize_model_path(str): The path to save quantized model using api
``fluid.io.save_inference_model``.
``paddle.static.io.save_inference_model``.
batch_generator(Python Generator): The batch generator provides
calibrate data for DataLoader, and it returns a batch every
time. For sample_generator and batch_generator, only one
......@@ -357,8 +356,8 @@ def quant_post_static(
batch_nums(int, optional): If batch_nums is not None, the number of calibrate
data is 'batch_size*batch_nums'. If batch_nums is None, use all data
generated by sample_generator as calibrate data.
scope(fluid.Scope, optional): The scope to run program, use it to load
and save variables. If scope is None, will use fluid.global_scope().
scope(paddle.static.Scope, optional): The scope to run program, use it to load
and save variables. If scope is None, will use paddle.static.global_scope().
algo(str, optional): If algo=KL, use KL-divergenc method to
get the more precise scale factor. If algo='direct', use
abs_max method to get the scale factor. Default: 'KL'.
......@@ -424,18 +423,18 @@ def convert(program, place, config=None, scope=None, save_int8=False):
``program``that can be used to save ``inference model``.
Args:
program(fluid.Program): quantized and well-trained ``test program``.
place(fluid.CPUPlace or fluid.CUDAPlace): This parameter represents
program(paddle.static.Program): quantized and well-trained ``test program``.
place(paddle.CPUPlace or paddle.CUDAPlace): This parameter represents
the executor run on which device.
config(dict, optional): configs for convert. if set None, will use
default config. It must be same with config that used in
'quant_aware'. Default is None.
scope(fluid.Scope, optional): Scope records the mapping between
scope(paddle.static.Scope, optional): Scope records the mapping between
variable names and variables, similar to brackets in
programming languages. Usually users can use
`fluid.global_scope <https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api_cn/executor_cn/global_scope_cn.html>`_.
`paddle.static.global_scope <https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api_cn/executor_cn/global_scope_cn.html>`_.
When ``None`` will use
`fluid.global_scope() <https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api_cn/executor_cn/global_scope_cn.html>`_
`paddle.static.global_scope() <https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api_cn/executor_cn/global_scope_cn.html>`_
. Default: ``None``.
save_int8: Whether to return ``program`` which model parameters'
dtype is ``int8``. This parameter can only be used to
......@@ -443,11 +442,11 @@ def convert(program, place, config=None, scope=None, save_int8=False):
Returns:
Tuple : freezed program which can be used for inference.
when ``save_int8`` is False, return ``freezed_program(fluid.Program)``.
when ``save_int8`` is True, return ``freezed_program(fluid.Program)``
and ``freezed_program_int8(fluid.Program)``
when ``save_int8`` is False, return ``freezed_program(paddle.static.Program)``.
when ``save_int8`` is True, return ``freezed_program(paddle.static.Program)``
and ``freezed_program_int8(paddle.static.Program)``
"""
scope = fluid.global_scope() if not scope else scope
scope = paddle.static.global_scope() if not scope else scope
if config is None:
config = _quant_config_default
......
......@@ -15,7 +15,6 @@ import sys
sys.path.append("../")
import unittest
import paddle
import paddle.fluid as fluid
from paddleslim.quant import quant_aware, convert
from static_case import StaticCase
sys.path.append("../demo")
......@@ -29,15 +28,15 @@ import numpy as np
class TestQuantAwareCase1(StaticCase):
def get_model(self):
image = fluid.layers.data(
name='image', shape=[1, 28, 28], dtype='float32')
label = fluid.layers.data(name='label', shape=[1], dtype='int64')
image = paddle.static.data(
name='image', shape=[None, 1, 28, 28], dtype='float32')
label = paddle.static.data(name='label', shape=[None, 1], dtype='int64')
model = MobileNet()
out = model.net(input=image, class_dim=10)
cost = fluid.layers.cross_entropy(input=out, label=label)
avg_cost = fluid.layers.mean(x=cost)
startup_prog = fluid.default_startup_program()
train_prog = fluid.default_main_program()
cost = paddle.nn.functional.loss.cross_entropy(input=out, label=label)
avg_cost = paddle.mean(x=cost)
startup_prog = paddle.static.default_startup_program()
train_prog = paddle.static.default_main_program()
return startup_prog, train_prog
def get_op_number(self, prog):
......@@ -54,9 +53,9 @@ class TestQuantAwareCase1(StaticCase):
def test_quant_op(self):
startup_prog, train_prog = self.get_model()
place = fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda(
) else fluid.CPUPlace()
exe = fluid.Executor(place)
place = paddle.CUDAPlace(0) if paddle.fluid.is_compiled_with_cuda(
) else paddle.CPUPlace()
exe = paddle.static.Executor(place)
exe.run(startup_prog)
config_1 = {
'weight_quantize_type': 'channel_wise_abs_max',
......@@ -91,39 +90,48 @@ class TestQuantAwareCase1(StaticCase):
class TestQuantAwareCase2(StaticCase):
def test_accuracy(self):
image = fluid.layers.data(
name='image', shape=[1, 28, 28], dtype='float32')
label = fluid.layers.data(name='label', shape=[1], dtype='int64')
image = paddle.static.data(
name='image', shape=[None, 1, 28, 28], dtype='float32')
label = paddle.static.data(name='label', shape=[None, 1], dtype='int64')
model = MobileNet()
out = model.net(input=image, class_dim=10)
cost = fluid.layers.cross_entropy(input=out, label=label)
avg_cost = fluid.layers.mean(x=cost)
acc_top1 = fluid.layers.accuracy(input=out, label=label, k=1)
acc_top5 = fluid.layers.accuracy(input=out, label=label, k=5)
optimizer = fluid.optimizer.Momentum(
cost = paddle.nn.functional.loss.cross_entropy(input=out, label=label)
avg_cost = paddle.mean(x=cost)
acc_top1 = paddle.metric.accuracy(input=out, label=label, k=1)
acc_top5 = paddle.metric.accuracy(input=out, label=label, k=5)
optimizer = paddle.optimizer.Momentum(
momentum=0.9,
learning_rate=0.01,
regularization=fluid.regularizer.L2Decay(4e-5))
weight_decay=paddle.regularizer.L2Decay(4e-5))
optimizer.minimize(avg_cost)
main_prog = fluid.default_main_program()
main_prog = paddle.static.default_main_program()
val_prog = main_prog.clone(for_test=True)
place = fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda(
) else fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
feeder = fluid.DataFeeder([image, label], place, program=main_prog)
train_reader = paddle.fluid.io.batch(
paddle.dataset.mnist.train(), batch_size=64)
eval_reader = paddle.fluid.io.batch(
paddle.dataset.mnist.test(), batch_size=64)
place = paddle.CUDAPlace(0) if paddle.fluid.is_compiled_with_cuda(
) else paddle.static.CPUPlace()
exe = paddle.static.Executor(place)
exe.run(paddle.static.default_startup_program())
train_loader = paddle.io.DataLoader.from_generator(
feed_list=[image, label],
capacity=512,
use_double_buffer=True,
iterable=True)
valid_loader = paddle.io.DataLoader.from_generator(
feed_list=[image, label],
capacity=512,
use_double_buffer=True,
iterable=True)
train_reader = paddle.batch(paddle.dataset.mnist.train(), batch_size=64)
eval_reader = paddle.batch(paddle.dataset.mnist.test(), batch_size=64)
train_loader.set_sample_list_generator(train_reader, place)
valid_loader.set_sample_list_generator(eval_reader, place)
def train(program):
iter = 0
for data in train_reader():
for data in train_loader():
cost, top1, top5 = exe.run(
program,
feed=feeder.feed(data),
feed=data,
fetch_list=[avg_cost, acc_top1, acc_top5])
iter += 1
if iter % 100 == 0:
......@@ -134,10 +142,10 @@ class TestQuantAwareCase2(StaticCase):
def test(program):
iter = 0
result = [[], [], []]
for data in eval_reader():
for data in valid_loader():
cost, top1, top5 = exe.run(
program,
feed=feeder.feed(data),
feed=data,
fetch_list=[avg_cost, acc_top1, acc_top5])
iter += 1
if iter % 100 == 0:
......
......@@ -15,90 +15,98 @@ import sys
sys.path.append("../")
import unittest
import paddle
import paddle.fluid as fluid
from paddleslim.quant import quant_aware, convert
from static_case import StaticCase
sys.path.append("../demo")
from models import MobileNet
from layers import conv_bn_layer
import paddle.dataset.mnist as reader
from paddle.fluid.framework import IrGraph
from paddle.fluid import core
import numpy as np
from paddle.fluid.layer_helper import LayerHelper
def pact(x, name=None):
def pact(x):
helper = LayerHelper("pact", **locals())
dtype = 'float32'
init_thres = 20
u_param_attr = fluid.ParamAttr(
u_param_attr = paddle.ParamAttr(
name=x.name + '_pact',
initializer=fluid.initializer.ConstantInitializer(value=init_thres),
regularizer=fluid.regularizer.L2Decay(0.0001),
initializer=paddle.nn.initializer.Constant(value=init_thres),
regularizer=paddle.regularizer.L2Decay(0.0001),
learning_rate=1)
u_param = helper.create_parameter(attr=u_param_attr, shape=[1], dtype=dtype)
x = fluid.layers.elementwise_sub(
x, fluid.layers.relu(fluid.layers.elementwise_sub(x, u_param)))
x = fluid.layers.elementwise_add(
x, fluid.layers.relu(fluid.layers.elementwise_sub(-u_param, x)))
part_a = paddle.nn.functional.relu(x - u_param)
part_b = paddle.nn.functional.relu(-u_param - x)
x = x - part_a + part_b
return x
def get_optimizer():
return fluid.optimizer.MomentumOptimizer(0.0001, 0.9)
return paddle.optimizer.Momentum(0.0001, 0.9)
class TestQuantAwareCase1(StaticCase):
def get_model(self):
image = fluid.layers.data(
name='image', shape=[1, 28, 28], dtype='float32')
label = fluid.layers.data(name='label', shape=[1], dtype='int64')
image = paddle.static.data(
name='image', shape=[None, 1, 28, 28], dtype='float32')
label = paddle.static.data(name='label', shape=[None, 1], dtype='int64')
model = MobileNet()
out = model.net(input=image, class_dim=10)
cost = fluid.layers.cross_entropy(input=out, label=label)
avg_cost = fluid.layers.mean(x=cost)
startup_prog = fluid.default_startup_program()
train_prog = fluid.default_main_program()
cost = paddle.nn.functional.loss.cross_entropy(input=out, label=label)
avg_cost = paddle.mean(x=cost)
startup_prog = paddle.static.default_startup_program()
train_prog = paddle.static.default_main_program()
return startup_prog, train_prog
def test_accuracy(self):
image = fluid.layers.data(
name='image', shape=[1, 28, 28], dtype='float32')
image = paddle.static.data(
name='image', shape=[None, 1, 28, 28], dtype='float32')
image.stop_gradient = False
label = fluid.layers.data(name='label', shape=[1], dtype='int64')
label = paddle.static.data(name='label', shape=[None, 1], dtype='int64')
model = MobileNet()
out = model.net(input=image, class_dim=10)
cost = fluid.layers.cross_entropy(input=out, label=label)
avg_cost = fluid.layers.mean(x=cost)
acc_top1 = fluid.layers.accuracy(input=out, label=label, k=1)
acc_top5 = fluid.layers.accuracy(input=out, label=label, k=5)
optimizer = fluid.optimizer.Momentum(
cost = paddle.nn.functional.loss.cross_entropy(input=out, label=label)
avg_cost = paddle.mean(x=cost)
acc_top1 = paddle.metric.accuracy(input=out, label=label, k=1)
acc_top5 = paddle.metric.accuracy(input=out, label=label, k=5)
optimizer = paddle.optimizer.Momentum(
momentum=0.9,
learning_rate=0.01,
regularization=fluid.regularizer.L2Decay(4e-5))
weight_decay=paddle.regularizer.L2Decay(4e-5))
optimizer.minimize(avg_cost)
main_prog = fluid.default_main_program()
main_prog = paddle.static.default_main_program()
val_prog = main_prog.clone(for_test=True)
place = fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda(
) else fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
feeder = fluid.DataFeeder([image, label], place, program=main_prog)
train_reader = paddle.fluid.io.batch(
paddle.dataset.mnist.train(), batch_size=64)
eval_reader = paddle.fluid.io.batch(
paddle.dataset.mnist.test(), batch_size=64)
train_loader = paddle.io.DataLoader.from_generator(
feed_list=[image, label],
capacity=512,
use_double_buffer=True,
iterable=True)
valid_loader = paddle.io.DataLoader.from_generator(
feed_list=[image, label],
capacity=512,
use_double_buffer=True,
iterable=True)
place = paddle.CUDAPlace(0) if paddle.fluid.is_compiled_with_cuda(
) else paddle.CPUPlace()
exe = paddle.static.Executor(place)
exe.run(paddle.static.default_startup_program())
train_reader = paddle.batch(paddle.dataset.mnist.train(), batch_size=64)
eval_reader = paddle.batch(paddle.dataset.mnist.test(), batch_size=64)
train_loader.set_sample_list_generator(train_reader, place)
valid_loader.set_sample_list_generator(eval_reader, place)
def train(program):
iter = 0
for data in train_reader():
for data in train_loader():
cost, top1, top5 = exe.run(
program,
feed=feeder.feed(data),
feed=data,
fetch_list=[avg_cost, acc_top1, acc_top5])
iter += 1
if iter % 100 == 0:
......@@ -109,10 +117,10 @@ class TestQuantAwareCase1(StaticCase):
def test(program):
iter = 0
result = [[], [], []]
for data in eval_reader():
for data in valid_loader():
cost, top1, top5 = exe.run(
program,
feed=feeder.feed(data),
feed=data,
fetch_list=[avg_cost, acc_top1, acc_top5])
iter += 1
if iter % 100 == 0:
......
......@@ -12,8 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import paddle
sys.path.append("../")
import paddle.fluid as fluid
import paddleslim.quant as quant
import unittest
......@@ -22,24 +22,25 @@ from static_case import StaticCase
class TestQuantEmbedding(StaticCase):
def test_quant_embedding(self):
train_program = fluid.Program()
with fluid.program_guard(train_program):
input_word = fluid.data(
train_program = paddle.static.Program()
with paddle.static.program_guard(train_program):
input_word = paddle.static.data(
name="input_word", shape=[None, 1], dtype='int64')
input_emb = fluid.embedding(
input=input_word,
is_sparse=False,
size=[100, 128],
param_attr=fluid.ParamAttr(
name='emb',
initializer=fluid.initializer.Uniform(-0.005, 0.005)))
param_attr = paddle.ParamAttr(
name='emb',
initializer=paddle.nn.initializer.Uniform(-0.005, 0.005))
weight = train_program.global_block().create_parameter(
(100, 128), attr=param_attr, dtype="float32")
input_emb = paddle.nn.functional.embedding(
x=input_word, weight=weight, sparse=True)
infer_program = train_program.clone(for_test=True)
use_gpu = True
place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
place = paddle.CUDAPlace(0) if use_gpu else paddle.CPUPlace()
exe = paddle.static.Executor(place)
exe.run(paddle.static.default_startup_program())
quant_program = quant.quant_embedding(infer_program, place)
......
......@@ -15,53 +15,59 @@ import sys
sys.path.append("../")
import unittest
import paddle
import paddle.fluid as fluid
from paddleslim.quant import quant_post_static
from static_case import StaticCase
sys.path.append("../demo")
from models import MobileNet
from layers import conv_bn_layer
import paddle.dataset.mnist as reader
from paddle.fluid.framework import IrGraph
from paddle.fluid import core
import numpy as np
class TestQuantAwareCase1(StaticCase):
def test_accuracy(self):
image = fluid.layers.data(
name='image', shape=[1, 28, 28], dtype='float32')
label = fluid.layers.data(name='label', shape=[1], dtype='int64')
image = paddle.static.data(
name='image', shape=[None, 1, 28, 28], dtype='float32')
label = paddle.static.data(name='label', shape=[None, 1], dtype='int64')
model = MobileNet()
out = model.net(input=image, class_dim=10)
cost = fluid.layers.cross_entropy(input=out, label=label)
avg_cost = fluid.layers.mean(x=cost)
acc_top1 = fluid.layers.accuracy(input=out, label=label, k=1)
acc_top5 = fluid.layers.accuracy(input=out, label=label, k=5)
optimizer = fluid.optimizer.Momentum(
cost = paddle.nn.functional.loss.cross_entropy(input=out, label=label)
avg_cost = paddle.mean(x=cost)
acc_top1 = paddle.metric.accuracy(input=out, label=label, k=1)
acc_top5 = paddle.metric.accuracy(input=out, label=label, k=5)
optimizer = paddle.optimizer.Momentum(
momentum=0.9,
learning_rate=0.01,
regularization=fluid.regularizer.L2Decay(4e-5))
weight_decay=paddle.regularizer.L2Decay(4e-5))
optimizer.minimize(avg_cost)
main_prog = fluid.default_main_program()
main_prog = paddle.static.default_main_program()
val_prog = main_prog.clone(for_test=True)
place = fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda(
) else fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
feeder = fluid.DataFeeder([image, label], place, program=main_prog)
train_reader = paddle.fluid.io.batch(
paddle.dataset.mnist.train(), batch_size=64)
eval_reader = paddle.fluid.io.batch(
paddle.dataset.mnist.test(), batch_size=64)
place = paddle.CUDAPlace(0) if paddle.fluid.is_compiled_with_cuda(
) else paddle.CPUPlace()
exe = paddle.static.Executor(place)
exe.run(paddle.static.default_startup_program())
train_loader = paddle.io.DataLoader.from_generator(
feed_list=[image, label],
capacity=512,
use_double_buffer=True,
iterable=True)
valid_loader = paddle.io.DataLoader.from_generator(
feed_list=[image, label],
capacity=512,
use_double_buffer=True,
iterable=True)
train_reader = paddle.batch(paddle.dataset.mnist.train(), batch_size=64)
eval_reader = paddle.batch(paddle.dataset.mnist.test(), batch_size=64)
train_loader.set_sample_list_generator(train_reader, place)
valid_loader.set_sample_list_generator(eval_reader, place)
def train(program):
iter = 0
for data in train_reader():
for data in train_loader():
cost, top1, top5 = exe.run(
program,
feed=feeder.feed(data),
feed=data,
fetch_list=[avg_cost, acc_top1, acc_top5])
iter += 1
if iter % 100 == 0:
......@@ -72,9 +78,9 @@ class TestQuantAwareCase1(StaticCase):
def test(program, outputs=[avg_cost, acc_top1, acc_top5]):
iter = 0
result = [[], [], []]
for data in train_reader():
for data in valid_loader():
cost, top1, top5 = exe.run(program,
feed=feeder.feed(data),
feed=data,
fetch_list=outputs)
iter += 1
if iter % 100 == 0:
......@@ -89,7 +95,7 @@ class TestQuantAwareCase1(StaticCase):
train(main_prog)
top1_1, top5_1 = test(val_prog)
fluid.io.save_inference_model(
paddle.static.save_inference_model(
dirname='./test_quant_post',
feeded_var_names=[image.name, label.name],
target_vars=[avg_cost, acc_top1, acc_top5],
......@@ -106,7 +112,7 @@ class TestQuantAwareCase1(StaticCase):
model_filename='model',
params_filename='params',
batch_nums=10)
quant_post_prog, feed_target_names, fetch_targets = fluid.io.load_inference_model(
quant_post_prog, feed_target_names, fetch_targets = paddle.static.load_inference_model(
dirname='./test_quant_post_inference',
executor=exe,
model_filename='__model__',
......
......@@ -15,53 +15,61 @@ import sys
sys.path.append("../")
import unittest
import paddle
import paddle.fluid as fluid
from paddleslim.quant import quant_post_dynamic
from static_case import StaticCase
sys.path.append("../demo")
from models import MobileNet
from layers import conv_bn_layer
import paddle.dataset.mnist as reader
from paddle.fluid.framework import IrGraph
from paddle.fluid import core
import numpy as np
class TestQuantPostOnlyWeightCase1(StaticCase):
def test_accuracy(self):
image = fluid.layers.data(
name='image', shape=[1, 28, 28], dtype='float32')
label = fluid.layers.data(name='label', shape=[1], dtype='int64')
image = paddle.static.data(
name='image', shape=[None, 1, 28, 28], dtype='float32')
label = paddle.static.data(name='label', shape=[None, 1], dtype='int64')
model = MobileNet()
out = model.net(input=image, class_dim=10)
cost = fluid.layers.cross_entropy(input=out, label=label)
avg_cost = fluid.layers.mean(x=cost)
acc_top1 = fluid.layers.accuracy(input=out, label=label, k=1)
acc_top5 = fluid.layers.accuracy(input=out, label=label, k=5)
optimizer = fluid.optimizer.Momentum(
cost = paddle.nn.functional.loss.cross_entropy(input=out, label=label)
avg_cost = paddle.mean(x=cost)
acc_top1 = paddle.metric.accuracy(input=out, label=label, k=1)
acc_top5 = paddle.metric.accuracy(input=out, label=label, k=5)
optimizer = paddle.optimizer.Momentum(
momentum=0.9,
learning_rate=0.01,
regularization=fluid.regularizer.L2Decay(4e-5))
weight_decay=paddle.regularizer.L2Decay(4e-5))
optimizer.minimize(avg_cost)
main_prog = fluid.default_main_program()
main_prog = paddle.static.default_main_program()
val_prog = main_prog.clone(for_test=True)
place = fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda(
) else fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
feeder = fluid.DataFeeder([image, label], place, program=main_prog)
train_reader = paddle.fluid.io.batch(
paddle.dataset.mnist.train(), batch_size=64)
eval_reader = paddle.fluid.io.batch(
paddle.dataset.mnist.test(), batch_size=64)
place = paddle.CUDAPlace(0) if paddle.fluid.is_compiled_with_cuda(
) else paddle.CPUPlace()
exe = paddle.static.Executor(place)
exe.run(paddle.static.default_startup_program())
train_loader = paddle.io.DataLoader.from_generator(
feed_list=[image, label],
capacity=512,
use_double_buffer=True,
iterable=True)
valid_loader = paddle.io.DataLoader.from_generator(
feed_list=[image, label],
capacity=512,
use_double_buffer=True,
iterable=True)
train_reader = paddle.batch(paddle.dataset.mnist.train(), batch_size=64)
eval_reader = paddle.batch(paddle.dataset.mnist.test(), batch_size=64)
train_loader.set_sample_list_generator(train_reader, place)
valid_loader.set_sample_list_generator(eval_reader, place)
def train(program):
iter = 0
for data in train_reader():
for data in train_loader():
cost, top1, top5 = exe.run(
program,
feed=feeder.feed(data),
feed=data,
fetch_list=[avg_cost, acc_top1, acc_top5])
iter += 1
if iter % 100 == 0:
......@@ -72,9 +80,9 @@ class TestQuantPostOnlyWeightCase1(StaticCase):
def test(program, outputs=[avg_cost, acc_top1, acc_top5]):
iter = 0
result = [[], [], []]
for data in train_reader():
for data in valid_loader():
cost, top1, top5 = exe.run(program,
feed=feeder.feed(data),
feed=data,
fetch_list=outputs)
iter += 1
if iter % 100 == 0:
......@@ -89,7 +97,7 @@ class TestQuantPostOnlyWeightCase1(StaticCase):
train(main_prog)
top1_1, top5_1 = test(val_prog)
fluid.io.save_inference_model(
paddle.static.save_inference_model(
dirname='./test_quant_post_dynamic',
feeded_var_names=[image.name, label.name],
target_vars=[avg_cost, acc_top1, acc_top5],
......@@ -104,7 +112,7 @@ class TestQuantPostOnlyWeightCase1(StaticCase):
model_filename='model',
params_filename='params',
generate_test_model=True)
quant_post_prog, feed_target_names, fetch_targets = fluid.io.load_inference_model(
quant_post_prog, feed_target_names, fetch_targets = paddle.static.load_inference_model(
dirname='./test_quant_post_inference/test_model', executor=exe)
top1_2, top5_2 = test(quant_post_prog, fetch_targets)
print("before quantization: top1: {}, top5: {}".format(top1_1, top5_1))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册