heart_library.estimators.object_detection package

Submodules

heart_library.estimators.object_detection.pytorch module

This module implements a JATIC compatible ART Object Detector.

class heart_library.estimators.object_detection.pytorch.JaticPyTorchObjectDetectionOutput(detection: dict[str, ndarray[Any, dtype[float32]]])[source]

Bases: object

Object Detection Output

property boxes: ndarray[Any, dtype[float32]]

Return detection bounding boxes

Returns:

The boxes in [y1, x1, y2, x2] format, with 0 <= x1 < x2 <= W and 0 <= y1 < y2 <= H.

Return type:

NDArray[np.float32]

property labels: ndarray[Any, dtype[float32]]

Return detection labels

Returns:

The labels for each image.

Return type:

NDArray[np.float32]

property scores: ndarray[Any, dtype[float32]]

Return detection scores

Returns:

The scores or each prediction.

Return type:

NDArray[np.float32]

class heart_library.estimators.object_detection.pytorch.JaticPyTorchObjectDetector(model: Module | str = '', model_type: str = '', metadata_id: str | None = None, **kwargs: Any)[source]

Bases: PyTorchObjectDetector

JATIC compatible extension of ART core PyTorchObjectDetector

Parameters:

PyTorchObjectDetector (PyTorchObjectDetector) – ART core PyTorchObjectDetector.

Examples

We can create a JaticPyTorchObjectDetector and pass in sample data for detection:

>>> from heart_library.estimators.object_detection import JaticPyTorchObjectDetector
>>> import torch
>>> import numpy
>>> from datasets import load_dataset
>>> from torchvision.transforms import transforms

Define the JaticPyTorchObjectDetector, in this case passing in a resnet model:

>>> MEAN = [0.485, 0.456, 0.406]
>>> STD = [0.229, 0.224, 0.225]
>>> preprocessing = (MEAN, STD)
>>> detector = JaticPyTorchObjectDetector(
...     model_type="detr_resnet50_dc5",
...     input_shape=(3, 800, 800),
...     clip_values=(0, 1),
...     attack_losses=(
...         "loss_ce",
...         "loss_bbox",
...         "loss_giou",
...     ),
...     device_type="cpu",
...     optimizer=torch.nn.CrossEntropyLoss(),
...     preprocessing=preprocessing,
... )

Prepare images for detection:

>>> data = load_dataset("guydada/quickstart-coco", split="train[20:25]")
>>> preprocess = transforms.Compose([transforms.Resize(800), transforms.CenterCrop(800), transforms.ToTensor()])
>>> data = data.map(lambda x: {"image": preprocess(x["image"]), "label": None})

Execute object detection and return JaticPyTorchObjectDetectionOutput:

>>> detections = detector(data)
metadata: dict[str, Any]

Module contents

Module providing object detection estimators

class heart_library.estimators.object_detection.JaticPyTorchObjectDetectionOutput(detection: dict[str, ndarray[Any, dtype[float32]]])[source]

Bases: object

Object Detection Output

property boxes: ndarray[Any, dtype[float32]]

Return detection bounding boxes

Returns:

The boxes in [y1, x1, y2, x2] format, with 0 <= x1 < x2 <= W and 0 <= y1 < y2 <= H.

Return type:

NDArray[np.float32]

property labels: ndarray[Any, dtype[float32]]

Return detection labels

Returns:

The labels for each image.

Return type:

NDArray[np.float32]

property scores: ndarray[Any, dtype[float32]]

Return detection scores

Returns:

The scores or each prediction.

Return type:

NDArray[np.float32]

class heart_library.estimators.object_detection.JaticPyTorchObjectDetector(model: Module | str = '', model_type: str = '', metadata_id: str | None = None, **kwargs: Any)[source]

Bases: PyTorchObjectDetector

JATIC compatible extension of ART core PyTorchObjectDetector

Parameters:

PyTorchObjectDetector (PyTorchObjectDetector) – ART core PyTorchObjectDetector.

Examples

We can create a JaticPyTorchObjectDetector and pass in sample data for detection:

>>> from heart_library.estimators.object_detection import JaticPyTorchObjectDetector
>>> import torch
>>> import numpy
>>> from datasets import load_dataset
>>> from torchvision.transforms import transforms

Define the JaticPyTorchObjectDetector, in this case passing in a resnet model:

>>> MEAN = [0.485, 0.456, 0.406]
>>> STD = [0.229, 0.224, 0.225]
>>> preprocessing = (MEAN, STD)
>>> detector = JaticPyTorchObjectDetector(
...     model_type="detr_resnet50_dc5",
...     input_shape=(3, 800, 800),
...     clip_values=(0, 1),
...     attack_losses=(
...         "loss_ce",
...         "loss_bbox",
...         "loss_giou",
...     ),
...     device_type="cpu",
...     optimizer=torch.nn.CrossEntropyLoss(),
...     preprocessing=preprocessing,
... )

Prepare images for detection:

>>> data = load_dataset("guydada/quickstart-coco", split="train[20:25]")
>>> preprocess = transforms.Compose([transforms.Resize(800), transforms.CenterCrop(800), transforms.ToTensor()])
>>> data = data.map(lambda x: {"image": preprocess(x["image"]), "label": None})

Execute object detection and return JaticPyTorchObjectDetectionOutput:

>>> detections = detector(data)
criterion: Module | None
metadata: dict[str, Any]
preprocessing_operations: list[Preprocessor]
weight_dict: dict[str, float] | None