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.
- 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.
- 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 aTensor
.
- class darwin.torch.transforms.ToPILImage(mode=None)[source]ο
Bases:
ToPILImage
Converts given
Tensor
to aPILImage
.
- 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 aList
) of tuples into aTuple
of Lists.- Parameters:
batch (Iterable[Tuple]) β Batch to collate.
- Returns:
The
Iterable
of Tupled aggregated into aTuple
.- 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.