

import os
import shutil
import sys
import tempfile
import unittest
from pathlib import Path

import pytest

import transformers
from transformers import (
    BERT_PRETRAINED_CONFIG_ARCHIVE_MAP,
    GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP,
    AutoTokenizer,
    BertConfig,
    BertTokenizer,
    BertTokenizerFast,
    CTRLTokenizer,
    GPTaTokenizer,
    GPTaTokenizerFast,
    PreTrainedTokenizerFast,
    RobertaTokenizer,
    RobertaTokenizerFast,
    is_tokenizers_available,
)
from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig
from transformers.models.auto.tokenization_auto import (
    TOKENIZER_MAPPING,
    get_tokenizer_config,
    tokenizer_class_from_name,
)
from transformers.models.roberta.configuration_roberta import RobertaConfig
from transformers.testing_utils import (
    DUMMY_DIFF_TOKENIZER_IDENTIFIER,
    DUMMY_UNKNOWN_IDENTIFIER,
    SMALL_MODEL_IDENTIFIER,
    RequestCounter,
    require_tokenizers,
    slow,
)


sys.path.append(str(Path(__file__).parent.parent.parent.parent / 'utils'))

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_tokenization import CustomTokenizer  # noqa E402


if is_tokenizers_available():
      from test_module.custom_tokenization_fast import CustomTokenizerFast

