Skip to content

yolov9_object_detection

YOLOv9ObjectDetection

Bases: ObjectDetectionBaseOnnxRoboflowInferenceModel

Roboflow ONNX Object detection model (Implements an object detection specific infer method).

This class is responsible for performing object detection using the YOLOv9 model with ONNX runtime.

Attributes:

Name Type Description
weights_file str

Path to the ONNX weights file.

Source code in inference/models/yolov9/yolov9_object_detection.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class YOLOv9ObjectDetection(ObjectDetectionBaseOnnxRoboflowInferenceModel):
    """Roboflow ONNX Object detection model (Implements an object detection specific infer method).

    This class is responsible for performing object detection using the YOLOv9 model
    with ONNX runtime.

    Attributes:
        weights_file (str): Path to the ONNX weights file.
    """

    @property
    def weights_file(self) -> str:
        """Gets the weights file for the YOLOv9 model.

        Returns:
            str: Path to the ONNX weights file.
        """
        return "weights.onnx"

    def predict(self, img_in: np.ndarray, **kwargs) -> Tuple[np.ndarray]:
        """Performs object detection on the given image using the ONNX session.

        Args:
            img_in (np.ndarray): Input image as a NumPy array.

        Returns:
            Tuple[np.ndarray]: NumPy array representing the predictions.
        """
        # (b x 8 x 8000)
        predictions = self.onnx_session.run(None, {self.input_name: img_in})[0]
        predictions = predictions.transpose(0, 2, 1)
        boxes = predictions[:, :, :4]
        class_confs = predictions[:, :, 4:]
        confs = np.expand_dims(np.max(class_confs, axis=2), axis=2)
        predictions = np.concatenate([boxes, confs, class_confs], axis=2)
        return (predictions,)

weights_file: str property

Gets the weights file for the YOLOv9 model.

Returns:

Name Type Description
str str

Path to the ONNX weights file.

predict(img_in, **kwargs)

Performs object detection on the given image using the ONNX session.

Parameters:

Name Type Description Default
img_in ndarray

Input image as a NumPy array.

required

Returns:

Type Description
Tuple[ndarray]

Tuple[np.ndarray]: NumPy array representing the predictions.

Source code in inference/models/yolov9/yolov9_object_detection.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def predict(self, img_in: np.ndarray, **kwargs) -> Tuple[np.ndarray]:
    """Performs object detection on the given image using the ONNX session.

    Args:
        img_in (np.ndarray): Input image as a NumPy array.

    Returns:
        Tuple[np.ndarray]: NumPy array representing the predictions.
    """
    # (b x 8 x 8000)
    predictions = self.onnx_session.run(None, {self.input_name: img_in})[0]
    predictions = predictions.transpose(0, 2, 1)
    boxes = predictions[:, :, :4]
    class_confs = predictions[:, :, 4:]
    confs = np.expand_dims(np.max(class_confs, axis=2), axis=2)
    predictions = np.concatenate([boxes, confs, class_confs], axis=2)
    return (predictions,)