codingutf8 2022 the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache orglicenseslicense2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license todo move batchfeature to be imported by both imageprocessingutils and imageprocessingutils we override the class string here but logic is the same todo amy factor out the common parts of this and the feature extractor class imageprocessingmixinpushtohubmixin autoclass none def initself kwargs pop processorclass as it should be saved as private attribute additional attributes without default values sets processor class as an attribute self processorclass processorclass classmethod def frompretrained cls pretrainedmodelnameorpath unionstr os pathlike cachedir optionalunionstr os pathlike none forcedownload bool false localfilesonly bool false token optionalunionstr bool none revision str main kwargs r instantiate a type of imageprocessingutils imageprocessingmixin from an image processor args pretrainedmodelnameorpath str or os pathlike this can be either a string the model id of a pretrained imageprocessor hosted inside a model repo on huggingface co valid model ids can be located at the rootlevel like bertbaseuncased or namespaced under a user or organization name like dbmdzbertbasegermancased a path to a directory containing a image processor file saved using the imageprocessingutils imageprocessingmixin savepretrained method e g mymodeldirectory a path or url to a saved image processor json file e g mymodeldirectorypreprocessorconfig json cachedir str or os pathlike optional path to a directory in which a downloaded pretrained model image processor should be cached if the standard cache should not be used forcedownload bool optional defaults to false whether or not to force to redownload the image processor files and override the cached versions if they exist resumedownload bool optional defaults to false whether or not to delete incompletely received file attempts to resume the download if such a file exists proxies dictstr str optional a dictionary of proxy servers to use by protocol or endpoint e g http foo bar 3128 http hostname foo bar 4012 the proxies are used on each request token str or bool optional the token to use as http bearer ization for remote files if true or not specified will use the token generated when running huggingfacecli login stored in huggingface revision str optional defaults to main the specific model version to use it can be a branch name a tag name or a commit id since we use a gitbased system for storing models and other artifacts on huggingface co so revision can be any identifier allowed by git tip to test a pull request you made on the hub you can pass revisionrefsprprnumber tip returnunusedkwargs bool optional defaults to false if false then this function returns just the final image processor object if true then this functions returns a tupleimageprocessor unusedkwargs where unusedkwargs is a dictionary consisting of the keyvalue pairs whose keys are not image processor attributes i e the part of kwargs which has not been used to update imageprocessor and is otherwise ignored subfolder str optional defaults to in case the relevant files are located inside a subfolder of the model repo on huggingface co you can specify the folder name here kwargs dictstr any optional the values in kwargs of any keys which are image processor attributes will be used to override the loaded values behavior concerning keyvalue pairs whose keys are not image processor attributes is controlled by the returnunusedkwargs keyword parameter returns a image processor of type imageprocessingutils imageprocessingmixin examples python we can t instantiate directly the base class imageprocessingmixin so let s show the examples on a derived class clipimageprocessor imageprocessor clipimageprocessor frompretrained openaiclipvitbasepatch32 download imageprocessingconfig from huggingface co and cache imageprocessor clipimageprocessor frompretrained testsavedmodel e g image processor or model was saved using savepretrained testsavedmodel imageprocessor clipimageprocessor frompretrained testsavedmodelpreprocessorconfig json imageprocessor clipimageprocessor frompretrained openaiclipvitbasepatch32 donormalizefalse foofalse assert imageprocessor donormalize is false imageprocessor unusedkwargs clipimageprocessor frompretrained openaiclipvitbasepatch32 donormalizefalse foofalse returnunusedkwargstrue assert imageprocessor donormalize is false assert unusedkwargs foo false kwargscachedir cachedir kwargsforcedownload forcedownload kwargslocalfilesonly localfilesonly kwargsrevision revision useauthtoken kwargs popuseauthtoken none if useauthtoken is not none warnings warn the useauthtoken argument is deprecated and will be removed in v5 of transformers please use token instead futurewarning if token is not none raise valueerror token and useauthtoken are both specified please set only the argument token token useauthtoken if token is not none kwargstoken token imageprocessordict kwargs cls getimageprocessordictpretrainedmodelnameorpath kwargs return cls fromdictimageprocessordict kwargs def savepretrainedself savedirectory unionstr os pathlike pushtohub bool false kwargs useauthtoken kwargs popuseauthtoken none if useauthtoken is not none warnings warn the useauthtoken argument is deprecated and will be removed in v5 of transformers please use token instead futurewarning if kwargs gettoken none is not none raise valueerror token and useauthtoken are both specified please set only the argument token kwargstoken useauthtoken if os path isfilesavedirectory raise assertionerrorfprovided path savedirectory should be a directory not a file os makedirssavedirectory existoktrue if pushtohub commitmessage kwargs popcommitmessage none repoid kwargs poprepoid savedirectory splitos path sep1 repoid self createreporepoid kwargs filestimestamps self getfilestimestampssavedirectory if we have a custom config we copy the file defining it in the folder and set the attributes so it can be loaded from the hub if self autoclass is not none customobjectsaveself savedirectory configself if we save using the predefined names we can load using frompretrained outputimageprocessorfile os path joinsavedirectory imageprocessorname self tojsonfileoutputimageprocessorfile logger infofimage processor saved in outputimageprocessorfile if pushtohub self uploadmodifiedfiles savedirectory repoid filestimestamps commitmessagecommitmessage tokenkwargs gettoken return outputimageprocessorfile classmethod def getimageprocessordict cls pretrainedmodelnameorpath unionstr os pathlike kwargs tupledictstr any dictstr any cachedir kwargs popcachedir none forcedownload kwargs popforcedownload false resumedownload kwargs popresumedownload false proxies kwargs popproxies none token kwargs poptoken none useauthtoken kwargs popuseauthtoken none localfilesonly kwargs poplocalfilesonly false revision kwargs poprevision none subfolder kwargs popsubfolder frompipeline kwargs popfrompipeline none fromautoclass kwargs popfromauto false if useauthtoken is not none warnings warn the useauthtoken argument is deprecated and will be removed in v5 of transformers please use token instead futurewarning if token is not none raise valueerror token and useauthtoken are both specified please set only the argument token token useauthtoken useragent filetype image processor fromautoclass fromautoclass if frompipeline is not none useragentusingpipeline frompipeline if isofflinemode and not localfilesonly logger infooffline mode forcing localfilesonlytrue localfilesonly true pretrainedmodelnameorpath strpretrainedmodelnameorpath islocal os path isdirpretrainedmodelnameorpath if os path isdirpretrainedmodelnameorpath imageprocessorfile os path joinpretrainedmodelnameorpath imageprocessorname if os path isfilepretrainedmodelnameorpath resolvedimageprocessorfile pretrainedmodelnameorpath islocal true elif isremoteurlpretrainedmodelnameorpath imageprocessorfile pretrainedmodelnameorpath resolvedimageprocessorfile downloadurlpretrainedmodelnameorpath else imageprocessorfile imageprocessorname try load from local folder or from cache or download from model hub and cache resolvedimageprocessorfile cachedfile pretrainedmodelnameorpath imageprocessorfile cachedircachedir forcedownloadforcedownload proxiesproxies resumedownloadresumedownload localfilesonlylocalfilesonly tokentoken useragentuseragent revisionrevision subfoldersubfolder except environmenterror raise any environment error raise by cachedfile it will have a helpful error message adapted to the original exception raise except exception for any other exception we throw a generic error raise environmenterror fcan t load image processor for pretrainedmodelnameorpath if you were trying to load it from https huggingface comodels make sure you don t have a local directory with the f same name otherwise make sure pretrainedmodelnameorpath is the correct path to a f directory containing a imageprocessorname file try load imageprocessor dict with openresolvedimageprocessorfile r encodingutf8 as reader text reader read imageprocessordict json loadstext except json jsondecodeerror raise environmenterror fit looks like the config file at resolvedimageprocessorfile is not a valid json file if islocal logger infofloading configuration file resolvedimageprocessorfile else logger info floading configuration file imageprocessorfile from cache at resolvedimageprocessorfile if automap in imageprocessordict and not islocal imageprocessordictautomap addmodelinfotoautomap imageprocessordictautomap pretrainedmodelnameorpath return imageprocessordict kwargs classmethod def fromdictcls imageprocessordict dictstr any kwargs imageprocessordict imageprocessordict copy returnunusedkwargs kwargs popreturnunusedkwargs false the size parameter is a dict and was previously an int or tuple in feature extractors we set size here directly to the imageprocessordict so that it is converted to the appropriate dict within the image processor and isn t overwritten if size is passed in as a kwarg if size in kwargs and size in imageprocessordict imageprocessordictsize kwargs popsize if cropsize in kwargs and cropsize in imageprocessordict imageprocessordictcropsize kwargs popcropsize imageprocessor clsimageprocessordict update imageprocessor with kwargs if needed toremove for key value in kwargs items if hasattrimageprocessor key setattrimageprocessor key value toremove appendkey for key in toremove kwargs popkey none logger infofimage processor imageprocessor if returnunusedkwargs return imageprocessor kwargs else return imageprocessor def todictself dictstr any output copy deepcopyself dict outputimageprocessortype self class name return output classmethod def fromjsonfilecls jsonfile unionstr os pathlike with openjsonfile r encodingutf8 as reader text reader read imageprocessordict json loadstext return clsimageprocessordict def tojsonstringself str dictionary self todict for key value in dictionary items if isinstancevalue np ndarray dictionarykey value tolist make sure private name processorclass is correctly saved as processorclass processorclass dictionary popprocessorclass none if processorclass is not none dictionaryprocessorclass processorclass return json dumpsdictionary indent2 sortkeystrue n def tojsonfileself jsonfilepath unionstr os pathlike with openjsonfilepath w encodingutf8 as writer writer writeself tojsonstring def reprself return fself class name self tojsonstring classmethod def registerforautoclasscls autoclassautoimageprocessor if not isinstanceautoclass str autoclass autoclass name import transformers models auto as automodule if not hasattrautomodule autoclass raise valueerrorfautoclass is not a valid auto class cls autoclass autoclass def fetchimagesself imageurlorurls unionstr liststr headers useragent mozilla5 0 macintosh intel mac os x 10157 applewebkit537 36 khtml like gecko chrome114 0 0 0 safari537 36 if isinstanceimageurlorurls list return self fetchimagesx for x in imageurlorurls elif isinstanceimageurlorurls str response requests getimageurlorurls streamtrue headersheaders response raiseforstatus return image openbytesioresponse content else raise valueerrorfonly a single or a list of entries is supported but got typetypeimageurlorurls class baseimageprocessorimageprocessingmixin def initself kwargs super initkwargs def callself images kwargs batchfeature rescale an image by a scale factor image image scale args image np ndarray image to rescale scale float the scaling factor to rescale pixel values by dataformat str or channeldimension optional the channel dimension format for the output image if unset the channel dimension format of the input image is used can be one of channelsfirst or channeldimension first image in numchannels height width format channelslast or channeldimension last image in height width numchannels format inputdataformat channeldimension or str optional the channel dimension format for the input image if unset the channel dimension format is inferred from the input image can be one of channelsfirst or channeldimension first image in numchannels height width format channelslast or channeldimension last image in height width numchannels format returns np ndarray the rescaled image normalize an image image image imagemean imagestd args image np ndarray image to normalize mean float or iterablefloat image mean to use for normalization std float or iterablefloat image standard deviation to use for normalization dataformat str or channeldimension optional the channel dimension format for the output image if unset the channel dimension format of the input image is used can be one of channelsfirst or channeldimension first image in numchannels height width format channelslast or channeldimension last image in height width numchannels format inputdataformat channeldimension or str optional the channel dimension format for the input image if unset the channel dimension format is inferred from the input image can be one of channelsfirst or channeldimension first image in numchannels height width format channelslast or channeldimension last image in height width numchannels format returns np ndarray the normalized image center crop an image to sizeheight sizewidth if the input size is smaller than cropsize along any edge the image is padded with 0 s and then center cropped args image np ndarray image to center crop size dictstr int size of the output image dataformat str or channeldimension optional the channel dimension format for the output image if unset the channel dimension format of the input image is used can be one of channelsfirst or channeldimension first image in numchannels height width format channelslast or channeldimension last image in height width numchannels format inputdataformat channeldimension or str optional the channel dimension format for the input image if unset the channel dimension format is inferred from the input image can be one of channelsfirst or channeldimension first image in numchannels height width format channelslast or channeldimension last image in height width numchannels format by default if size is an int we assume it represents a tuple of size size in other configs if size is an int and defaulttosquare is false size represents the length of the shortest edge after resizing otherwise if size is a tuple it s either height width or width height converts the old size parameter in the config into the new dict expected in the config this is to ensure backwards compatibility with the old image processor configs and removes ambiguity over whether the tuple is in height width or width height format if size is tuple it is converted to height size0 width size1 or height size1 width size0 if heightwidthorder is false if size is an int and defaulttosquare is true it is converted to height size width size if size is an int and defaulttosquare is false it is converted to shortestedge size if maxsize is set it is added to the dict as longestedge maxsize args size unionint iterableint dictstr int optional the size parameter to be cast into a size dictionary maxsize optionalint optional the maxsize parameter to be cast into a size dictionary heightwidthorder bool optional defaults to true if size is a tuple whether it s in height width or width height order defaulttosquare bool optional defaults to true if size is an int whether to default to a square image or not coding utf 8 2022 the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache org licenses license 2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license todo move batchfeature to be imported by both image_processing_utils and image_processing_utils we override the class string here but logic is the same holds the output of the image processor specific __call__ methods this class is derived from a python dictionary and can be used as a dictionary args data dict dictionary of lists arrays tensors returned by the __call__ method pixel_values etc tensor_type union none str tensortype optional you can give a tensor_type here to convert the lists of integers in pytorch tensorflow numpy tensors at initialization todo amy factor out the common parts of this and the feature extractor this is an image processor mixin used to provide saving loading functionality for sequential and image feature extractors set elements of kwargs as attributes pop processor_class as it should be saved as private attribute additional attributes without default values sets processor class as an attribute instantiate a type of image_processing_utils imageprocessingmixin from an image processor args pretrained_model_name_or_path str or os pathlike this can be either a string the model id of a pretrained image_processor hosted inside a model repo on huggingface co valid model ids can be located at the root level like bert base uncased or namespaced under a user or organization name like dbmdz bert base german cased a path to a directory containing a image processor file saved using the image_processing_utils imageprocessingmixin save_pretrained method e g my_model_directory a path or url to a saved image processor json file e g my_model_directory preprocessor_config json cache_dir str or os pathlike optional path to a directory in which a downloaded pretrained model image processor should be cached if the standard cache should not be used force_download bool optional defaults to false whether or not to force to re download the image processor files and override the cached versions if they exist resume_download bool optional defaults to false whether or not to delete incompletely received file attempts to resume the download if such a file exists proxies dict str str optional a dictionary of proxy servers to use by protocol or endpoint e g http foo bar 3128 http hostname foo bar 4012 the proxies are used on each request token str or bool optional the token to use as http bearer ization for remote files if true or not specified will use the token generated when running huggingface cli login stored in huggingface revision str optional defaults to main the specific model version to use it can be a branch name a tag name or a commit id since we use a git based system for storing models and other artifacts on huggingface co so revision can be any identifier allowed by git tip to test a pull request you made on the hub you can pass revision refs pr pr_number tip return_unused_kwargs bool optional defaults to false if false then this function returns just the final image processor object if true then this functions returns a tuple image_processor unused_kwargs where unused_kwargs is a dictionary consisting of the key value pairs whose keys are not image processor attributes i e the part of kwargs which has not been used to update image_processor and is otherwise ignored subfolder str optional defaults to in case the relevant files are located inside a subfolder of the model repo on huggingface co you can specify the folder name here kwargs dict str any optional the values in kwargs of any keys which are image processor attributes will be used to override the loaded values behavior concerning key value pairs whose keys are not image processor attributes is controlled by the return_unused_kwargs keyword parameter returns a image processor of type image_processing_utils imageprocessingmixin examples python we can t instantiate directly the base class imageprocessingmixin so let s show the examples on a derived class clipimageprocessor image_processor clipimageprocessor from_pretrained openai clip vit base patch32 download image_processing_config from huggingface co and cache image_processor clipimageprocessor from_pretrained test saved_model e g image processor or model was saved using save_pretrained test saved_model image_processor clipimageprocessor from_pretrained test saved_model preprocessor_config json image_processor clipimageprocessor from_pretrained openai clip vit base patch32 do_normalize false foo false assert image_processor do_normalize is false image_processor unused_kwargs clipimageprocessor from_pretrained openai clip vit base patch32 do_normalize false foo false return_unused_kwargs true assert image_processor do_normalize is false assert unused_kwargs foo false save an image processor object to the directory save_directory so that it can be re loaded using the image_processing_utils imageprocessingmixin from_pretrained class method args save_directory str or os pathlike directory where the image processor json file will be saved will be created if it does not exist push_to_hub bool optional defaults to false whether or not to push your model to the hugging face model hub after saving it you can specify the repository you want to push to with repo_id will default to the name of save_directory in your namespace kwargs dict str any optional additional key word arguments passed along to the utils pushtohubmixin push_to_hub method if we have a custom config we copy the file defining it in the folder and set the attributes so it can be loaded from the hub if we save using the predefined names we can load using from_pretrained from a pretrained_model_name_or_path resolve to a dictionary of parameters to be used for instantiating a image processor of type image_processor_utils imageprocessingmixin using from_dict parameters pretrained_model_name_or_path str or os pathlike the identifier of the pre trained checkpoint from which we want the dictionary of parameters subfolder str optional defaults to in case the relevant files are located inside a subfolder of the model repo on huggingface co you can specify the folder name here returns tuple dict dict the dictionary ies that will be used to instantiate the image processor object load from local folder or from cache or download from model hub and cache raise any environment error raise by cached_file it will have a helpful error message adapted to the original exception for any other exception we throw a generic error load image_processor dict instantiates a type of image_processing_utils imageprocessingmixin from a python dictionary of parameters args image_processor_dict dict str any dictionary that will be used to instantiate the image processor object such a dictionary can be retrieved from a pretrained checkpoint by leveraging the image_processing_utils imageprocessingmixin to_dict method kwargs dict str any additional parameters from which to initialize the image processor object returns image_processing_utils imageprocessingmixin the image processor object instantiated from those parameters the size parameter is a dict and was previously an int or tuple in feature extractors we set size here directly to the image_processor_dict so that it is converted to the appropriate dict within the image processor and isn t overwritten if size is passed in as a kwarg update image_processor with kwargs if needed serializes this instance to a python dictionary returns dict str any dictionary of all the attributes that make up this image processor instance instantiates a image processor of type image_processing_utils imageprocessingmixin from the path to a json file of parameters args json_file str or os pathlike path to the json file containing the parameters returns a image processor of type image_processing_utils imageprocessingmixin the image_processor object instantiated from that json file serializes this instance to a json string returns str string containing all the attributes that make up this feature_extractor instance in json format make sure private name _processor_class is correctly saved as processor_class save this instance to a json file args json_file_path str or os pathlike path to the json file in which this image_processor instance s parameters will be saved register this class with a given auto class this should only be used for custom image processors as the ones in the library are already mapped with autoimageprocessor tip warning true this api is experimental and may have some slight breaking changes in the next releases tip args auto_class str or type optional defaults to autoimageprocessor the auto class to register this new image processor with convert a single or a list of urls into the corresponding pil image objects if a single url is passed the return value will be a single object if a list is passed a list of objects is returned preprocess an image or a batch of images rescale an image by a scale factor image image scale args image np ndarray image to rescale scale float the scaling factor to rescale pixel values by data_format str or channeldimension optional the channel dimension format for the output image if unset the channel dimension format of the input image is used can be one of channels_first or channeldimension first image in num_channels height width format channels_last or channeldimension last image in height width num_channels format input_data_format channeldimension or str optional the channel dimension format for the input image if unset the channel dimension format is inferred from the input image can be one of channels_first or channeldimension first image in num_channels height width format channels_last or channeldimension last image in height width num_channels format returns np ndarray the rescaled image normalize an image image image image_mean image_std args image np ndarray image to normalize mean float or iterable float image mean to use for normalization std float or iterable float image standard deviation to use for normalization data_format str or channeldimension optional the channel dimension format for the output image if unset the channel dimension format of the input image is used can be one of channels_first or channeldimension first image in num_channels height width format channels_last or channeldimension last image in height width num_channels format input_data_format channeldimension or str optional the channel dimension format for the input image if unset the channel dimension format is inferred from the input image can be one of channels_first or channeldimension first image in num_channels height width format channels_last or channeldimension last image in height width num_channels format returns np ndarray the normalized image center crop an image to size height size width if the input size is smaller than crop_size along any edge the image is padded with 0 s and then center cropped args image np ndarray image to center crop size dict str int size of the output image data_format str or channeldimension optional the channel dimension format for the output image if unset the channel dimension format of the input image is used can be one of channels_first or channeldimension first image in num_channels height width format channels_last or channeldimension last image in height width num_channels format input_data_format channeldimension or str optional the channel dimension format for the input image if unset the channel dimension format is inferred from the input image can be one of channels_first or channeldimension first image in num_channels height width format channels_last or channeldimension last image in height width num_channels format by default if size is an int we assume it represents a tuple of size size in other configs if size is an int and default_to_square is false size represents the length of the shortest edge after resizing otherwise if size is a tuple it s either height width or width height converts the old size parameter in the config into the new dict expected in the config this is to ensure backwards compatibility with the old image processor configs and removes ambiguity over whether the tuple is in height width or width height format if size is tuple it is converted to height size 0 width size 1 or height size 1 width size 0 if height_width_order is false if size is an int and default_to_square is true it is converted to height size width size if size is an int and default_to_square is false it is converted to shortest_edge size if max_size is set it is added to the dict as longest_edge max_size args size union int iterable int dict str int optional the size parameter to be cast into a size dictionary max_size optional int optional the max_size parameter to be cast into a size dictionary height_width_order bool optional defaults to true if size is a tuple whether it s in height width or width height order default_to_square bool optional defaults to true if size is an int whether to default to a square image or not
import copy
import json
import os
import warnings
from io import BytesIO
from typing import Any, Dict, Iterable, List, Optional, Tuple, Union

