Skip to content

yolov8_instance_segmentation

YOLOv8InstanceSegmentation

Bases: InstanceSegmentationBaseOnnxRoboflowInferenceModel

YOLOv8 Instance Segmentation ONNX Inference Model.

This class is responsible for performing instance segmentation using the YOLOv8 model with ONNX runtime.

Attributes:

Name Type Description
weights_file str

Path to the ONNX weights file.

Methods:

Name Description
predict

Performs inference on the given image using the ONNX session.

Source code in inference/models/yolov8/yolov8_instance_segmentation.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
46
47
48
49
50
class YOLOv8InstanceSegmentation(InstanceSegmentationBaseOnnxRoboflowInferenceModel):
    """YOLOv8 Instance Segmentation ONNX Inference Model.

    This class is responsible for performing instance segmentation using the YOLOv8 model
    with ONNX runtime.

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

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

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

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

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

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

        Returns:
            Tuple[np.ndarray, np.ndarray]: Tuple containing two NumPy arrays representing the predictions and protos. The predictions include boxes, confidence scores, class confidence scores, and masks.
        """
        predictions = self.onnx_session.run(None, {self.input_name: img_in})
        protos = predictions[1]
        predictions = predictions[0]
        predictions = predictions.transpose(0, 2, 1)
        boxes = predictions[:, :, :4]
        class_confs = predictions[:, :, 4:-32]
        confs = np.expand_dims(np.max(class_confs, axis=2), axis=2)
        masks = predictions[:, :, -32:]
        predictions = np.concatenate([boxes, confs, class_confs, masks], axis=2)
        return predictions, protos

weights_file: str property

Gets the weights file for the YOLOv8 model.

Returns:

Name Type Description
str str

Path to the ONNX weights file.

predict(img_in, **kwargs)

Performs inference 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, ndarray]

Tuple[np.ndarray, np.ndarray]: Tuple containing two NumPy arrays representing the predictions and protos. The predictions include boxes, confidence scores, class confidence scores, and masks.

Source code in inference/models/yolov8/yolov8_instance_segmentation.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def predict(self, img_in: np.ndarray, **kwargs) -> Tuple[np.ndarray, np.ndarray]:
    """Performs inference on the given image using the ONNX session.

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

    Returns:
        Tuple[np.ndarray, np.ndarray]: Tuple containing two NumPy arrays representing the predictions and protos. The predictions include boxes, confidence scores, class confidence scores, and masks.
    """
    predictions = self.onnx_session.run(None, {self.input_name: img_in})
    protos = predictions[1]
    predictions = predictions[0]
    predictions = predictions.transpose(0, 2, 1)
    boxes = predictions[:, :, :4]
    class_confs = predictions[:, :, 4:-32]
    confs = np.expand_dims(np.max(class_confs, axis=2), axis=2)
    masks = predictions[:, :, -32:]
    predictions = np.concatenate([boxes, confs, class_confs, masks], axis=2)
    return predictions, protos