image_processor.py 1.36 KB
Newer Older
1
from typing import cast
2
3
4
5
6
7
8


def get_image_processor(
    processor_name: str,
    *args,
    trust_remote_code: bool = False,
    **kwargs,
9
):
10
    """Gets an image processor for the given model name via HuggingFace."""
11
12
13
14
15
    # don't put this import at the top level
    # it will call torch.cuda.device_count()
    from transformers import AutoImageProcessor
    from transformers.image_processing_utils import BaseImageProcessor

16
    try:
17
        processor = AutoImageProcessor.from_pretrained(
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
            processor_name,
            *args,
            trust_remote_code=trust_remote_code,
            **kwargs)
    except ValueError as e:
        # If the error pertains to the processor class not existing or not
        # currently being imported, suggest using the --trust-remote-code flag.
        # Unlike AutoTokenizer, AutoImageProcessor does not separate such errors
        if not trust_remote_code:
            err_msg = (
                "Failed to load the image processor. If the image processor is "
                "a custom processor not yet available in the HuggingFace "
                "transformers library, consider setting "
                "`trust_remote_code=True` in LLM or using the "
                "`--trust-remote-code` flag in the CLI.")
            raise RuntimeError(err_msg) from e
        else:
            raise e

37
    return cast(BaseImageProcessor, processor)