





import unittest

import numpy as np
from datasets import load_dataset

from transformers.testing_utils import require_torch, require_vision
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 BeitImageProcessor

class 		SCREAMING_SNAKE_CASE__				(       unittest.TestCase  ):


      def __init__(self       :      Optional[Any]				,			a__       :      str				,			a__       :      Any=7				,			a__       :      List[Any]=3				,			a__       :      List[Any]=18				,			a__       :      Optional[int]=30				,			a__       :      Dict=400				,			a__       :      str=True				,			a__       :      int=None				,			a__       :      List[str]=True				,			a__       :      Union[str, Any]=None				,			a__       :      List[str]=True				,			a__       :      int=[0.5, 0.5, 0.5]				,			a__       :      Optional[int]=[0.5, 0.5, 0.5]				,			a__       :      Any=False				,			):





         """simple docstring"""



         __snake_case							       =   size if size is not None else {'''height''': 20, '''width''': 20}
         __snake_case							       =   crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}
         __snake_case							       =   parent
         __snake_case							       =   batch_size
         __snake_case							       =   num_channels
         __snake_case							       =   image_size
         __snake_case							       =   min_resolution
         __snake_case							       =   max_resolution
         __snake_case							       =   do_resize
         __snake_case							       =   size
         __snake_case							       =   do_center_crop
         __snake_case							       =   crop_size
         __snake_case							       =   do_normalize
         __snake_case							       =   image_mean
         __snake_case							       =   image_std
         __snake_case							       =   do_reduce_labels


      def    a (self       :      List[str]      ):





         """simple docstring"""



         return {
             "do_resize": self.do_resize,
             "size": self.size,
             "do_center_crop": self.do_center_crop,
             "crop_size": self.crop_size,
             "do_normalize": self.do_normalize,
             "image_mean": self.image_mean,
             "image_std": self.image_std,
             "do_reduce_labels": self.do_reduce_labels,
         }






def lowerCamelCase__    (							)      ->				Any:
   __snake_case							       =   load_dataset('''hf-internal-testing/fixtures_ade20k'''       ,      split='''test'''       )

   __snake_case							       =   Image.open(dataset[0]['''file''']       )
   __snake_case							       =   Image.open(dataset[1]['''file''']       )

   return image, map






def lowerCamelCase__    (							)      ->				List[Any]:
   __snake_case							       =   load_dataset('''hf-internal-testing/fixtures_ade20k'''       ,      split='''test'''       )

   __snake_case							       =   Image.open(ds[0]['''file''']       )
   __snake_case							       =   Image.open(ds[1]['''file''']       )
   __snake_case							       =   Image.open(ds[2]['''file''']       )
   __snake_case							       =   Image.open(ds[3]['''file''']       )

   return [imagea, imagea], [mapa, mapa]