class   _a					(			unittest.TestCase				):




      """simple docstring"""

      def 		__A	(   self							:				List[str]	):
            A_						  =				0





      @slow
      def 		__A	(   self							:				str	):
            for model_name in (x for x in BERT_PRETRAINED_CONFIG_ARCHIVE_MAP.keys() if "japanese" not in x):
                  A_						  =				AutoTokenizer.from_pretrained(UpperCAmelCase	)
                  self.assertIsNotNone(UpperCAmelCase	)
                  self.assertIsInstance(UpperCAmelCase						,			(BertTokenizer, BertTokenizerFast)	)
                  self.assertGreater(len(UpperCAmelCase	)						,			0	)

            for model_name in GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP.keys():
                  A_						  =				AutoTokenizer.from_pretrained(UpperCAmelCase	)
                  self.assertIsNotNone(UpperCAmelCase	)
                  self.assertIsInstance(UpperCAmelCase						,			(GPTaTokenizer, GPTaTokenizerFast)	)
                  self.assertGreater(len(UpperCAmelCase	)						,			0	)





      def 		__A	(   self							:				List[Any]	):
            A_						  =				AutoTokenizer.from_pretrained(UpperCAmelCase	)
            self.assertIsInstance(UpperCAmelCase						,			(BertTokenizer, BertTokenizerFast)	)
            self.assertEqual(tokenizer.vocab_size						,			12	)





      def 		__A	(   self							:				Tuple	):
            A_						  =				AutoTokenizer.from_pretrained(UpperCAmelCase	)
            self.assertIsInstance(UpperCAmelCase						,			(RobertaTokenizer, RobertaTokenizerFast)	)
            self.assertEqual(tokenizer.vocab_size						,			20	)





      def 		__A	(   self							:				Tuple	):
            A_						  =				AutoConfig.from_pretrained(UpperCAmelCase	)
            self.assertIsInstance(UpperCAmelCase						,			UpperCAmelCase	)
            # Check that tokenizer_type ≠ model_type
            A_						  =				AutoTokenizer.from_pretrained(UpperCAmelCase						,			config=UpperCAmelCase	)
            self.assertIsInstance(UpperCAmelCase						,			(BertTokenizer, BertTokenizerFast)	)
            self.assertEqual(tokenizer.vocab_size						,			12	)





      def 		__A	(   self							:				List[str]	):
            with tempfile.TemporaryDirectory() as tmp_dir:
                  shutil.copy("./tests/fixtures/vocab.txt"						,			os.path.join(UpperCAmelCase						,			"vocab.txt"	)	)

                  A_						  =				AutoTokenizer.from_pretrained(UpperCAmelCase						,			tokenizer_type="bert"						,			use_fast=UpperCAmelCase	)
                  self.assertIsInstance(UpperCAmelCase						,			UpperCAmelCase	)

            with tempfile.TemporaryDirectory() as tmp_dir:
                  shutil.copy("./tests/fixtures/vocab.json"						,			os.path.join(UpperCAmelCase						,			"vocab.json"	)	)
                  shutil.copy("./tests/fixtures/merges.txt"						,			os.path.join(UpperCAmelCase						,			"merges.txt"	)	)

                  A_						  =				AutoTokenizer.from_pretrained(UpperCAmelCase						,			tokenizer_type="gpt2"						,			use_fast=UpperCAmelCase	)
                  self.assertIsInstance(UpperCAmelCase						,			UpperCAmelCase	)





      @require_tokenizers
      def 		__A	(   self							:				str	):
            with tempfile.TemporaryDirectory() as tmp_dir:
                  shutil.copy("./tests/fixtures/vocab.txt"						,			os.path.join(UpperCAmelCase						,			"vocab.txt"	)	)

                  A_						  =				AutoTokenizer.from_pretrained(UpperCAmelCase						,			tokenizer_type="bert"	)
                  self.assertIsInstance(UpperCAmelCase						,			UpperCAmelCase	)

            with tempfile.TemporaryDirectory() as tmp_dir:
                  shutil.copy("./tests/fixtures/vocab.json"						,			os.path.join(UpperCAmelCase						,			"vocab.json"	)	)
                  shutil.copy("./tests/fixtures/merges.txt"						,			os.path.join(UpperCAmelCase						,			"merges.txt"	)	)

                  A_						  =				AutoTokenizer.from_pretrained(UpperCAmelCase						,			tokenizer_type="gpt2"	)
                  self.assertIsInstance(UpperCAmelCase						,			UpperCAmelCase	)





      def 		__A	(   self							:				List[str]	):
            with pytest.raises(UpperCAmelCase	):
                  AutoTokenizer.from_pretrained("./"						,			tokenizer_type="xxx"	)





      @require_tokenizers
      def 		__A	(   self							:				str	):
            for tokenizer_class in [BertTokenizer, BertTokenizerFast, AutoTokenizer]:
                  A_						  =				tokenizer_class.from_pretrained("wietsedv/bert-base-dutch-cased"	)
                  self.assertIsInstance(UpperCAmelCase						,			(BertTokenizer, BertTokenizerFast)	)

                  if isinstance(UpperCAmelCase						,			UpperCAmelCase	):
                        self.assertEqual(tokenizer.basic_tokenizer.do_lower_case						,			UpperCAmelCase	)
                  else:
                        self.assertEqual(tokenizer.do_lower_case						,			UpperCAmelCase	)

                  self.assertEqual(tokenizer.model_max_length						,			512	)





      @require_tokenizers
      def 		__A	(   self							:				List[Any]	):
            for tokenizer_class in [BertTokenizer, BertTokenizerFast, AutoTokenizer]:
                  with self.assertRaisesRegex(
                      UpperCAmelCase						,			"julien-c/herlolip-not-exists is not a local folder and is not a valid model identifier"						,			):
                        A_						  =				tokenizer_class.from_pretrained("julien-c/herlolip-not-exists"	)





      def 		__A	(   self							:				Tuple	):
            # tests: https://github.com/huggingface/transformers/pull/13251
            # 1. models with `-`, e.g. xlm-roberta -> xlm_roberta
            # 2. models that don't remap 1-1 from model-name to model file, e.g., openai-gpt -> openai
            A_						  =				TOKENIZER_MAPPING.values()
            A_						  =				[]

            for slow_tok, fast_tok in tokenizers:
                  if slow_tok is not None:
                        tokenizer_names.append(slow_tok.__name__	)

                  if fast_tok is not None:
                        tokenizer_names.append(fast_tok.__name__	)

            for tokenizer_name in tokenizer_names:
                  # must find the right class
                  tokenizer_class_from_name(UpperCAmelCase	)





      @require_tokenizers
      def 		__A	(   self							:				List[str]	):
            self.assertIsInstance(AutoTokenizer.from_pretrained("bert-base-cased"						,			use_fast=UpperCAmelCase	)						,			UpperCAmelCase	)
            self.assertIsInstance(AutoTokenizer.from_pretrained("bert-base-cased"	)						,			UpperCAmelCase	)





      @require_tokenizers
      def 		__A	(   self							:				int	):
            A_						  =				AutoTokenizer.from_pretrained("distilbert-base-uncased"						,			do_lower_case=UpperCAmelCase	)
            A_						  =				"Hello, world. How are you?"
            A_						  =				tokenizer.tokenize(UpperCAmelCase	)
            self.assertEqual("[UNK]"						,			tokens[0]	)

            A_						  =				AutoTokenizer.from_pretrained("microsoft/mpnet-base"						,			do_lower_case=UpperCAmelCase	)
            A_						  =				tokenizer.tokenize(UpperCAmelCase	)
            self.assertEqual("[UNK]"						,			tokens[0]	)





      @require_tokenizers
      def 		__A	(   self							:				Union[str, Any]	):
            A_						  =				AutoTokenizer.from_pretrained("robot-test/dummy-tokenizer-fast-with-model-config"	)
            self.assertEqual(type(UpperCAmelCase	)						,			UpperCAmelCase	)
            self.assertEqual(tokenizer.model_max_length						,			512	)
            self.assertEqual(tokenizer.vocab_size						,			30000	)
            self.assertEqual(tokenizer.unk_token						,			"[UNK]"	)
            self.assertEqual(tokenizer.padding_side						,			"right"	)
            self.assertEqual(tokenizer.truncation_side						,			"right"	)





      def 		__A	(   self							:				Any	):
            A_						  =				AutoTokenizer.from_pretrained(UpperCAmelCase	)
            self.assertIsInstance(UpperCAmelCase						,			(BertTokenizer, BertTokenizerFast)	)
            with tempfile.TemporaryDirectory() as tmp_dir:
                  tokenizer.save_pretrained(UpperCAmelCase	)
                  A_						  =				AutoTokenizer.from_pretrained(UpperCAmelCase	)

            self.assertIsInstance(UpperCAmelCase						,			tokenizer.__class__	)
            self.assertEqual(tokenizera.vocab_size						,			12	)





      def 		__A	(   self							:				Any	):
            A_						  =				AutoTokenizer.from_pretrained("ctrl"	)
            # There is no fast CTRL so this always gives us a slow tokenizer.
            self.assertIsInstance(UpperCAmelCase						,			UpperCAmelCase	)





      def 		__A	(   self							:				int	):
            # Check we can load the tokenizer config of an online model.
            A_						  =				get_tokenizer_config("bert-base-cased"	)
            A_						  =				config.pop("_commit_hash"						,			UpperCAmelCase	)
            # If we ever update bert-base-cased tokenizer config, this dict here will need to be updated.
            self.assertEqual(UpperCAmelCase						,			{"do_lower_case": False}	)

            # This model does not have a tokenizer_config so we get back an empty dict.
            A_						  =				get_tokenizer_config(UpperCAmelCase	)
            self.assertDictEqual(UpperCAmelCase						,			{}	)

            # A tokenizer saved with `save_pretrained` always creates a tokenizer config.
            A_						  =				AutoTokenizer.from_pretrained(UpperCAmelCase	)
            with tempfile.TemporaryDirectory() as tmp_dir:
                  tokenizer.save_pretrained(UpperCAmelCase	)
                  A_						  =				get_tokenizer_config(UpperCAmelCase	)

            # Check the class of the tokenizer was properly saved (note that it always saves the slow class).
            self.assertEqual(config["tokenizer_class"]						,			"BertTokenizer"	)





      def 		__A	(   self							:				Any	):
            try:
                  AutoConfig.register("custom"						,			UpperCAmelCase	)

                  AutoTokenizer.register(UpperCAmelCase						,			slow_tokenizer_class=UpperCAmelCase	)
                  # Trying to register something existing in the Transformers library will raise an error
                  with self.assertRaises(UpperCAmelCase	):
                        AutoTokenizer.register(UpperCAmelCase						,			slow_tokenizer_class=UpperCAmelCase	)

                  A_						  =				CustomTokenizer.from_pretrained(UpperCAmelCase	)
                  with tempfile.TemporaryDirectory() as tmp_dir:
                        tokenizer.save_pretrained(UpperCAmelCase	)

                        A_						  =				AutoTokenizer.from_pretrained(UpperCAmelCase	)
                        self.assertIsInstance(UpperCAmelCase						,			UpperCAmelCase	)

            finally:
                  if "custom" in CONFIG_MAPPING._extra_content:
                        del CONFIG_MAPPING._extra_content["custom"]
                  if CustomConfig in TOKENIZER_MAPPING._extra_content:
                        del TOKENIZER_MAPPING._extra_content[CustomConfig]





      @require_tokenizers
      def 		__A	(   self							:				Optional[int]	):
            try:
                  AutoConfig.register("custom"						,			UpperCAmelCase	)

                  # Can register in two steps
                  AutoTokenizer.register(UpperCAmelCase						,			slow_tokenizer_class=UpperCAmelCase	)
                  self.assertEqual(TOKENIZER_MAPPING[CustomConfig]						,			(CustomTokenizer, None)	)
                  AutoTokenizer.register(UpperCAmelCase						,			fast_tokenizer_class=UpperCAmelCase	)
                  self.assertEqual(TOKENIZER_MAPPING[CustomConfig]						,			(CustomTokenizer, CustomTokenizerFast)	)

                  del TOKENIZER_MAPPING._extra_content[CustomConfig]
                  # Can register in one step
                  AutoTokenizer.register(
                      UpperCAmelCase						,			slow_tokenizer_class=UpperCAmelCase						,			fast_tokenizer_class=UpperCAmelCase	)
                  self.assertEqual(TOKENIZER_MAPPING[CustomConfig]						,			(CustomTokenizer, CustomTokenizerFast)	)

                  # Trying to register something existing in the Transformers library will raise an error
                  with self.assertRaises(UpperCAmelCase	):
                        AutoTokenizer.register(UpperCAmelCase						,			fast_tokenizer_class=UpperCAmelCase	)

                  # We pass through a bert tokenizer fast cause there is no converter slow to fast for our new toknizer
                  # and that model does not have a tokenizer.json
                  with tempfile.TemporaryDirectory() as tmp_dir:
                        A_						  =				BertTokenizerFast.from_pretrained(UpperCAmelCase	)
                        bert_tokenizer.save_pretrained(UpperCAmelCase	)
                        A_						  =				CustomTokenizerFast.from_pretrained(UpperCAmelCase	)

                  with tempfile.TemporaryDirectory() as tmp_dir:
                        tokenizer.save_pretrained(UpperCAmelCase	)

                        A_						  =				AutoTokenizer.from_pretrained(UpperCAmelCase	)
                        self.assertIsInstance(UpperCAmelCase						,			UpperCAmelCase	)

                        A_						  =				AutoTokenizer.from_pretrained(UpperCAmelCase						,			use_fast=UpperCAmelCase	)
                        self.assertIsInstance(UpperCAmelCase						,			UpperCAmelCase	)

            finally:
                  if "custom" in CONFIG_MAPPING._extra_content:
                        del CONFIG_MAPPING._extra_content["custom"]
                  if CustomConfig in TOKENIZER_MAPPING._extra_content:
                        del TOKENIZER_MAPPING._extra_content[CustomConfig]





      def 		__A	(   self							:				Tuple	):
            # If remote code is not set, we will time out when asking whether to load the model.
            with self.assertRaises(UpperCAmelCase	):
                  A_						  =				AutoTokenizer.from_pretrained("hf-internal-testing/test_dynamic_tokenizer"	)
            # If remote code is disabled, we can't load this config.
            with self.assertRaises(UpperCAmelCase	):
                  A_						  =				AutoTokenizer.from_pretrained(
                      "hf-internal-testing/test_dynamic_tokenizer"						,			trust_remote_code=UpperCAmelCase	)

            A_						  =				AutoTokenizer.from_pretrained("hf-internal-testing/test_dynamic_tokenizer"						,			trust_remote_code=UpperCAmelCase	)
            self.assertTrue(tokenizer.special_attribute_present	)
            # Test tokenizer can be reloaded.
            with tempfile.TemporaryDirectory() as tmp_dir:
                  tokenizer.save_pretrained(UpperCAmelCase	)
                  A_						  =				AutoTokenizer.from_pretrained(UpperCAmelCase						,			trust_remote_code=UpperCAmelCase	)
            self.assertTrue(reloaded_tokenizer.special_attribute_present	)

            if is_tokenizers_available():
                  self.assertEqual(tokenizer.__class__.__name__						,			"NewTokenizerFast"	)
                  self.assertEqual(reloaded_tokenizer.__class__.__name__						,			"NewTokenizerFast"	)

                  # Test we can also load the slow version
                  A_						  =				AutoTokenizer.from_pretrained(
                      "hf-internal-testing/test_dynamic_tokenizer"						,			trust_remote_code=UpperCAmelCase						,			use_fast=UpperCAmelCase	)
                  self.assertTrue(tokenizer.special_attribute_present	)
                  self.assertEqual(tokenizer.__class__.__name__						,			"NewTokenizer"	)
                  # Test tokenizer can be reloaded.
                  with tempfile.TemporaryDirectory() as tmp_dir:
                        tokenizer.save_pretrained(UpperCAmelCase	)
                        A_						  =				AutoTokenizer.from_pretrained(UpperCAmelCase						,			trust_remote_code=UpperCAmelCase						,			use_fast=UpperCAmelCase	)
                  self.assertEqual(reloaded_tokenizer.__class__.__name__						,			"NewTokenizer"	)
                  self.assertTrue(reloaded_tokenizer.special_attribute_present	)
            else:
                  self.assertEqual(tokenizer.__class__.__name__						,			"NewTokenizer"	)
                  self.assertEqual(reloaded_tokenizer.__class__.__name__						,			"NewTokenizer"	)





      @require_tokenizers
      def 		__A	(   self							:				Union[str, Any]	):

            class   _a					(			snake_case_				):




                  """simple docstring"""

                  _lowerCamelCase       :			Optional[int]      		=       False




            class   _a					(			snake_case_				):




                  """simple docstring"""

                  _lowerCamelCase       :			Optional[Any]      		=       NewTokenizer
                  _lowerCamelCase       :			Tuple      		=       False

            try:
                  AutoConfig.register("custom"						,			UpperCAmelCase	)
                  AutoTokenizer.register(UpperCAmelCase						,			slow_tokenizer_class=UpperCAmelCase	)
                  AutoTokenizer.register(UpperCAmelCase						,			fast_tokenizer_class=UpperCAmelCase	)
                  # If remote code is not set, the default is to use local
                  A_						  =				AutoTokenizer.from_pretrained("hf-internal-testing/test_dynamic_tokenizer"	)
                  self.assertEqual(tokenizer.__class__.__name__						,			"NewTokenizerFast"	)
                  self.assertFalse(tokenizer.special_attribute_present	)
                  A_						  =				AutoTokenizer.from_pretrained("hf-internal-testing/test_dynamic_tokenizer"						,			use_fast=UpperCAmelCase	)
                  self.assertEqual(tokenizer.__class__.__name__						,			"NewTokenizer"	)
                  self.assertFalse(tokenizer.special_attribute_present	)

                  # If remote code is disabled, we load the local one.
                  A_						  =				AutoTokenizer.from_pretrained(
                      "hf-internal-testing/test_dynamic_tokenizer"						,			trust_remote_code=UpperCAmelCase	)
                  self.assertEqual(tokenizer.__class__.__name__						,			"NewTokenizerFast"	)
                  self.assertFalse(tokenizer.special_attribute_present	)
                  A_						  =				AutoTokenizer.from_pretrained(
                      "hf-internal-testing/test_dynamic_tokenizer"						,			trust_remote_code=UpperCAmelCase						,			use_fast=UpperCAmelCase	)
                  self.assertEqual(tokenizer.__class__.__name__						,			"NewTokenizer"	)
                  self.assertFalse(tokenizer.special_attribute_present	)

                  # If remote is enabled, we load from the Hub
                  A_						  =				AutoTokenizer.from_pretrained(
                      "hf-internal-testing/test_dynamic_tokenizer"						,			trust_remote_code=UpperCAmelCase	)
                  self.assertEqual(tokenizer.__class__.__name__						,			"NewTokenizerFast"	)
                  self.assertTrue(tokenizer.special_attribute_present	)
                  A_						  =				AutoTokenizer.from_pretrained(
                      "hf-internal-testing/test_dynamic_tokenizer"						,			trust_remote_code=UpperCAmelCase						,			use_fast=UpperCAmelCase	)
                  self.assertEqual(tokenizer.__class__.__name__						,			"NewTokenizer"	)
                  self.assertTrue(tokenizer.special_attribute_present	)

            finally:
                  if "custom" in CONFIG_MAPPING._extra_content:
                        del CONFIG_MAPPING._extra_content["custom"]
                  if CustomConfig in TOKENIZER_MAPPING._extra_content:
                        del TOKENIZER_MAPPING._extra_content[CustomConfig]





      def 		__A	(   self							:				str	):
            A_						  =				AutoTokenizer.from_pretrained(
                "hf-internal-testing/test_dynamic_tokenizer_legacy"						,			trust_remote_code=UpperCAmelCase	)
            self.assertTrue(tokenizer.special_attribute_present	)
            if is_tokenizers_available():
                  self.assertEqual(tokenizer.__class__.__name__						,			"NewTokenizerFast"	)

                  # Test we can also load the slow version
                  A_						  =				AutoTokenizer.from_pretrained(
                      "hf-internal-testing/test_dynamic_tokenizer_legacy"						,			trust_remote_code=UpperCAmelCase						,			use_fast=UpperCAmelCase	)
                  self.assertTrue(tokenizer.special_attribute_present	)
                  self.assertEqual(tokenizer.__class__.__name__						,			"NewTokenizer"	)
            else:
                  self.assertEqual(tokenizer.__class__.__name__						,			"NewTokenizer"	)





      def 		__A	(   self							:				Tuple	):
            with self.assertRaisesRegex(
                UpperCAmelCase						,			"bert-base is not a local folder and is not a valid model identifier"	):
                  A_						  =				AutoTokenizer.from_pretrained("bert-base"	)





      def 		__A	(   self							:				List[str]	):
            with self.assertRaisesRegex(
                UpperCAmelCase						,			R"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"	):
                  A_						  =				AutoTokenizer.from_pretrained(UpperCAmelCase						,			revision="aaaaaa"	)







      def 		__A	(   self							:				List[Any]	):
            # Make sure we have cached the tokenizer.
            A_						  =				AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-bert"	)
            with RequestCounter() as counter:
                  A_						  =				AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-bert"	)
                  self.assertEqual(counter.get_request_count						,			0	)
                  self.assertEqual(counter.head_request_count						,			1	)
                  self.assertEqual(counter.other_request_count						,			0	)





