AutoML工具支持Python API接口插入训练代码进行性能调优。在基于API方式进行模型性能调优时,用户需自行提供模型与数据集,调用API接口完成模型的调优过程。目前仅支持MindSpore框架模型的量化调优。模型量化期间,用户可手动配置参数,并使用部分数据完成对模型的校准,获取一个量化后的模型。
from ascend_model_compression.mindspore.quant.ptq_quant.quantize_model import quantize_model from ascend_model_compression.mindspore.quant.ptq_quant.create_config import create_quant_config from ascend_model_compression.mindspore.quant.ptq_quant.save_model import save_model
import logging logging.getLogger().setLevel(logging.INFO) #请根据实际情况进行配置
model = get_user_network() #指定预训练模型 load_checkpoint(ckpt_file_path, model) #模型加载预训练参数,请根据实际情况配置
config_file = "./quant_config.json" #待生成的量化配置文件存放路径及名称,请根据实际情况配置 create_quant_config(config_file, model)
通过create_quant_config接口生成的默认配置文件,用户可以手动修改其中的配置项,以达到手动控制量化的目的。
input_data = ms.Tensor(np.random.uniform(size=[256, 3, 224, 224]), dtype=mstype.float32) #请根据实际情况配置 model_calibrate = quantize_model(config_file, model, input_data) #通过调用quantize_model接口生成的量化后的模型
若原模型包含logger,将无法对原模型进行deepcopy,影响后续量化算子的插入,用户在使用 quantize_mdoel() 接口前需删除原模型中的logger属性。
calibrate(model_calibrate, dataset) #请根据实际情况配置数据集
file_name = "./quantized_model" #指定量化后模型保存路径和文件名 save_model(file_name, model_calibrate, input_data, file_format="AIR") #请根据需要配置量化后模型的格式
“file_format”支持AIR、MINDIR两种格式,默认值为AIR。
python3 ms_quant_model.py