@require_torch
@require_vision
class 		SCREAMING_SNAKE_CASE__				(       _UpperCAmelCase						,      unittest.TestCase  ):
      A_						:				Optional[int]							=    BeitImageProcessor if is_vision_available() else None


      def    a (self       :      Union[str, Any]      ):





         """simple docstring"""



         __snake_case							       =   BeitImageProcessingTester(self      )


      @property
      def    a (self       :      Union[str, Any]      ):





         """simple docstring"""



         return self.image_processor_tester.prepare_image_processor_dict()


      def    a (self       :      List[Any]      ):





         """simple docstring"""



         __snake_case							       =   self.image_processing_class(**self.image_processor_dict      )
         self.assertTrue(hasattr(a__				,			'''do_resize'''      )      )
         self.assertTrue(hasattr(a__				,			'''size'''      )      )
         self.assertTrue(hasattr(a__				,			'''do_center_crop'''      )      )
         self.assertTrue(hasattr(a__				,			'''center_crop'''      )      )
         self.assertTrue(hasattr(a__				,			'''do_normalize'''      )      )
         self.assertTrue(hasattr(a__				,			'''image_mean'''      )      )
         self.assertTrue(hasattr(a__				,			'''image_std'''      )      )


      def    a (self       :      Tuple      ):





         """simple docstring"""



         __snake_case							       =   self.image_processing_class.from_dict(self.image_processor_dict      )
         self.assertEqual(image_processor.size				,			{'''height''': 20, '''width''': 20}      )
         self.assertEqual(image_processor.crop_size				,			{'''height''': 18, '''width''': 18}      )
         self.assertEqual(image_processor.do_reduce_labels				,			a__      )

         __snake_case							       =   self.image_processing_class.from_dict(
             self.image_processor_dict				,			size=42				,			crop_size=84				,			reduce_labels=a__      )
         self.assertEqual(image_processor.size				,			{'''height''': 42, '''width''': 42}      )
         self.assertEqual(image_processor.crop_size				,			{'''height''': 84, '''width''': 84}      )
         self.assertEqual(image_processor.do_reduce_labels				,			a__      )


      def    a (self       :      Any      ):





         """simple docstring"""



         pass


      def    a (self       :      Any      ):





         """simple docstring"""



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

         # Test not batched input
         __snake_case							       =   image_processing(image_inputs[0]				,			return_tensors='''pt'''      ).pixel_values
         self.assertEqual(
             encoded_images.shape				,			(
                 1,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )				,			)

         # Test batched
         __snake_case							       =   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,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )				,			)


      def    a (self       :      List[str]      ):





         """simple docstring"""



         __snake_case							       =   self.image_processing_class(**self.image_processor_dict      )
         # create random numpy tensors
         __snake_case							       =   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
         __snake_case							       =   image_processing(image_inputs[0]				,			return_tensors='''pt'''      ).pixel_values
         self.assertEqual(
             encoded_images.shape				,			(
                 1,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )				,			)

         # Test batched
         __snake_case							       =   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,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )				,			)


      def    a (self       :      Any      ):





         """simple docstring"""



         __snake_case							       =   self.image_processing_class(**self.image_processor_dict      )
         # create random PyTorch tensors
         __snake_case							       =   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
         __snake_case							       =   image_processing(image_inputs[0]				,			return_tensors='''pt'''      ).pixel_values
         self.assertEqual(
             encoded_images.shape				,			(
                 1,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )				,			)

         # Test batched
         __snake_case							       =   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,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )				,			)


      def    a (self       :      Dict      ):





         """simple docstring"""



         __snake_case							       =   self.image_processing_class(**self.image_processor_dict      )
         # create random PyTorch tensors
         __snake_case							       =   prepare_image_inputs(self.image_processor_tester				,			equal_resolution=a__				,			torchify=a__      )
         __snake_case							       =   []
         for image in image_inputs:
            self.assertIsInstance(a__				,			torch.Tensor      )
            maps.append(torch.zeros(image.shape[-2:]      ).long()      )

         # Test not batched input
         __snake_case							       =   image_processing(image_inputs[0]				,			maps[0]				,			return_tensors='''pt'''      )
         self.assertEqual(
             encoding['''pixel_values'''].shape				,			(
                 1,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )				,			)
         self.assertEqual(
             encoding['''labels'''].shape				,			(
                 1,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )				,			)
         self.assertEqual(encoding['''labels'''].dtype				,			torch.long      )
         self.assertTrue(encoding['''labels'''].min().item() >= 0      )
         self.assertTrue(encoding['''labels'''].max().item() <= 255      )

         # Test batched
         __snake_case							       =   image_processing(a__				,			a__				,			return_tensors='''pt'''      )
         self.assertEqual(
             encoding['''pixel_values'''].shape				,			(
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )				,			)
         self.assertEqual(
             encoding['''labels'''].shape				,			(
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )				,			)
         self.assertEqual(encoding['''labels'''].dtype				,			torch.long      )
         self.assertTrue(encoding['''labels'''].min().item() >= 0      )
         self.assertTrue(encoding['''labels'''].max().item() <= 255      )

         # Test not batched input (PIL images)
         __snake_case       ,	__snake_case							       =   prepare_semantic_single_inputs()

         __snake_case							       =   image_processing(a__				,			a__				,			return_tensors='''pt'''      )
         self.assertEqual(
             encoding['''pixel_values'''].shape				,			(
                 1,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )				,			)
         self.assertEqual(
             encoding['''labels'''].shape				,			(
                 1,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )				,			)
         self.assertEqual(encoding['''labels'''].dtype				,			torch.long      )
         self.assertTrue(encoding['''labels'''].min().item() >= 0      )
         self.assertTrue(encoding['''labels'''].max().item() <= 255      )

         # Test batched input (PIL images)
         __snake_case       ,	__snake_case							       =   prepare_semantic_batch_inputs()

         __snake_case							       =   image_processing(a__				,			a__				,			return_tensors='''pt'''      )
         self.assertEqual(
             encoding['''pixel_values'''].shape				,			(
                 2,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )				,			)
         self.assertEqual(
             encoding['''labels'''].shape				,			(
                 2,
                 self.image_processor_tester.crop_size['''height'''],
                 self.image_processor_tester.crop_size['''width'''],
             )				,			)
         self.assertEqual(encoding['''labels'''].dtype				,			torch.long      )
         self.assertTrue(encoding['''labels'''].min().item() >= 0      )
         self.assertTrue(encoding['''labels'''].max().item() <= 255      )


      def    a (self       :      str      ):





         """simple docstring"""



         __snake_case							       =   self.image_processing_class(**self.image_processor_dict      )

         # ADE20k has 150 classes, and the background is included, so labels should be between 0 and 150
         __snake_case       ,	__snake_case							       =   prepare_semantic_single_inputs()
         __snake_case							       =   image_processing(a__				,			a__				,			return_tensors='''pt'''      )
         self.assertTrue(encoding['''labels'''].min().item() >= 0      )
         self.assertTrue(encoding['''labels'''].max().item() <= 150      )

         __snake_case							       =   True
         __snake_case							       =   image_processing(a__				,			a__				,			return_tensors='''pt'''      )
         self.assertTrue(encoding['''labels'''].min().item() >= 0      )
         self.assertTrue(encoding['''labels'''].max().item() <= 255      )








