Open mustafakasap opened 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.
Someone needs to update the documentation before more potential users spend unnecessary time googling fruitlessly for a solution like I did.
@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?
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
@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])
In the pre-processing sample code snippet:
image size variable type is set to int32 as:
But tiny Yolo V3 model's input data types are float32. Above (current sample) code gives following error:
Correct sample code should be: