





'''simple docstring'''







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







     def __init__( self    ,   A__    ,   A__=7    ,   A__=3    ,   A__=18    ,   A__=30    ,   A__=4_00    ,   A__=True    ,   A__=None    ,   A__=True    ,   A__=None    ,   A__=True    ,   A__=[0.5, 0.5, 0.5]    ,   A__=[0.5, 0.5, 0.5]    ,   A__=False    ,   ) ->	int:
      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     UpperCamelCase			( self    ) ->	Optional[Any]:
      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    __UpperCamelCase	(	) ->Tuple:
 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    __UpperCamelCase	(	) ->List[str]:
 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 			_lowercase						(       __a				,		unittest.TestCase					):
     _UpperCAmelCase										=					BeitImageProcessor if is_vision_available() else None







     def     UpperCamelCase			( self    ) ->	Optional[int]:
      snake_case				=			BeitImageProcessingTester(self    )







     @property
     def     UpperCamelCase			( self    ) ->	Any:
      return self.image_processor_tester.prepare_image_processor_dict()







     def     UpperCamelCase			( self    ) ->	List[Any]:
      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     UpperCamelCase			( self    ) ->	Dict:
      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     UpperCamelCase			( self    ) ->	Union[str, Any]:
      pass







     def     UpperCamelCase			( self    ) ->	Optional[Any]:
      # Initialize image_processing
      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     UpperCamelCase			( self    ) ->	Optional[int]:
      # Initialize image_processing
      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     UpperCamelCase			( self    ) ->	List[Any]:
      # Initialize image_processing
      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     UpperCamelCase			( self    ) ->	Union[str, Any]:
      # Initialize image_processing
      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() <= 2_55    )

      # 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() <= 2_55    )

      # 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() <= 2_55    )

      # 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() <= 2_55    )







     def     UpperCamelCase			( self    ) ->	Any:
      # Initialize image_processing
      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() <= 1_50    )

      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() <= 2_55    )








'''simple docstring'''







def    __UpperCamelCase	(	a    : int					,   a    : int     ) ->int:
 while b:
  snake_case				,		snake_case				=			b, a % b
 return a

def    __UpperCamelCase	(	a    : int					,   a    : int     ) ->int:
 return a if b == 0 else euclidean_gcd_recursive(a					,   a % b     )

def    __UpperCamelCase	(	) ->Optional[Any]:
 print(f"""euclidean_gcd(3, 5) = {euclidean_gcd(3					,   5     )}"""     )
 print(f"""euclidean_gcd(5, 3) = {euclidean_gcd(5					,   3     )}"""     )
 print(f"""euclidean_gcd(1, 3) = {euclidean_gcd(1					,   3     )}"""     )
 print(f"""euclidean_gcd(3, 6) = {euclidean_gcd(3					,   6     )}"""     )
 print(f"""euclidean_gcd(6, 3) = {euclidean_gcd(6					,   3     )}"""     )

 print(f"""euclidean_gcd_recursive(3, 5) = {euclidean_gcd_recursive(3					,   5     )}"""     )
 print(f"""euclidean_gcd_recursive(5, 3) = {euclidean_gcd_recursive(5					,   3     )}"""     )
 print(f"""euclidean_gcd_recursive(1, 3) = {euclidean_gcd_recursive(1					,   3     )}"""     )
 print(f"""euclidean_gcd_recursive(3, 6) = {euclidean_gcd_recursive(3					,   6     )}"""     )
 print(f"""euclidean_gcd_recursive(6, 3) = {euclidean_gcd_recursive(6					,   3     )}"""     )


if __name__ == "__main__":
   main()

