Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 86 additions & 12 deletions darwin/torch/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@


class Compose(transforms.Compose):
"""
Composes a sequence of Transformations.
"""

def __call__(self, image: PILImage.Image, target: Optional[TargetType] = None):
if target is None:
return super(Compose, self).__call__(image)
Expand All @@ -22,9 +26,30 @@ def __call__(self, image: PILImage.Image, target: Optional[TargetType] = None):


class RandomHorizontalFlip(transforms.RandomHorizontalFlip):
"""
Allows for horizontal flipping of an image, randomly.
"""

def forward(
self, image: PILImage.Image, target: Optional[TargetType] = None
) -> Union[Union[torch.Tensor, PILImage.Image], Tuple[Union[torch.Tensor, PILImage.Image], TargetType]]:
self, image: torch.Tensor, target: Optional[TargetType] = None
) -> Union[torch.Tensor, Tuple[torch.Tensor, TargetType]]:
"""
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
-------
Union[torch.Tensor, Tuple[torch.Tensor, TargetType]]
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.

"""
if random.random() < self.p:
image = F.hflip(image)
if target is None:
Expand All @@ -45,9 +70,30 @@ def forward(


class RandomVerticalFlip(transforms.RandomVerticalFlip):
"""
Allows for vertical flipping of an image, randomly.
"""

def forward(
self, image: PILImage.Image, target: Optional[TargetType] = None
) -> Union[Union[torch.Tensor, PILImage.Image], Tuple[Union[torch.Tensor, PILImage.Image], TargetType]]:
self, image: torch.Tensor, target: Optional[TargetType] = None
) -> Union[torch.Tensor, Tuple[torch.Tensor, TargetType]]:
"""
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
-------
Union[torch.Tensor, Tuple[torch.Tensor, TargetType]]
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.

"""
if random.random() < self.p:
image = F.vflip(image)
if target is None:
Expand All @@ -67,6 +113,10 @@ def forward(


class ColorJitter(transforms.ColorJitter):
"""
Jitters the colors of the given transformation.
"""

def __call__(
self, image: PILImage.Image, target: Optional[TargetType] = None
) -> Union[PILImage.Image, Tuple[PILImage.Image, TargetType]]:
Expand All @@ -78,26 +128,38 @@ def __call__(


class ToTensor(transforms.ToTensor):
"""
Converts given ``PILImage`` to a ``Tensor``.
"""

def __call__(
self, image: torch.Tensor, target: Optional[TargetType] = None
self, image: PILImage.Image, target: Optional[TargetType] = None
) -> Union[torch.Tensor, Tuple[torch.Tensor, TargetType]]:
image = F.to_tensor(image)
image_tensor: torch.Tensor = F.to_tensor(image)
if target is None:
return image
return image, target
return image_tensor
return image_tensor, target


class ToPILImage(transforms.ToPILImage):
"""
Converts given ``Tensor`` to a ``PILImage``.
"""

def __call__(
self, image: PILImage.Image, target: Optional[TargetType] = None
self, image: torch.Tensor, target: Optional[TargetType] = None
) -> Union[PILImage.Image, Tuple[PILImage.Image, TargetType]]:
image = F.to_pil_image(image)
pil_image: PILImage.Image = F.to_pil_image(image)
if target is None:
return image
return image, target
return pil_image
return pil_image, target


class Normalize(transforms.Normalize):
"""
Normalizes the given ``Tensor``.
"""

def __call__(
self, tensor: torch.Tensor, target: Optional[TargetType] = None
) -> Union[torch.Tensor, Tuple[torch.Tensor, TargetType]]:
Expand All @@ -109,6 +171,10 @@ def __call__(


class ConvertPolygonsToInstanceMasks(object):
"""
Converts given polygon to an ``InstanceMask``.
"""

def __call__(self, image: PILImage.Image, target: TargetType) -> Tuple[PILImage.Image, TargetType]:
w, h = image.size

Expand Down Expand Up @@ -164,6 +230,10 @@ def __call__(self, image: PILImage.Image, target: TargetType) -> Tuple[PILImage.


class ConvertPolygonsToSemanticMask(object):
"""
Converts given polygon to an ``SemanticMask``.
"""

def __call__(self, image: PILImage.Image, target: TargetType) -> Tuple[PILImage.Image, TargetType]:
w, h = image.size
image_id = target["image_id"]
Expand All @@ -189,6 +259,10 @@ def __call__(self, image: PILImage.Image, target: TargetType) -> Tuple[PILImage.


class ConvertPolygonToMask(object):
"""
Converts given polygon to a ``Mask``.
"""

def __call__(self, image: PILImage.Image, annotation: Dict[str, Any]) -> Tuple[PILImage.Image, PILImage.Image]:
w, h = image.size
segmentations = [obj["segmentation"] for obj in annotation]
Expand Down