




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





       '''simple docstring'''






       def __init__(      self,							A,							A=7,							A=3,							A=30,							A=400,							A=True,							A=None,							A=True,							A=[0.5, 0.5, 0.5],							A=[0.5, 0.5, 0.5],							A=True,							A=1 / 255,							A=True,							):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      List[str]    					=						size if size is not None else {'shortest_edge': 18, 'longest_edge': 1_333}
             SCREAMING_SNAKE_CASE   :      List[Any]    					=						parent
             SCREAMING_SNAKE_CASE   :      Dict    					=						batch_size
             SCREAMING_SNAKE_CASE   :      int    					=						num_channels
             SCREAMING_SNAKE_CASE   :      Tuple    					=						min_resolution
             SCREAMING_SNAKE_CASE   :      int    					=						max_resolution
             SCREAMING_SNAKE_CASE   :      Tuple    					=						do_resize
             SCREAMING_SNAKE_CASE   :      Tuple    					=						size
             SCREAMING_SNAKE_CASE   :      Any    					=						do_normalize
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						image_mean
             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						image_std
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						do_rescale
             SCREAMING_SNAKE_CASE   :      int    					=						rescale_factor
             SCREAMING_SNAKE_CASE   :      List[str]    					=						do_pad







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             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        UpperCamelCase_							(      self,							A,							A=False  ):







             '''simple docstring'''







             if not batched:
                   SCREAMING_SNAKE_CASE   :      List[Any]    					=						image_inputs[0]
                   if isinstance(A,							Image.Image  ):
                         SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						image.size
                   else:
                         SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      List[str]    					=						image.shape[1], image.shape[2]
                   if w < h:
                         SCREAMING_SNAKE_CASE   :      int    					=						int(self.size['shortest_edge'] * h / w  )
                         SCREAMING_SNAKE_CASE   :      int    					=						self.size['shortest_edge']
                   elif w > h:
                         SCREAMING_SNAKE_CASE   :      Any    					=						self.size['shortest_edge']
                         SCREAMING_SNAKE_CASE   :      Dict    					=						int(self.size['shortest_edge'] * w / h  )
                   else:
                         SCREAMING_SNAKE_CASE   :      Any    					=						self.size['shortest_edge']
                         SCREAMING_SNAKE_CASE   :      int    					=						self.size['shortest_edge']

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

             return expected_height, expected_width






