

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






     '''simple docstring'''



     def __init__(		self			:					Tuple					,      lowercase_			:					Optional[Any]					,      lowercase_			:					int=7					,      lowercase_			:					int=3					,      lowercase_			:					List[Any]=18					,      lowercase_			:					Dict=30					,      lowercase_			:					Tuple=400					,      lowercase_			:					Optional[Any]=True					,      lowercase_			:					Dict=None					,      lowercase_			:					List[Any]=True					,      lowercase_			:					Any=None					,      lowercase_			:					int=True					,      lowercase_			:					Any=[0.5, 0.5, 0.5]					,      lowercase_			:					str=[0.5, 0.5, 0.5]					,      lowercase_			:					List[str]=False					,      )						->     Optional[int]:
      UpperCAmelCase	:     Tuple						 =			size if size is not None else {'height': 20, 'width': 20}
      UpperCAmelCase	:     str						 =			crop_size if crop_size is not None else {'height': 18, 'width': 18}
      UpperCAmelCase	:     List[Any]						 =			parent
      UpperCAmelCase	:     Tuple						 =			batch_size
      UpperCAmelCase	:     Optional[Any]						 =			num_channels
      UpperCAmelCase	:     List[str]						 =			image_size
      UpperCAmelCase	:     Dict						 =			min_resolution
      UpperCAmelCase	:     List[Any]						 =			max_resolution
      UpperCAmelCase	:     List[Any]						 =			do_resize
      UpperCAmelCase	:     Optional[int]						 =			size
      UpperCAmelCase	:     List[Any]						 =			do_center_crop
      UpperCAmelCase	:     List[str]						 =			crop_size
      UpperCAmelCase	:     Dict						 =			do_normalize
      UpperCAmelCase	:     Union[str, Any]						 =			image_mean
      UpperCAmelCase	:     Optional[Any]						 =			image_std
      UpperCAmelCase	:     List[str]						 =			do_reduce_labels




     def        UpperCAmelCase_	(		self			:					int	)						->     int:
      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(					):
 UpperCAmelCase	:     Any						 =			load_dataset('hf-internal-testing/fixtures_ade20k'  ,	split='test'     )

 UpperCAmelCase	:     List[str]						 =			Image.open(dataset[0]['file']     )
 UpperCAmelCase	:     Optional[int]						 =			Image.open(dataset[1]['file']     )

 return image, map




def 				UpperCamelCase(					):
 UpperCAmelCase	:     List[str]						 =			load_dataset('hf-internal-testing/fixtures_ade20k'  ,	split='test'     )

 UpperCAmelCase	:     Optional[Any]						 =			Image.open(ds[0]['file']     )
 UpperCAmelCase	:     Optional[Any]						 =			Image.open(ds[1]['file']     )
 UpperCAmelCase	:     Union[str, Any]						 =			Image.open(ds[2]['file']     )
 UpperCAmelCase	:     Union[str, Any]						 =			Image.open(ds[3]['file']     )

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




