onnx / models

A collection of pre-trained, state-of-the-art models in the ONNX format
http://onnx.ai/models/
Apache License 2.0
7.95k stars 1.41k forks source link

Data type error #257

Open mustafakasap opened 4 years ago

mustafakasap commented 4 years ago

In the pre-processing sample code snippet:

# input
image_data = preprocess(image)
image_size = np.array([image.size[1], image.size[0]], dtype=np.int32).reshape(1, 2)

image size variable type is set to int32 as:

image_size = np.array([image.size[1], image.size[0]], **dtype=np.int32**).reshape(1, 2)

But tiny Yolo V3 model's input data types are float32. Above (current sample) code gives following error:

    boxes, scores, indices = session.run(None, {"input_1": imgData, "image_shape":imgSize})
  File "/opt/miniconda/lib/python3.7/site-packages/onnxruntime/capi/session.py", line 142, in run
    return self._sess.run(output_names, input_feed, run_options)
onnxruntime.capi.onnxruntime_pybind11_state.InvalidArgument: [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Unexpected input data type. Actual: (N11onnxruntime17PrimitiveDataTypeIiEE) , expected: (N11onnxruntime17PrimitiveDataTypeIfEE)

Correct sample code should be:

# input
image_data = preprocess(image)
image_size = np.array([image.size[1], image.size[0]], dtype=np.float32).reshape(1, 2)
cardboardcode commented 4 years ago

Hi, faced the same error when attempting to run yolov3-10 onnx model.

Verified that @mustafakasap 's correct sample code resolves the error.

Follow-Up Action

Someone needs to update the documentation before more potential users spend unnecessary time googling fruitlessly for a solution like I did.

Documentations to Update

[yolov3-10] [tiny-yolov3-11]

MuhammadAsadJaved commented 4 years ago

@mustafakasap Hi, I am using yolov3.onnx model for inference. The sample code only defines image pre and post processing step. Can you please share a complete example code to use Yolov3.onnx model for inference on the image input?

mustafakasap commented 4 years ago

please see the “class MLModel” here for full class with pre/post processes: https://github.com/Azure/live-video-analytics/blob/master/utilities/video-analysis/notebooks/Yolo/yolov3/yolov3-http-icpu-onnx/create_yolov3_icpu_inference_engine.ipynb

in this sample it assumes to receive the images in 416x416 pixel size. replace 416 with image.shape.width and height appropriately

MuhammadAsadJaved commented 4 years ago

@mustafakasap Sir that is a kind of different example. I am using the following code and it can run, but Now I do not know how to pass the input image and draw bounding boxes. on the image.

import numpy as np
from PIL import Image
import onnxruntime

# this function is from yolo3.utils.letterbox_image
def letterbox_image(image, size):
    '''resize image with unchanged aspect ratio using padding'''
    iw, ih = image.size
    w, h = size
    scale = min(w/iw, h/ih)
    nw = int(iw*scale)
    nh = int(ih*scale)

    image = image.resize((nw,nh), Image.BICUBIC)
    new_image = Image.new('RGB', size, (128,128,128))
    new_image.paste(image, ((w-nw)//2, (h-nh)//2))
    return new_image

def preprocess(img):
    model_image_size = (416, 416)
    boxed_image = letterbox_image(img, tuple(reversed(model_image_size)))
    image_data = np.array(boxed_image, dtype='float32')
    image_data /= 255.
    image_data = np.transpose(image_data, [2, 0, 1])
    image_data = np.expand_dims(image_data, 0)
    return image_data

#image = Image.open(img_path)
# input
#image_data = preprocess(image)

session = onnxruntime.InferenceSession('./models/yolov3-10.onnx')
image = Image.open('./dog416.jpg')
# input
#image_data = preprocess(image)
#image_size = np.array([image.size[1], image.size[0]], dtype=np.int32).reshape(1, 2)

image_data = preprocess(image)
image_size = np.array([image.size[1], image.size[0]], dtype=np.float32).reshape(1, 2)

boxes, scores, indices = session.run(None, {"input_1": image_data, "image_shape":image_size})

out_boxes, out_scores, out_classes = [], [], []
for idx_ in indices:
    out_classes.append(idx_[1])
    out_scores.append(scores[tuple(idx_)])
    idx_1 = (idx_[0], idx_[2])
    out_boxes.append(boxes[idx_1])