提交 b1e04ba7 编写于 作者: W wanghaoshuang

Modify quant model

上级 c7ec8584
...@@ -36,6 +36,8 @@ sys.path.append(os.path.join(os.path.dirname("__file__"))) ...@@ -36,6 +36,8 @@ sys.path.append(os.path.join(os.path.dirname("__file__")))
from optimizer import create_optimizer from optimizer import create_optimizer
sys.path.append( sys.path.append(
os.path.join(os.path.dirname("__file__"), os.path.pardir, os.path.pardir)) 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 utility import add_arguments, print_arguments
from models.dygraph.mobilenet_v3 import MobileNetV3_large_x1_0 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): ...@@ -74,8 +76,8 @@ def load_dygraph_pretrain(model, path=None, load_static_weights=False):
for key in model_dict.keys(): for key in model_dict.keys():
weight_name = model_dict[key].name weight_name = model_dict[key].name
if weight_name in pre_state_dict.keys(): if weight_name in pre_state_dict.keys():
print('Load weight: {}, shape: {}'.format( # print('Load weight: {}, shape: {}'.format(
weight_name, pre_state_dict[weight_name].shape)) # weight_name, pre_state_dict[weight_name].shape))
param_state_dict[key] = pre_state_dict[weight_name] param_state_dict[key] = pre_state_dict[weight_name]
else: else:
param_state_dict[key] = model_dict[key] param_state_dict[key] = model_dict[key]
...@@ -125,7 +127,7 @@ def compress(args): ...@@ -125,7 +127,7 @@ def compress(args):
else: else:
raise ValueError("{} is not supported.".format(args.model)) raise ValueError("{} is not supported.".format(args.model))
_logger.info("Origin model summary:") _logger.info("Origin model summary:")
paddle.summary(net, (1, 3, 224, 224)) # paddle.summary(net, (1, 3, 224, 224))
############################################################################################################ ############################################################################################################
# 1. quantization configs # 1. quantization configs
...@@ -163,7 +165,18 @@ def compress(args): ...@@ -163,7 +165,18 @@ def compress(args):
quanter = QAT(config=quant_config) quanter = QAT(config=quant_config)
quanter.quantize(net) 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)) paddle.summary(net, (1, 3, 224, 224))
opt, lr = create_optimizer(net, trainer_num, args) opt, lr = create_optimizer(net, trainer_num, args)
...@@ -178,7 +191,7 @@ def compress(args): ...@@ -178,7 +191,7 @@ def compress(args):
batch_sampler=train_batch_sampler, batch_sampler=train_batch_sampler,
places=place, places=place,
return_list=True, return_list=True,
num_workers=4) num_workers=1)
valid_loader = paddle.io.DataLoader( valid_loader = paddle.io.DataLoader(
val_dataset, val_dataset,
...@@ -187,7 +200,14 @@ def compress(args): ...@@ -187,7 +200,14 @@ def compress(args):
shuffle=False, shuffle=False,
drop_last=False, drop_last=False,
return_list=True, 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() @paddle.no_grad()
def test(epoch, net): def test(epoch, net):
...@@ -304,6 +324,25 @@ def compress(args): ...@@ -304,6 +324,25 @@ def compress(args):
train_reader_cost = 0.0 train_reader_cost = 0.0
train_run_cost = 0.0 train_run_cost = 0.0
total_samples = 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 batch_id += 1
reader_start = time.time() reader_start = time.time()
......
...@@ -159,9 +159,10 @@ class MobileNetV3(nn.Layer): ...@@ -159,9 +159,10 @@ class MobileNetV3(nn.Layer):
def forward(self, inputs, label=None): def forward(self, inputs, label=None):
x = self.conv1(inputs) x = self.conv1(inputs)
i=0
for block in self.block_list: for block in self.block_list:
x = block(x) x = block(x)
i += 1
x = self.last_second_conv(x) x = self.last_second_conv(x)
x = self.pool(x) x = self.pool(x)
......
...@@ -15,6 +15,7 @@ __all__ = [ ...@@ -15,6 +15,7 @@ __all__ = [
class MobileNetV3(): class MobileNetV3():
def __init__(self, scale=1.0, model_name='small'): def __init__(self, scale=1.0, model_name='small'):
print("init mobilenetv3------------------------")
self.scale = scale self.scale = scale
self.inplanes = 16 self.inplanes = 16
if model_name == "large": if model_name == "large":
...@@ -59,6 +60,7 @@ class MobileNetV3(): ...@@ -59,6 +60,7 @@ class MobileNetV3():
raise NotImplementedError raise NotImplementedError
def net(self, input, class_dim=1000): def net(self, input, class_dim=1000):
print("mobilenetv3 net-------------------------")
scale = self.scale scale = self.scale
inplanes = self.inplanes inplanes = self.inplanes
cfg = self.cfg cfg = self.cfg
...@@ -77,6 +79,7 @@ class MobileNetV3(): ...@@ -77,6 +79,7 @@ class MobileNetV3():
if_act=True, if_act=True,
act='hard_swish', act='hard_swish',
name='conv1') name='conv1')
i = 0 i = 0
for layer_cfg in cfg: for layer_cfg in cfg:
conv = self.residual_unit( conv = self.residual_unit(
......
...@@ -285,6 +285,16 @@ def compress(args): ...@@ -285,6 +285,16 @@ def compress(args):
optimizer_func=optimizer_func, optimizer_func=optimizer_func,
executor=executor, executor=executor,
for_test=True) 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( compiled_train_prog = quant_aware(
train_prog, train_prog,
place, place,
......
...@@ -143,21 +143,55 @@ def compress(args): ...@@ -143,21 +143,55 @@ def compress(args):
# According to the weight and activation quantization type, the graph will be added # According to the weight and activation quantization type, the graph will be added
# some fake quantize operators and fake dequantize operators. # 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) 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 = create_optimizer(args)
opt.minimize(avg_cost) opt.minimize(avg_cost)
exe = paddle.static.Executor(place) exe = paddle.static.Executor(place)
exe.run(paddle.static.default_startup_program()) exe.run(paddle.static.default_startup_program())
assert os.path.exists( float_path = "./outputs"
args.pretrained_model), "pretrained_model doesn't exist"
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: # if args.pretrained_model:
paddle.static.load(train_prog, args.pretrained_model, exe) # paddle.static.load(train_prog, args.pretrained_model, exe)
places = paddle.static.cuda_places( places = paddle.static.cuda_places(
) if args.use_gpu else paddle.static.cpu_places() ) if args.use_gpu else paddle.static.cpu_places()
...@@ -232,6 +266,7 @@ def compress(args): ...@@ -232,6 +266,7 @@ def compress(args):
build_strategy.fuse_all_reduce_ops = False build_strategy.fuse_all_reduce_ops = False
build_strategy.sync_batch_norm = False build_strategy.sync_batch_norm = False
exec_strategy = paddle.static.ExecutionStrategy() exec_strategy = paddle.static.ExecutionStrategy()
compiled_train_prog = paddle.static.CompiledProgram(train_prog)
compiled_train_prog = compiled_train_prog.with_data_parallel( compiled_train_prog = compiled_train_prog.with_data_parallel(
loss_name=avg_cost.name, loss_name=avg_cost.name,
build_strategy=build_strategy, build_strategy=build_strategy,
......
...@@ -18,20 +18,20 @@ import logging ...@@ -18,20 +18,20 @@ import logging
import paddle import paddle
import paddle.nn as nn import paddle.nn as nn
import paddle.fluid.contrib.slim.quantization as Q import paddle.fluid.contrib.slim.quantization as Q
from paddle.fluid.contrib.slim.quantization import AbsmaxQuantizer #from paddle.fluid.contrib.slim.quantization import AbsmaxQuantizer
from paddle.fluid.contrib.slim.quantization import HistQuantizer #from paddle.fluid.contrib.slim.quantization import HistQuantizer
from paddle.fluid.contrib.slim.quantization import KLQuantizer #from paddle.fluid.contrib.slim.quantization import KLQuantizer
from paddle.fluid.contrib.slim.quantization import PerChannelAbsmaxQuantizer #from paddle.fluid.contrib.slim.quantization import PerChannelAbsmaxQuantizer
from ...common import get_logger from ...common import get_logger
_logger = get_logger(__name__, level=logging.INFO) _logger = get_logger(__name__, level=logging.INFO)
__all__ = [ __all__ = [
'PTQ', 'PTQ',
'AbsmaxQuantizer', # 'AbsmaxQuantizer',
'HistQuantizer', # 'HistQuantizgger',
'KLQuantizer', # 'KLQuantizer',
'PerChannelAbsmaxQuantizer', # 'PerChannelAbsmaxQuantizer',
] ]
......
...@@ -16,7 +16,7 @@ import copy ...@@ -16,7 +16,7 @@ import copy
import logging import logging
import paddle import paddle
from paddle.fluid.contrib.slim.quantization import ImperativeQuantAware from paddle.fluid.contrib.slim.quantization import ImperativeQuantAwareV2, ImperativeQuantAware
from ...common import get_logger from ...common import get_logger
_logger = get_logger(__name__, level=logging.INFO) _logger = get_logger(__name__, level=logging.INFO)
...@@ -195,7 +195,7 @@ class QAT(object): ...@@ -195,7 +195,7 @@ class QAT(object):
self.weight_quantize = weight_quantize self.weight_quantize = weight_quantize
self.act_quantize = act_quantize self.act_quantize = act_quantize
self.imperative_qat = ImperativeQuantAware( self.imperative_qat = ImperativeQuantAwareV2(
weight_bits=self.config['weight_bits'], weight_bits=self.config['weight_bits'],
activation_bits=self.config['activation_bits'], activation_bits=self.config['activation_bits'],
weight_quantize_type=self.config['weight_quantize_type'], 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.
先完成此消息的编辑!
想要评论请 注册