Files
FastDeploy/tools/rknpu2/export.py
Zheng_Bicheng dc13eb7049 [RKNPU2] Update quantitative model (#879)
* 对RKNPU2后端进行修改,当模型为非量化模型时,不在NPU执行normalize操作,当模型为量化模型时,在NUP上执行normalize操作

* 更新RKNPU2框架,输出数据的数据类型统一返回fp32类型

* 更新scrfd,拆分disable_normalize和disable_permute

* 更新scrfd代码,支持量化

* 更新scrfd python example代码

* 更新模型转换代码,支持量化模型

* 更新文档

* 按照要求修改

* 按照要求修改

* 修正模型转换文档

* 更新一下转换脚本
2022-12-19 13:58:43 +08:00

67 lines
2.4 KiB
Python

# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import yaml
import argparse
from rknn.api import RKNN
def get_config():
parser = argparse.ArgumentParser()
parser.add_argument("--verbose", default=True, help="rknntoolkit verbose")
parser.add_argument("--config_path")
parser.add_argument("--target_platform")
args = parser.parse_args()
return args
if __name__ == "__main__":
config = get_config()
with open(config.config_path) as file:
file_data = file.read()
yaml_config = yaml.safe_load(file_data)
print(yaml_config)
model = RKNN(config.verbose)
# Config
mean_values = yaml_config["mean"]
std_values = yaml_config["std"]
model.config(mean_values=mean_values, std_values=std_values, target_platform=config.target_platform)
# Load ONNX model
if yaml_config["outputs_nodes"] is None:
ret = model.load_onnx(model=yaml_config["model_path"])
else:
ret = model.load_onnx(model=yaml_config["model_path"], outputs=yaml_config["outputs_nodes"])
assert ret == 0, "Load model failed!"
# Build model
ret = model.build(do_quantization=yaml_config["do_quantization"], dataset=yaml_config["dataset"])
assert ret == 0, "Build model failed!"
# Init Runtime
ret = model.init_runtime()
assert ret == 0, "Init runtime environment failed!"
# Export
if not os.path.exists(yaml_config["output_folder"]):
os.mkdir(yaml_config["output_folder"])
model_base_name = os.path.basename(yaml_config["model_path"]).split(".")[0]
model_device_name = config.target_platform.lower()
model_save_name = model_base_name + "_" + model_device_name + ".rknn"
ret = model.export_rknn(os.path.join(yaml_config["output_folder"], model_save_name))
assert ret == 0, "Export rknn model failed!"
print("Export OK!")