import numpy as np
import requests

from .dynamic_module_utils import custom_object_save
from .feature_extraction_utils import BatchFeature as BaseBatchFeature
from .image_transforms import center_crop, normalize, rescale
from .image_utils import ChannelDimension
from .utils import (
    IMAGE_PROCESSOR_NAME,
    PushToHubMixin,
    add_model_info_to_auto_map,
    cached_file,
    copy_func,
    download_url,
    is_offline_mode,
    is_remote_url,
    is_vision_available,
    logging,
)


if is_vision_available():
    from PIL import Image

logger = logging.get_logger(__name__)




class BatchFeature(BaseBatchFeature):
    r



class ImageProcessingMixin(PushToHubMixin):
    

    _auto_class = None

    def __init__(self, **kwargs):
        
        
        self._processor_class = kwargs.pop("processor_class", None)
        
        for key, value in kwargs.items():
            try:
                setattr(self, key, value)
            except AttributeError as err:
                logger.error(f"Can't set {key} with value {value} for {self}")
                raise err

    def _set_processor_class(self, processor_class: str):
        
        self._processor_class = processor_class

    @classmethod
    def from_pretrained(
        cls,
        pretrained_model_name_or_path: Union[str, os.PathLike],
        cache_dir: Optional[Union[str, os.PathLike]] = None,
        force_download: bool = False,
        local_files_only: bool = False,
        token: Optional[Union[str, bool]] = None,
        revision: str = "main",
        **kwargs,
    ):
        r
        kwargs["cache_dir"] = cache_dir
        kwargs["force_download"] = force_download
        kwargs["local_files_only"] = local_files_only
        kwargs["revision"] = revision

        use_auth_token = kwargs.pop("use_auth_token", None)
        if use_auth_token is not None:
            warnings.warn(
                "The `use_auth_token` argument is deprecated and will be removed in v5 of Transformers. Please use `token` instead.",
                FutureWarning,
            )
            if token is not None:
                raise ValueError(
                    "`token` and `use_auth_token` are both specified. Please set only the argument `token`."
                )
            token = use_auth_token

        if token is not None:
            kwargs["token"] = token

        image_processor_dict, kwargs = cls.get_image_processor_dict(pretrained_model_name_or_path, **kwargs)

        return cls.from_dict(image_processor_dict, **kwargs)

    def save_pretrained(self, save_directory: Union[str, os.PathLike], push_to_hub: bool = False, **kwargs):
        
        use_auth_token = kwargs.pop("use_auth_token", None)

        if use_auth_token is not None:
            warnings.warn(
                "The `use_auth_token` argument is deprecated and will be removed in v5 of Transformers. Please use `token` instead.",
                FutureWarning,
            )
            if kwargs.get("token", None) is not None:
                raise ValueError(
                    "`token` and `use_auth_token` are both specified. Please set only the argument `token`."
                )
            kwargs["token"] = use_auth_token

        if os.path.isfile(save_directory):
            raise AssertionError(f"Provided path ({save_directory}) should be a directory, not a file")

        os.makedirs(save_directory, exist_ok=True)

        if push_to_hub:
            commit_message = kwargs.pop("commit_message", None)
            repo_id = kwargs.pop("repo_id", save_directory.split(os.path.sep)[-1])
            repo_id = self._create_repo(repo_id, **kwargs)
            files_timestamps = self._get_files_timestamps(save_directory)

        
        
        if self._auto_class is not None:
            custom_object_save(self, save_directory, config=self)

        
        output_image_processor_file = os.path.join(save_directory, IMAGE_PROCESSOR_NAME)

        self.to_json_file(output_image_processor_file)
        logger.info(f"Image processor saved in {output_image_processor_file}")

        if push_to_hub:
            self._upload_modified_files(
                save_directory,
                repo_id,
                files_timestamps,
                commit_message=commit_message,
                token=kwargs.get("token"),
            )

        return [output_image_processor_file]

    @classmethod
    def get_image_processor_dict(
        cls, pretrained_model_name_or_path: Union[str, os.PathLike], **kwargs
    ) -> Tuple[Dict[str, Any], Dict[str, Any]]:
        
        cache_dir = kwargs.pop("cache_dir", None)
        force_download = kwargs.pop("force_download", False)
        resume_download = kwargs.pop("resume_download", False)
        proxies = kwargs.pop("proxies", None)
        token = kwargs.pop("token", None)
        use_auth_token = kwargs.pop("use_auth_token", None)
        local_files_only = kwargs.pop("local_files_only", False)
        revision = kwargs.pop("revision", None)
        subfolder = kwargs.pop("subfolder", "")

        from_pipeline = kwargs.pop("_from_pipeline", None)
        from_auto_class = kwargs.pop("_from_auto", False)

        if use_auth_token is not None:
            warnings.warn(
                "The `use_auth_token` argument is deprecated and will be removed in v5 of Transformers. Please use `token` instead.",
                FutureWarning,
            )
            if token is not None:
                raise ValueError(
                    "`token` and `use_auth_token` are both specified. Please set only the argument `token`."
                )
            token = use_auth_token

        user_agent = {"file_type": "image processor", "from_auto_class": from_auto_class}
        if from_pipeline is not None:
            user_agent["using_pipeline"] = from_pipeline

        if is_offline_mode() and not local_files_only:
            logger.info("Offline mode: forcing local_files_only=True")
            local_files_only = True

        pretrained_model_name_or_path = str(pretrained_model_name_or_path)
        is_local = os.path.isdir(pretrained_model_name_or_path)
        if os.path.isdir(pretrained_model_name_or_path):
            image_processor_file = os.path.join(pretrained_model_name_or_path, IMAGE_PROCESSOR_NAME)
        if os.path.isfile(pretrained_model_name_or_path):
            resolved_image_processor_file = pretrained_model_name_or_path
            is_local = True
        elif is_remote_url(pretrained_model_name_or_path):
            image_processor_file = pretrained_model_name_or_path
            resolved_image_processor_file = download_url(pretrained_model_name_or_path)
        else:
            image_processor_file = IMAGE_PROCESSOR_NAME
            try:
                
                resolved_image_processor_file = cached_file(
                    pretrained_model_name_or_path,
                    image_processor_file,
                    cache_dir=cache_dir,
                    force_download=force_download,
                    proxies=proxies,
                    resume_download=resume_download,
                    local_files_only=local_files_only,
                    token=token,
                    user_agent=user_agent,
                    revision=revision,
                    subfolder=subfolder,
                )
            except EnvironmentError:
                
                
                raise
            except Exception:
                
                raise EnvironmentError(
                    f"Can't load image processor for '{pretrained_model_name_or_path}'. If you were trying to load"
                    " it from 'https://huggingface.co/models', make sure you don't have a local directory with the"
                    f" same name. Otherwise, make sure '{pretrained_model_name_or_path}' is the correct path to a"
                    f" directory containing a {IMAGE_PROCESSOR_NAME} file"
                )

        try:
            
            with open(resolved_image_processor_file, "r", encoding="utf-8") as reader:
                text = reader.read()
            image_processor_dict = json.loads(text)

        except json.JSONDecodeError:
            raise EnvironmentError(
                f"It looks like the config file at '{resolved_image_processor_file}' is not a valid JSON file."
            )

        if is_local:
            logger.info(f"loading configuration file {resolved_image_processor_file}")
        else:
            logger.info(
                f"loading configuration file {image_processor_file} from cache at {resolved_image_processor_file}"
            )

        if "auto_map" in image_processor_dict and not is_local:
            image_processor_dict["auto_map"] = add_model_info_to_auto_map(
                image_processor_dict["auto_map"], pretrained_model_name_or_path
            )

        return image_processor_dict, kwargs

    @classmethod
    def from_dict(cls, image_processor_dict: Dict[str, Any], **kwargs):
        
        image_processor_dict = image_processor_dict.copy()
        return_unused_kwargs = kwargs.pop("return_unused_kwargs", False)

        
        
        
        if "size" in kwargs and "size" in image_processor_dict:
            image_processor_dict["size"] = kwargs.pop("size")
        if "crop_size" in kwargs and "crop_size" in image_processor_dict:
            image_processor_dict["crop_size"] = kwargs.pop("crop_size")

        image_processor = cls(**image_processor_dict)

        
        to_remove = []
        for key, value in kwargs.items():
            if hasattr(image_processor, key):
                setattr(image_processor, key, value)
                to_remove.append(key)
        for key in to_remove:
            kwargs.pop(key, None)

        logger.info(f"Image processor {image_processor}")
        if return_unused_kwargs:
            return image_processor, kwargs
        else:
            return image_processor

    def to_dict(self) -> Dict[str, Any]:
        
        output = copy.deepcopy(self.__dict__)
        output["image_processor_type"] = self.__class__.__name__

        return output

    @classmethod
    def from_json_file(cls, json_file: Union[str, os.PathLike]):
        
        with open(json_file, "r", encoding="utf-8") as reader:
            text = reader.read()
        image_processor_dict = json.loads(text)
        return cls(**image_processor_dict)

    def to_json_string(self) -> str:
        
        dictionary = self.to_dict()

        for key, value in dictionary.items():
            if isinstance(value, np.ndarray):
                dictionary[key] = value.tolist()

        
        
        _processor_class = dictionary.pop("_processor_class", None)
        if _processor_class is not None:
            dictionary["processor_class"] = _processor_class

        return json.dumps(dictionary, indent=2, sort_keys=True) + "\n"

    def to_json_file(self, json_file_path: Union[str, os.PathLike]):
        
        with open(json_file_path, "w", encoding="utf-8") as writer:
            writer.write(self.to_json_string())

    def __repr__(self):
        return f"{self.__class__.__name__} {self.to_json_string()}"

    @classmethod
    def register_for_auto_class(cls, auto_class="AutoImageProcessor"):
        
        if not isinstance(auto_class, str):
            auto_class = auto_class.__name__

        import transformers.models.auto as auto_module

        if not hasattr(auto_module, auto_class):
            raise ValueError(f"{auto_class} is not a valid auto class.")

        cls._auto_class = auto_class

    def fetch_images(self, image_url_or_urls: Union[str, List[str]]):
        
        headers = {
            "User-Agent": (
                "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0"
                " Safari/537.36"
            )
        }
        if isinstance(image_url_or_urls, list):
            return [self.fetch_images(x) for x in image_url_or_urls]
        elif isinstance(image_url_or_urls, str):
            response = requests.get(image_url_or_urls, stream=True, headers=headers)
            response.raise_for_status()
            return Image.open(BytesIO(response.content))
        else:
            raise ValueError(f"only a single or a list of entries is supported but got type={type(image_url_or_urls)}")


