Loading docs/source/api_doc/preprocess/transformers.rst +13 −0 Original line number Diff line number Diff line Loading @@ -85,4 +85,17 @@ create_transforms_from_siglip_processor create_bit_transforms -------------------------------------------------------------------- .. autofunction:: create_bit_transforms create_transforms_from_bit_processor -------------------------------------------------------------------- .. autofunction:: create_transforms_from_bit_processor imgutils/preprocess/transformers/bit.py +52 −0 Original line number Diff line number Diff line """ This module provides image transformation utilities for BiT (Big Transfer) models. It includes functions for creating image preprocessing pipelines that can handle operations like resizing, cropping, normalization and RGB conversion. The module is designed to work with both standalone usage and Hugging Face's transformers library integration. """ from PIL import Image from .base import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, _DEFAULT, register_creators_for_transformers, _check_transformers, \ Loading @@ -22,6 +29,39 @@ def create_bit_transforms( image_std=_DEFAULT, do_convert_rgb: bool = True, ): """ Create an image transformation pipeline for BiT models. This function creates a composition of image transformations including RGB conversion, resizing, center cropping, tensor conversion, rescaling and normalization. :param do_resize: Whether to resize the image. :type do_resize: bool :param size: Target size for resizing. Can be {"shortest_edge": int} or {"height": int, "width": int}. :type size: dict :param resample: PIL interpolation method for resizing. :type resample: int :param do_center_crop: Whether to perform center cropping. :type do_center_crop: bool :param crop_size: Size for center cropping, in format {"height": int, "width": int}. :type crop_size: dict :param do_rescale: Whether to rescale pixel values. :type do_rescale: bool :param rescale_factor: Factor to rescale pixel values. :type rescale_factor: float :param do_normalize: Whether to normalize the image. :type do_normalize: bool :param image_mean: Mean values for normalization. :type image_mean: list or tuple :param image_std: Standard deviation values for normalization. :type image_std: list or tuple :param do_convert_rgb: Whether to convert image to RGB. :type do_convert_rgb: bool :return: A composition of image transformations. :rtype: PillowCompose :raises ValueError: If size configuration is invalid. """ # Set default values size = size if size is not _DEFAULT else _DEFAULT_SIZE crop_size = crop_size if crop_size is not _DEFAULT else _DEFAULT_CROP_SIZE Loading Loading @@ -63,6 +103,18 @@ def create_bit_transforms( @register_creators_for_transformers() def create_transforms_from_bit_processor(processor): """ Create image transformations from a BiT image processor. This function creates a transformation pipeline based on the configuration of a Hugging Face BitImageProcessor. :param processor: The BiT image processor to create transforms from. :type processor: BitImageProcessor :return: A composition of image transformations. :rtype: PillowCompose :raises NotProcessorTypeError: If the processor is not a BitImageProcessor. """ _check_transformers() from transformers import BitImageProcessor Loading Loading
docs/source/api_doc/preprocess/transformers.rst +13 −0 Original line number Diff line number Diff line Loading @@ -85,4 +85,17 @@ create_transforms_from_siglip_processor create_bit_transforms -------------------------------------------------------------------- .. autofunction:: create_bit_transforms create_transforms_from_bit_processor -------------------------------------------------------------------- .. autofunction:: create_transforms_from_bit_processor
imgutils/preprocess/transformers/bit.py +52 −0 Original line number Diff line number Diff line """ This module provides image transformation utilities for BiT (Big Transfer) models. It includes functions for creating image preprocessing pipelines that can handle operations like resizing, cropping, normalization and RGB conversion. The module is designed to work with both standalone usage and Hugging Face's transformers library integration. """ from PIL import Image from .base import OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, _DEFAULT, register_creators_for_transformers, _check_transformers, \ Loading @@ -22,6 +29,39 @@ def create_bit_transforms( image_std=_DEFAULT, do_convert_rgb: bool = True, ): """ Create an image transformation pipeline for BiT models. This function creates a composition of image transformations including RGB conversion, resizing, center cropping, tensor conversion, rescaling and normalization. :param do_resize: Whether to resize the image. :type do_resize: bool :param size: Target size for resizing. Can be {"shortest_edge": int} or {"height": int, "width": int}. :type size: dict :param resample: PIL interpolation method for resizing. :type resample: int :param do_center_crop: Whether to perform center cropping. :type do_center_crop: bool :param crop_size: Size for center cropping, in format {"height": int, "width": int}. :type crop_size: dict :param do_rescale: Whether to rescale pixel values. :type do_rescale: bool :param rescale_factor: Factor to rescale pixel values. :type rescale_factor: float :param do_normalize: Whether to normalize the image. :type do_normalize: bool :param image_mean: Mean values for normalization. :type image_mean: list or tuple :param image_std: Standard deviation values for normalization. :type image_std: list or tuple :param do_convert_rgb: Whether to convert image to RGB. :type do_convert_rgb: bool :return: A composition of image transformations. :rtype: PillowCompose :raises ValueError: If size configuration is invalid. """ # Set default values size = size if size is not _DEFAULT else _DEFAULT_SIZE crop_size = crop_size if crop_size is not _DEFAULT else _DEFAULT_CROP_SIZE Loading Loading @@ -63,6 +103,18 @@ def create_bit_transforms( @register_creators_for_transformers() def create_transforms_from_bit_processor(processor): """ Create image transformations from a BiT image processor. This function creates a transformation pipeline based on the configuration of a Hugging Face BitImageProcessor. :param processor: The BiT image processor to create transforms from. :type processor: BitImageProcessor :return: A composition of image transformations. :rtype: PillowCompose :raises NotProcessorTypeError: If the processor is not a BitImageProcessor. """ _check_transformers() from transformers import BitImageProcessor Loading