@require_torch
@require_vision
class 	A_			(    _snake_case	,       unittest.TestCase							):






     '''simple docstring'''



     UpperCAmelCase_						:     List[str]  		=  BeitImageProcessor if is_vision_available() else None


     def        UpperCAmelCase_	(		self			:					List[Any]	)						->     Any:
      UpperCAmelCase	:     Union[str, Any]						 =			BeitImageProcessingTester(self	)


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


     def        UpperCAmelCase_	(		self			:					Dict	)						->     Optional[Any]:
      UpperCAmelCase	:     Union[str, Any]						 =			self.image_processing_class(**self.image_processor_dict	)
      self.assertTrue(hasattr(lowercase_					,      'do_resize'	)	)
      self.assertTrue(hasattr(lowercase_					,      'size'	)	)
      self.assertTrue(hasattr(lowercase_					,      'do_center_crop'	)	)
      self.assertTrue(hasattr(lowercase_					,      'center_crop'	)	)
      self.assertTrue(hasattr(lowercase_					,      'do_normalize'	)	)
      self.assertTrue(hasattr(lowercase_					,      'image_mean'	)	)
      self.assertTrue(hasattr(lowercase_					,      'image_std'	)	)


     def        UpperCAmelCase_	(		self			:					List[str]	)						->     Optional[int]:
      UpperCAmelCase	:     Optional[int]						 =			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					,      lowercase_	)

      UpperCAmelCase	:     List[str]						 =			self.image_processing_class.from_dict(
          self.image_processor_dict					,      size=42					,      crop_size=84					,      reduce_labels=lowercase_	)
      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					,      lowercase_	)


     def        UpperCAmelCase_	(		self			:					Union[str, Any]	)						->     Optional[int]:
      pass


     def        UpperCAmelCase_	(		self			:					List[Any]	)						->     Tuple:
      # Initialize image_processing
      UpperCAmelCase	:     str						 =			self.image_processing_class(**self.image_processor_dict	)
      # create random PIL images
      UpperCAmelCase	:     Optional[int]						 =			prepare_image_inputs(self.image_processor_tester					,      equal_resolution=lowercase_	)
      for image in image_inputs:
       self.assertIsInstance(lowercase_					,      Image.Image	)

      # Test not batched input
      UpperCAmelCase	:     Any						 =			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
      UpperCAmelCase	:     str						 =			image_processing(lowercase_					,      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]	)						->     Tuple:
      # Initialize image_processing
      UpperCAmelCase	:     Optional[Any]						 =			self.image_processing_class(**self.image_processor_dict	)
      # create random numpy tensors
      UpperCAmelCase	:     List[Any]						 =			prepare_image_inputs(self.image_processor_tester					,      equal_resolution=lowercase_					,      numpify=lowercase_	)
      for image in image_inputs:
       self.assertIsInstance(lowercase_					,      np.ndarray	)

      # Test not batched input
      UpperCAmelCase	:     List[str]						 =			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
      UpperCAmelCase	:     Union[str, Any]						 =			image_processing(lowercase_					,      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			:					Dict	)						->     str:
      # Initialize image_processing
      UpperCAmelCase	:     List[str]						 =			self.image_processing_class(**self.image_processor_dict	)
      # create random PyTorch tensors
      UpperCAmelCase	:     Dict						 =			prepare_image_inputs(self.image_processor_tester					,      equal_resolution=lowercase_					,      torchify=lowercase_	)
      for image in image_inputs:
       self.assertIsInstance(lowercase_					,      torch.Tensor	)

      # Test not batched input
      UpperCAmelCase	:     Tuple						 =			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
      UpperCAmelCase	:     Dict						 =			image_processing(lowercase_					,      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[str]	)						->     List[str]:
      # Initialize image_processing
      UpperCAmelCase	:     Optional[int]						 =			self.image_processing_class(**self.image_processor_dict	)
      # create random PyTorch tensors
      UpperCAmelCase	:     Union[str, Any]						 =			prepare_image_inputs(self.image_processor_tester					,      equal_resolution=lowercase_					,      torchify=lowercase_	)
      UpperCAmelCase	:     Any						 =			[]
      for image in image_inputs:
       self.assertIsInstance(lowercase_					,      torch.Tensor	)
       maps.append(torch.zeros(image.shape[-2:]	).long()	)

      # Test not batched input
      UpperCAmelCase	:     Union[str, Any]						 =			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
      UpperCAmelCase	:     Optional[int]						 =			image_processing(lowercase_					,      lowercase_					,      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)
      UpperCAmelCase ,			UpperCAmelCase	:     List[str]						 =			prepare_semantic_single_inputs()

      UpperCAmelCase	:     Tuple						 =			image_processing(lowercase_					,      lowercase_					,      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)
      UpperCAmelCase ,			UpperCAmelCase	:     List[str]						 =			prepare_semantic_batch_inputs()

      UpperCAmelCase	:     Optional[int]						 =			image_processing(lowercase_					,      lowercase_					,      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        UpperCAmelCase_	(		self			:					List[Any]	)						->     Any:
      # Initialize image_processing
      UpperCAmelCase	:     Dict						 =			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
      UpperCAmelCase ,			UpperCAmelCase	:     Dict						 =			prepare_semantic_single_inputs()
      UpperCAmelCase	:     int						 =			image_processing(lowercase_					,      lowercase_					,      return_tensors='pt'	)
      self.assertTrue(encoding['labels'].min().item() >= 0	)
      self.assertTrue(encoding['labels'].max().item() <= 150	)

      UpperCAmelCase	:     int						 =			True
      UpperCAmelCase	:     Dict						 =			image_processing(lowercase_					,      lowercase_					,      return_tensors='pt'	)
      self.assertTrue(encoding['labels'].min().item() >= 0	)
      self.assertTrue(encoding['labels'].max().item() <= 255	)







'''simple docstring'''

def 				UpperCamelCase(					UpperCAmelCase_  ,	UpperCAmelCase_     ):
 while a != 0:
  UpperCAmelCase ,			UpperCAmelCase	:     Tuple						 =			b % a, a
 return b




def 				UpperCamelCase(					UpperCAmelCase_  ,	UpperCAmelCase_     ):
 if gcd(UpperCAmelCase_  ,	UpperCAmelCase_     ) != 1:
  UpperCAmelCase	:     List[str]						 =			F"""mod inverse of {a!r} and {m!r} does not exist"""
  raise ValueError(UpperCAmelCase_     )
 UpperCAmelCase ,			UpperCAmelCase ,			UpperCAmelCase	:     Any						 =			1, 0, a
 UpperCAmelCase ,			UpperCAmelCase ,			UpperCAmelCase	:     Dict						 =			0, 1, m
 while va != 0:
  UpperCAmelCase	:     Tuple						 =			ua // va
  UpperCAmelCase ,			UpperCAmelCase ,			UpperCAmelCase ,			UpperCAmelCase ,			UpperCAmelCase ,			UpperCAmelCase	:     List[Any]						 =			(ua - q * va), (ua - q * va), (ua - q * va), va, va, va
 return ua % m




