Loading docs/source/api_doc/preprocess/pillow.rst +4 −4 Original line number Diff line number Diff line Loading @@ -13,17 +13,17 @@ register_pillow_transform create_pillow_transforms register_pillow_parse ------------------------------------------------------------------------ .. autofunction:: create_pillow_transforms .. autofunction:: register_pillow_parse register_pillow_parse create_pillow_transforms ------------------------------------------------------------------------ .. autofunction:: register_pillow_parse .. autofunction:: create_pillow_transforms Loading docs/source/api_doc/preprocess/torchvision.rst +4 −4 Original line number Diff line number Diff line Loading @@ -13,17 +13,17 @@ register_torchvision_transform create_torchvision_transforms register_torchvision_parse ------------------------------------------------------------------------ .. autofunction:: create_torchvision_transforms .. autofunction:: register_torchvision_parse register_torchvision_parse create_torchvision_transforms ------------------------------------------------------------------------ .. autofunction:: register_torchvision_parse .. autofunction:: create_torchvision_transforms Loading imgutils/preprocess/pillow.py +79 −4 Original line number Diff line number Diff line Loading @@ -285,7 +285,6 @@ class PillowCenterCrop: Initialize the PillowCenterCrop instance. :param size: The target size for cropping. :type size: Union[int, Tuple[int, int], List[int]] """ if isinstance(size, int): # noinspection PyTypeChecker Loading Loading @@ -546,9 +545,7 @@ class PillowNormalize: Normalizes an image by subtracting the mean and dividing by the standard deviation. :param mean: The mean value(s) for normalization. :type mean: np.ndarray :param std: The standard deviation value(s) for normalization. :type std: np.ndarray :param inplace: If True, perform normalization in-place. :type inplace: bool """ Loading Loading @@ -703,6 +700,36 @@ def create_pillow_transforms(tvalue: Union[list, dict]): :return: A transformation or a composition of transformations. :rtype: Union[PillowCompose, Any] :raises TypeError: If the input value is not a list or dictionary. :example: >>> from imgutils.preprocess import create_pillow_transforms >>> >>> create_pillow_transforms({ ... 'type': 'resize', ... 'size': 384, ... 'interpolation': 'bicubic', ... }) PillowResize(size=384, interpolation=bicubic, max_size=None, antialias=True) >>> create_pillow_transforms({ ... 'type': 'resize', ... 'size': (224, 256), ... 'interpolation': 'bilinear', ... }) PillowResize(size=(224, 256), interpolation=bilinear, max_size=None, antialias=True) >>> create_pillow_transforms({'type': 'center_crop', 'size': 224}) PillowCenterCrop(size=(224, 224)) >>> create_pillow_transforms({'type': 'to_tensor'}) PillowToTensor() >>> create_pillow_transforms({'type': 'maybe_to_tensor'}) PillowMaybeToTensor() >>> create_pillow_transforms({'type': 'normalize', 'mean': 0.5, 'std': 0.5}) PillowNormalize(mean=[0.5], std=[0.5]) >>> create_pillow_transforms({ ... 'type': 'normalize', ... 'mean': [0.485, 0.456, 0.406], ... 'std': [0.229, 0.224, 0.225], ... }) PillowNormalize(mean=[0.485 0.456 0.406], std=[0.229 0.224 0.225]) """ if isinstance(tvalue, list): return PillowCompose([create_pillow_transforms(titem) for titem in tvalue]) Loading @@ -719,10 +746,58 @@ def parse_pillow_transforms(value): Parse transformations into a serializable format. :param value: The transformation or composition to parse. :type value: Union[PillowCompose, Any] :type value: Any :return: A serializable representation of the transformation. :rtype: Union[list, dict] :raises TypeError: If the transformation cannot be parsed. :example: >>> from PIL import Image >>> >>> from imgutils.preprocess import parse_pillow_transforms >>> from imgutils.preprocess.pillow import PillowResize, PillowCenterCrop, PillowMaybeToTensor, PillowToTensor, \ ... PillowNormalize >>> >>> parse_pillow_transforms(PillowResize( ... size=384, ... interpolation=Image.BICUBIC, ... )) {'type': 'resize', 'size': 384, 'interpolation': 'bicubic', 'max_size': None, 'antialias': True} >>> parse_pillow_transforms(PillowResize( ... size=(224, 256), ... interpolation=Image.BILINEAR, ... )) {'type': 'resize', 'size': (224, 256), 'interpolation': 'bilinear', 'max_size': None, 'antialias': True} >>> parse_pillow_transforms(PillowCenterCrop(size=224)) {'type': 'center_crop', 'size': [224, 224]} >>> parse_pillow_transforms(PillowToTensor()) {'type': 'to_tensor'} >>> parse_pillow_transforms(PillowMaybeToTensor()) {'type': 'maybe_to_tensor'} >>> parse_pillow_transforms(PillowNormalize(mean=0.5, std=0.5)) {'type': 'normalize', 'mean': [0.5], 'std': [0.5]} >>> parse_pillow_transforms(PillowNormalize( ... mean=[0.485, 0.456, 0.406], ... std=[0.229, 0.224, 0.225], ... )) {'type': 'normalize', 'mean': [0.48500001430511475, 0.4560000002384186, 0.4059999883174896], 'std': [0.2290000021457672, 0.2240000069141388, 0.22499999403953552]} >>> parse_pillow_transforms(PillowCompose([ ... PillowResize( ... size=384, ... interpolation=Image.BICUBIC, ... ), ... PillowCenterCrop(size=224), ... PillowMaybeToTensor(), ... PillowNormalize(mean=0.5, std=0.5), ... ])) [{'antialias': True, 'interpolation': 'bicubic', 'max_size': None, 'size': 384, 'type': 'resize'}, {'size': [224, 224], 'type': 'center_crop'}, {'type': 'maybe_to_tensor'}, {'mean': [0.5], 'std': [0.5], 'type': 'normalize'}] """ if isinstance(value, PillowCompose): return [parse_pillow_transforms(trans) for trans in value.transforms] Loading imgutils/preprocess/torchvision.py +88 −2 Original line number Diff line number Diff line Loading @@ -310,8 +310,8 @@ def _parse_normalize(obj): obj: Normalize return { 'mean': obj.mean.tolist(), 'std': obj.std.tolist(), 'mean': obj.mean.tolist() if hasattr(obj.mean, 'tolist') else obj.mean, 'std': obj.std.tolist() if hasattr(obj.std, 'tolist') else obj.std, } Loading @@ -322,6 +322,45 @@ def create_torchvision_transforms(tvalue: Union[list, dict]): :param tvalue: Transform configuration as list or dict :return: Composed transforms or single transform :raises TypeError: If tvalue has unsupported type :example: >>> from imgutils.preprocess import create_torchvision_transforms >>> >>> create_torchvision_transforms({ ... 'type': 'resize', ... 'size': 384, ... 'interpolation': 'bicubic', ... }) Resize(size=384, interpolation=bicubic, max_size=None, antialias=True) >>> create_torchvision_transforms({ ... 'type': 'resize', ... 'size': (224, 256), ... 'interpolation': 'bilinear', ... }) Resize(size=(224, 256), interpolation=bilinear, max_size=None, antialias=True) >>> create_torchvision_transforms({'type': 'center_crop', 'size': 224}) CenterCrop(size=(224, 224)) >>> create_torchvision_transforms({'type': 'to_tensor'}) ToTensor() >>> create_torchvision_transforms({'type': 'maybe_to_tensor'}) MaybeToTensor() >>> create_torchvision_transforms({'type': 'normalize', 'mean': 0.5, 'std': 0.5}) Normalize(mean=0.5, std=0.5) >>> create_torchvision_transforms({ ... 'type': 'normalize', ... 'mean': [0.485, 0.456, 0.406], ... 'std': [0.229, 0.224, 0.225], ... }) Normalize(mean=tensor([0.4850, 0.4560, 0.4060]), std=tensor([0.2290, 0.2240, 0.2250])) .. note:: Currently the following transforms are supported: - `torchvision.transforms.Resize` - `torchvision.transforms.CenterCrop` - `torchvision.transforms.ToTensor` - `timm.data.MaybeToTensor` - `torchvision.transforms.Normalize` """ _check_torchvision() Loading @@ -343,6 +382,53 @@ def parse_torchvision_transforms(value): :param value: Transform object to parse :return: Transform configuration as list or dict :raises TypeError: If transform type is not supported :example: >>> from timm.data import MaybeToTensor >>> from torchvision.transforms import Resize, InterpolationMode, CenterCrop, ToTensor, Normalize >>> >>> from imgutils.preprocess import parse_torchvision_transforms >>> >>> parse_torchvision_transforms(Resize( ... size=384, ... interpolation=InterpolationMode.BICUBIC, ... )) {'type': 'resize', 'size': 384, 'interpolation': 'bicubic', 'max_size': None, 'antialias': True} >>> parse_torchvision_transforms(Resize( ... size=(224, 256), ... interpolation=InterpolationMode.BILINEAR, ... )) {'type': 'resize', 'size': (224, 256), 'interpolation': 'bilinear', 'max_size': None, 'antialias': True} >>> parse_torchvision_transforms(CenterCrop(size=224)) {'type': 'center_crop', 'size': (224, 224)} >>> parse_torchvision_transforms(ToTensor()) {'type': 'to_tensor'} >>> parse_torchvision_transforms(MaybeToTensor()) {'type': 'maybe_to_tensor'} >>> parse_torchvision_transforms(Normalize(mean=0.5, std=0.5)) {'type': 'normalize', 'mean': 0.5, 'std': 0.5} >>> parse_torchvision_transforms(Normalize( ... mean=[0.485, 0.456, 0.406], ... std=[0.229, 0.224, 0.225], ... )) {'type': 'normalize', 'mean': [0.485, 0.456, 0.406], 'std': [0.229, 0.224, 0.225]} >>> parse_torchvision_transforms(Compose([ ... Resize( ... size=384, ... interpolation=Image.BICUBIC, ... ), ... CenterCrop(size=224), ... MaybeToTensor(), ... Normalize(mean=0.5, std=0.5), ... ])) [{'antialias': True, 'interpolation': 'bicubic', 'max_size': None, 'size': 384, 'type': 'resize'}, {'size': (224, 224), 'type': 'center_crop'}, {'type': 'maybe_to_tensor'}, {'mean': 0.5, 'std': 0.5, 'type': 'normalize'}] """ _check_torchvision() Loading Loading
docs/source/api_doc/preprocess/pillow.rst +4 −4 Original line number Diff line number Diff line Loading @@ -13,17 +13,17 @@ register_pillow_transform create_pillow_transforms register_pillow_parse ------------------------------------------------------------------------ .. autofunction:: create_pillow_transforms .. autofunction:: register_pillow_parse register_pillow_parse create_pillow_transforms ------------------------------------------------------------------------ .. autofunction:: register_pillow_parse .. autofunction:: create_pillow_transforms Loading
docs/source/api_doc/preprocess/torchvision.rst +4 −4 Original line number Diff line number Diff line Loading @@ -13,17 +13,17 @@ register_torchvision_transform create_torchvision_transforms register_torchvision_parse ------------------------------------------------------------------------ .. autofunction:: create_torchvision_transforms .. autofunction:: register_torchvision_parse register_torchvision_parse create_torchvision_transforms ------------------------------------------------------------------------ .. autofunction:: register_torchvision_parse .. autofunction:: create_torchvision_transforms Loading
imgutils/preprocess/pillow.py +79 −4 Original line number Diff line number Diff line Loading @@ -285,7 +285,6 @@ class PillowCenterCrop: Initialize the PillowCenterCrop instance. :param size: The target size for cropping. :type size: Union[int, Tuple[int, int], List[int]] """ if isinstance(size, int): # noinspection PyTypeChecker Loading Loading @@ -546,9 +545,7 @@ class PillowNormalize: Normalizes an image by subtracting the mean and dividing by the standard deviation. :param mean: The mean value(s) for normalization. :type mean: np.ndarray :param std: The standard deviation value(s) for normalization. :type std: np.ndarray :param inplace: If True, perform normalization in-place. :type inplace: bool """ Loading Loading @@ -703,6 +700,36 @@ def create_pillow_transforms(tvalue: Union[list, dict]): :return: A transformation or a composition of transformations. :rtype: Union[PillowCompose, Any] :raises TypeError: If the input value is not a list or dictionary. :example: >>> from imgutils.preprocess import create_pillow_transforms >>> >>> create_pillow_transforms({ ... 'type': 'resize', ... 'size': 384, ... 'interpolation': 'bicubic', ... }) PillowResize(size=384, interpolation=bicubic, max_size=None, antialias=True) >>> create_pillow_transforms({ ... 'type': 'resize', ... 'size': (224, 256), ... 'interpolation': 'bilinear', ... }) PillowResize(size=(224, 256), interpolation=bilinear, max_size=None, antialias=True) >>> create_pillow_transforms({'type': 'center_crop', 'size': 224}) PillowCenterCrop(size=(224, 224)) >>> create_pillow_transforms({'type': 'to_tensor'}) PillowToTensor() >>> create_pillow_transforms({'type': 'maybe_to_tensor'}) PillowMaybeToTensor() >>> create_pillow_transforms({'type': 'normalize', 'mean': 0.5, 'std': 0.5}) PillowNormalize(mean=[0.5], std=[0.5]) >>> create_pillow_transforms({ ... 'type': 'normalize', ... 'mean': [0.485, 0.456, 0.406], ... 'std': [0.229, 0.224, 0.225], ... }) PillowNormalize(mean=[0.485 0.456 0.406], std=[0.229 0.224 0.225]) """ if isinstance(tvalue, list): return PillowCompose([create_pillow_transforms(titem) for titem in tvalue]) Loading @@ -719,10 +746,58 @@ def parse_pillow_transforms(value): Parse transformations into a serializable format. :param value: The transformation or composition to parse. :type value: Union[PillowCompose, Any] :type value: Any :return: A serializable representation of the transformation. :rtype: Union[list, dict] :raises TypeError: If the transformation cannot be parsed. :example: >>> from PIL import Image >>> >>> from imgutils.preprocess import parse_pillow_transforms >>> from imgutils.preprocess.pillow import PillowResize, PillowCenterCrop, PillowMaybeToTensor, PillowToTensor, \ ... PillowNormalize >>> >>> parse_pillow_transforms(PillowResize( ... size=384, ... interpolation=Image.BICUBIC, ... )) {'type': 'resize', 'size': 384, 'interpolation': 'bicubic', 'max_size': None, 'antialias': True} >>> parse_pillow_transforms(PillowResize( ... size=(224, 256), ... interpolation=Image.BILINEAR, ... )) {'type': 'resize', 'size': (224, 256), 'interpolation': 'bilinear', 'max_size': None, 'antialias': True} >>> parse_pillow_transforms(PillowCenterCrop(size=224)) {'type': 'center_crop', 'size': [224, 224]} >>> parse_pillow_transforms(PillowToTensor()) {'type': 'to_tensor'} >>> parse_pillow_transforms(PillowMaybeToTensor()) {'type': 'maybe_to_tensor'} >>> parse_pillow_transforms(PillowNormalize(mean=0.5, std=0.5)) {'type': 'normalize', 'mean': [0.5], 'std': [0.5]} >>> parse_pillow_transforms(PillowNormalize( ... mean=[0.485, 0.456, 0.406], ... std=[0.229, 0.224, 0.225], ... )) {'type': 'normalize', 'mean': [0.48500001430511475, 0.4560000002384186, 0.4059999883174896], 'std': [0.2290000021457672, 0.2240000069141388, 0.22499999403953552]} >>> parse_pillow_transforms(PillowCompose([ ... PillowResize( ... size=384, ... interpolation=Image.BICUBIC, ... ), ... PillowCenterCrop(size=224), ... PillowMaybeToTensor(), ... PillowNormalize(mean=0.5, std=0.5), ... ])) [{'antialias': True, 'interpolation': 'bicubic', 'max_size': None, 'size': 384, 'type': 'resize'}, {'size': [224, 224], 'type': 'center_crop'}, {'type': 'maybe_to_tensor'}, {'mean': [0.5], 'std': [0.5], 'type': 'normalize'}] """ if isinstance(value, PillowCompose): return [parse_pillow_transforms(trans) for trans in value.transforms] Loading
imgutils/preprocess/torchvision.py +88 −2 Original line number Diff line number Diff line Loading @@ -310,8 +310,8 @@ def _parse_normalize(obj): obj: Normalize return { 'mean': obj.mean.tolist(), 'std': obj.std.tolist(), 'mean': obj.mean.tolist() if hasattr(obj.mean, 'tolist') else obj.mean, 'std': obj.std.tolist() if hasattr(obj.std, 'tolist') else obj.std, } Loading @@ -322,6 +322,45 @@ def create_torchvision_transforms(tvalue: Union[list, dict]): :param tvalue: Transform configuration as list or dict :return: Composed transforms or single transform :raises TypeError: If tvalue has unsupported type :example: >>> from imgutils.preprocess import create_torchvision_transforms >>> >>> create_torchvision_transforms({ ... 'type': 'resize', ... 'size': 384, ... 'interpolation': 'bicubic', ... }) Resize(size=384, interpolation=bicubic, max_size=None, antialias=True) >>> create_torchvision_transforms({ ... 'type': 'resize', ... 'size': (224, 256), ... 'interpolation': 'bilinear', ... }) Resize(size=(224, 256), interpolation=bilinear, max_size=None, antialias=True) >>> create_torchvision_transforms({'type': 'center_crop', 'size': 224}) CenterCrop(size=(224, 224)) >>> create_torchvision_transforms({'type': 'to_tensor'}) ToTensor() >>> create_torchvision_transforms({'type': 'maybe_to_tensor'}) MaybeToTensor() >>> create_torchvision_transforms({'type': 'normalize', 'mean': 0.5, 'std': 0.5}) Normalize(mean=0.5, std=0.5) >>> create_torchvision_transforms({ ... 'type': 'normalize', ... 'mean': [0.485, 0.456, 0.406], ... 'std': [0.229, 0.224, 0.225], ... }) Normalize(mean=tensor([0.4850, 0.4560, 0.4060]), std=tensor([0.2290, 0.2240, 0.2250])) .. note:: Currently the following transforms are supported: - `torchvision.transforms.Resize` - `torchvision.transforms.CenterCrop` - `torchvision.transforms.ToTensor` - `timm.data.MaybeToTensor` - `torchvision.transforms.Normalize` """ _check_torchvision() Loading @@ -343,6 +382,53 @@ def parse_torchvision_transforms(value): :param value: Transform object to parse :return: Transform configuration as list or dict :raises TypeError: If transform type is not supported :example: >>> from timm.data import MaybeToTensor >>> from torchvision.transforms import Resize, InterpolationMode, CenterCrop, ToTensor, Normalize >>> >>> from imgutils.preprocess import parse_torchvision_transforms >>> >>> parse_torchvision_transforms(Resize( ... size=384, ... interpolation=InterpolationMode.BICUBIC, ... )) {'type': 'resize', 'size': 384, 'interpolation': 'bicubic', 'max_size': None, 'antialias': True} >>> parse_torchvision_transforms(Resize( ... size=(224, 256), ... interpolation=InterpolationMode.BILINEAR, ... )) {'type': 'resize', 'size': (224, 256), 'interpolation': 'bilinear', 'max_size': None, 'antialias': True} >>> parse_torchvision_transforms(CenterCrop(size=224)) {'type': 'center_crop', 'size': (224, 224)} >>> parse_torchvision_transforms(ToTensor()) {'type': 'to_tensor'} >>> parse_torchvision_transforms(MaybeToTensor()) {'type': 'maybe_to_tensor'} >>> parse_torchvision_transforms(Normalize(mean=0.5, std=0.5)) {'type': 'normalize', 'mean': 0.5, 'std': 0.5} >>> parse_torchvision_transforms(Normalize( ... mean=[0.485, 0.456, 0.406], ... std=[0.229, 0.224, 0.225], ... )) {'type': 'normalize', 'mean': [0.485, 0.456, 0.406], 'std': [0.229, 0.224, 0.225]} >>> parse_torchvision_transforms(Compose([ ... Resize( ... size=384, ... interpolation=Image.BICUBIC, ... ), ... CenterCrop(size=224), ... MaybeToTensor(), ... Normalize(mean=0.5, std=0.5), ... ])) [{'antialias': True, 'interpolation': 'bicubic', 'max_size': None, 'size': 384, 'type': 'resize'}, {'size': (224, 224), 'type': 'center_crop'}, {'type': 'maybe_to_tensor'}, {'mean': 0.5, 'std': 0.5, 'type': 'normalize'}] """ _check_torchvision() Loading