darwin.torch package

Submodules

darwin.torch.dataset module

darwin.torch.dataset.get_dataset(dataset_slug: str, dataset_type: str, partition: str | None = None, split: str = 'default', split_type: str = 'random', transform: List | None = None, client: Client | None = None) LocalDataset[source]

Creates and returns a LocalDataset.

Parameters:
  • dataset_slug (str) – Slug of the dataset to retrieve.

  • dataset_type (str) – The type of dataset ["classification", "instance-segmentation", "object-detection", "semantic-segmentation"].

  • partition (str, default: None) – Selects one of the partitions ["train", "val", "test", None].

  • split (str, default: "default") – Selects the split that defines the percentages used.

  • split_type (str, default: "random") – Heuristic used to do the split [random, stratified].

  • transform (Optional[List], default: None) – List of PyTorch transforms.

  • client (Optional[Client], default: None) – Client to use to retrieve the dataset.

class darwin.torch.dataset.ClassificationDataset(transform: Callable | List | None = None, **kwargs)[source]

Bases: LocalDataset

Represents a LocalDataset used for training on classification tasks.

transform

torchvision transform function to run on the dataset.

Type:

Optional[Callable], default: None

is_multi_label

Whether the dataset is multilabel or not.

Type:

bool, default: False

Parameters:

transform (Optional[Union[Callable, List[Callable]]], default: None) – torchvision function or list to set the transform attribute. If it is a list, it will be composed via torchvision.

get_target(index: int) Tensor[source]

Returns the classification target.

Parameters:

index (int) – Index of the image.

Returns:

The target’s tensor.

Return type:

Tensor

check_if_multi_label() None[source]

Loops over all the .json files and checks if we have more than one tag in at least one file, if yes we assume the dataset is for multi label classification.

get_class_idx(index: int) int[source]

Returns the category_id of the image with the given index.

Parameters:

index (int) – Index of the image.

Returns:

category_id of the image.

Return type:

int

measure_weights() ndarray[source]

Computes the class balancing weights (not the frequencies!!) given the train loader. Gets the weights proportional to the inverse of their class frequencies. The vector sums up to 1.

Returns:

Weight for each class in the train set (one for each class) as a 1D array normalized.

Return type:

np.ndarray[float]

class darwin.torch.dataset.InstanceSegmentationDataset(transform: Callable | List | None = None, **kwargs)[source]

Bases: LocalDataset

Represents an instance of a LocalDataset used for training on instance segmentation tasks.

Parameters:

transform (Optional[Union[Callable, List[Callable]]], default: None) – torchvision function or list to set the transform attribute. If it is a list, it will be composed via torchvision.

transform

torchvision transform function to run on the dataset.

Type:

Optional[Callable], default: None

is_multi_label

Whether the dataset is multilabel or not.

Type:

bool, default: False

convert_polygons

Object used to convert polygons to instance masks.

Type:

ConvertPolygonsToInstanceMasks

get_target(index: int) Dict[str, Any][source]

Builds and returns the target dictionary for the item at the given index. The target dictionary will have the following format:

{
    "annotations": [
        {
            "category_id": int,
            "segmentation": List[List[int | float]],
            "bbox": List[float],
            "area": float
        }
    ]
}
Parameters:

index (int) – The actual index of the item in the Dataset.

Returns:

The target.

Return type:

Dict[str, Any]

measure_weights() ndarray[source]

Computes the class balancing weights (not the frequencies!!) given the train loader Get the weights proportional to the inverse of their class frequencies. The vector sums up to 1.

Returns:

class_weights – Weight for each class in the train set (one for each class) as a 1D array normalized.

Return type:

np.ndarray[float]

class darwin.torch.dataset.SemanticSegmentationDataset(transform: List[Callable] | Callable | None = None, **kwargs)[source]

Bases: LocalDataset

Represents an instance of a LocalDataset used for training on semantic segmentation tasks.

Parameters:

transform (Optional[Union[List[Callable], Callable]], default: None) – torchvision function or list to set the transform attribute. If it is a list, it will be composed via torchvision.

transform

torchvision transform function(s) to run on the dataset.

Type:

Optional[Callable], default: None

convert_polygons

Object used to convert polygons to semantic masks.

Type:

ConvertPolygonsToSemanticMask

