sportslabkit.detection_model#

Overview#

Classes#

BaseDetectionModel

Base class for detection models. This class implements basic functionality for handling input and output data, and requires subclasses to implement model loading and forward pass functionality.

DummyDetectionModel

Base class for detection models. This class implements basic functionality for handling input and output data, and requires subclasses to implement model loading and forward pass functionality.

YOLOv8

YOLO model wrapper.

YOLOv8l

YOLO model wrapper.

YOLOv8m

YOLO model wrapper.

YOLOv8n

YOLO model wrapper.

YOLOv8s

YOLO model wrapper.

YOLOv8x

YOLO model wrapper.

Function#

show_available_models()

Print the names of all available BaseDetectionModel models.

load(model_name, **model_config)

Load a model by name.

Classes#

class sportslabkit.detection_model.BaseDetectionModel[source]#

Bases: abc.ABC

Base class for detection models. This class implements basic functionality for handling input and output data, and requires subclasses to implement model loading and forward pass functionality.

Subclasses should override the ‘load’ and ‘forward’ methods. The ‘load’ method should handle loading the model from the specified repository and checkpoint, and ‘forward’ should define the forward pass of the model. Then add ConfigTemplates for your model to define the available configuration options.

The input to the model should be flexible. It accepts numpy.ndarray, torch.Tensor, pathlib Path, string file, PIL Image, or a list of any of these. All inputs will be converted to a list of numpy arrays representing the images.

The output of the model is expected to be a list of Detection objects, where each Detection object represents a detected object in an image. If the model’s output does not meet this expectation, _check_and_fix_outputs method should convert the output into a compatible format.

Example

class CustomDetectionModel(BaseDetectionModel):
def load(self):

# Load your model here pass

def forward(self, x):

# Define the forward pass here pass

model_config#

The configuration for the model.

Type:

Optional[dict]

input_is_batched#

Whether the input is batched or not. This is set by the _check_and_fix_inputs method.

Type:

bool

Overview

Methods#

read_image(img)

-

forward(x)

abc :param x: input tensor

test()

-

Members

read_image(img)#
abstract forward(x)#
Parameters:

x (Tensor) – input tensor

Returns:

output tensor

Return type:

Tensor

test()#
class sportslabkit.detection_model.DummyDetectionModel(detections)[source]#

Bases: sportslabkit.detection_model.base.BaseDetectionModel

Base class for detection models. This class implements basic functionality for handling input and output data, and requires subclasses to implement model loading and forward pass functionality.

Subclasses should override the ‘load’ and ‘forward’ methods. The ‘load’ method should handle loading the model from the specified repository and checkpoint, and ‘forward’ should define the forward pass of the model. Then add ConfigTemplates for your model to define the available configuration options.

The input to the model should be flexible. It accepts numpy.ndarray, torch.Tensor, pathlib Path, string file, PIL Image, or a list of any of these. All inputs will be converted to a list of numpy arrays representing the images.

The output of the model is expected to be a list of Detection objects, where each Detection object represents a detected object in an image. If the model’s output does not meet this expectation, _check_and_fix_outputs method should convert the output into a compatible format.

Example

class CustomDetectionModel(BaseDetectionModel):
def load(self):

# Load your model here pass

def forward(self, x):

# Define the forward pass here pass

model_config#

The configuration for the model.

Type:

Optional[dict]

input_is_batched#

Whether the input is batched or not. This is set by the _check_and_fix_inputs method.

Type:

bool

Overview

Methods#

forward(x)

param x:

input tensor

reset_image_count()

-

from_bbdf(bbdf)

static -

Members

forward(x)#
Parameters:

x (Tensor) – input tensor

Returns:

output tensor

Return type:

Tensor

reset_image_count()#
static from_bbdf(bbdf)#
class sportslabkit.detection_model.YOLOv8(model: str = '', agnostic_nms: bool = False, multi_label: bool = False, classes: list[str] | None = None, max_det: int = 1000, amp: bool = False, imgsz: int = 640, conf: float = 0.25, iou: float = 0.45, device: str = 'cpu', verbose: bool = False, augment: bool = False)[source]#

Bases: sportslabkit.detection_model.base.BaseDetectionModel

YOLO model wrapper. Receives the arguments controlling inference as ‘inference_config’ when initialized.

Overview

Attributes#

hparam_search_space

-

Methods#

forward(x, **kwargs)

param x:

input tensor

Members

hparam_search_space#
forward(x, **kwargs)#
Parameters:

x (Tensor) – input tensor

Returns:

output tensor

Return type:

Tensor

class sportslabkit.detection_model.YOLOv8l(**model_config)[source]#

Bases: YOLOv8

YOLO model wrapper. Receives the arguments controlling inference as ‘inference_config’ when initialized.

class sportslabkit.detection_model.YOLOv8m(**model_config)[source]#

Bases: YOLOv8

YOLO model wrapper. Receives the arguments controlling inference as ‘inference_config’ when initialized.

class sportslabkit.detection_model.YOLOv8n(**model_config)[source]#

Bases: YOLOv8

YOLO model wrapper. Receives the arguments controlling inference as ‘inference_config’ when initialized.

class sportslabkit.detection_model.YOLOv8s(**model_config)[source]#

Bases: YOLOv8

YOLO model wrapper. Receives the arguments controlling inference as ‘inference_config’ when initialized.

class sportslabkit.detection_model.YOLOv8x(model: str = 'yolov8x', agnostic_nms: bool = False, multi_label: bool = False, classes: list[str] | None = None, max_det: int = 1000, amp: bool = False, imgsz: int = 640, conf: float = 0.25, iou: float = 0.45, device: str = 'cpu', verbose: bool = False, augment: bool = False)[source]#

Bases: YOLOv8

YOLO model wrapper. Receives the arguments controlling inference as ‘inference_config’ when initialized.

Functions#

sportslabkit.detection_model.show_available_models()[source]#

Print the names of all available BaseDetectionModel models.

The models are subclasses of BaseDetectionModel. The names are printed as a list to the console.

sportslabkit.detection_model.load(model_name, **model_config)[source]#

Load a model by name.

The function searches subclasses of BaseDetectionModel for a match with the given name. If a match is found, an instance of the model is returned. If no match is found, a warning is logged and the function returns None.

Parameters:
  • model_name (str) – The name of the model to load.

  • model_config (dict, optional) – The model configuration to use when instantiating the model.

Returns:

An instance of the requested model, or None if no match was found.

Return type:

BaseDetectionModel