-
Notifications
You must be signed in to change notification settings - Fork 331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inference on ONNX YOLOv8 model #2460
Comments
I tried following two issues reported 1337 and 2298 and implemented the following code from keras_cv import ops
from keras_cv.models.object_detection.yolo_v8.yolo_v8_detector import YOLOV8Detector, decode_regression_to_boxes, dist2bbox, get_anchors
BOX_REGRESSION_CHANNELS = 64
preds = model.outputs[0]
model.outputs[0] = tf.reshape(preds,
[-1, 4, BOX_REGRESSION_CHANNELS // 4])
model.outputs[0] = tf.linalg.matmul(keras.backend.softmax(model.outputs[0], axis=-1),
keras.backend.arange(BOX_REGRESSION_CHANNELS // 4, dtype="float32")[..., None])
model.outputs[0] = tf.squeeze(model.outputs[0], -1)
anchor_points, stride_tensor = get_anchors(image_shape=model.input_shape[1:3])
stride_tensor = keras.backend.expand_dims(stride_tensor, axis=-1)
model.outputs[0] = dist2bbox(model.outputs[0], anchor_points) * stride_tensor
model = tf.keras.Model(inputs=model.inputs, outputs=model.outputs)
model.summary() I saved the model as onnx file but when I perform a sess.run I get this exception. RuntimeException: [ONNXRuntimeError] : 6 : RUNTIME_EXCEPTION : Non-zero status code returned while running Sub node. Name:'model_7/tf.math.subtract_5/Sub' Status Message: /onnxruntime_src/onnxruntime/core/providers/cpu/math/element_wise_ops.h:629 onnxruntime::Broadcaster::Broadcaster(gsl::span, gsl::span) largest <= 1 was false. Can broadcast 0 by 0 or 1. 31950 is invalid. |
@sachinprasadhs do you have any updates on this issue? |
I'm having the same issue. I tried the solution here and I run into the same problem: Error: SessionRun(Msg("Non-zero status code returned while running Sub node. Name:'YOLOv8_1/Sub' Status Message: /home/runn I'm using onnxruntime 1.19.0 to do the inference. The model was produced with tf2onnx v1.16.1 with opset set to 18. If I lower the opset to 13, I instead get the error: Error: SessionRun(Msg("Non-zero status code returned while running Add node. Name:'YOLOv8_1/Add' Status Message: /home/runn |
I think I have isolated the problem. With the solution proposed here, the YOLOV8Detector model is wrapped and the prediction decoding is done after the fact with the helper functions from keras_cv. The steps are as follows:
It results in this shape because in the dist2bbox method this happens:
This results in the shape |
I figured out the problem. The input shape is variable (None, None, None, 3) (batch size, width, height, rgb). When this is passed to the get_anchors, it doesn't know how large to make the resulting tensor resulting in a tensor of shape (0,2) as I described. If I instead fix the image size (in my case 1280 to 1024), every call to get anchors will instead have a shape of (26880, 2), which is the same size as the number of bounding boxes, which means that the add and subtract operations in the wrapped model can broadcast properly, eliminating the errors when inferencing. Hopefully this is helpful to someone else. |
I trained a yolo xs model and exported as onnx file.
I created the inference session by following the code below
When executing the code, the output_names are ['box', 'class']. However, when I check each output, I get box shapes as (1, 8400, 64) and raw_scores shape equal to (1, 8400, 6).
Checking the box I have an array of 64 values, including negative values. How can I extract the bouding boxes from this output?
The text was updated successfully, but these errors were encountered: