





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




       def __init__(						self		,SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__=7		,SCREAMING_SNAKE_CASE__=3		,SCREAMING_SNAKE_CASE__=18		,SCREAMING_SNAKE_CASE__=30		,SCREAMING_SNAKE_CASE__=4_00		,SCREAMING_SNAKE_CASE__=True		,SCREAMING_SNAKE_CASE__=None		,SCREAMING_SNAKE_CASE__=True		,SCREAMING_SNAKE_CASE__=None		,SCREAMING_SNAKE_CASE__=True		,SCREAMING_SNAKE_CASE__=[0.5, 0.5, 0.5]		,SCREAMING_SNAKE_CASE__=[0.5, 0.5, 0.5]		,SCREAMING_SNAKE_CASE__=False		,)      ->				Optional[Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :str		    =  size if size is not None else {'''height''': 20, '''width''': 20}
             __SCREAMING_SNAKE_CASE       :int		    =  crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}
             __SCREAMING_SNAKE_CASE       :List[Any]		    =  parent
             __SCREAMING_SNAKE_CASE       :Optional[int]		    =  batch_size
             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  num_channels
             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  image_size
             __SCREAMING_SNAKE_CASE       :Optional[int]		    =  min_resolution
             __SCREAMING_SNAKE_CASE       :str		    =  max_resolution
             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  do_resize
             __SCREAMING_SNAKE_CASE       :List[str]		    =  size
             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  do_center_crop
             __SCREAMING_SNAKE_CASE       :str		    =  crop_size
             __SCREAMING_SNAKE_CASE       :int		    =  do_normalize
             __SCREAMING_SNAKE_CASE       :Dict		    =  image_mean
             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  image_std
             __SCREAMING_SNAKE_CASE       :Tuple		    =  do_reduce_labels




       def    _UpperCamelCase					(						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       (							)      ->				Optional[Any]:
      __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  load_dataset('''hf-internal-testing/fixtures_ade20k'''			,       split='''test'''							)

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

      return image, map



def 					__lowerCamelCase       (							)      ->				Dict:
      __SCREAMING_SNAKE_CASE       :str		    =  load_dataset('''hf-internal-testing/fixtures_ade20k'''			,       split='''test'''							)

      __SCREAMING_SNAKE_CASE       :Optional[int]		    =  Image.open(ds[0]['''file''']							)
      __SCREAMING_SNAKE_CASE       :Dict		    =  Image.open(ds[1]['''file''']							)
      __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  Image.open(ds[2]['''file''']							)
      __SCREAMING_SNAKE_CASE       :Optional[int]		    =  Image.open(ds[3]['''file''']							)

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







@require_torch
@require_vision
class  _SCREAMING_SNAKE_CASE(       A   ,     unittest.TestCase  ):
       SCREAMING_SNAKE_CASE_       :				Tuple     						=  BeitImageProcessor if is_vision_available() else None




       def    _UpperCamelCase					(						self							)      ->				Any:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :int		    =  BeitImageProcessingTester(self							)




       @property
       def    _UpperCamelCase					(						self							)      ->				int:





             """simple docstring"""






             return self.image_processor_tester.prepare_image_processor_dict()




       def    _UpperCamelCase					(						self							)      ->				Tuple:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :List[str]		    =  self.image_processing_class(**self.image_processor_dict							)
             self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__		,'''do_resize'''							)							)
             self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__		,'''size'''							)							)
             self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__		,'''do_center_crop'''							)							)
             self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__		,'''center_crop'''							)							)
             self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__		,'''do_normalize'''							)							)
             self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__		,'''image_mean'''							)							)
             self.assertTrue(hasattr(SCREAMING_SNAKE_CASE__		,'''image_std'''							)							)




       def    _UpperCamelCase					(						self							)      ->				List[str]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  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		,SCREAMING_SNAKE_CASE__							)

             __SCREAMING_SNAKE_CASE       :Dict		    =  self.image_processing_class.from_dict(
                 self.image_processor_dict		,size=42		,crop_size=84		,reduce_labels=SCREAMING_SNAKE_CASE__							)
             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		,SCREAMING_SNAKE_CASE__							)




       def    _UpperCamelCase					(						self							)      ->				List[str]:





             """simple docstring"""






             pass




       def    _UpperCamelCase					(						self							)      ->				Optional[int]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Dict		    =  self.image_processing_class(**self.image_processor_dict							)
             # create random PIL images
             __SCREAMING_SNAKE_CASE       :List[str]		    =  prepare_image_inputs(self.image_processor_tester		,equal_resolution=SCREAMING_SNAKE_CASE__							)
             for image in image_inputs:
                   self.assertIsInstance(SCREAMING_SNAKE_CASE__		,Image.Image							)

             # Test not batched input
             __SCREAMING_SNAKE_CASE       :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
             __SCREAMING_SNAKE_CASE       :Tuple		    =  image_processing(SCREAMING_SNAKE_CASE__		,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]:





             """simple docstring"""






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

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





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :List[str]		    =  self.image_processing_class(**self.image_processor_dict							)
             # create random PyTorch tensors
             __SCREAMING_SNAKE_CASE       :Any		    =  prepare_image_inputs(self.image_processor_tester		,equal_resolution=SCREAMING_SNAKE_CASE__		,torchify=SCREAMING_SNAKE_CASE__							)
             for image in image_inputs:
                   self.assertIsInstance(SCREAMING_SNAKE_CASE__		,torch.Tensor							)

             # Test not batched input
             __SCREAMING_SNAKE_CASE       :Optional[int]		    =  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
             __SCREAMING_SNAKE_CASE       :Dict		    =  image_processing(SCREAMING_SNAKE_CASE__		,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]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Dict		    =  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=SCREAMING_SNAKE_CASE__		,torchify=SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE       :List[Any]		    =  []
             for image in image_inputs:
                   self.assertIsInstance(SCREAMING_SNAKE_CASE__		,torch.Tensor							)
                   maps.append(torch.zeros(image.shape[-2:]							).long()							)

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

             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  image_processing(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,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)
             __SCREAMING_SNAKE_CASE       :Optional[int]		    =  prepare_semantic_batch_inputs()

             __SCREAMING_SNAKE_CASE       :int		    =  image_processing(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,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							)      ->				Dict:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  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
             __SCREAMING_SNAKE_CASE       :Any		    =  prepare_semantic_single_inputs()
             __SCREAMING_SNAKE_CASE       :Dict		    =  image_processing(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,return_tensors='''pt'''							)
             self.assertTrue(encoding['''labels'''].min().item() >= 0							)
             self.assertTrue(encoding['''labels'''].max().item() <= 1_50							)

             __SCREAMING_SNAKE_CASE       :List[str]		    =  True
             __SCREAMING_SNAKE_CASE       :Tuple		    =  image_processing(SCREAMING_SNAKE_CASE__		,SCREAMING_SNAKE_CASE__		,return_tensors='''pt'''							)
             self.assertTrue(encoding['''labels'''].min().item() >= 0							)
             self.assertTrue(encoding['''labels'''].max().item() <= 2_55							)