class BaseImageProcessor(ImageProcessingMixin):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def __call__(self, images, **kwargs) -> BatchFeature:
        
        return self.preprocess(images, **kwargs)

    def preprocess(self, images, **kwargs) -> BatchFeature:
        raise NotImplementedError("Each image processor must implement its own preprocess method")

    def rescale(
        self,
        image: np.ndarray,
        scale: float,
        data_format: Optional[Union[str, ChannelDimension]] = None,
        input_data_format: Optional[Union[str, ChannelDimension]] = None,
        **kwargs,
    ) -> np.ndarray:
        
        return rescale(image, scale=scale, data_format=data_format, input_data_format=input_data_format, **kwargs)

    def normalize(
        self,
        image: np.ndarray,
        mean: Union[float, Iterable[float]],
        std: Union[float, Iterable[float]],
        data_format: Optional[Union[str, ChannelDimension]] = None,
        input_data_format: Optional[Union[str, ChannelDimension]] = None,
        **kwargs,
    ) -> np.ndarray:
        
        return normalize(
            image, mean=mean, std=std, data_format=data_format, input_data_format=input_data_format, **kwargs
        )

    def center_crop(
        self,
        image: np.ndarray,
        size: Dict[str, int],
        data_format: Optional[Union[str, ChannelDimension]] = None,
        input_data_format: Optional[Union[str, ChannelDimension]] = None,
        **kwargs,
    ) -> np.ndarray:
        
        size = get_size_dict(size)
        if "height" not in size or "width" not in size:
            raise ValueError(f"The size dictionary must have keys 'height' and 'width'. Got {size.keys()}")
        return center_crop(
            image,
            size=(size["height"], size["width"]),
            data_format=data_format,
            input_data_format=input_data_format,
            **kwargs,
        )


