




# Copyright 2021 The HuggingFace Inc. team. All rights reserved.
#
# 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.

from packaging import version

from .. import __version__
from .constants import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD, IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD
from .doc import (
    add_code_sample_docstrings,
    add_end_docstrings,
    add_start_docstrings,
    add_start_docstrings_to_model_forward,
    copy_func,
    replace_return_docstrings,
)
from .generic import (
    ContextManagers,
    ExplicitEnum,
    ModelOutput,
    PaddingStrategy,
    TensorType,
    add_model_info_to_auto_map,
    cached_property,
    can_return_loss,
    expand_dims,
    find_labels,
    flatten_dict,
    infer_framework,
    is_jax_tensor,
    is_numpy_array,
    is_tensor,
    is_tf_symbolic_tensor,
    is_tf_tensor,
    is_torch_device,
    is_torch_dtype,
    is_torch_tensor,
    reshape,
    squeeze,
    strtobool,
    tensor_size,
    to_numpy,
    to_py_obj,
    transpose,
    working_or_temp_dir,
)
from .hub import (
    CLOUDFRONT_DISTRIB_PREFIX,
    DISABLE_TELEMETRY,
    HF_MODULES_CACHE,
    HUGGINGFACE_CO_PREFIX,
    HUGGINGFACE_CO_RESOLVE_ENDPOINT,
    PYTORCH_PRETRAINED_BERT_CACHE,
    PYTORCH_TRANSFORMERS_CACHE,
    S3_BUCKET_PREFIX,
    TRANSFORMERS_CACHE,
    TRANSFORMERS_DYNAMIC_MODULE_NAME,
    EntryNotFoundError,
    PushToHubMixin,
    RepositoryNotFoundError,
    RevisionNotFoundError,
    cached_file,
    default_cache_path,
    define_sagemaker_information,
    download_url,
    extract_commit_hash,
    get_cached_models,
    get_file_from_repo,
    get_full_repo_name,
    has_file,
    http_user_agent,
    is_offline_mode,
    is_remote_url,
    move_cache,
    send_example_telemetry,
    try_to_load_from_cache,
)
from .import_utils import (
    ENV_VARS_TRUE_AND_AUTO_VALUES,
    ENV_VARS_TRUE_VALUES,
    TORCH_FX_REQUIRED_VERSION,
    USE_JAX,
    USE_TF,
    USE_TORCH,
    DummyObject,
    OptionalDependencyNotAvailable,
    _LazyModule,
    ccl_version,
    direct_transformers_import,
    get_torch_version,
    is_accelerate_available,
    is_apex_available,
    is_bitsandbytes_available,
    is_bsa_available,
    is_coloredlogs_available,
    is_cython_available,
    is_datasets_available,
    is_decord_available,
    is_detectrona_available,
    is_faiss_available,
    is_flax_available,
    is_ftfy_available,
    is_in_notebook,
    is_ipex_available,
    is_jieba_available,
    is_jumanpp_available,
    is_kenlm_available,
    is_keras_nlp_available,
    is_librosa_available,
    is_natten_available,
    is_ninja_available,
    is_onnx_available,
    is_openai_available,
    is_optimum_available,
    is_pandas_available,
    is_peft_available,
    is_phonemizer_available,
    is_protobuf_available,
    is_psutil_available,
    is_pyanvml_available,
    is_pyctcdecode_available,
    is_pytesseract_available,
    is_pytest_available,
    is_pytorch_quantization_available,
    is_rjieba_available,
    is_sacremoses_available,
    is_safetensors_available,
    is_sagemaker_dp_enabled,
    is_sagemaker_mp_enabled,
    is_scipy_available,
    is_sentencepiece_available,
    is_seqio_available,
    is_sklearn_available,
    is_soundfile_availble,
    is_spacy_available,
    is_speech_available,
    is_sudachi_available,
    is_tensorflow_probability_available,
    is_tensorflow_text_available,
    is_tfaonnx_available,
    is_tf_available,
    is_timm_available,
    is_tokenizers_available,
    is_torch_available,
    is_torch_bfaa_available,
    is_torch_bfaa_cpu_available,
    is_torch_bfaa_gpu_available,
    is_torch_compile_available,
    is_torch_cuda_available,
    is_torch_fx_available,
    is_torch_fx_proxy,
    is_torch_mps_available,
    is_torch_neuroncore_available,
    is_torch_tensorrt_fx_available,
    is_torch_tfaa_available,
    is_torch_tpu_available,
    is_torchaudio_available,
    is_torchdistx_available,
    is_torchdynamo_available,
    is_torchvision_available,
    is_training_run_on_sagemaker,
    is_vision_available,
    requires_backends,
    torch_only_method,
)


UpperCAmelCase_		:				List[str]									=     "pytorch_model.bin"
UpperCAmelCase_		:				List[str]									=     "pytorch_model.bin.index.json"
UpperCAmelCase_		:				Optional[Any]									=     "adapter_config.json"
UpperCAmelCase_		:				List[Any]									=     "adapter_model.bin"
UpperCAmelCase_		:				Optional[Any]									=     "adapter_model.safetensors"
UpperCAmelCase_		:				Optional[Any]									=     "tf_model.h5"
UpperCAmelCase_		:				Union[str, Any]									=     "tf_model.h5.index.json"
UpperCAmelCase_		:				Union[str, Any]									=     "model.ckpt"
UpperCAmelCase_		:				Tuple									=     "flax_model.msgpack"
UpperCAmelCase_		:				str									=     "flax_model.msgpack.index.json"
UpperCAmelCase_		:				Union[str, Any]									=     "model.safetensors"
UpperCAmelCase_		:				int									=     "model.safetensors.index.json"
UpperCAmelCase_		:				Optional[Any]									=     "config.json"
UpperCAmelCase_		:				List[str]									=     "preprocessor_config.json"
UpperCAmelCase_		:				Dict									=     FEATURE_EXTRACTOR_NAME
UpperCAmelCase_		:				Any									=     "generation_config.json"
UpperCAmelCase_		:				Dict									=     "modelcard.json"

UpperCAmelCase_		:				int									=     "▁"
UpperCAmelCase_		:				Optional[int]									=     SENTENCEPIECE_UNDERLINE  # Kept for backward compatibility

UpperCAmelCase_		:				Union[str, Any]									=     [
    [[0, 1, 0, 1], [1, 0, 0, 1]]
] * 2  # Needs to have 0s and 1s only since XLM uses it for langs too.
UpperCAmelCase_		:				str									=     [[7, 6, 0, 0, 1], [1, 2, 3, 0, 0], [0, 0, 0, 4, 5]]
UpperCAmelCase_		:				Optional[int]									=     [[1, 1, 1, 1, 1], [1, 1, 1, 0, 0], [0, 0, 0, 1, 1]]




def 		lowerCAmelCase_   (	lowerCamelCase   ):
				if version.parse(snake_case__   ) < version.parse(snake_case__   ):
								if "dev" in min_version:
												__magic_name__	:	Dict								=(
												    """This example requires a source install from HuggingFace Transformers (see """
												    """`https://huggingface.co/docs/transformers/installation#install-from-source`),"""
												)
								else:
												__magic_name__	:	Union[str, Any]								=F"This example requires a minimum version of {min_version},"
								error_message += F" but the version found is {__version__}.\n"
								raise ImportError(
								    error_message
								    + """Check out https://github.com/huggingface/transformers/tree/main/examples#important-note for the examples corresponding to other """
								    """versions of HuggingFace Transformers."""   )










import json
import pathlib
import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision, slow
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
							import torch

if is_vision_available():
							from PIL import Image

							from transformers import DetaImageProcessor




class 		__A      (			unittest.TestCase			):

						def __init__(		self   :List[str]    , __snake_case   :str    , __snake_case   :List[Any]=7    , __snake_case   :List[str]=3    , __snake_case   :int=30    , __snake_case   :int=4_00    , __snake_case   :List[str]=True    , __snake_case   :List[str]=None    , __snake_case   :Tuple=True    , __snake_case   :Any=[0.5, 0.5, 0.5]    , __snake_case   :Any=[0.5, 0.5, 0.5]    , __snake_case   :Union[str, Any]=True    , __snake_case   :str=1 / 2_55    , __snake_case   :int=True    , ):
										'''simple docstring'''

										__magic_name__	:	Optional[int]								=size if size is not None else {"""shortest_edge""": 18, """longest_edge""": 13_33}
										__magic_name__	:	str								=parent
										__magic_name__	:	Tuple								=batch_size
										__magic_name__	:	Optional[Any]								=num_channels
										__magic_name__	:	Optional[int]								=min_resolution
										__magic_name__	:	Optional[Any]								=max_resolution
										__magic_name__	:	Optional[Any]								=do_resize
										__magic_name__	:	Any								=size
										__magic_name__	:	str								=do_normalize
										__magic_name__	:	str								=image_mean
										__magic_name__	:	List[Any]								=image_std
										__magic_name__	:	Optional[int]								=do_rescale
										__magic_name__	:	List[str]								=rescale_factor
										__magic_name__	:	Optional[int]								=do_pad

						def     A__						(		self   :Optional[int] ):
										'''simple docstring'''

										return {
										    "do_resize": self.do_resize,
										    "size": self.size,
										    "do_normalize": self.do_normalize,
										    "image_mean": self.image_mean,
										    "image_std": self.image_std,
										    "do_rescale": self.do_rescale,
										    "rescale_factor": self.rescale_factor,
										    "do_pad": self.do_pad,
										}


						def     A__						(		self   :List[str]    , __snake_case   :Any    , __snake_case   :List[Any]=False ):
										'''simple docstring'''

										if not batched:
														__magic_name__	:	Any								=image_inputs[0]
														if isinstance(__snake_case    , Image.Image ):
																		__magic_name__				, __magic_name__	:	Dict								=image.size
														else:
																		__magic_name__				, __magic_name__	:	Dict								=image.shape[1], image.shape[2]
														if w < h:
																		__magic_name__	:	Any								=int(self.size["""shortest_edge"""] * h / w )
																		__magic_name__	:	Dict								=self.size["""shortest_edge"""]
														elif w > h:
																		__magic_name__	:	Union[str, Any]								=self.size["""shortest_edge"""]
																		__magic_name__	:	Optional[int]								=int(self.size["""shortest_edge"""] * w / h )
														else:
																		__magic_name__	:	Any								=self.size["""shortest_edge"""]
																		__magic_name__	:	List[str]								=self.size["""shortest_edge"""]

										else:
														__magic_name__	:	List[str]								=[]
														for image in image_inputs:
																		__magic_name__				, __magic_name__	:	Tuple								=self.get_expected_values([image] )
																		expected_values.append((expected_height, expected_width) )
														__magic_name__	:	Optional[int]								=max(__snake_case    , key=lambda __snake_case : item[0] )[0]
														__magic_name__	:	Tuple								=max(__snake_case    , key=lambda __snake_case : item[1] )[1]

										return expected_height, expected_width




