from typing import Tuple, Union
import torch
import torch.nn.functional as F
from torchvision.transforms import v2 as transforms
[docs]
def data_to_device(
data: Union[tuple, list], device: torch.device
) -> Union[tuple, list]:
"""Move provided data to given device (CPU or GPU)
:param data: Data provided (it can be a single or multiple tensors)
:type data: Union[tuple, list]
:param device: Device to move data to
:type device: torch.device
:return: Data moved to device
:rtype: Union[tuple, list]
:raises TypeError: If data is not a tensor, list, or tuple
"""
if isinstance(data, (tuple, list)):
return type(data)(
d.to(device) if torch.is_tensor(d) else data_to_device(d, device)
for d in data
)
elif torch.is_tensor(data):
return data.to(device)
else:
raise TypeError(
f"data_to_device expected torch.Tensor, list, or tuple, but got {type(data)}"
)
[docs]
def get_data_shape(data: Union[tuple, list]) -> Union[tuple, list]:
"""Get the shape of the provided data
:param data: Data provided (it can be a single or multiple tensors)
:type data: Union[tuple, list]
:return: Data shape
:rtype: Union[tuple, list]
:raises TypeError: If data is not a tensor, list, or tuple
"""
if isinstance(data, (tuple, list)):
return type(data)(
tuple(d.shape) if torch.is_tensor(d) else get_data_shape(d) for d in data
)
elif torch.is_tensor(data):
return tuple(data.shape)
else:
raise TypeError(
f"get_data_shape expected torch.Tensor, list, or tuple, but got {type(data)}"
)
[docs]
def unsqueeze_data(data: Union[tuple, list], dim: int = 0) -> Union[tuple, list]:
"""Unsqueeze provided data along given dimension
:param data: Data provided (it can be a single or multiple tensors)
:type data: Union[tuple, list]
:param dim: Dimension that will be unsqueezed, defaults to 0
:type dim: int, optional
:return: Unsqueezed data
:rtype: Union[tuple, list]
:raises TypeError: If data is not a tensor, list, or tuple
"""
if isinstance(data, (tuple, list)):
return type(data)(
d.unsqueeze(dim) if torch.is_tensor(d) else unsqueeze_data(d, dim)
for d in data
)
elif torch.is_tensor(data):
return data.unsqueeze(dim)
else:
raise TypeError(
f"unsqueeze_data expected torch.Tensor, list, or tuple, but got {type(data)}"
)
[docs]
def get_device_info():
"""Get the best available device(CPU or GPU) and list of available devices.
:return: Tuple of (best_device, available_devices) where best_device is a string and avaible_devices is a list of strings.
:rtype: tuple(str, list[str])
"""
available_devices = []
if torch.cuda.device_count() > 0:
available_devices.append("cuda")
if hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
available_devices.append("mps")
available_devices.append("cpu")
best_device = available_devices[0]
return best_device, available_devices
[docs]
def get_resize_args(resize_cfg: dict) -> dict:
"""
Get the resize arguments for torchvision.transforms.Resize from the configuration.
:param resize_cfg: Resize configuration dictionary
:type resize_cfg: dict
:return: Dictionary with arguments for transforms.Resize
:rtype: dict
"""
resize_args = {"interpolation": transforms.InterpolationMode.BILINEAR}
fixed_h = resize_cfg.get("height")
fixed_w = resize_cfg.get("width")
min_side = resize_cfg.get("min_side")
max_side = resize_cfg.get("max_side")
if fixed_h is not None and fixed_w is not None:
if min_side is not None or max_side is not None:
raise ValueError(
"Resize config cannot satisfy both fixed dimensions (width/height) and "
"min_side/max_side. They are mutually exclusive."
)
resize_args["size"] = (fixed_h, fixed_w)
elif min_side is not None or max_side is not None:
resize_args["size"] = min_side
resize_args["max_size"] = max_side
if fixed_h is not None or fixed_w is not None:
raise ValueError(
"Resize config cannot satisfy both fixed dimensions (width/height) and "
"min_side/max_side. They are mutually exclusive."
)
else:
raise ValueError(
"Resize config must contain either 'height' and 'width' or 'min_side' "
"and/or 'max_side'."
)
return resize_args
[docs]
def pad_to_closest_divisor(
tensor: torch.Tensor, closest_divisor: int
) -> Tuple[torch.Tensor, Tuple[int, int]]:
"""
Pad a tensor with zeros right and bottom so that its height and width are a multiple
of closest_divisor.
:param tensor: Input tensor of shape (..., H, W)
:type tensor: torch.Tensor
:param closest_divisor: The divisor to pad to
:type closest_divisor: int
:return: Padded tensor and original (H, W)
:rtype: Tuple[torch.Tensor, Tuple[int, int]]
"""
h, w = tensor.shape[-2:]
pad_h = (closest_divisor - (h % closest_divisor)) % closest_divisor
pad_w = (closest_divisor - (w % closest_divisor)) % closest_divisor
if pad_h > 0 or pad_w > 0:
tensor = F.pad(tensor, (0, pad_w, 0, pad_h), mode="constant", value=0.0)
return tensor, (h, w)