get_target(index: int) Dict[str, Any][source]

Builds and returns the target dictionary for the item at the given index. The returned dictionary has the following structure:

{
    "annotations": [
        {
            "category_id": int,
            "segmentation": List[List[float | int]]
        }
    ]
}
Parameters:

index (int) – The actual index of the item in the Dataset.

Returns:

The target.

Return type:

Dict[str, Any]

measure_weights() ndarray[source]

Computes the class balancing weights (not the frequencies!!) given the train loader Get the weights proportional to the inverse of their class frequencies. The vector sums up to 1.

Returns:

class_weights – Weight for each class in the train set (one for each class) as a 1D array normalized.

Return type:

np.ndarray[float]

class darwin.torch.dataset.ObjectDetectionDataset(transform: List | None = None, **kwargs)[source]

Bases: LocalDataset

Represents an instance of a LocalDataset used for training on object detection tasks.

Parameters:

transform (Optional[Union[List[Callable], Callable]], default: None) – torchvision function or list to set the transform attribute. If it is a list, it will be composed via torchvision.

transform

torchvision transform function(s) to run on the dataset.

Type:

Optional[Callable], default: None

get_target(index: int) Dict[str, Tensor][source]

Builds and returns the target dictionary for the item at the given index. The returned dictionary has the following structure:

{
    "boxes": Tensor,
    "area": Tensor,
    "labels": Tensor,
    "image_id": Tensor,
    "iscrowd": Tensor
}
Parameters:

index (int) – The actual index of the item in the Dataset.

Returns:

The target.

Return type:

Dict[str, Any]

measure_weights() ndarray[source]

Computes the class balancing weights (not the frequencies!!) given the train loader Get the weights proportional to the inverse of their class frequencies. The vector sums up to 1.

Returns:

class_weights – Weight for each class in the train set (one for each class) as a 1D array normalized.

Return type:

np.ndarray[float]

darwin.torch.transforms module

class darwin.torch.transforms.Compose(transforms)[source]

Bases: Compose

Composes a sequence of Transformations.

class darwin.torch.transforms.RandomHorizontalFlip(p=0.5)[source]

Bases: RandomHorizontalFlip

Allows for horizontal flipping of an image, randomly.

forward(image: Tensor, target: Dict[Literal['boxes', 'labels', 'mask', 'masks', 'image_id', 'area', 'iscrowd'], Tensor] | None = None) Tensor | Tuple[Tensor, Dict[Literal['boxes', 'labels', 'mask', 'masks', 'image_id', 'area', 'iscrowd'], Tensor]][source]

May or may not horizontally flip an image depending on a random factor.

Parameters:
  • image (torch.Tensor) – Image Tensor to flip.

  • target (Optional[TargetType] = None) – The target.

Returns:

Will return a single image Tensor if the flip did not happen, or a tuple of the image tensor and the target type if the flip did happen.

Return type:

Union[torch.Tensor, Tuple[torch.Tensor, TargetType]]

class darwin.torch.transforms.RandomVerticalFlip(p=0.5)[source]

Bases: RandomVerticalFlip

Allows for vertical flipping of an image, randomly.

forward(image: Tensor, target: Dict[Literal['boxes', 'labels', 'mask', 'masks', 'image_id', 'area', 'iscrowd'], Tensor] | None = None) Tensor | Tuple[Tensor, Dict[Literal['boxes', 'labels', 'mask', 'masks', 'image_id', 'area', 'iscrowd'], Tensor]][source]

May or may not vertically flip an image depending on a random factor.

Parameters:
  • image (torch.Tensor) – Image Tensor to flip.

  • target (Optional[TargetType] = None) – The target.

Returns:

Will return a single image Tensor if the flip did not happen, or a tuple of the image tensor and the target type if the flip did happen.

Return type:

Union[torch.Tensor, Tuple[torch.Tensor, TargetType]]

class darwin.torch.transforms.ColorJitter(brightness: float | Tuple[float, float] = 0, contrast: float | Tuple[float, float] = 0, saturation: float | Tuple[float, float] = 0, hue: float | Tuple[float, float] = 0)[source]

Bases: ColorJitter

Jitters the colors of the given transformation.

class darwin.torch.transforms.ToTensor[source]

Bases: ToTensor