from datasets.utils.patching import _PatchedModuleObj, patch_submodule

from . import _test_patching






def lowerCamelCase__    (							)      ->				Any:
   import os as original_os
   from os import path as original_path
   from os import rename as original_rename
   from os.path import dirname as original_dirname
   from os.path import join as original_join

   assert _test_patching.os is original_os
   assert _test_patching.path is original_path
   assert _test_patching.join is original_join

   assert _test_patching.renamed_os is original_os
   assert _test_patching.renamed_path is original_path
   assert _test_patching.renamed_join is original_join

   __snake_case							       =   '''__test_patch_submodule_mock__'''
   with patch_submodule(_test_patching       ,      '''os.path.join'''       ,      snake_case_       ):
      # Every way to access os.path.join must be patched, and the rest must stay untouched

      # check os.path.join
      assert isinstance(_test_patching.os       ,      _PatchedModuleObj       )
      assert isinstance(_test_patching.os.path       ,      _PatchedModuleObj       )
      assert _test_patching.os.path.join is mock

      # check path.join
      assert isinstance(_test_patching.path       ,      _PatchedModuleObj       )
      assert _test_patching.path.join is mock

      # check join
      assert _test_patching.join is mock

      # check that the other attributes are untouched
      assert _test_patching.os.rename is original_rename
      assert _test_patching.path.dirname is original_dirname
      assert _test_patching.os.path.dirname is original_dirname

      # Even renamed modules or objects must be patched

      # check renamed_os.path.join
      assert isinstance(_test_patching.renamed_os       ,      _PatchedModuleObj       )
      assert isinstance(_test_patching.renamed_os.path       ,      _PatchedModuleObj       )
      assert _test_patching.renamed_os.path.join is mock

      # check renamed_path.join
      assert isinstance(_test_patching.renamed_path       ,      _PatchedModuleObj       )
      assert _test_patching.renamed_path.join is mock

      # check renamed_join
      assert _test_patching.renamed_join is mock

      # check that the other attributes are untouched
      assert _test_patching.renamed_os.rename is original_rename
      assert _test_patching.renamed_path.dirname is original_dirname
      assert _test_patching.renamed_os.path.dirname is original_dirname

   # check that everthing is back to normal when the patch is over

   assert _test_patching.os is original_os
   assert _test_patching.path is original_path
   assert _test_patching.join is original_join

   assert _test_patching.renamed_os is original_os
   assert _test_patching.renamed_path is original_path
   assert _test_patching.renamed_join is original_join