@require_torch
@require_vision
class 							_a				(			SCREAMING_SNAKE_CASE ,      unittest.TestCase ):





       '''simple docstring'''






       A						:				List[Any]        =						YolosImageProcessor if is_vision_available() else None







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      List[Any]    					=						YolosImageProcessingTester(self  )







       @property
       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             return self.image_processor_tester.prepare_image_processor_dict()







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Any    					=						self.image_processing_class(**self.image_processor_dict  )
             self.assertTrue(hasattr(A,							'image_mean'  )  )
             self.assertTrue(hasattr(A,							'image_std'  )  )
             self.assertTrue(hasattr(A,							'do_normalize'  )  )
             self.assertTrue(hasattr(A,							'do_resize'  )  )
             self.assertTrue(hasattr(A,							'size'  )  )







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						self.image_processing_class.from_dict(self.image_processor_dict  )
             self.assertEqual(image_processor.size,							{'shortest_edge': 18, 'longest_edge': 1_333}  )
             self.assertEqual(image_processor.do_pad,							A  )

             SCREAMING_SNAKE_CASE   :      str    					=						self.image_processing_class.from_dict(
                 self.image_processor_dict,							size=42,							max_size=84,							pad_and_return_pixel_mask=A  )
             self.assertEqual(image_processor.size,							{'shortest_edge': 42, 'longest_edge': 84}  )
             self.assertEqual(image_processor.do_pad,							A  )







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             pass







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      int    					=						self.image_processing_class(**self.image_processor_dict  )
             # create random PIL images
             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						prepare_image_inputs(self.image_processor_tester,							equal_resolution=A  )
             for image in image_inputs:
                   self.assertIsInstance(A,							Image.Image  )

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

             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      Any    					=						self.image_processor_tester.get_expected_values(A  )

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

             # Test batched
             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      Optional[int]    					=						self.image_processor_tester.get_expected_values(A,							batched=A  )

             SCREAMING_SNAKE_CASE   :      Tuple    					=						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,
                     expected_height,
                     expected_width,
                 ),							)







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      List[str]    					=						self.image_processing_class(**self.image_processor_dict  )
             # create random numpy tensors
             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						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
             SCREAMING_SNAKE_CASE   :      int    					=						image_processing(image_inputs[0],							return_tensors='pt'  ).pixel_values

             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      List[str]    					=						self.image_processor_tester.get_expected_values(A  )

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

             # Test batched
             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						image_processing(A,							return_tensors='pt'  ).pixel_values

             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						self.image_processor_tester.get_expected_values(A,							batched=A  )

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







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						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=A,							torchify=A  )
             for image in image_inputs:
                   self.assertIsInstance(A,							torch.Tensor  )

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

             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      List[Any]    					=						self.image_processor_tester.get_expected_values(A  )

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

             # Test batched
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						image_processing(A,							return_tensors='pt'  ).pixel_values

             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      str    					=						self.image_processor_tester.get_expected_values(A,							batched=A  )

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







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Dict    					=						self.image_processing_class(**self.image_processor_dict  )
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						self.image_processing_class(do_resize=A,							do_normalize=A,							do_rescale=A  )
             # create random PyTorch tensors
             SCREAMING_SNAKE_CASE   :      int    					=						prepare_image_inputs(self.image_processor_tester,							equal_resolution=A,							torchify=A  )
             for image in image_inputs:
                   self.assertIsInstance(A,							torch.Tensor  )

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

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







       @slow
       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'  )
             with open('./tests/fixtures/tests_samples/COCO/coco_annotations.txt',							'r'  ) as f:
                   SCREAMING_SNAKE_CASE   :      Dict    					=						json.loads(f.read()  )

             SCREAMING_SNAKE_CASE   :      Any    					=						{'image_id': 39_769, 'annotations': target}

             # encode them
             SCREAMING_SNAKE_CASE   :      Any    					=						YolosImageProcessor.from_pretrained('hustvl/yolos-small'  )
             SCREAMING_SNAKE_CASE   :      int    					=						image_processing(images=A,							annotations=A,							return_tensors='pt'  )

             # verify pixel values
             SCREAMING_SNAKE_CASE   :      List[str]    					=						torch.Size([1, 3, 800, 1_066]  )
             self.assertEqual(encoding['pixel_values'].shape,							A  )

             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						torch.tensor([0.27_96, 0.31_38, 0.34_81]  )
             self.assertTrue(torch.allclose(encoding['pixel_values'][0, 0, 0, :3],							A,							atol=1E-4  )  )

             # verify area
             SCREAMING_SNAKE_CASE   :      Tuple    					=						torch.tensor([58_87.96_00, 1_12_50.20_61, 48_93_53.84_38, 83_71_22.75_00, 14_79_67.51_56, 16_57_32.34_38]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['area'],							A  )  )
             # verify boxes
             SCREAMING_SNAKE_CASE   :      str    					=						torch.Size([6, 4]  )
             self.assertEqual(encoding['labels'][0]['boxes'].shape,							A  )
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						torch.tensor([0.55_03, 0.27_65, 0.06_04, 0.22_15]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['boxes'][0],							A,							atol=1E-3  )  )
             # verify image_id
             SCREAMING_SNAKE_CASE   :      Tuple    					=						torch.tensor([39_769]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['image_id'],							A  )  )
             # verify is_crowd
             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						torch.tensor([0, 0, 0, 0, 0, 0]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['iscrowd'],							A  )  )
             # verify class_labels
             SCREAMING_SNAKE_CASE   :      int    					=						torch.tensor([75, 75, 63, 65, 17, 17]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['class_labels'],							A  )  )
             # verify orig_size
             SCREAMING_SNAKE_CASE   :      Tuple    					=						torch.tensor([480, 640]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['orig_size'],							A  )  )
             # verify size
             SCREAMING_SNAKE_CASE   :      str    					=						torch.tensor([800, 1_066]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['size'],							A  )  )







       @slow
       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Tuple    					=						Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'  )
             with open('./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt',							'r'  ) as f:
                   SCREAMING_SNAKE_CASE   :      int    					=						json.loads(f.read()  )

             SCREAMING_SNAKE_CASE   :      List[Any]    					=						{'file_name': '000000039769.png', 'image_id': 39_769, 'segments_info': target}

             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						pathlib.Path('./tests/fixtures/tests_samples/COCO/coco_panoptic'  )

             # encode them
             SCREAMING_SNAKE_CASE   :      int    					=						YolosImageProcessor(format='coco_panoptic'  )
             SCREAMING_SNAKE_CASE   :      str    					=						image_processing(images=A,							annotations=A,							masks_path=A,							return_tensors='pt'  )

             # verify pixel values
             SCREAMING_SNAKE_CASE   :      List[str]    					=						torch.Size([1, 3, 800, 1_066]  )
             self.assertEqual(encoding['pixel_values'].shape,							A  )

             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						torch.tensor([0.27_96, 0.31_38, 0.34_81]  )
             self.assertTrue(torch.allclose(encoding['pixel_values'][0, 0, 0, :3],							A,							atol=1E-4  )  )

             # verify area
             SCREAMING_SNAKE_CASE   :      Tuple    					=						torch.tensor([14_79_79.68_75, 16_55_27.04_69, 48_46_38.59_38, 1_12_92.93_75, 58_79.65_62, 76_34.11_47]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['area'],							A  )  )
             # verify boxes
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						torch.Size([6, 4]  )
             self.assertEqual(encoding['labels'][0]['boxes'].shape,							A  )
             SCREAMING_SNAKE_CASE   :      Tuple    					=						torch.tensor([0.26_25, 0.54_37, 0.46_88, 0.86_25]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['boxes'][0],							A,							atol=1E-3  )  )
             # verify image_id
             SCREAMING_SNAKE_CASE   :      List[str]    					=						torch.tensor([39_769]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['image_id'],							A  )  )
             # verify is_crowd
             SCREAMING_SNAKE_CASE   :      Any    					=						torch.tensor([0, 0, 0, 0, 0, 0]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['iscrowd'],							A  )  )
             # verify class_labels
             SCREAMING_SNAKE_CASE   :      Any    					=						torch.tensor([17, 17, 63, 75, 75, 93]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['class_labels'],							A  )  )
             # verify masks
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						822_873
             self.assertEqual(encoding['labels'][0]['masks'].sum().item(),							A  )
             # verify orig_size
             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						torch.tensor([480, 640]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['orig_size'],							A  )  )
             # verify size
             SCREAMING_SNAKE_CASE   :      Tuple    					=						torch.tensor([800, 1_066]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['size'],							A  )  )




"""simple docstring"""




def     __magic_name__      ( __snake_case							:			int	,					__snake_case							:			int    )					->							Any:
       if b == 0:
              return 1
       if (b % 2) == 0:
              return actual_power(__snake_case	,					int(b / 2    )    ) * actual_power(__snake_case	,					int(b / 2    )    )
       else:
              return a * actual_power(__snake_case	,					int(b / 2    )    ) * actual_power(__snake_case	,					int(b / 2    )    )


def     __magic_name__      ( __snake_case							:			int	,					__snake_case							:			int    )					->							float:
       if b < 0:
              return 1 / actual_power(__snake_case	,					__snake_case    )
       return actual_power(__snake_case	,					__snake_case    )


if __name__ == "__main__":
   print(power(-2, -3))