"""simple docstring"""
from itertools import product



def 					__lowerCamelCase       (							a_					:		int			,       a_					:		int							)      ->				list[int]:
      __SCREAMING_SNAKE_CASE       :Tuple		    =  sides_number
      __SCREAMING_SNAKE_CASE       :List[Any]		    =  max_face_number * dice_number
      __SCREAMING_SNAKE_CASE       :List[Any]		    =  [0] * (max_total + 1)

      __SCREAMING_SNAKE_CASE       :Optional[int]		    =  1
      __SCREAMING_SNAKE_CASE       :Tuple		    =  range(a_			,       max_face_number + 1							)
      for dice_numbers in product(a_			,       repeat=a_							):
            __SCREAMING_SNAKE_CASE       :Any		    =  sum(a_							)
            totals_frequencies[total] += 1

      return totals_frequencies



def 					__lowerCamelCase       (							)      ->				float:
      __SCREAMING_SNAKE_CASE       :Dict		    =  total_frequency_distribution(
          sides_number=4			,       dice_number=9							)
      __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  total_frequency_distribution(
          sides_number=6			,       dice_number=6							)

      __SCREAMING_SNAKE_CASE       :Optional[Any]		    =  0
      __SCREAMING_SNAKE_CASE       :Any		    =  9
      __SCREAMING_SNAKE_CASE       :List[str]		    =  4 * 9
      __SCREAMING_SNAKE_CASE       :Dict		    =  6
      for peter_total in range(a_			,       max_peter_total + 1							):
            peter_wins_count += peter_totals_frequencies[peter_total] * sum(
                colin_totals_frequencies[min_colin_total:peter_total]							)

      __SCREAMING_SNAKE_CASE       :List[str]		    =  (4**9) * (6**6)
      __SCREAMING_SNAKE_CASE       :Union[str, Any]		    =  peter_wins_count / total_games_number

      __SCREAMING_SNAKE_CASE       :str		    =  round(a_			,       ndigits=7							)

      return rounded_peter_win_probability


if __name__ == "__main__":
  print(f'{solution() = }')