FieldwiseDataset#
- class maite.protocols.object_detection.FieldwiseDataset(*args, **kwargs)[source]#
A specialization of Dataset protocol (i.e., a subprotocol) that specifies additional accessor methods for getting input, target, and metadata individually.
Methods
__getitem__(ind: int) -> tuple[Image, ObjectDetectionTarget, DatumMetadata]
Provide map-style access to dataset elements. Returned tuple elements correspond to model input type, model target type, and datum-specific metadata type, respectively.
__len__() -> int
Return the number of data elements in the dataset.
get_input(index: int, /) -> Image:
Get input at the given index.
get_target(index: int, /) -> ObjectDetectionTarget:
Get target at the given index.
get_metadata(index: int, /) -> DatumMetadata:
Get metadata at the given index.
Examples
We generate a dummy set of data and use it with a class that implements the dataset protocol:
>>> import numpy as np >>> from dataclasses import dataclass >>> from maite.protocols import DatumMetadata, DatasetMetadata, object_detection as od >>> from typing import Generator
We will generate 12 images with random data, each having 3 color channels and a height and width of 10 pixels.
>>> N_DATUM = 12 >>> C, H, W = 3, 10, 10 >>> images: list[np.ndarray] = list(np.random.rand(N_DATUM, C, H, W))
Each image may have zero, one, or two detections, which will be chosen at random. Each detection will have a randomly-drawn bounding box (with a minimum and maximum size in each dimension) and a label chosen randomly from 2 classes. The bounding box coordinates and labels are the “annotations” for the image.
>>> MAX_DETECTIONS = 2 >>> MIN_BOX_SIZE, MAX_BOX_SIZE = 2, 4 >>> N_CLASSES = 2 >>> def generate_random_annotations() -> Generator[np.ndarray]: ... for _ in range(N_DATUM): ... n_detections = np.random.choice(MAX_DETECTIONS + 1) ... box_sizes = np.random.randint(MIN_BOX_SIZE, MAX_BOX_SIZE, (n_detections, 2)) ... box_tl_coords = np.random.randint(0, np.array([W, H]) - box_sizes) ... box_br_coords = box_tl_coords + box_sizes ... labels = np.random.choice(N_CLASSES, (n_detections, 1)) ... yield np.hstack([box_tl_coords, box_br_coords, labels]) >>> annotations: list[np.ndarray] = list(generate_random_annotations())
Each image has a randomly-chosen hour of day that it was taken. The datum-specific metadata class is extended to include the hour of day (along with the unique id).
>>> hours_of_day: list[int] = list(np.random.choice(24, N_DATUM)) >>> class MyDatumMetadata(DatumMetadata): ... hour_of_day: int
An object detection target class defines the bounding boxes, labels, and scores for each detection in an image.
>>> @dataclass ... class MyObjectDetectionTarget: ... boxes: np.ndarray ... labels: np.ndarray ... scores: np.ndarray
Now we can construct a compliant dataset with a simple wrapper that fetches individual data points, where a data point is a single image, ground truth detection(s), and metadata.
>>> class ExampleDataset: ... # Set up required dataset-level metadata ... metadata: DatasetMetadata = { ... "id": "Dummy Dataset", ... "index2label": {i: f"class_{i}" for i in range(5)}, ... } ... ... def __init__(self, images: list[np.ndarray], annotations: list[np.ndarray], hours_of_day: list[int]): ... self.images = images ... self.annotations = annotations ... self.hours_of_day = hours_of_day ... ... def __len__(self) -> int: ... return len(self.images) ... ... def __getitem__(self, index: int) -> tuple[np.ndarray, MyObjectDetectionTarget, MyDatumMetadata]: ... return self.get_input(index), self.get_target(index), self.get_metadata(index) ... ... def get_input(self, index: int, /) -> np.ndarray: ... return self.images[index] ... ... def get_target(self, index: int, /) -> MyObjectDetectionTarget: ... annotations = self.annotations[index] ... boxes, labels = np.hsplit(annotations, [-1]) ... scores = np.ones_like(labels) ... return MyObjectDetectionTarget(boxes, labels, scores) ... ... def get_metadata(self, index: int, /) -> MyDatumMetadata: ... hour_of_day = self.hours_of_day[index] ... return { ... "id": str(index), ... "hour_of_day": hour_of_day, ... }
We can instantiate this class and type hint it as an object_detection.FieldwiseDataset. By using type hinting, we permit a static typechecker to verify protocol compliance.
>>> maite_od_dataset: od.FieldwiseDataset = ExampleDataset(images, annotations, hours_of_day) >>> print(len(maite_od_dataset)) 12