'''simple docstring'''



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 YolosImageProcessor






class 							_a				(			unittest.TestCase ):





       '''simple docstring'''






       def __init__(      self,							A,							A=7,							A=3,							A=30,							A=400,							A=True,							A=None,							A=True,							A=[0.5, 0.5, 0.5],							A=[0.5, 0.5, 0.5],							A=True,							A=1 / 255,							A=True,							):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      List[str]    					=						size if size is not None else {'shortest_edge': 18, 'longest_edge': 1_333}
             SCREAMING_SNAKE_CASE   :      List[Any]    					=						parent
             SCREAMING_SNAKE_CASE   :      Dict    					=						batch_size
             SCREAMING_SNAKE_CASE   :      int    					=						num_channels
             SCREAMING_SNAKE_CASE   :      Tuple    					=						min_resolution
             SCREAMING_SNAKE_CASE   :      int    					=						max_resolution
             SCREAMING_SNAKE_CASE   :      Tuple    					=						do_resize
             SCREAMING_SNAKE_CASE   :      Tuple    					=						size
             SCREAMING_SNAKE_CASE   :      Any    					=						do_normalize
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						image_mean
             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						image_std
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						do_rescale
             SCREAMING_SNAKE_CASE   :      int    					=						rescale_factor
             SCREAMING_SNAKE_CASE   :      List[str]    					=						do_pad







       def        UpperCamelCase_							(      self  ):







             '''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        UpperCamelCase_							(      self,							A,							A=False  ):







             '''simple docstring'''







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

             else:
                   SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						[]
                   for image in image_inputs:
                         SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      Optional[int]    					=						self.get_expected_values([image]  )
                         expected_values.append((expected_height, expected_width)  )
                   SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						max(A,							key=lambda A  : item[0]  )[0]
                   SCREAMING_SNAKE_CASE   :      str    					=						max(A,							key=lambda A  : item[1]  )[1]

             return expected_height, expected_width






@require_torch
@require_vision
class 							_a				(			SCREAMING_SNAKE_CASE ,      unittest.TestCase ):





       '''simple docstring'''






       A						:				List[Any]        =						YolosImageProcessor if is_vision_available() else None







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      List[Any]    					=						YolosImageProcessingTester(self  )







       @property
       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             return self.image_processor_tester.prepare_image_processor_dict()







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Any    					=						self.image_processing_class(**self.image_processor_dict  )
             self.assertTrue(hasattr(A,							'image_mean'  )  )
             self.assertTrue(hasattr(A,							'image_std'  )  )
             self.assertTrue(hasattr(A,							'do_normalize'  )  )
             self.assertTrue(hasattr(A,							'do_resize'  )  )
             self.assertTrue(hasattr(A,							'size'  )  )







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						self.image_processing_class.from_dict(self.image_processor_dict  )
             self.assertEqual(image_processor.size,							{'shortest_edge': 18, 'longest_edge': 1_333}  )
             self.assertEqual(image_processor.do_pad,							A  )

             SCREAMING_SNAKE_CASE   :      str    					=						self.image_processing_class.from_dict(
                 self.image_processor_dict,							size=42,							max_size=84,							pad_and_return_pixel_mask=A  )
             self.assertEqual(image_processor.size,							{'shortest_edge': 42, 'longest_edge': 84}  )
             self.assertEqual(image_processor.do_pad,							A  )







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             pass







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      int    					=						self.image_processing_class(**self.image_processor_dict  )
             # create random PIL images
             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						prepare_image_inputs(self.image_processor_tester,							equal_resolution=A  )
             for image in image_inputs:
                   self.assertIsInstance(A,							Image.Image  )

             # Test not batched input
             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						image_processing(image_inputs[0],							return_tensors='pt'  ).pixel_values

             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      Any    					=						self.image_processor_tester.get_expected_values(A  )

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

             # Test batched
             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      Optional[int]    					=						self.image_processor_tester.get_expected_values(A,							batched=A  )

             SCREAMING_SNAKE_CASE   :      Tuple    					=						image_processing(A,							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        UpperCamelCase_							(      self  ):







             '''simple docstring'''







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

             # Test not batched input
             SCREAMING_SNAKE_CASE   :      int    					=						image_processing(image_inputs[0],							return_tensors='pt'  ).pixel_values

             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      List[str]    					=						self.image_processor_tester.get_expected_values(A  )

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

             # Test batched
             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						image_processing(A,							return_tensors='pt'  ).pixel_values

             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						self.image_processor_tester.get_expected_values(A,							batched=A  )

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







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						self.image_processing_class(**self.image_processor_dict  )
             # create random PyTorch tensors
             SCREAMING_SNAKE_CASE   :      int    					=						prepare_image_inputs(self.image_processor_tester,							equal_resolution=A,							torchify=A  )
             for image in image_inputs:
                   self.assertIsInstance(A,							torch.Tensor  )

             # Test not batched input
             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						image_processing(image_inputs[0],							return_tensors='pt'  ).pixel_values

             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      List[Any]    					=						self.image_processor_tester.get_expected_values(A  )

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

             # Test batched
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						image_processing(A,							return_tensors='pt'  ).pixel_values

             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      str    					=						self.image_processor_tester.get_expected_values(A,							batched=A  )

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







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Dict    					=						self.image_processing_class(**self.image_processor_dict  )
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						self.image_processing_class(do_resize=A,							do_normalize=A,							do_rescale=A  )
             # create random PyTorch tensors
             SCREAMING_SNAKE_CASE   :      int    					=						prepare_image_inputs(self.image_processor_tester,							equal_resolution=A,							torchify=A  )
             for image in image_inputs:
                   self.assertIsInstance(A,							torch.Tensor  )

             # Test whether the method "pad" and calling the image processor return the same tensors
             SCREAMING_SNAKE_CASE   :      List[str]    					=						image_processing_a.pad(A,							return_tensors='pt'  )
             SCREAMING_SNAKE_CASE   :      Dict    					=						image_processing_a(A,							return_tensors='pt'  )

             self.assertTrue(
                 torch.allclose(encoded_images_with_method['pixel_values'],							encoded_images['pixel_values'],							atol=1E-4  )  )







       @slow
       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'  )
             with open('./tests/fixtures/tests_samples/COCO/coco_annotations.txt',							'r'  ) as f:
                   SCREAMING_SNAKE_CASE   :      Dict    					=						json.loads(f.read()  )

             SCREAMING_SNAKE_CASE   :      Any    					=						{'image_id': 39_769, 'annotations': target}

             # encode them
             SCREAMING_SNAKE_CASE   :      Any    					=						YolosImageProcessor.from_pretrained('hustvl/yolos-small'  )
             SCREAMING_SNAKE_CASE   :      int    					=						image_processing(images=A,							annotations=A,							return_tensors='pt'  )

             # verify pixel values
             SCREAMING_SNAKE_CASE   :      List[str]    					=						torch.Size([1, 3, 800, 1_066]  )
             self.assertEqual(encoding['pixel_values'].shape,							A  )

             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						torch.tensor([0.27_96, 0.31_38, 0.34_81]  )
             self.assertTrue(torch.allclose(encoding['pixel_values'][0, 0, 0, :3],							A,							atol=1E-4  )  )

             # verify area
             SCREAMING_SNAKE_CASE   :      Tuple    					=						torch.tensor([58_87.96_00, 1_12_50.20_61, 48_93_53.84_38, 83_71_22.75_00, 14_79_67.51_56, 16_57_32.34_38]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['area'],							A  )  )
             # verify boxes
             SCREAMING_SNAKE_CASE   :      str    					=						torch.Size([6, 4]  )
             self.assertEqual(encoding['labels'][0]['boxes'].shape,							A  )
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						torch.tensor([0.55_03, 0.27_65, 0.06_04, 0.22_15]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['boxes'][0],							A,							atol=1E-3  )  )
             # verify image_id
             SCREAMING_SNAKE_CASE   :      Tuple    					=						torch.tensor([39_769]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['image_id'],							A  )  )
             # verify is_crowd
             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						torch.tensor([0, 0, 0, 0, 0, 0]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['iscrowd'],							A  )  )
             # verify class_labels
             SCREAMING_SNAKE_CASE   :      int    					=						torch.tensor([75, 75, 63, 65, 17, 17]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['class_labels'],							A  )  )
             # verify orig_size
             SCREAMING_SNAKE_CASE   :      Tuple    					=						torch.tensor([480, 640]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['orig_size'],							A  )  )
             # verify size
             SCREAMING_SNAKE_CASE   :      str    					=						torch.tensor([800, 1_066]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['size'],							A  )  )







       @slow
       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Tuple    					=						Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'  )
             with open('./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt',							'r'  ) as f:
                   SCREAMING_SNAKE_CASE   :      int    					=						json.loads(f.read()  )

             SCREAMING_SNAKE_CASE   :      List[Any]    					=						{'file_name': '000000039769.png', 'image_id': 39_769, 'segments_info': target}

             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						pathlib.Path('./tests/fixtures/tests_samples/COCO/coco_panoptic'  )

             # encode them
             SCREAMING_SNAKE_CASE   :      int    					=						YolosImageProcessor(format='coco_panoptic'  )
             SCREAMING_SNAKE_CASE   :      str    					=						image_processing(images=A,							annotations=A,							masks_path=A,							return_tensors='pt'  )

             # verify pixel values
             SCREAMING_SNAKE_CASE   :      List[str]    					=						torch.Size([1, 3, 800, 1_066]  )
             self.assertEqual(encoding['pixel_values'].shape,							A  )

             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						torch.tensor([0.27_96, 0.31_38, 0.34_81]  )
             self.assertTrue(torch.allclose(encoding['pixel_values'][0, 0, 0, :3],							A,							atol=1E-4  )  )

             # verify area
             SCREAMING_SNAKE_CASE   :      Tuple    					=						torch.tensor([14_79_79.68_75, 16_55_27.04_69, 48_46_38.59_38, 1_12_92.93_75, 58_79.65_62, 76_34.11_47]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['area'],							A  )  )
             # verify boxes
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						torch.Size([6, 4]  )
             self.assertEqual(encoding['labels'][0]['boxes'].shape,							A  )
             SCREAMING_SNAKE_CASE   :      Tuple    					=						torch.tensor([0.26_25, 0.54_37, 0.46_88, 0.86_25]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['boxes'][0],							A,							atol=1E-3  )  )
             # verify image_id
             SCREAMING_SNAKE_CASE   :      List[str]    					=						torch.tensor([39_769]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['image_id'],							A  )  )
             # verify is_crowd
             SCREAMING_SNAKE_CASE   :      Any    					=						torch.tensor([0, 0, 0, 0, 0, 0]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['iscrowd'],							A  )  )
             # verify class_labels
             SCREAMING_SNAKE_CASE   :      Any    					=						torch.tensor([17, 17, 63, 75, 75, 93]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['class_labels'],							A  )  )
             # verify masks
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						822_873
             self.assertEqual(encoding['labels'][0]['masks'].sum().item(),							A  )
             # verify orig_size
             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						torch.tensor([480, 640]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['orig_size'],							A  )  )
             # verify size
             SCREAMING_SNAKE_CASE   :      Tuple    					=						torch.tensor([800, 1_066]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['size'],							A  )  )