Converts given PILImage to a Tensor.

class darwin.torch.transforms.ToPILImage(mode=None)[source]

Bases: ToPILImage

Converts given Tensor to a PILImage.

class darwin.torch.transforms.Normalize(mean, std, inplace=False)[source]

Bases: Normalize

Normalizes the given Tensor.

class darwin.torch.transforms.ConvertPolygonsToInstanceMasks[source]

Bases: object

Converts given polygon to an InstanceMask.

class darwin.torch.transforms.ConvertPolygonsToSemanticMask[source]

Bases: object

Converts given polygon to an SemanticMask.

class darwin.torch.transforms.ConvertPolygonToMask[source]

Bases: object

Converts given polygon to a Mask.

class darwin.torch.transforms.AlbumentationsTransform(transform: Compose)[source]

Bases: object

Wrapper class for Albumentations augmentations.

classmethod from_path(config_path: str) AlbumentationsTransform[source]
classmethod from_dict(alb_dict: dict) AlbumentationsTransform[source]

darwin.torch.utils module

darwin.torch.utils.flatten_masks_by_category(masks: Tensor, cats: List[int]) Tensor[source]

Takes a list of masks and flattens into a single mask output with category id’s overlaid into one tensor. Overlapping sections of masks are replaced with the top most annotation in that position :param masks: lists of masks with shape [x, image_height, image_width] where x is the number of categories :type masks: torch.Tensor :param cats: int list of category id’s with len(x) :type cats: List[int]

Returns:

Flattened mask of category id’s

Return type:

torch.Tensor

darwin.torch.utils.convert_segmentation_to_mask(segmentations: List[List[int]], height: int, width: int) Tensor[source]

Converts a polygon represented as a sequence of coordinates into a mask.

Parameters:
  • segmentations (List[Segment]) – List of float values -> [[x11, y11, x12, y12], ..., [xn1, yn1, xn2, yn2]].

  • height (int) – Image’s height.

  • width (int) – Image’s width.

Returns:

A Tensor representing a segmentation mask.

Return type:

torch.tensor

darwin.torch.utils.polygon_area(x: _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], y: _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) float[source]

Returns the area of the input polygon, represented by two numpy arrays for x and y coordinates.

Parameters:
  • x (np.ndarray) – Numpy array for x coordinates.

  • y (np.ndarray) – Numpy array for y coordinates.

Returns:

The area of the polygon.

Return type:

float

darwin.torch.utils.collate_fn(batch: Iterable[Tuple]) Tuple[source]

Aggregates the given Iterable (usually a List) of tuples into a Tuple of Lists.

Parameters:

batch (Iterable[Tuple]) – Batch to collate.

Returns:

The Iterable of Tupled aggregated into a Tuple.

Return type:

Tuple

darwin.torch.utils.detectron2_register_dataset(dataset: str, release_name: str | None = 'latest', partition: str | None = None, split: str | None = 'default', split_type: str | None = 'stratified', evaluator_type: str | None = None) str[source]

Registers a local Darwin-formatted dataset in Detectron2.

Parameters:
  • dataset (str) – Dataset slug.

  • release_name (Optional[str], default: "latest") – Version of the dataset.

  • partition (Optional[str], default: None) – Selects one of the partitions ["train", "val", "test"].

  • split (Optional[str], default: "default") – Selects the split that defines the percentages used.

  • split_type (Optional[str], default: "stratified") – Heuristic used to do the split ["random", "stratified"].

  • evaluator_type (Optional[str], default: None) – Evaluator to be used in the val and test sets.

Returns:

The name of the registered dataset in the format of {dataset-name}_{partition}.

Return type:

str

darwin.torch.utils.clamp_bbox_to_image_size(annotations, img_width, img_height, format='xywh')[source]

Clamps bounding boxes in annotations to the given image dimensions.

Parameters:
  • annotations – Dictionary containing bounding box coordinates in β€˜boxes’ key.

  • img_width – Width of the image.

  • img_height – Height of the image.

  • format – Format of the bounding boxes, either β€œxywh” or β€œxyxy”.

Returns:

Annotations with clamped bounding boxes.

The function modifies the input annotations dictionary to clamp the bounding box coordinates based on the specified format, ensuring they lie within the image dimensions.

Module contents