quant_aware训练出来的权重不是在int8范围
Created by: HaoLiuHust
训练代码如下:
import os
# os.environ['CUDA_VISIBLE_DEVICES']="1,2,3,4,5,6,7,8"
import paddle.fluid as fluid
from paddleslim.quant import quant_aware,convert
import numpy as np
import cv2
import models.paddle_caffe2.model_with_code.model as paddle_model
import batch_reader
import math
from PaddleCV.metric_learning.losses import arcmarginloss
import sklearn.preprocessing as sp
from quant_config import quant_config
train_program=fluid.default_main_program()
inputs, outputs,labels = paddle_model.x2paddle_net()
place=fluid.CUDAPlace(0)
val_program = fluid.default_main_program().clone(for_test=True)
# hidden=fluid.layers.fc(outputs[0],366984,bias_attr=False)
arc_loss=arcmarginloss.ArcMarginLoss(class_dim=366984,margin=0.4,scale=64,weight_init='./fc7_weight_init.npy')
out_loss,out=arc_loss.loss(outputs[0],labels[0])
loss=fluid.layers.mean(out_loss)
sgd=fluid.optimizer.Momentum(learning_rate=0.0000001,
momentum=0.9,
regularization=fluid.regularizer.L2DecayRegularizer(0.00004))
sgd.minimize(loss)
exe=fluid.Executor(fluid.CUDAPlace(0))
exe.run(fluid.default_startup_program())
def if_exist(var):
b = os.path.exists(os.path.join(param_dir, var.name))
return b
save_dir="trained_models"
if not os.path.join(save_dir):
os.makedirs(save_dir)
param_dir="models/paddle_caffe2/model_with_code/"
fluid.io.load_vars(exe, param_dir,
predicate=if_exist)
# fluid.io.load_persistables(exe, dirname=os.path.join(save_dir, "best_model"), main_program=train_program)
py_reader=fluid.io.PyReader(feed_list=[inputs[0],labels[0]],capacity=10)
py_reader.decorate_batch_generator(batch_reader.BatchReader,places=fluid.cuda_places())
build_strategy=fluid.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()
compile_prog = quant_aware(program=train_program,place=place,config=quant_config)
val_program = quant_aware(program=val_program,place=fluid.CUDAPlace(1),config=quant_config,for_test=True)
compile_prog=compile_prog.with_data_parallel(loss_name=loss.name,build_strategy=build_strategy,exec_strategy=exec_strategy)
feeder=fluid.DataFeeder(place=fluid.CUDAPlace(1),feed_list=[inputs[0].name,labels[0].name],program=val_program)
cnt=0
for data in py_reader():
loss_val=exe.run(compile_prog,feed=data,fetch_list=[loss])
# loss_val=exe.run(train_program,feed=data,fetch_list=[loss])
if cnt % 100 == 0:
print("loss {}".format(np.mean(loss_val)))
if cnt % 1000 == 0:
# float_program, int8_program = convert(val_program, place, quant_config, scope=None, save_int8=True)
fluid.io.save_persistables(exe, dirname=os.path.join(save_dir, "best_model"), main_program=val_program)
# fluid.io.save_inference_model(dirname=os.path.join(save_dir, "float"), feeded_var_names=[inputs[0].name],
# target_vars=[outputs[0]], executor=exe,
# main_program=float_program, model_filename="__model__", params_filename="__params__")
#
# fluid.io.save_inference_model(dirname=os.path.join(save_dir, "int8"), feeded_var_names=[inputs[0].name],
# target_vars=[outputs[0]], executor=exe,
# main_program=int8_program, model_filename="__model__", params_filename="__params__")
cnt += 1