"""simple docstring"""






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

from huggingface_hub import HfFolder, Repository, create_repo, delete_repo
from requests.exceptions import HTTPError

import transformers
from transformers import (
    CONFIG_MAPPING,
    FEATURE_EXTRACTOR_MAPPING,
    PROCESSOR_MAPPING,
    TOKENIZER_MAPPING,
    AutoConfig,
    AutoFeatureExtractor,
    AutoProcessor,
    AutoTokenizer,
    BertTokenizer,
    ProcessorMixin,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
    WavaVecaProcessor,
)
from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test
from transformers.tokenization_utils import TOKENIZER_CONFIG_FILE
from transformers.utils import FEATURE_EXTRACTOR_NAME, is_tokenizers_available


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

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402
from test_module.custom_processing import CustomProcessor  # noqa E402
from test_module.custom_tokenization import CustomTokenizer  # noqa E402


__A			   = get_tests_dir("fixtures/dummy_feature_extractor_config.json")
__A			   = get_tests_dir("fixtures/vocab.json")
__A			   = get_tests_dir("fixtures")


class     UpperCAmelCase						(unittest.TestCase  ):



    """simple docstring"""

    _UpperCAmelCase	:Tuple						    = ["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "bla", "blou"]






    def _snake_case			(   self							):
       lowercase__:   List[str]					=      0






    def _snake_case			(   self							):
       lowercase__:   List[str]					=      AutoProcessor.from_pretrained('''facebook/wav2vec2-base-960h'''							)
       self.assertIsInstance(_UpperCAmelCase    ,			_UpperCAmelCase							)






    def _snake_case			(   self							):
       with tempfile.TemporaryDirectory() as tmpdirname:
          lowercase__:   str					=      WavaVecaConfig()
          lowercase__:   Union[str, Any]					=      AutoProcessor.from_pretrained('''facebook/wav2vec2-base-960h'''							)

          # save in new folder
          model_config.save_pretrained(_UpperCAmelCase							)
          processor.save_pretrained(_UpperCAmelCase							)

          lowercase__:   Optional[int]					=      AutoProcessor.from_pretrained(_UpperCAmelCase							)

       self.assertIsInstance(_UpperCAmelCase    ,			_UpperCAmelCase							)






    def _snake_case			(   self							):
       with tempfile.TemporaryDirectory() as tmpdirname:
          # copy relevant files
          copyfile(_UpperCAmelCase    ,			os.path.join(_UpperCAmelCase    ,			_UpperCAmelCase							)							)
          copyfile(_UpperCAmelCase    ,			os.path.join(_UpperCAmelCase    ,			'''vocab.json'''							)							)

          lowercase__:   str					=      AutoProcessor.from_pretrained(_UpperCAmelCase							)

       self.assertIsInstance(_UpperCAmelCase    ,			_UpperCAmelCase							)






    def _snake_case			(   self							):
       with tempfile.TemporaryDirectory() as tmpdirname:
          lowercase__:   Union[str, Any]					=      WavaVecaFeatureExtractor()
          lowercase__:   List[str]					=      AutoTokenizer.from_pretrained('''facebook/wav2vec2-base-960h'''							)

          lowercase__:   Union[str, Any]					=      WavaVecaProcessor(_UpperCAmelCase    ,			_UpperCAmelCase							)

          # save in new folder
          processor.save_pretrained(_UpperCAmelCase							)

          # drop `processor_class` in tokenizer
          with open(os.path.join(_UpperCAmelCase    ,			_UpperCAmelCase							)    ,			'''r'''							) as f:
             lowercase__:   Dict					=      json.load(_UpperCAmelCase							)
             config_dict.pop('''processor_class'''							)

          with open(os.path.join(_UpperCAmelCase    ,			_UpperCAmelCase							)    ,			'''w'''							) as f:
             f.write(json.dumps(_UpperCAmelCase							)							)

          lowercase__:   str					=      AutoProcessor.from_pretrained(_UpperCAmelCase							)

       self.assertIsInstance(_UpperCAmelCase    ,			_UpperCAmelCase							)






    def _snake_case			(   self							):
       with tempfile.TemporaryDirectory() as tmpdirname:
          lowercase__:   Union[str, Any]					=      WavaVecaFeatureExtractor()
          lowercase__:   Any					=      AutoTokenizer.from_pretrained('''facebook/wav2vec2-base-960h'''							)

          lowercase__:   Optional[int]					=      WavaVecaProcessor(_UpperCAmelCase    ,			_UpperCAmelCase							)

          # save in new folder
          processor.save_pretrained(_UpperCAmelCase							)

          # drop `processor_class` in feature extractor
          with open(os.path.join(_UpperCAmelCase    ,			_UpperCAmelCase							)    ,			'''r'''							) as f:
             lowercase__:   List[str]					=      json.load(_UpperCAmelCase							)
             config_dict.pop('''processor_class'''							)

          with open(os.path.join(_UpperCAmelCase    ,			_UpperCAmelCase							)    ,			'''w'''							) as f:
             f.write(json.dumps(_UpperCAmelCase							)							)

          lowercase__:   Union[str, Any]					=      AutoProcessor.from_pretrained(_UpperCAmelCase							)

       self.assertIsInstance(_UpperCAmelCase    ,			_UpperCAmelCase							)






    def _snake_case			(   self							):
       with tempfile.TemporaryDirectory() as tmpdirname:
          lowercase__:   Union[str, Any]					=      WavaVecaConfig(processor_class='''Wav2Vec2Processor'''							)
          model_config.save_pretrained(_UpperCAmelCase							)
          # copy relevant files
          copyfile(_UpperCAmelCase    ,			os.path.join(_UpperCAmelCase    ,			'''vocab.json'''							)							)
          # create emtpy sample processor
          with open(os.path.join(_UpperCAmelCase    ,			_UpperCAmelCase							)    ,			'''w'''							) as f:
             f.write('''{}'''							)

          lowercase__:   List[str]					=      AutoProcessor.from_pretrained(_UpperCAmelCase							)

       self.assertIsInstance(_UpperCAmelCase    ,			_UpperCAmelCase							)






    def _snake_case			(   self							):
       # If remote code is not set, we will time out when asking whether to load the model.
       with self.assertRaises(_UpperCAmelCase							):
          lowercase__:   Any					=      AutoProcessor.from_pretrained('''hf-internal-testing/test_dynamic_processor'''							)
       # If remote code is disabled, we can't load this config.
       with self.assertRaises(_UpperCAmelCase							):
          lowercase__:   Tuple					=      AutoProcessor.from_pretrained(
              '''hf-internal-testing/test_dynamic_processor'''    ,			trust_remote_code=_UpperCAmelCase							)

       lowercase__:   str					=      AutoProcessor.from_pretrained('''hf-internal-testing/test_dynamic_processor'''    ,			trust_remote_code=_UpperCAmelCase							)
       self.assertTrue(processor.special_attribute_present							)
       self.assertEqual(processor.__class__.__name__    ,			'''NewProcessor'''							)

       lowercase__:   List[str]					=      processor.feature_extractor
       self.assertTrue(feature_extractor.special_attribute_present							)
       self.assertEqual(feature_extractor.__class__.__name__    ,			'''NewFeatureExtractor'''							)

       lowercase__:   Optional[Any]					=      processor.tokenizer
       self.assertTrue(tokenizer.special_attribute_present							)
       if is_tokenizers_available():
          self.assertEqual(tokenizer.__class__.__name__    ,			'''NewTokenizerFast'''							)

          # Test we can also load the slow version
          lowercase__:   Any					=      AutoProcessor.from_pretrained(
              '''hf-internal-testing/test_dynamic_processor'''    ,			trust_remote_code=_UpperCAmelCase    ,			use_fast=_UpperCAmelCase							)
          lowercase__:   Tuple					=      new_processor.tokenizer
          self.assertTrue(new_tokenizer.special_attribute_present							)
          self.assertEqual(new_tokenizer.__class__.__name__    ,			'''NewTokenizer'''							)
       else:
          self.assertEqual(tokenizer.__class__.__name__    ,			'''NewTokenizer'''							)






    def _snake_case			(   self							):
       try:
          AutoConfig.register('''custom'''    ,			_UpperCAmelCase							)
          AutoFeatureExtractor.register(_UpperCAmelCase    ,			_UpperCAmelCase							)
          AutoTokenizer.register(_UpperCAmelCase    ,			slow_tokenizer_class=_UpperCAmelCase							)
          AutoProcessor.register(_UpperCAmelCase    ,			_UpperCAmelCase							)
          # Trying to register something existing in the Transformers library will raise an error
          with self.assertRaises(_UpperCAmelCase							):
             AutoProcessor.register(_UpperCAmelCase    ,			_UpperCAmelCase							)

          # Now that the config is registered, it can be used as any other config with the auto-API
          lowercase__:   Union[str, Any]					=      CustomFeatureExtractor.from_pretrained(_UpperCAmelCase							)

          with tempfile.TemporaryDirectory() as tmp_dir:
             lowercase__:   List[str]					=      os.path.join(_UpperCAmelCase    ,			'''vocab.txt'''							)
             with open(_UpperCAmelCase    ,			'''w'''    ,			encoding='''utf-8'''							) as vocab_writer:
                vocab_writer.write(''''''.join([x + '''\n''' for x in self.vocab_tokens]							)							)
             lowercase__:   Tuple					=      CustomTokenizer(_UpperCAmelCase							)

          lowercase__:   Optional[int]					=      CustomProcessor(_UpperCAmelCase    ,			_UpperCAmelCase							)

          with tempfile.TemporaryDirectory() as tmp_dir:
             processor.save_pretrained(_UpperCAmelCase							)
             lowercase__:   List[str]					=      AutoProcessor.from_pretrained(_UpperCAmelCase							)
             self.assertIsInstance(_UpperCAmelCase    ,			_UpperCAmelCase							)

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






    def _snake_case			(   self							):


       class     UpperCAmelCase						(_UpperCAmelCase  ):



           """simple docstring"""

           _UpperCAmelCase	:Union[str, Any]						    = False



       class     UpperCAmelCase						(_UpperCAmelCase  ):



           """simple docstring"""

           _UpperCAmelCase	:str						    = False



       class     UpperCAmelCase						(_UpperCAmelCase  ):



           """simple docstring"""

           _UpperCAmelCase	:Union[str, Any]						    = "AutoFeatureExtractor"
           _UpperCAmelCase	:int						    = "AutoTokenizer"
           _UpperCAmelCase	:Optional[int]						    = False

       try:
          AutoConfig.register('''custom'''    ,			_UpperCAmelCase							)
          AutoFeatureExtractor.register(_UpperCAmelCase    ,			_UpperCAmelCase							)
          AutoTokenizer.register(_UpperCAmelCase    ,			slow_tokenizer_class=_UpperCAmelCase							)
          AutoProcessor.register(_UpperCAmelCase    ,			_UpperCAmelCase							)
          # If remote code is not set, the default is to use local classes.
          lowercase__:   int					=      AutoProcessor.from_pretrained('''hf-internal-testing/test_dynamic_processor'''							)
          self.assertEqual(processor.__class__.__name__    ,			'''NewProcessor'''							)
          self.assertFalse(processor.special_attribute_present							)
          self.assertFalse(processor.feature_extractor.special_attribute_present							)
          self.assertFalse(processor.tokenizer.special_attribute_present							)

          # If remote code is disabled, we load the local ones.
          lowercase__:   Tuple					=      AutoProcessor.from_pretrained(
              '''hf-internal-testing/test_dynamic_processor'''    ,			trust_remote_code=_UpperCAmelCase							)
          self.assertEqual(processor.__class__.__name__    ,			'''NewProcessor'''							)
          self.assertFalse(processor.special_attribute_present							)
          self.assertFalse(processor.feature_extractor.special_attribute_present							)
          self.assertFalse(processor.tokenizer.special_attribute_present							)

          # If remote is enabled, we load from the Hub.
          lowercase__:   List[str]					=      AutoProcessor.from_pretrained(
              '''hf-internal-testing/test_dynamic_processor'''    ,			trust_remote_code=_UpperCAmelCase							)
          self.assertEqual(processor.__class__.__name__    ,			'''NewProcessor'''							)
          self.assertTrue(processor.special_attribute_present							)
          self.assertTrue(processor.feature_extractor.special_attribute_present							)
          self.assertTrue(processor.tokenizer.special_attribute_present							)

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






    def _snake_case			(   self							):
       lowercase__:   str					=      AutoProcessor.from_pretrained('''hf-internal-testing/tiny-random-bert'''							)
       self.assertEqual(processor.__class__.__name__    ,			'''BertTokenizerFast'''							)






    def _snake_case			(   self							):
       lowercase__:   Any					=      AutoProcessor.from_pretrained('''hf-internal-testing/tiny-random-convnext'''							)
       self.assertEqual(processor.__class__.__name__    ,			'''ConvNextImageProcessor'''							)



@is_staging_test
class     UpperCAmelCase						(unittest.TestCase  ):



    """simple docstring"""

    _UpperCAmelCase	:List[Any]						    = ["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "bla", "blou"]






    @classmethod
    def _snake_case			(   cls							):
       lowercase__:   List[Any]					=      TOKEN
       HfFolder.save_token(_UpperCAmelCase							)






    @classmethod
    def _snake_case			(   cls							):
       try:
          delete_repo(token=cls._token    ,			repo_id='''test-processor'''							)
       except HTTPError:
          pass

       try:
          delete_repo(token=cls._token    ,			repo_id='''valid_org/test-processor-org'''							)
       except HTTPError:
          pass

       try:
          delete_repo(token=cls._token    ,			repo_id='''test-dynamic-processor'''							)
       except HTTPError:
          pass






    def _snake_case			(   self							):
       lowercase__:   Optional[Any]					=      WavaVecaProcessor.from_pretrained(_UpperCAmelCase							)
       with tempfile.TemporaryDirectory() as tmp_dir:
          processor.save_pretrained(
              os.path.join(_UpperCAmelCase    ,			'''test-processor'''							)    ,			push_to_hub=_UpperCAmelCase    ,			use_auth_token=self._token							)

          lowercase__:   List[Any]					=      WavaVecaProcessor.from_pretrained(F"""{USER}/test-processor"""							)
          for k, v in processor.feature_extractor.__dict__.items():
             self.assertEqual(_UpperCAmelCase    ,			getattr(new_processor.feature_extractor    ,			_UpperCAmelCase							)							)
          self.assertDictEqual(new_processor.tokenizer.get_vocab()    ,			processor.tokenizer.get_vocab()							)






    def _snake_case			(   self							):
       lowercase__:   Optional[Any]					=      WavaVecaProcessor.from_pretrained(_UpperCAmelCase							)

       with tempfile.TemporaryDirectory() as tmp_dir:
          processor.save_pretrained(
              os.path.join(_UpperCAmelCase    ,			'''test-processor-org'''							)    ,			push_to_hub=_UpperCAmelCase    ,			use_auth_token=self._token    ,			organization='''valid_org'''    ,			)

          lowercase__:   List[str]					=      WavaVecaProcessor.from_pretrained('''valid_org/test-processor-org'''							)
          for k, v in processor.feature_extractor.__dict__.items():
             self.assertEqual(_UpperCAmelCase    ,			getattr(new_processor.feature_extractor    ,			_UpperCAmelCase							)							)
          self.assertDictEqual(new_processor.tokenizer.get_vocab()    ,			processor.tokenizer.get_vocab()							)






    def _snake_case			(   self							):
       CustomFeatureExtractor.register_for_auto_class()
       CustomTokenizer.register_for_auto_class()
       CustomProcessor.register_for_auto_class()

       lowercase__:   Optional[Any]					=      CustomFeatureExtractor.from_pretrained(_UpperCAmelCase							)

       with tempfile.TemporaryDirectory() as tmp_dir:
          lowercase__:   Optional[Any]					=      os.path.join(_UpperCAmelCase    ,			'''vocab.txt'''							)
          with open(_UpperCAmelCase    ,			'''w'''    ,			encoding='''utf-8'''							) as vocab_writer:
             vocab_writer.write(''''''.join([x + '''\n''' for x in self.vocab_tokens]							)							)
          lowercase__:   List[Any]					=      CustomTokenizer(_UpperCAmelCase							)

       lowercase__:   Any					=      CustomProcessor(_UpperCAmelCase    ,			_UpperCAmelCase							)

       with tempfile.TemporaryDirectory() as tmp_dir:
          create_repo(F"""{USER}/test-dynamic-processor"""    ,			token=self._token							)
          lowercase__:   List[Any]					=      Repository(_UpperCAmelCase    ,			clone_from=F"""{USER}/test-dynamic-processor"""    ,			token=self._token							)
          processor.save_pretrained(_UpperCAmelCase							)

          # This has added the proper auto_map field to the feature extractor config
          self.assertDictEqual(
              processor.feature_extractor.auto_map    ,			{
                  '''AutoFeatureExtractor''': '''custom_feature_extraction.CustomFeatureExtractor''',
                  '''AutoProcessor''': '''custom_processing.CustomProcessor''',
              }    ,			)

          # This has added the proper auto_map field to the tokenizer config
          with open(os.path.join(_UpperCAmelCase    ,			'''tokenizer_config.json'''							)							) as f:
             lowercase__:   int					=      json.load(_UpperCAmelCase							)
          self.assertDictEqual(
              tokenizer_config['''auto_map''']    ,			{
                  '''AutoTokenizer''': ['''custom_tokenization.CustomTokenizer''', None],
                  '''AutoProcessor''': '''custom_processing.CustomProcessor''',
              }    ,			)

          # The code has been copied from fixtures
          self.assertTrue(os.path.isfile(os.path.join(_UpperCAmelCase    ,			'''custom_feature_extraction.py'''							)							)							)
          self.assertTrue(os.path.isfile(os.path.join(_UpperCAmelCase    ,			'''custom_tokenization.py'''							)							)							)
          self.assertTrue(os.path.isfile(os.path.join(_UpperCAmelCase    ,			'''custom_processing.py'''							)							)							)

          repo.push_to_hub()

       lowercase__:   Any					=      AutoProcessor.from_pretrained(F"""{USER}/test-dynamic-processor"""    ,			trust_remote_code=_UpperCAmelCase							)
       # Can't make an isinstance check because the new_processor is from the CustomProcessor class of a dynamic module
       self.assertEqual(new_processor.__class__.__name__    ,			'''CustomProcessor'''							)

"""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     UpperCAmelCase						(unittest.TestCase  ):



    """simple docstring"""

    def __init__(   self    ,			_UpperCAmelCase    ,			_UpperCAmelCase=7    ,			_UpperCAmelCase=3    ,			_UpperCAmelCase=30    ,			_UpperCAmelCase=400    ,			_UpperCAmelCase=True    ,			_UpperCAmelCase=None    ,			_UpperCAmelCase=True    ,			_UpperCAmelCase=[0.5, 0.5, 0.5]    ,			_UpperCAmelCase=[0.5, 0.5, 0.5]    ,			_UpperCAmelCase=True    ,			_UpperCAmelCase=1 / 255    ,			_UpperCAmelCase=True    ,			):
       # by setting size["longest_edge"] > max_resolution we're effectively not testing this :p
       lowercase__:   str					=      size if size is not None else {'''shortest_edge''': 18, '''longest_edge''': 1333}
       lowercase__:   Optional[Any]					=      parent
       lowercase__:   List[Any]					=      batch_size
       lowercase__:   Tuple					=      num_channels
       lowercase__:   Optional[Any]					=      min_resolution
       lowercase__:   Dict					=      max_resolution
       lowercase__:   Optional[int]					=      do_resize
       lowercase__:   Any					=      size
       lowercase__:   Optional[Any]					=      do_normalize
       lowercase__:   Union[str, Any]					=      image_mean
       lowercase__:   Tuple					=      image_std
       lowercase__:   str					=      do_rescale
       lowercase__:   Any					=      rescale_factor
       lowercase__:   List[Any]					=      do_pad






    def _snake_case			(   self							):
       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 _snake_case			(   self    ,			_UpperCAmelCase    ,			_UpperCAmelCase=False							):
       if not batched:
          lowercase__:   Optional[Any]					=      image_inputs[0]
          if isinstance(_UpperCAmelCase    ,			Image.Image							):
             lowercase__, lowercase__:   Dict					=      image.size
          else:
             lowercase__, lowercase__:   Optional[Any]					=      image.shape[1], image.shape[2]
          if w < h:
             lowercase__:   List[str]					=      int(self.size['''shortest_edge'''] * h / w							)
             lowercase__:   Union[str, Any]					=      self.size['''shortest_edge''']
          elif w > h:
             lowercase__:   int					=      self.size['''shortest_edge''']
             lowercase__:   int					=      int(self.size['''shortest_edge'''] * w / h							)
          else:
             lowercase__:   Union[str, Any]					=      self.size['''shortest_edge''']
             lowercase__:   Union[str, Any]					=      self.size['''shortest_edge''']

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

       return expected_height, expected_width



@require_torch
@require_vision
class     UpperCAmelCase						(_UpperCAmelCase    ,unittest.TestCase  ):



    """simple docstring"""

    _UpperCAmelCase	:Optional[int]						    = YolosImageProcessor if is_vision_available() else None






    def _snake_case			(   self							):
       lowercase__:   int					=      YolosImageProcessingTester(self							)






    @property
    def _snake_case			(   self							):
       return self.image_processor_tester.prepare_image_processor_dict()






    def _snake_case			(   self							):
       lowercase__:   List[str]					=      self.image_processing_class(**self.image_processor_dict							)
       self.assertTrue(hasattr(_UpperCAmelCase    ,			'''image_mean'''							)							)
       self.assertTrue(hasattr(_UpperCAmelCase    ,			'''image_std'''							)							)
       self.assertTrue(hasattr(_UpperCAmelCase    ,			'''do_normalize'''							)							)
       self.assertTrue(hasattr(_UpperCAmelCase    ,			'''do_resize'''							)							)
       self.assertTrue(hasattr(_UpperCAmelCase    ,			'''size'''							)							)






    def _snake_case			(   self							):
       lowercase__:   Any					=      self.image_processing_class.from_dict(self.image_processor_dict							)
       self.assertEqual(image_processor.size    ,			{'''shortest_edge''': 18, '''longest_edge''': 1333}							)
       self.assertEqual(image_processor.do_pad    ,			_UpperCAmelCase							)

       lowercase__:   Any					=      self.image_processing_class.from_dict(
           self.image_processor_dict    ,			size=42    ,			max_size=84    ,			pad_and_return_pixel_mask=_UpperCAmelCase							)
       self.assertEqual(image_processor.size    ,			{'''shortest_edge''': 42, '''longest_edge''': 84}							)
       self.assertEqual(image_processor.do_pad    ,			_UpperCAmelCase							)






    def _snake_case			(   self							):
       pass






    def _snake_case			(   self							):
       # Initialize image_processing
       lowercase__:   Union[str, Any]					=      self.image_processing_class(**self.image_processor_dict							)
       # create random PIL images
       lowercase__:   Dict					=      prepare_image_inputs(self.image_processor_tester    ,			equal_resolution=_UpperCAmelCase							)
       for image in image_inputs:
          self.assertIsInstance(_UpperCAmelCase    ,			Image.Image							)

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

       lowercase__, lowercase__:   Optional[int]					=      self.image_processor_tester.get_expected_values(_UpperCAmelCase							)

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

       # Test batched
       lowercase__, lowercase__:   Any					=      self.image_processor_tester.get_expected_values(_UpperCAmelCase    ,			batched=_UpperCAmelCase							)

       lowercase__:   int					=      image_processing(_UpperCAmelCase    ,			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 _snake_case			(   self							):
       # Initialize image_processing
       lowercase__:   List[str]					=      self.image_processing_class(**self.image_processor_dict							)
       # create random numpy tensors
       lowercase__:   Optional[Any]					=      prepare_image_inputs(self.image_processor_tester    ,			equal_resolution=_UpperCAmelCase    ,			numpify=_UpperCAmelCase							)
       for image in image_inputs:
          self.assertIsInstance(_UpperCAmelCase    ,			np.ndarray							)

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

       lowercase__, lowercase__:   str					=      self.image_processor_tester.get_expected_values(_UpperCAmelCase							)

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

       # Test batched
       lowercase__:   Dict					=      image_processing(_UpperCAmelCase    ,			return_tensors='''pt'''							).pixel_values

       lowercase__, lowercase__:   str					=      self.image_processor_tester.get_expected_values(_UpperCAmelCase    ,			batched=_UpperCAmelCase							)

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






    def _snake_case			(   self							):
       # Initialize image_processing
       lowercase__:   Any					=      self.image_processing_class(**self.image_processor_dict							)
       # create random PyTorch tensors
       lowercase__:   Optional[Any]					=      prepare_image_inputs(self.image_processor_tester    ,			equal_resolution=_UpperCAmelCase    ,			torchify=_UpperCAmelCase							)
       for image in image_inputs:
          self.assertIsInstance(_UpperCAmelCase    ,			torch.Tensor							)

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

       lowercase__, lowercase__:   int					=      self.image_processor_tester.get_expected_values(_UpperCAmelCase							)

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

       # Test batched
       lowercase__:   List[str]					=      image_processing(_UpperCAmelCase    ,			return_tensors='''pt'''							).pixel_values

       lowercase__, lowercase__:   List[Any]					=      self.image_processor_tester.get_expected_values(_UpperCAmelCase    ,			batched=_UpperCAmelCase							)

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






    def _snake_case			(   self							):
       # Initialize image_processings
       lowercase__:   Union[str, Any]					=      self.image_processing_class(**self.image_processor_dict							)
       lowercase__:   Optional[Any]					=      self.image_processing_class(do_resize=_UpperCAmelCase    ,			do_normalize=_UpperCAmelCase    ,			do_rescale=_UpperCAmelCase							)
       # create random PyTorch tensors
       lowercase__:   Optional[Any]					=      prepare_image_inputs(self.image_processor_tester    ,			equal_resolution=_UpperCAmelCase    ,			torchify=_UpperCAmelCase							)
       for image in image_inputs:
          self.assertIsInstance(_UpperCAmelCase    ,			torch.Tensor							)

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

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






    @slow
    def _snake_case			(   self							):
       # prepare image and target
       lowercase__:   Tuple					=      Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''							)
       with open('''./tests/fixtures/tests_samples/COCO/coco_annotations.txt'''    ,			'''r'''							) as f:
          lowercase__:   Any					=      json.loads(f.read()							)

       lowercase__:   Dict					=      {'''image_id''': 39769, '''annotations''': target}

       # encode them
       lowercase__:   Dict					=      YolosImageProcessor.from_pretrained('''hustvl/yolos-small'''							)
       lowercase__:   Any					=      image_processing(images=_UpperCAmelCase    ,			annotations=_UpperCAmelCase    ,			return_tensors='''pt'''							)

       # verify pixel values
       lowercase__:   Optional[Any]					=      torch.Size([1, 3, 800, 1066]							)
       self.assertEqual(encoding['''pixel_values'''].shape    ,			_UpperCAmelCase							)

       lowercase__:   Optional[Any]					=      torch.tensor([0.2_796, 0.3_138, 0.3_481]							)
       self.assertTrue(torch.allclose(encoding['''pixel_values'''][0, 0, 0, :3]    ,			_UpperCAmelCase    ,			atol=1e-4							)							)

       # verify area
       lowercase__:   Tuple					=      torch.tensor([5_887.9_600, 11_250.2_061, 489_353.8_438, 837_122.7_500, 147_967.5_156, 165_732.3_438]							)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''area''']    ,			_UpperCAmelCase							)							)
       # verify boxes
       lowercase__:   str					=      torch.Size([6, 4]							)
       self.assertEqual(encoding['''labels'''][0]['''boxes'''].shape    ,			_UpperCAmelCase							)
       lowercase__:   List[Any]					=      torch.tensor([0.5_503, 0.2_765, 0.0_604, 0.2_215]							)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''boxes'''][0]    ,			_UpperCAmelCase    ,			atol=1e-3							)							)
       # verify image_id
       lowercase__:   Optional[int]					=      torch.tensor([39769]							)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''image_id''']    ,			_UpperCAmelCase							)							)
       # verify is_crowd
       lowercase__:   Optional[int]					=      torch.tensor([0, 0, 0, 0, 0, 0]							)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''iscrowd''']    ,			_UpperCAmelCase							)							)
       # verify class_labels
       lowercase__:   Dict					=      torch.tensor([75, 75, 63, 65, 17, 17]							)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''class_labels''']    ,			_UpperCAmelCase							)							)
       # verify orig_size
       lowercase__:   List[Any]					=      torch.tensor([480, 640]							)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''orig_size''']    ,			_UpperCAmelCase							)							)
       # verify size
       lowercase__:   List[Any]					=      torch.tensor([800, 1066]							)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''size''']    ,			_UpperCAmelCase							)							)






    @slow
    def _snake_case			(   self							):
       # prepare image, target and masks_path
       lowercase__:   str					=      Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''							)
       with open('''./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt'''    ,			'''r'''							) as f:
          lowercase__:   str					=      json.loads(f.read()							)

       lowercase__:   List[Any]					=      {'''file_name''': '''000000039769.png''', '''image_id''': 39769, '''segments_info''': target}

       lowercase__:   Dict					=      pathlib.Path('''./tests/fixtures/tests_samples/COCO/coco_panoptic'''							)

       # encode them
       lowercase__:   Union[str, Any]					=      YolosImageProcessor(format='''coco_panoptic'''							)
       lowercase__:   Optional[Any]					=      image_processing(images=_UpperCAmelCase    ,			annotations=_UpperCAmelCase    ,			masks_path=_UpperCAmelCase    ,			return_tensors='''pt'''							)

       # verify pixel values
       lowercase__:   Optional[int]					=      torch.Size([1, 3, 800, 1066]							)
       self.assertEqual(encoding['''pixel_values'''].shape    ,			_UpperCAmelCase							)

       lowercase__:   Dict					=      torch.tensor([0.2_796, 0.3_138, 0.3_481]							)
       self.assertTrue(torch.allclose(encoding['''pixel_values'''][0, 0, 0, :3]    ,			_UpperCAmelCase    ,			atol=1e-4							)							)

       # verify area
       lowercase__:   str					=      torch.tensor([147_979.6_875, 165_527.0_469, 484_638.5_938, 11_292.9_375, 5_879.6_562, 7_634.1_147]							)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''area''']    ,			_UpperCAmelCase							)							)
       # verify boxes
       lowercase__:   List[str]					=      torch.Size([6, 4]							)
       self.assertEqual(encoding['''labels'''][0]['''boxes'''].shape    ,			_UpperCAmelCase							)
       lowercase__:   List[Any]					=      torch.tensor([0.2_625, 0.5_437, 0.4_688, 0.8_625]							)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''boxes'''][0]    ,			_UpperCAmelCase    ,			atol=1e-3							)							)
       # verify image_id
       lowercase__:   int					=      torch.tensor([39769]							)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''image_id''']    ,			_UpperCAmelCase							)							)
       # verify is_crowd
       lowercase__:   int					=      torch.tensor([0, 0, 0, 0, 0, 0]							)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''iscrowd''']    ,			_UpperCAmelCase							)							)
       # verify class_labels
       lowercase__:   Dict					=      torch.tensor([17, 17, 63, 75, 75, 93]							)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''class_labels''']    ,			_UpperCAmelCase							)							)
       # verify masks
       lowercase__:   Union[str, Any]					=      822873
       self.assertEqual(encoding['''labels'''][0]['''masks'''].sum().item()    ,			_UpperCAmelCase							)
       # verify orig_size
       lowercase__:   List[Any]					=      torch.tensor([480, 640]							)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''orig_size''']    ,			_UpperCAmelCase							)							)
       # verify size
       lowercase__:   Union[str, Any]					=      torch.tensor([800, 1066]							)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''size''']    ,			_UpperCAmelCase							)							)
