|
想请教下问题:
- 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)
输出为: Graph output 'Concat_41' is not an output of any node in graph.
请问这个代码哪里出现问题呢,或者说输出"Concat_41"结果无法通过这个方法实现? |
|