L isaddlZddlZddlZddlZddlmZmZmZmZddl Z ddl m Z ddl mZddlmZmZddlmZmZmZmZmZmZmZmZddlmZed d Zej>e Z!Gd d eZGdd eZ"ee"jFe"_#e"jFjH8e"jFjHjKddde"jF_$yy)N)AnyOptionalTypeVarUnion)custom_object_save) BatchFeature)is_valid_image load_image)IMAGE_PROCESSOR_NAMEPROCESSOR_NAMEPushToHubMixin copy_func download_urlis_offline_mode is_remote_urllogging) cached_fileImageProcessorTypeImageProcessingMixin)boundceZdZdZy)r a 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. N)__name__ __module__ __qualname____doc__h/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/transformers/image_processing_base.pyr r 0s rr c$eZdZdZdZdZdefdZe dde e de ee jfdee ee jfd ed ed ee eefd ed e fdZd de ee jfdefdZede ee jfd eeeefeeefffdZedeeeffdZd eeeffdZede ee jffdZd efdZde ee jffdZdZed!dZde eeeeeeffdZy)"rz This is an image processor mixin used to provide saving/loading functionality for sequential and image feature extractors. Nc |jdd|jdd|_|jD]\}} t|||y#t$r%}t j d|d|d||d}~wwxYw)z'Set elements of `kwargs` as attributes.feature_extractor_typeNprocessor_classz Can't set z with value z for )pop_processor_classitemssetattrAttributeErrorloggererror)selfkwargskeyvalueerrs r__init__zImageProcessingMixin.__init__Hs  +T2 & + To test a pull request you made on the Hub, you can pass `revision="refs/pr/"`. 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} ```r5r6r7r9use_auth_tokenNrThe `use_auth_token` argument is deprecated and will be removed in v5 of Transformers. Please use `token` instead.V`token` and `use_auth_token` are both specified. Please set only the argument `token`.r8)r$warningswarn FutureWarning ValueErrorget_image_processor_dict from_dict) r3r4r5r6r7r8r9r,r<image_processor_dicts rfrom_pretrainedz$ImageProcessingMixin.from_pretrained[st({#1 %5!"%z$4d;  % MME   l#E  #F7O'Cs'C'CDa'lek'l$fs}}1zProvided path (z#) should be a directory, not a fileT)exist_okcommit_messagerepo_id)configzImage processor saved in )rKr8)r$r?r@rAgetrBospathisfileAssertionErrormakedirssplitsep _create_repo_get_files_timestamps _auto_classrjoinr to_json_filer)info_upload_modified_files) r+rGrHr,r<rKrLfiles_timestampsoutput_image_processor_files rsave_pretrainedz$ImageProcessingMixin.save_pretraineds $4d;  % MME zz'". l-F7O 77>>. ) ?>2BBe!fg g NT2 #ZZ(8$?NjjN,@,@,Mb,QRG'd'':6:G#99.I     ' t^D A')ggll>CW&X# 56 /0K/LMN   ' ' -jj) ( ,,,rc |jdd}|jdd}|jdd}|jdd}|jdd}|jdd}|jd d} |jd d} |jd d } |jd t} |jdd} |jdd}|)tjdt| t d|}d|d}| | |d<t r| stjdd} t|}tjj|}tjj|r tjj|| }tjj|r|}d}nPt|r|}t!|}n7| } |t"fDcgc]}t%||||||| ||| | d x} | }}|d} t+|d5}|j-}dddt/j0}|j3d|}|rtjd"|||fStjd"d#|||fScc}w#t&$rt($rt'd|d|d| dwxYw#1swYxYw#t.j4$rt'd |d!wxYw)$a 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. image_processor_filename (`str`, *optional*, defaults to `"config.json"`): The name of the file in the model directory to use for the image processor config. Returns: `tuple[Dict, Dict]`: The dictionary(ies) that will be used to instantiate the image processor object. r5Nr6Fresume_downloadproxiesr8r<r7r9 subfolderimage_processor_filename_from_pipeline _from_autor=r>image processor) file_typefrom_auto_classusing_pipelinez+Offline mode: forcing local_files_only=TrueT) filenamer5r6rcrbr7r8 user_agentr9rd%_raise_exceptions_for_missing_entriesrz Can't load image processor for 'z'. If you were trying to load it from 'https://huggingface.co/models', make sure you don't have a local directory with the same name. Otherwise, make sure 'z2' is the correct path to a directory containing a z fileutf-8encodingimage_processorz"It looks like the config file at 'z' is not a valid JSON file.zloading configuration file z from cache at )r$r r?r@rArBrr)r\strrPrQisdirrZrRrrr rOSError ExceptionopenreadjsonloadsrOJSONDecodeError)r3r4r,r5r6rbrcr8r<r7r9rdrf from_pipelinerkrnis_localimage_processor_fileresolved_image_processor_filerm resolved_fileresolved_image_processor_filesreadertextrEs rrCz-ImageProcessingMixin.get_image_processor_dict sp(JJ{D1 $4e< **%6=**Y- 7D)$4d;!::&8%@::j$/JJ{B/ #)::.HJ^#_  #3T:  **\59  % MME   l#E#4Y  $+8J' (  %5 KKE F# (+,I(J%77==!>? 77==6 7#%77<<0MOg#h 77>>7 8,I )H 8 9#@ ,89V,W )#; # &:>$J2 )49%-&/+9$+,;-="''1%-&/BG *   ! "2.2*1Oq0Q- 3gF %&{{} %#'::d#3 #7#;#;rEc |j}|jdd}d|vrd|vr|jd|d<d|vrd|vr|jd|d<|di|}g}|jD]0\}}t||st ||||j |2|D]}|j|dt jd||r||fS|S)a 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. return_unused_kwargsFsize crop_sizeNzImage processor r)copyr$r&hasattrr'appendr)r\)r3rEr,rrs to_remover-r.s rrDzImageProcessingMixin.from_dicts " 488:%zz*@%H V *> >+1::f+=  ( & [4H%H06 ;0G  -5 45  ,,. &JC,e4  % & "C JJsD ! "  &&789 "F* *" "rcvtj|j}|jj|d<|S)z Serializes this instance to a Python dictionary. Returns: `dict[str, Any]`: Dictionary of all the attributes that make up this image processor instance. image_processor_type)rdeepcopy__dict__ __class__r)r+outputs rto_dictzImageProcessingMixin.to_dicts0t}}-)-)@)@%& r json_filect|d5}|j}dddtj}|di|S#1swY&xYw)a 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. rprqNr)rxryrzr{)r3rrrrEs rfrom_json_filez#ImageProcessingMixin.from_json_filesN)g . !&;;=D !#zz$/*)** ! !s AA c|j}|jD]3\}}t|tjs!|j ||<5|j dd}|||d<tj|dddzS)z Serializes this instance to a JSON string. Returns: `str`: String containing all the attributes that make up this feature_extractor instance in JSON format. r%Nr#T)indent sort_keys ) rr& isinstancenpndarraytolistr$rzdumps)r+ dictionaryr-r.r%s rto_json_stringz#ImageProcessingMixin.to_json_strings\\^ $**, 1JC%,"',,. 3 1 &>>*o=E#r{{"234o= o=  o= c4i() o=o= o=o=b;-eC4D.E;-TX;-zx,,1#r{{2B,Cx, tCH~tCH~- .x,x,t*#T#s(^*#*#X c3h +uS"++-='>++$GG* 05bkk1A+B 0D%%*weCcDcO4S.Twrrirzimage processor file)object object_class object_files)&rrzrPr?typingrrrrnumpyrdynamic_module_utilsrfeature_extraction_utilsr BaseBatchFeature image_utilsr r utilsr r rrrrrr utils.hubrr get_loggerrr)rrHrformatrrrrs  004F3   #19OP   H % #  Xw>Xwv$--A-M-M#N ##++7/C/O/O/W/W/^/^ /CRh0_0$$,8r