VALID_SIZE_DICT_KEYS = ({"height", "width"}, {"shortest_edge"}, {"shortest_edge", "longest_edge"}, {"longest_edge"})


def is_valid_size_dict(size_dict):
    if not isinstance(size_dict, dict):
        return False

    size_dict_keys = set(size_dict.keys())
    for allowed_keys in VALID_SIZE_DICT_KEYS:
        if size_dict_keys == allowed_keys:
            return True
    return False


def convert_to_size_dict(
    size, max_size: Optional[int] = None, default_to_square: bool = True, height_width_order: bool = True
):
    
    if isinstance(size, int) and default_to_square:
        if max_size is not None:
            raise ValueError("Cannot specify both size as an int, with default_to_square=True and max_size")
        return {"height": size, "width": size}
    
    
    elif isinstance(size, int) and not default_to_square:
        size_dict = {"shortest_edge": size}
        if max_size is not None:
            size_dict["longest_edge"] = max_size
        return size_dict
    
    elif isinstance(size, (tuple, list)) and height_width_order:
        return {"height": size[0], "width": size[1]}
    elif isinstance(size, (tuple, list)) and not height_width_order:
        return {"height": size[1], "width": size[0]}
    elif size is None and max_size is not None:
        if default_to_square:
            raise ValueError("Cannot specify both default_to_square=True and max_size")
        return {"longest_edge": max_size}

    raise ValueError(f"Could not convert size input to size dict: {size}")


def get_size_dict(
    size: Union[int, Iterable[int], Dict[str, int]] = None,
    max_size: Optional[int] = None,
    height_width_order: bool = True,
    default_to_square: bool = True,
    param_name="size",
) -> dict:
    
    if not isinstance(size, dict):
        size_dict = convert_to_size_dict(size, max_size, default_to_square, height_width_order)
        logger.info(
            f"{param_name} should be a dictionary on of the following set of keys: {VALID_SIZE_DICT_KEYS}, got {size}."
            f" Converted to {size_dict}.",
        )
    else:
        size_dict = size

    if not is_valid_size_dict(size_dict):
        raise ValueError(
            f"{param_name} must have one of the following set of keys: {VALID_SIZE_DICT_KEYS}, got {size_dict.keys()}"
        )
    return size_dict


ImageProcessingMixin.push_to_hub = copy_func(ImageProcessingMixin.push_to_hub)
if ImageProcessingMixin.push_to_hub.__doc__ is not None:
    ImageProcessingMixin.push_to_hub.__doc__ = ImageProcessingMixin.push_to_hub.__doc__.format(
        object="image processor", object_class="AutoImageProcessor", object_files="image processor file"
    )