- from rknn.api import RKNN
- import os
- if __name__ == '__main__':
- DATASET_PATH = "./car_dataset/car_subset_20.txt"
- # modified_model.onnx remove Floor op
- model_path = "../onnx/onnx_model_modify_replacefloor.onnx"
- platform = "rk3588"
- output_path = "./rk3588_modified_new.rknn"
- # Create RKNN object
- rknn = RKNN(verbose=False)
- # Pre-process config
- print('--> Config model')
- rknn.config(target_platform=platform, quantized_dtype="asymmetric_quantized-8")
- print('done')
- # Load model
- print('--> Loading model')
- ret = rknn.load_onnx(model=model_path)
- if ret != 0:
- print('Load model failed!')
- exit(ret)
- print('done')
- # Build model
- print('--> Building model')
- ret = rknn.build(do_quantization=True, dataset=DATASET_PATH)
- if ret != 0:
- print('Build model failed!')
- exit(ret)
- print('done')
- # Export rknn model
- print('--> Export rknn model')
- ret = rknn.export_rknn(output_path)
- if ret != 0:
- print('Export rknn model failed!')
- exit(ret)
- print('done')
- # Release
- rknn.release()
复制代码
jefferyzhang 发表于 2024-12-25 09:57
1. 是否量化
2. 量化后rknn模型输出是否和onnx一致
3. 不量化rknn模型输出是否和onnx一致
jefferyzhang 发表于 2024-12-25 09:57
1. 是否量化
2. 量化后rknn模型输出是否和onnx一致
3. 不量化rknn模型输出是否和onnx一致
- import numpy as np
- from PIL import Image
- import onnx
- from onnx import helper, TensorProto
- import numpy as np
- import onnxruntime as ort
- image_path = '1.jpg' # 你的图像路径
- image = Image.open(image_path)
- # 调整图像尺寸为模型输入要求的尺寸 (896, 448)
- image = image.resize((896, 448)) # (宽, 高)
- # 将图像转换为RGB(如果它是灰度图或者有其他颜色模式)
- image = image.convert('RGB')
- # 转换为numpy数组,并将数据类型设置为float32
- image_np = np.array(image).astype(np.float32)
- # 归一化:将像素值缩放到 [-1, 1]
- image_np = (image_np / 127.5) - 1.0 # 将 [0, 255] 范围的像素值归一化到 [-1, 1]
- # 调整形状为 (1, 3, 448, 896)
- image_np = np.transpose(image_np, (2, 0, 1)) # 转换为 (3, 448, 896) 形式
- image_np = np.expand_dims(image_np, axis=0) # 增加批次维度,变为 (1, 3, 448, 896)
- print(image_np.shape)
- # load model
- model = onnx.load_model("model_nchw.onnx")
- new_model_name="model_nchw_output.onnx"
- input_shape=[1,12,224,448]
- # add output
- intermediate_layer_value_info = helper.make_tensor_value_info('Concat_41', TensorProto.FLOAT, input_shape)
- model.graph.output.extend([intermediate_layer_value_info])
- onnx.save(model, new_model_name)
- onnx.checker.check_model(model)
- # run model
- ort_session = ort.InferenceSession(new_model_name)
- input_name = ort_session.get_inputs()[0].name
- conf_onnx, boxes_onnx, tmp_onnx = ort_session.run(None, {input_name: image_np})
- ## ========================== Result Compare ===============================
- print('********************* Intermidiante Layer Number: %s *******************' % 'input')
- tmp_onnx = np.squeeze(tmp_onnx)
- tmp_onnx = tmp_onnx.reshape(-1,tmp_onnx.shape[-1])
- # for idx in range(tmp_onnx.shape[0],2):
- for idx in range(0,4):
- print('********************* Onnx:%d ****************************' % idx)
- print(tmp_onnx[idx,:20])
- total_weight = np.sum(np.abs(tmp_onnx))
- print('totoal-weight:', total_weight)
复制代码
月夜留香 发表于 2024-12-26 18:01
想请教下问题:
输出为: Graph output 'Concat_41' is not an output of any node in graph.
欢迎光临 Toybrick (https://t.rock-chips.com/) | Powered by Discuz! X3.3 |