microsoft / microsoft/onnxruntime-inference-examples

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

Open
#447 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
1.7k
Forks
414
Avg merge
1d 6h
Merged PRs (30d)
14

Description

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,
)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with _preprocess_images and DataReader, then reproduce the quantize_static call using the shown model and calibration image paths. Check the constructed batch_data against the model input shape and inspect the ReduceMax failure during quantization. Done means quantize_static completes and writes int8.onnx without the RuntimeException.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.