Commit ac4d3226 authored by narugo1992's avatar narugo1992
Browse files

dev(narugo): add the doc framework of transformers

parent e6c8d1f8
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -12,4 +12,5 @@ imgutils.preprocess
    base
    pillow
    torchvision
    transformers
+59 −0
Original line number Diff line number Diff line
imgutils.preprocess.transformers
===========================================

.. currentmodule:: imgutils.preprocess.transformers

.. automodule:: imgutils.preprocess.transformers


register_creators_for_transformers
--------------------------------------------------------------------

.. autofunction:: register_creators_for_transformers



NotProcessorTypeError
--------------------------------------------------------------------

.. autoclass:: NotProcessorTypeError



create_transforms_from_transformers
--------------------------------------------------------------------

.. autofunction:: create_transforms_from_transformers




create_clip_transforms
--------------------------------------------------------------------

.. autofunction:: create_clip_transforms



create_transforms_from_clip_processor
--------------------------------------------------------------------

.. autofunction:: create_transforms_from_clip_processor




create_convnext_transforms
--------------------------------------------------------------------

.. autofunction:: create_convnext_transforms



create_transforms_from_convnext_processor
--------------------------------------------------------------------

.. autofunction:: create_transforms_from_convnext_processor


+57 −0
Original line number Diff line number Diff line
"""
Transformers Integration Module

This module provides functionality for integrating with the transformers library,
particularly for image processing tasks. It includes constants for standard image
normalization values and utilities for creating image transforms from transformers
processors.
"""

try:
    import transformers
except (ImportError, ModuleNotFoundError):
@@ -7,11 +16,17 @@ else:


def _check_transformers():
    """
    Check if transformers library is available.

    :raises EnvironmentError: If transformers is not installed
    """
    if not _HAS_TRANSFORMERS:
        raise EnvironmentError('No torchvision available.\n'
                               'Please install it by `pip install dghs-imgutils[transformers]`.')


# Standard normalization constants
IMAGENET_DEFAULT_MEAN = [0.485, 0.456, 0.406]
IMAGENET_DEFAULT_STD = [0.229, 0.224, 0.225]
IMAGENET_STANDARD_MEAN = [0.5, 0.5, 0.5]
@@ -21,7 +36,14 @@ OPENAI_CLIP_STD = [0.26862954, 0.26130258, 0.27577711]

_DEFAULT = object()


class NotProcessorTypeError(TypeError):
    """
    Exception raised when a processor type is not recognized or supported.

    This error occurs when attempting to create transforms from an unsupported
    or unknown transformers processor type.
    """
    pass


@@ -29,6 +51,24 @@ _FN_CREATORS = []


def register_creators_for_transformers():
    """
    Decorator for registering transform creator functions.

    This decorator adds the decorated function to the list of available
    transform creators that will be tried when creating transforms from
    a transformers processor.

    :return: Decorator function
    :rtype: callable

    :example:

        >>> @register_creators_for_transformers()
        >>> def my_transform_creator(processor):
        ...     # Create and return transforms
        ...     pass
    """

    def _decorator(func):
        _FN_CREATORS.append(func)
        return func
@@ -37,6 +77,23 @@ def register_creators_for_transformers():


def create_transforms_from_transformers(processor):
    """
    Create image transforms from a transformers processor.

    This function attempts to create appropriate image transforms by trying
    each registered creator function until one succeeds.

    :param processor: A transformers processor object
    :type processor: object
    :return: Image transforms appropriate for the given processor
    :rtype: object
    :raises NotProcessorTypeError: If no suitable creator is found for the processor

    :example:

        >>> processor = AutoProcessor.from_pretrained("openai/clip-vit-base-patch32")
        >>> transforms = create_transforms_from_transformers(processor)
    """
    for _fn in _FN_CREATORS:
        try:
            return _fn(processor)