@require_torch
@require_vision
class 		__A      (			UpperCamelCase__					,						unittest.TestCase			):
						UpperCamelCase     						=					DetaImageProcessor if is_vision_available() else None

						def     A__						(		self   :Optional[int] ):
										'''simple docstring'''

										__magic_name__	:	int								=DetaImageProcessingTester(self )

						@property
						def     A__						(		self   :List[str] ):
										'''simple docstring'''

										return self.image_processor_tester.prepare_image_processor_dict()

						def     A__						(		self   :Optional[Any] ):
										'''simple docstring'''

										__magic_name__	:	List[str]								=self.image_processing_class(**self.image_processor_dict )
										self.assertTrue(hasattr(__snake_case    , """image_mean""" ) )
										self.assertTrue(hasattr(__snake_case    , """image_std""" ) )
										self.assertTrue(hasattr(__snake_case    , """do_normalize""" ) )
										self.assertTrue(hasattr(__snake_case    , """do_resize""" ) )
										self.assertTrue(hasattr(__snake_case    , """do_rescale""" ) )
										self.assertTrue(hasattr(__snake_case    , """do_pad""" ) )
										self.assertTrue(hasattr(__snake_case    , """size""" ) )

						def     A__						(		self   :List[Any] ):
										'''simple docstring'''

										__magic_name__	:	Optional[Any]								=self.image_processing_class.from_dict(self.image_processor_dict )
										self.assertEqual(image_processor.size    , {"""shortest_edge""": 18, """longest_edge""": 13_33} )
										self.assertEqual(image_processor.do_pad    , __snake_case )

						def     A__						(		self   :Any ):
										'''simple docstring'''

										pass

						def     A__						(		self   :str ):
										'''simple docstring'''

										__magic_name__	:	Tuple								=self.image_processing_class(**self.image_processor_dict )
										# create random PIL images
										__magic_name__	:	Tuple								=prepare_image_inputs(self.image_processor_tester    , equal_resolution=__snake_case )
										for image in image_inputs:
														self.assertIsInstance(__snake_case    , Image.Image )

										# Test not batched input
										__magic_name__	:	Tuple								=image_processing(image_inputs[0]    , return_tensors="""pt""" ).pixel_values

										__magic_name__				, __magic_name__	:	str								=self.image_processor_tester.get_expected_values(__snake_case )

										self.assertEqual(
										    encoded_images.shape    , (1, self.image_processor_tester.num_channels, expected_height, expected_width)    , )

										# Test batched
										__magic_name__				, __magic_name__	:	int								=self.image_processor_tester.get_expected_values(__snake_case    , batched=__snake_case )

										__magic_name__	:	Dict								=image_processing(__snake_case    , return_tensors="""pt""" ).pixel_values
										self.assertEqual(
										    encoded_images.shape    , (
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        expected_height,
										        expected_width,
										    )    , )

						def     A__						(		self   :List[str] ):
										'''simple docstring'''

										__magic_name__	:	int								=self.image_processing_class(**self.image_processor_dict )
										# create random numpy tensors
										__magic_name__	:	Optional[Any]								=prepare_image_inputs(self.image_processor_tester    , equal_resolution=__snake_case    , numpify=__snake_case )
										for image in image_inputs:
														self.assertIsInstance(__snake_case    , np.ndarray )

										# Test not batched input
										__magic_name__	:	str								=image_processing(image_inputs[0]    , return_tensors="""pt""" ).pixel_values

										__magic_name__				, __magic_name__	:	int								=self.image_processor_tester.get_expected_values(__snake_case )

										self.assertEqual(
										    encoded_images.shape    , (1, self.image_processor_tester.num_channels, expected_height, expected_width)    , )

										# Test batched
										__magic_name__	:	List[str]								=image_processing(__snake_case    , return_tensors="""pt""" ).pixel_values

										__magic_name__				, __magic_name__	:	Any								=self.image_processor_tester.get_expected_values(__snake_case    , batched=__snake_case )

										self.assertEqual(
										    encoded_images.shape    , (
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        expected_height,
										        expected_width,
										    )    , )

						def     A__						(		self   :Optional[Any] ):
										'''simple docstring'''

										__magic_name__	:	List[Any]								=self.image_processing_class(**self.image_processor_dict )
										# create random PyTorch tensors
										__magic_name__	:	Dict								=prepare_image_inputs(self.image_processor_tester    , equal_resolution=__snake_case    , torchify=__snake_case )
										for image in image_inputs:
														self.assertIsInstance(__snake_case    , torch.Tensor )

										# Test not batched input
										__magic_name__	:	Tuple								=image_processing(image_inputs[0]    , return_tensors="""pt""" ).pixel_values

										__magic_name__				, __magic_name__	:	int								=self.image_processor_tester.get_expected_values(__snake_case )

										self.assertEqual(
										    encoded_images.shape    , (1, self.image_processor_tester.num_channels, expected_height, expected_width)    , )

										# Test batched
										__magic_name__	:	Dict								=image_processing(__snake_case    , return_tensors="""pt""" ).pixel_values

										__magic_name__				, __magic_name__	:	Tuple								=self.image_processor_tester.get_expected_values(__snake_case    , batched=__snake_case )

										self.assertEqual(
										    encoded_images.shape    , (
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        expected_height,
										        expected_width,
										    )    , )

						@slow
						def     A__						(		self   :str ):
										'''simple docstring'''

										__magic_name__	:	Optional[int]								=Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" )
										with open("""./tests/fixtures/tests_samples/COCO/coco_annotations.txt"""    , """r""" ) as f:
														__magic_name__	:	Optional[int]								=json.loads(f.read() )

										__magic_name__	:	str								={"""image_id""": 3_97_69, """annotations""": target}

										# encode them
										__magic_name__	:	Optional[Any]								=DetaImageProcessor()
										__magic_name__	:	Any								=image_processing(images=__snake_case    , annotations=__snake_case    , return_tensors="""pt""" )

										# verify pixel values
										__magic_name__	:	Tuple								=torch.Size([1, 3, 8_00, 10_66] )
										self.assertEqual(encoding["""pixel_values"""].shape    , __snake_case )

										__magic_name__	:	Optional[int]								=torch.tensor([0.2796, 0.3138, 0.3481] )
										self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3]    , __snake_case    , atol=1E-4 ) )

										# verify area
										__magic_name__	:	Dict								=torch.tensor([5887.9600, 11250.2061, 489353.8438, 837122.7500, 147967.5156, 165732.3438] )
										self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""]    , __snake_case ) )
										# verify boxes
										__magic_name__	:	Optional[int]								=torch.Size([6, 4] )
										self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape    , __snake_case )
										__magic_name__	:	int								=torch.tensor([0.5503, 0.2765, 0.0604, 0.2215] )
										self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0]    , __snake_case    , atol=1E-3 ) )
										# verify image_id
										__magic_name__	:	Any								=torch.tensor([3_97_69] )
										self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""]    , __snake_case ) )
										# verify is_crowd
										__magic_name__	:	Optional[Any]								=torch.tensor([0, 0, 0, 0, 0, 0] )
										self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""]    , __snake_case ) )
										# verify class_labels
										__magic_name__	:	List[str]								=torch.tensor([75, 75, 63, 65, 17, 17] )
										self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""]    , __snake_case ) )
										# verify orig_size
										__magic_name__	:	Optional[Any]								=torch.tensor([4_80, 6_40] )
										self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""]    , __snake_case ) )
										# verify size
										__magic_name__	:	Tuple								=torch.tensor([8_00, 10_66] )
										self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""]    , __snake_case ) )


						@slow
						def     A__						(		self   :int ):
										'''simple docstring'''

										__magic_name__	:	List[str]								=Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" )
										with open("""./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt"""    , """r""" ) as f:
														__magic_name__	:	str								=json.loads(f.read() )

										__magic_name__	:	Optional[Any]								={"""file_name""": """000000039769.png""", """image_id""": 3_97_69, """segments_info""": target}

										__magic_name__	:	List[str]								=pathlib.Path("""./tests/fixtures/tests_samples/COCO/coco_panoptic""" )

										# encode them
										__magic_name__	:	int								=DetaImageProcessor(format="""coco_panoptic""" )
										__magic_name__	:	Tuple								=image_processing(images=__snake_case    , annotations=__snake_case    , masks_path=__snake_case    , return_tensors="""pt""" )

										# verify pixel values
										__magic_name__	:	Union[str, Any]								=torch.Size([1, 3, 8_00, 10_66] )
										self.assertEqual(encoding["""pixel_values"""].shape    , __snake_case )

										__magic_name__	:	str								=torch.tensor([0.2796, 0.3138, 0.3481] )
										self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3]    , __snake_case    , atol=1E-4 ) )

										# verify area
										__magic_name__	:	str								=torch.tensor([147979.6875, 165527.0469, 484638.5938, 11292.9375, 5879.6562, 7634.1147] )
										self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""]    , __snake_case ) )
										# verify boxes
										__magic_name__	:	Dict								=torch.Size([6, 4] )
										self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape    , __snake_case )
										__magic_name__	:	Optional[Any]								=torch.tensor([0.2625, 0.5437, 0.4688, 0.8625] )
										self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0]    , __snake_case    , atol=1E-3 ) )
										# verify image_id
										__magic_name__	:	Dict								=torch.tensor([3_97_69] )
										self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""]    , __snake_case ) )
										# verify is_crowd
										__magic_name__	:	int								=torch.tensor([0, 0, 0, 0, 0, 0] )
										self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""]    , __snake_case ) )
										# verify class_labels
										__magic_name__	:	List[Any]								=torch.tensor([17, 17, 63, 75, 75, 93] )
										self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""]    , __snake_case ) )
										# verify masks
										__magic_name__	:	List[Any]								=82_28_73
										self.assertEqual(encoding["""labels"""][0]["""masks"""].sum().item()    , __snake_case )
										# verify orig_size
										__magic_name__	:	Union[str, Any]								=torch.tensor([4_80, 6_40] )
										self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""]    , __snake_case ) )
										# verify size
										__magic_name__	:	List[Any]								=torch.tensor([8_00, 10_66] )
										self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""]    , __snake_case ) )




