Skip to content

yolov10_object_detection

YOLOv10ObjectDetection

Bases: ObjectDetectionBaseOnnxRoboflowInferenceModel

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

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

Attributes:

Name Type Description
weights_file str

Path to the ONNX weights file.

Methods:

Name Description
predict

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

Source code in inference/models/yolov10/yolov10_object_detection.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
class YOLOv10ObjectDetection(ObjectDetectionBaseOnnxRoboflowInferenceModel):
    """Roboflow ONNX Object detection model (Implements an object detection specific infer method).

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

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

    Methods:
        predict: Performs object detection on the given image using the ONNX session.
    """

    box_format = "xyxy"

    @property
    def weights_file(self) -> str:
        """Gets the weights file for the YOLOv10 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, including boxes, confidence scores, and class confidence scores.
        """
        predictions = self.onnx_session.run(None, {self.input_name: img_in})[0]

        return (predictions,)

    def postprocess(
        self,
        predictions: Tuple[np.ndarray, ...],
        preproc_return_metadata: PreprocessReturnMetadata,
        confidence: float = DEFAULT_CONFIDENCE,
        max_detections: int = DEFAUlT_MAX_DETECTIONS,
        **kwargs,
    ) -> List[ObjectDetectionInferenceResponse]:
        """Postprocesses the object detection predictions.

        Args:
            predictions (np.ndarray): Raw predictions from the model.
            img_dims (List[Tuple[int, int]]): Dimensions of the images.
            confidence (float): Confidence threshold for filtering detections. Default is 0.5.
            max_detections (int): Maximum number of final detections. Default is 300.

        Returns:
            List[ObjectDetectionInferenceResponse]: The post-processed predictions.
        """
        predictions = predictions[0]
        predictions = np.append(predictions, predictions[..., 5:], axis=-1)
        predictions[..., 5] = predictions[..., 4]

        mask = predictions[..., 4] > confidence
        predictions = [
            p[mask[idx]][:max_detections] for idx, p in enumerate(predictions)
        ]

        infer_shape = (self.img_size_h, self.img_size_w)
        img_dims = preproc_return_metadata["img_dims"]
        predictions = post_process_bboxes(
            predictions,
            infer_shape,
            img_dims,
            self.preproc,
            resize_method=self.resize_method,
            disable_preproc_static_crop=preproc_return_metadata[
                "disable_preproc_static_crop"
            ],
        )
        return self.make_response(predictions, img_dims, **kwargs)

    def validate_model_classes(self) -> None:
        pass

weights_file: str property

Gets the weights file for the YOLOv10 model.

Returns:

Name Type Description
str str

Path to the ONNX weights file.

postprocess(predictions, preproc_return_metadata, confidence=DEFAULT_CONFIDENCE, max_detections=DEFAUlT_MAX_DETECTIONS, **kwargs)

Postprocesses the object detection predictions.

Parameters:

Name Type Description Default
predictions ndarray

Raw predictions from the model.

required
img_dims List[Tuple[int, int]]

Dimensions of the images.

required
confidence float

Confidence threshold for filtering detections. Default is 0.5.

DEFAULT_CONFIDENCE
max_detections int

Maximum number of final detections. Default is 300.

DEFAUlT_MAX_DETECTIONS

Returns:

Type Description
List[ObjectDetectionInferenceResponse]

List[ObjectDetectionInferenceResponse]: The post-processed predictions.

Source code in inference/models/yolov10/yolov10_object_detection.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def postprocess(
    self,
    predictions: Tuple[np.ndarray, ...],
    preproc_return_metadata: PreprocessReturnMetadata,
    confidence: float = DEFAULT_CONFIDENCE,
    max_detections: int = DEFAUlT_MAX_DETECTIONS,
    **kwargs,
) -> List[ObjectDetectionInferenceResponse]:
    """Postprocesses the object detection predictions.

    Args:
        predictions (np.ndarray): Raw predictions from the model.
        img_dims (List[Tuple[int, int]]): Dimensions of the images.
        confidence (float): Confidence threshold for filtering detections. Default is 0.5.
        max_detections (int): Maximum number of final detections. Default is 300.

    Returns:
        List[ObjectDetectionInferenceResponse]: The post-processed predictions.
    """
    predictions = predictions[0]
    predictions = np.append(predictions, predictions[..., 5:], axis=-1)
    predictions[..., 5] = predictions[..., 4]

    mask = predictions[..., 4] > confidence
    predictions = [
        p[mask[idx]][:max_detections] for idx, p in enumerate(predictions)
    ]

    infer_shape = (self.img_size_h, self.img_size_w)
    img_dims = preproc_return_metadata["img_dims"]
    predictions = post_process_bboxes(
        predictions,
        infer_shape,
        img_dims,
        self.preproc,
        resize_method=self.resize_method,
        disable_preproc_static_crop=preproc_return_metadata[
            "disable_preproc_static_crop"
        ],
    )
    return self.make_response(predictions, img_dims, **kwargs)

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, including boxes, confidence scores, and class confidence scores.

Source code in inference/models/yolov10/yolov10_object_detection.py
38
39
40
41
42
43
44
45
46
47
48
49
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, including boxes, confidence scores, and class confidence scores.
    """
    predictions = self.onnx_session.run(None, {self.input_name: img_in})[0]

    return (predictions,)