Skip to content

Base Detector

Base detector class.

BaseDetector

Bases: Module

Base detector class. This class provides utility methods for loading the model, generating results, and performing single and batch image detections.

Source code in PytorchWildlife/models/detection/base_detector.py
class BaseDetector(nn.Module):
    """
    Base detector class. This class provides utility methods for
    loading the model, generating results, and performing single and batch image detections.
    """

    # Placeholder class-level attributes to be defined in derived classes
    IMAGE_SIZE = None
    STRIDE = None
    CLASS_NAMES = None
    TRANSFORM = None

    def __init__(self, weights=None, device="cpu", url=None):
        """
        Initialize the base detector.

        Args:
            weights (str, optional): 
                Path to the model weights. Defaults to None.
            device (str, optional): 
                Device for model inference. Defaults to "cpu".
            url (str, optional): 
                URL to fetch the model weights. Defaults to None.
        """
        super(BaseDetector, self).__init__()
        self.device = device


    def _load_model(self, weights=None, device="cpu", url=None):
        """
        Load model weights.

        Args:
            weights (str, optional): 
                Path to the model weights. Defaults to None.
            device (str, optional): 
                Device for model inference. Defaults to "cpu".
            url (str, optional): 
                URL to fetch the model weights. Defaults to None.
        Raises:
            Exception: If weights are not provided.
        """
        pass

    def results_generation(self, preds, img_id: str, id_strip: str = None) -> dict:
        """
        Generate results for detection based on model predictions.

        Args:
            preds (numpy.ndarray): Model predictions.
            img_id (str): Image identifier.
            id_strip (str, optional): Strip specific characters from img_id. Defaults to None.

        Returns:
            dict: Dictionary containing image ID, detections, and labels.
        """
        pass

    def single_image_detection(self, img, img_size=None, img_path=None, conf_thres=0.2, id_strip=None) -> dict:
        """
        Perform detection on a single image.

        Args:
            img (str or ndarray): 
                Image path or ndarray of images.
            img_size (tuple): 
                Original image size.
            img_path (str): 
                Image path or identifier.
            conf_thres (float, optional): 
                Confidence threshold for predictions. Defaults to 0.2.
            id_strip (str, optional): 
                Characters to strip from img_id. Defaults to None.

        Returns:
            dict: Detection results.
        """
        pass

    def batch_image_detection(self, dataloader, conf_thres: float = 0.2, id_strip: str = None) -> list[dict]:
        """
        Perform detection on a batch of images.

        Args:
            dataloader (DataLoader): DataLoader containing image batches.
            conf_thres (float, optional): Confidence threshold for predictions. Defaults to 0.2.
            id_strip (str, optional): Characters to strip from img_id. Defaults to None.

        Returns:
            list[dict]: List of detection results for all images.
        """
        pass

__init__(weights=None, device='cpu', url=None)

Initialize the base detector.

Parameters:

Name Type Description Default
weights str

Path to the model weights. Defaults to None.

None
device str

Device for model inference. Defaults to "cpu".

'cpu'
url str

URL to fetch the model weights. Defaults to None.

None
Source code in PytorchWildlife/models/detection/base_detector.py
def __init__(self, weights=None, device="cpu", url=None):
    """
    Initialize the base detector.

    Args:
        weights (str, optional): 
            Path to the model weights. Defaults to None.
        device (str, optional): 
            Device for model inference. Defaults to "cpu".
        url (str, optional): 
            URL to fetch the model weights. Defaults to None.
    """
    super(BaseDetector, self).__init__()
    self.device = device

batch_image_detection(dataloader, conf_thres=0.2, id_strip=None)

Perform detection on a batch of images.

Parameters:

Name Type Description Default
dataloader DataLoader

DataLoader containing image batches.

required
conf_thres float

Confidence threshold for predictions. Defaults to 0.2.

0.2
id_strip str

Characters to strip from img_id. Defaults to None.

None

Returns:

Type Description
list[dict]

list[dict]: List of detection results for all images.

Source code in PytorchWildlife/models/detection/base_detector.py
def batch_image_detection(self, dataloader, conf_thres: float = 0.2, id_strip: str = None) -> list[dict]:
    """
    Perform detection on a batch of images.

    Args:
        dataloader (DataLoader): DataLoader containing image batches.
        conf_thres (float, optional): Confidence threshold for predictions. Defaults to 0.2.
        id_strip (str, optional): Characters to strip from img_id. Defaults to None.

    Returns:
        list[dict]: List of detection results for all images.
    """
    pass

results_generation(preds, img_id, id_strip=None)

Generate results for detection based on model predictions.

Parameters:

Name Type Description Default
preds ndarray

Model predictions.

required
img_id str

Image identifier.

required
id_strip str

Strip specific characters from img_id. Defaults to None.

None

Returns:

Name Type Description
dict dict

Dictionary containing image ID, detections, and labels.

Source code in PytorchWildlife/models/detection/base_detector.py
def results_generation(self, preds, img_id: str, id_strip: str = None) -> dict:
    """
    Generate results for detection based on model predictions.

    Args:
        preds (numpy.ndarray): Model predictions.
        img_id (str): Image identifier.
        id_strip (str, optional): Strip specific characters from img_id. Defaults to None.

    Returns:
        dict: Dictionary containing image ID, detections, and labels.
    """
    pass

single_image_detection(img, img_size=None, img_path=None, conf_thres=0.2, id_strip=None)

Perform detection on a single image.

Parameters:

Name Type Description Default
img str or ndarray

Image path or ndarray of images.

required
img_size tuple

Original image size.

None
img_path str

Image path or identifier.

None
conf_thres float

Confidence threshold for predictions. Defaults to 0.2.

0.2
id_strip str

Characters to strip from img_id. Defaults to None.

None

Returns:

Name Type Description
dict dict

Detection results.

Source code in PytorchWildlife/models/detection/base_detector.py
def single_image_detection(self, img, img_size=None, img_path=None, conf_thres=0.2, id_strip=None) -> dict:
    """
    Perform detection on a single image.

    Args:
        img (str or ndarray): 
            Image path or ndarray of images.
        img_size (tuple): 
            Original image size.
        img_path (str): 
            Image path or identifier.
        conf_thres (float, optional): 
            Confidence threshold for predictions. Defaults to 0.2.
        id_strip (str, optional): 
            Characters to strip from img_id. Defaults to None.

    Returns:
        dict: Detection results.
    """
    pass