Commit 6a37e135 authored by narugo1992's avatar narugo1992
Browse files

dev(narugo): add this full docs

parent ac4d3226
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -53,6 +53,7 @@ jobs:
        if: ${{ github.event_name == 'push' }}
        env:
          CI: 'true'
          HF_TOKEN: ${{ secrets.HF_TOKEN }}
        with:
          shell: bash
          timeout_minutes: 20
@@ -65,6 +66,7 @@ jobs:
        env:
          ENV_PROD: 'true'
          PLANTUML_HOST: http://localhost:18080
          HF_TOKEN: ${{ secrets.HF_TOKEN }}
        run: |
          plantumlcli -c
          make docs
+56 −0
Original line number Diff line number Diff line
import re
import warnings

import pandas as pd
import transformers
from hfutils.operate import get_hf_client

from imgutils.preprocess.transformers.base import _FN_CREATORS

hf_client = get_hf_client()
df = pd.read_parquet(hf_client.hf_hub_download(
    repo_id='deepghs/hf_models_preprocessors',
    repo_type='dataset',
    filename='repos.parquet'
))
df = df[~df['image_processor_type'].isnull()]
df = df.sort_values(by=['likes'], ascending=[False])

d_repo_count = {
    item['image_processor_type']: item['count']
    for item in df['image_processor_type'].value_counts().reset_index().to_dict('records')
}

d_create_functions = {}
for xfn in _FN_CREATORS:
    xname = xfn.__name__
    matching = re.fullmatch('^create_transforms_from_(?P<name>[\s\S]+)_processor$', xname)
    if not matching:
        warnings.warn(f'Cannot determine transformer type of {xfn!r}.')
        continue
    raw_name = matching.group('name').replace('_', '').lower()
    d_create_functions[raw_name] = xname

suffix = 'ImageProcessor'

rows = []
for name in dir(transformers):
    if name.endswith(suffix) and isinstance(getattr(transformers, name), type) \
            and issubclass(getattr(transformers, name), transformers.BaseImageProcessor) \
            and getattr(transformers, name) is not transformers.BaseImageProcessor:
        cls = getattr(transformers, name)
        pname = name[:-len(suffix)].lower()

        rows.append({
            'Name': name,
            'Supported': '' if pname in d_create_functions else '',
            'Repos': d_repo_count.get(name, 0),
            'Function': f':func:`{d_create_functions[pname]}`' if pname in d_create_functions else 'N/A'
        })

df = pd.DataFrame(rows)
df['Ratio'] = (df['Repos'] / df['Repos'].sum()).map(lambda x: f'{x * 100.0:.2f}%')
df = df.sort_values(by=['Repos', 'Supported', 'Name'], ascending=[False, True, True])
df = df[['Name', 'Supported', 'Repos', 'Ratio', 'Function']]
df = df[df['Repos'] >= 5]
print(df.to_markdown(headers='keys', tablefmt='rst', index=False))
+8 −0
Original line number Diff line number Diff line
"""
Overview:
    Convert transformers image processors to PillowCompose objects.

Supported Processors:

    .. include:: transformers_supported.demo.py.txt
"""
from .base import register_creators_for_transformers, NotProcessorTypeError, create_transforms_from_transformers
from .clip import create_clip_transforms, create_transforms_from_clip_processor
from .convnext import create_convnext_transforms, create_transforms_from_convnext_processor
+12 −2
Original line number Diff line number Diff line
@@ -90,9 +90,19 @@ def create_transforms_from_transformers(processor):
    :raises NotProcessorTypeError: If no suitable creator is found for the processor

    :example:

        >>> processor = AutoProcessor.from_pretrained("openai/clip-vit-base-patch32")
        >>> from transformers import AutoImageProcessor
        >>> from imgutils.preprocess.transformers import create_transforms_from_transformers
        >>>
        >>> processor = AutoImageProcessor.from_pretrained("openai/clip-vit-base-patch32")
        >>> transforms = create_transforms_from_transformers(processor)
        >>> transforms
        PillowCompose(
            PillowConvertRGB(force_background='white')
            PillowResize(size=224, interpolation=bicubic, max_size=None, antialias=True)
            PillowCenterCrop(size=(224, 224))
            PillowToTensor()
            PillowNormalize(mean=[0.48145467 0.4578275  0.40821072], std=[0.26862955 0.2613026  0.2757771 ])
        )
    """
    for _fn in _FN_CREATORS:
        try:
+46 −6
Original line number Diff line number Diff line
"""
This module provides functionality for creating image transformation pipelines compatible with CLIP (Contrastive Language-Image Pre-training) models.
It includes utilities for resizing, cropping, normalizing and converting images to the format expected by CLIP models.

The module integrates with the Hugging Face transformers library and provides compatibility with CLIP processors.
"""

from PIL import Image

from .base import _check_transformers, NotProcessorTypeError, register_creators_for_transformers, OPENAI_CLIP_MEAN, \
@@ -22,6 +29,35 @@ def create_clip_transforms(
        image_std=_DEFAULT,
        do_convert_rgb: bool = True
):
    """
    Creates a composition of image transforms typically used for CLIP models.

    :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 resampling filter to use for resizing.
    :type resample: int
    :param do_center_crop: Whether to center crop the image.
    :type do_center_crop: bool
    :param crop_size: Size for center cropping in {"height": int, "width": int} format.
    :type crop_size: dict
    :param do_rescale: Whether to rescale pixel values.
    :type do_rescale: bool
    :param rescale_factor: Factor to use for rescaling pixels.
    :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 composed transformation pipeline.
    :rtype: PillowCompose
    """
    size = size if size is not _DEFAULT else _DEFAULT_SIZE
    crop_size = crop_size if crop_size is not _DEFAULT else _DEFAULT_CROP_SIZE
    image_mean = image_mean if image_mean is not _DEFAULT else OPENAI_CLIP_MEAN
@@ -29,29 +65,23 @@ def create_clip_transforms(

    transform_list = []

    # Convert to RGB
    if do_convert_rgb:
        transform_list.append(PillowConvertRGB())

    # Resize
    if do_resize:
        if "shortest_edge" in size:
            transform_list.append(PillowResize(size["shortest_edge"], interpolation=resample))
        elif "height" in size and "width" in size:
            transform_list.append(PillowResize((size["height"], size["width"]), interpolation=resample))

    # Center crop
    if do_center_crop:
        transform_list.append(PillowCenterCrop((crop_size["height"], crop_size["width"])))

    # Convert to tensor (implicitly rescales to [0,1])
    transform_list.append(PillowToTensor())

    # Rescale (if different from 1/255)
    if do_rescale and rescale_factor != 1 / 255:
        transform_list.append(PillowRescale(rescale_factor * 255))

    # Normalize
    if do_normalize:
        transform_list.append(PillowNormalize(mean=image_mean, std=image_std))

@@ -60,6 +90,16 @@ def create_clip_transforms(

@register_creators_for_transformers()
def create_transforms_from_clip_processor(processor):
    """
    Creates image transforms from a CLIP processor configuration.

    :param processor: A CLIP processor or image processor instance from transformers library.
    :type processor: Union[CLIPProcessor, CLIPImageProcessor]

    :return: A composed transformation pipeline matching the processor's configuration.
    :rtype: PillowCompose
    :raises NotProcessorTypeError: If the provided processor is not a CLIP processor.
    """
    _check_transformers()
    from transformers import CLIPProcessor, CLIPImageProcessor

Loading