microsoft / onnxruntime-inference-examples

Examples for using ONNX Runtime for machine learning inferencing.
MIT License
1.07k stars 312 forks source link

onnxruntime.capi.onnxruntime_pybind11_state.RuntimeException: [ONNXRuntimeError] : 6 : RUNTIME_EXCEPTION : Non-zero status code returned while running ReduceMax node. Name:'488_ReduceMax' Status Message: #447

Open fanshao123456 opened 3 weeks ago

fanshao123456 commented 3 weeks ago

def _preprocess_images(images_folder: str, height: int, width: int, size_limit=0): """ Loads a batch of images and preprocess them parameter images_folder: path to folder storing images parameter height: image height in pixels parameter width: image width in pixels parameter size_limit: number of images to load. Default is 0 which means all images are picked. return: list of matrices characterizing multiple images """ image_names = os.listdir(images_folder) if size_limit > 0 and len(image_names) >= size_limit: batch_filenames = [image_names[i] for i in range(size_limit)] else: batch_filenames = image_names unconcatenated_batch_data = []

for image_name in batch_filenames:
    image_filepath = images_folder + "/" + image_name
    pillow_img = Image.new("RGB", (width, height))
    pillow_img.paste(Image.open(image_filepath).resize((width, height)))
    input_data = (numpy.float32(pillow_img) - numpy.array(
        [0, 0,0], dtype=numpy.float32
    ))/255.0
    nhwc_data = numpy.expand_dims(input_data, axis=0)
    nchw_data = nhwc_data.transpose(0, 3, 1, 2)  # ONNX Runtime standard
    unconcatenated_batch_data.append(nchw_data)
batch_data = numpy.concatenate(
    numpy.expand_dims(unconcatenated_batch_data, axis=0), axis=0
)
return batch_data

class DataReader(CalibrationDataReader): def init(self, calibration_image_folder: str, model_path: str): self.enum_data = None

    # Use inference session to get input shape.
    session = onnxruntime.InferenceSession(model_path, None)
    (_, _, height, width) = session.get_inputs()[0].shape

    # Convert image to input data
    self.nhwc_data_list = _preprocess_images(
        calibration_image_folder, height, width, size_limit=0
    )
    self.input_name = session.get_inputs()[0].name
    self.datasize = len(self.nhwc_data_list)

def get_next(self):
    if self.enum_data is None:
        self.enum_data = iter(
            [{self.input_name: nhwc_data} for nhwc_data in self.nhwc_data_list]
        )
    return next(self.enum_data, None)

def rewind(self):
    self.enum_data = None

input_model_path = '/home/fanshao/mmlab/mmyolo-0.6.0/projects/easydeploy/tools/work_dir/best_coco_bbox_mAP_epoch_300.onnx' # 输入onnx模型 output_model_path = '/home/fanshao/mmlab/mmyolo-0.6.0/projects/easydeploy/tools/work_dir/int8.onnx' # 输出模型名 calibration_dataset_path = '/home/fanshao/mmlab/mmyolo-0.6.0/projects/easydeploy/tools/calibration_img' # 校准数据集图像地址

用于校准数据加载,注意这个方法里面需要做图像一些操作,与pytorch训练的时候加载数据操作一致

dr = DataReader(calibration_dataset_path, input_model_path)

开始量化

quantize_static( input_model_path, output_model_path, dr, quant_format=QuantFormat.QDQ, weight_type=QuantType.QUInt8, )

fanshao123456 commented 3 weeks ago

i have a problem

VictoriaKlyueva commented 3 weeks ago

Try to downgrade to version 1.15.0, a similar error occurs on later releases. It helped me with a similar problem.