How to run yolow ONNX locally?

#10
by nospotfer - opened

Hi! is there any snippet anywhere about how to load and run inference in yolow-l.onnx locally with onnxruntime?
It is necessary to know the image and text preprocessing steps.
Thank you!

I can save and predict my own custom classes with custom prompts with ultralytics, but I can't do that here.

Load the exported ONNX model

from ultralytics import YOLO
onnx_model = YOLO("yolov8x-worldv2.onnx", task="detect")

Run inference

results = onnx_model("https://ultralytics.com/images/bus.jpg")

Tried to detect garage doors. Downloaded the onnx model from the demo. But can not run locally.

from ultralytics import YOLO
onnx_model = YOLO("yolow-l.onnx", task="detect")
results = onnx_model("garage.jpeg")

Error:

{
    "name": "IndexError",
    "message": "amax(): Expected reduction dim 1 to have non-zero size.",
    "stack": "---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[6], line 1
----> 1 results = onnx_model(\"garage.jpeg\")

File ~/miniconda3/envs/onnx_rt/lib/python3.11/site-packages/ultralytics/engine/model.py:176, in Model.__call__(self, source, stream, **kwargs)
    147 def __call__(
    148     self,
    149     source: Union[str, Path, int, Image.Image, list, tuple, np.ndarray, torch.Tensor] = None,
    150     stream: bool = False,
    151     **kwargs,
    152 ) -> list:
    153     \"\"\"
    154     Alias for the predict method, enabling the model instance to be callable for predictions.
    155 
   (...)
    174         ...     print(f\"Detected {len(r)} objects in image\")
    175     \"\"\"
--> 176     return self.predict(source, stream, **kwargs)

File ~/miniconda3/envs/onnx_rt/lib/python3.11/site-packages/ultralytics/engine/model.py:554, in Model.predict(self, source, stream, predictor, **kwargs)
    552 if prompts and hasattr(self.predictor, \"set_prompts\"):  # for SAM-type models
    553     self.predictor.set_prompts(prompts)
--> 554 return self.predictor.predict_cli(source=source) if is_cli else self.predictor(source=source, stream=stream)

File ~/miniconda3/envs/onnx_rt/lib/python3.11/site-packages/ultralytics/engine/predictor.py:168, in BasePredictor.__call__(self, source, model, stream, *args, **kwargs)
    166     return self.stream_inference(source, model, *args, **kwargs)
    167 else:
--> 168     return list(self.stream_inference(source, model, *args, **kwargs))

File ~/miniconda3/envs/onnx_rt/lib/python3.11/site-packages/torch/utils/_contextlib.py:36, in _wrap_generator.<locals>.generator_context(*args, **kwargs)
     33 try:
     34     # Issuing `None` to a generator fires it up
     35     with ctx_factory():
---> 36         response = gen.send(None)
     38     while True:
     39         try:
     40             # Forward the response to our caller and get its next request

File ~/miniconda3/envs/onnx_rt/lib/python3.11/site-packages/ultralytics/engine/predictor.py:261, in BasePredictor.stream_inference(self, source, model, *args, **kwargs)
    259 # Postprocess
    260 with profilers[2]:
--> 261     self.results = self.postprocess(preds, im, im0s)
    262 self.run_callbacks(\"on_predict_postprocess_end\")
    264 # Visualize, save, write results

File ~/miniconda3/envs/onnx_rt/lib/python3.11/site-packages/ultralytics/models/yolo/detect/predict.py:25, in DetectionPredictor.postprocess(self, preds, img, orig_imgs)
     23 def postprocess(self, preds, img, orig_imgs):
     24     \"\"\"Post-processes predictions and returns a list of Results objects.\"\"\"
---> 25     preds = ops.non_max_suppression(
     26         preds,
     27         self.args.conf,
     28         self.args.iou,
     29         agnostic=self.args.agnostic_nms,
     30         max_det=self.args.max_det,
     31         classes=self.args.classes,
     32     )
     34     if not isinstance(orig_imgs, list):  # input images are a torch.Tensor, not a list
     35         orig_imgs = ops.convert_torch2numpy_batch(orig_imgs)

File ~/miniconda3/envs/onnx_rt/lib/python3.11/site-packages/ultralytics/utils/ops.py:230, in non_max_suppression(prediction, conf_thres, iou_thres, classes, agnostic, multi_label, labels, max_det, nc, max_time_img, max_nms, max_wh, in_place, rotated)
    228 nm = prediction.shape[1] - nc - 4  # number of masks
    229 mi = 4 + nc  # mask start index
--> 230 xc = prediction[:, 4:mi].amax(1) > conf_thres  # candidates
    232 # Settings
    233 # min_wh = 2  # (pixels) minimum box width and height
    234 time_limit = 2.0 + max_time_img * bs  # seconds to quit after

IndexError: amax(): Expected reduction dim 1 to have non-zero size."
}

Sign up or log in to comment