def lowerCamelCase__    (							)      ->				Any:
   assert _test_patching.open is open

   __snake_case							       =   '''__test_patch_submodule_builtin_mock__'''
   # _test_patching has "open" in its globals
   assert _test_patching.open is open
   with patch_submodule(_test_patching       ,      '''open'''       ,      snake_case_       ):
      assert _test_patching.open is mock

   # check that everthing is back to normal when the patch is over

   assert _test_patching.open is open






def lowerCamelCase__    (							)      ->				List[str]:
   # pandas.read_csv is not present in _test_patching
   __snake_case							       =   '''__test_patch_submodule_missing_mock__'''
   with patch_submodule(_test_patching       ,      '''pandas.read_csv'''       ,      snake_case_       ):
      pass






def lowerCamelCase__    (							)      ->				Union[str, Any]:
   # builtin should always be mocked even if they're not in the globals
   # in case they're loaded at one point
   __snake_case							       =   '''__test_patch_submodule_missing_builtin_mock__'''
   # _test_patching doesn't have "len" in its globals
   assert getattr(_test_patching       ,      '''len'''       ,      snake_case_       ) is None
   with patch_submodule(_test_patching       ,      '''len'''       ,      snake_case_       ):
      assert _test_patching.len is mock
   assert _test_patching.len is len






def lowerCamelCase__    (							)      ->				Union[str, Any]:
   __snake_case							       =   '''__test_patch_submodule_start_and_stop_mock__'''
   __snake_case							       =   patch_submodule(_test_patching       ,      '''open'''       ,      snake_case_       )
   assert _test_patching.open is open
   patch.start()
   assert _test_patching.open is mock
   patch.stop()
   assert _test_patching.open is open






def lowerCamelCase__    (							)      ->				Optional[int]:
   from os import rename as original_rename
   from os.path import dirname as original_dirname
   from os.path import join as original_join

   __snake_case							       =   '''__test_patch_submodule_successive_join__'''
   __snake_case							       =   '''__test_patch_submodule_successive_dirname__'''
   __snake_case							       =   '''__test_patch_submodule_successive_rename__'''
   assert _test_patching.os.path.join is original_join
   assert _test_patching.os.path.dirname is original_dirname
   assert _test_patching.os.rename is original_rename

   with patch_submodule(_test_patching       ,      '''os.path.join'''       ,      snake_case_       ):
      with patch_submodule(_test_patching       ,      '''os.rename'''       ,      snake_case_       ):
         with patch_submodule(_test_patching       ,      '''os.path.dirname'''       ,      snake_case_       ):
            assert _test_patching.os.path.join is mock_join
            assert _test_patching.os.path.dirname is mock_dirname
            assert _test_patching.os.rename is mock_rename

    # try another order
   with patch_submodule(_test_patching       ,      '''os.rename'''       ,      snake_case_       ):
      with patch_submodule(_test_patching       ,      '''os.path.join'''       ,      snake_case_       ):
         with patch_submodule(_test_patching       ,      '''os.path.dirname'''       ,      snake_case_       ):
            assert _test_patching.os.path.join is mock_join
            assert _test_patching.os.path.dirname is mock_dirname
            assert _test_patching.os.rename is mock_rename

   assert _test_patching.os.path.join is original_join
   assert _test_patching.os.path.dirname is original_dirname
   assert _test_patching.os.rename is original_rename







def lowerCamelCase__    (							)      ->				Tuple:
   __snake_case							       =   '''__test_patch_submodule_doesnt_exist_mock__'''
   with patch_submodule(_test_patching       ,      '''__module_that_doesn_exist__.__attribute_that_doesn_exist__'''       ,      snake_case_       ):
      pass
   with patch_submodule(_test_patching       ,      '''os.__attribute_that_doesn_exist__'''       ,      snake_case_       ):
      pass

