


'''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 DeformableDetrImageProcessor






class   __lowercase				(  unittest.TestCase  ):





      def __init__(		self						:					Any					,       __lowerCamelCase						:					Any					,       __lowerCamelCase						:					str=7					,       __lowerCamelCase						:					List[str]=3					,       __lowerCamelCase						:					List[str]=3_0					,       __lowerCamelCase						:					Union[str, Any]=4_0_0					,       __lowerCamelCase						:					int=True					,       __lowerCamelCase						:					Tuple=None					,       __lowerCamelCase						:					Any=True					,       __lowerCamelCase						:					Optional[int]=[0.5, 0.5, 0.5]					,       __lowerCamelCase						:					Optional[Any]=[0.5, 0.5, 0.5]					,       __lowerCamelCase						:					List[Any]=True					,       __lowerCamelCase						:					List[Any]=1 / 2_5_5					,       __lowerCamelCase						:					str=True					,       )    ->   Optional[Any]:






         """simple docstring"""







         UpperCAmelCase							=     size if size is not None else {"""shortest_edge""": 1_8, """longest_edge""": 1_3_3_3}
         UpperCAmelCase							=     parent
         UpperCAmelCase							=     batch_size
         UpperCAmelCase							=     num_channels
         UpperCAmelCase							=     min_resolution
         UpperCAmelCase							=     max_resolution
         UpperCAmelCase							=     do_resize
         UpperCAmelCase							=     size
         UpperCAmelCase							=     do_normalize
         UpperCAmelCase							=     image_mean
         UpperCAmelCase							=     image_std
         UpperCAmelCase							=     do_rescale
         UpperCAmelCase							=     rescale_factor
         UpperCAmelCase							=     do_pad





      def        _lowercase				(		self						:					Any       )    ->   Any:






         """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        _lowercase				(		self						:					Dict					,       __lowerCamelCase						:					int					,       __lowerCamelCase						:					Any=False       )    ->   Optional[int]:






         """simple docstring"""







         if not batched:
            UpperCAmelCase							=     image_inputs[0]
            if isinstance(_a					,       Image.Image       ):
               UpperCAmelCase				,  UpperCAmelCase							=     image.size
            else:
               UpperCAmelCase				,  UpperCAmelCase							=     image.shape[1], image.shape[2]
            if w < h:
               UpperCAmelCase							=     int(self.size["""shortest_edge"""] * h / w       )
               UpperCAmelCase							=     self.size["""shortest_edge"""]
            elif w > h:
               UpperCAmelCase							=     self.size["""shortest_edge"""]
               UpperCAmelCase							=     int(self.size["""shortest_edge"""] * w / h       )
            else:
               UpperCAmelCase							=     self.size["""shortest_edge"""]
               UpperCAmelCase							=     self.size["""shortest_edge"""]

         else:
            UpperCAmelCase							=     []
            for image in image_inputs:
               UpperCAmelCase				,  UpperCAmelCase							=     self.get_expected_values([image]       )
               expected_values.append((expected_height, expected_width)       )
            UpperCAmelCase							=     max(_a					,       key=lambda __lowerCamelCase       : item[0]       )[0]
            UpperCAmelCase							=     max(_a					,       key=lambda __lowerCamelCase       : item[1]       )[1]

         return expected_height, expected_width






@require_torch
@require_vision
class   __lowercase				(  __SCREAMING_SNAKE_CASE ,					unittest.TestCase  ):
      UpperCamelCase								=       DeformableDetrImageProcessor if is_vision_available() else None





      def        _lowercase				(		self						:					int       )    ->   Optional[Any]:






         """simple docstring"""







         UpperCAmelCase							=     DeformableDetrImageProcessingTester(self       )





      @property
      def        _lowercase				(		self						:					Tuple       )    ->   Dict:






         """simple docstring"""







         return self.image_processor_tester.prepare_image_processor_dict()





      def        _lowercase				(		self						:					Union[str, Any]       )    ->   Union[str, Any]:






         """simple docstring"""







         UpperCAmelCase							=     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					,       """do_rescale"""       )       )
         self.assertTrue(hasattr(_a					,       """do_pad"""       )       )
         self.assertTrue(hasattr(_a					,       """size"""       )       )





      def        _lowercase				(		self						:					Tuple       )    ->   str:






         """simple docstring"""







         UpperCAmelCase							=     self.image_processing_class.from_dict(self.image_processor_dict       )
         self.assertEqual(image_processor.size					,       {"""shortest_edge""": 1_8, """longest_edge""": 1_3_3_3}       )
         self.assertEqual(image_processor.do_pad					,       _a       )

         UpperCAmelCase							=     self.image_processing_class.from_dict(
             self.image_processor_dict					,       size=4_2					,       max_size=8_4					,       pad_and_return_pixel_mask=_a       )
         self.assertEqual(image_processor.size					,       {"""shortest_edge""": 4_2, """longest_edge""": 8_4}       )
         self.assertEqual(image_processor.do_pad					,       _a       )





      def        _lowercase				(		self						:					Optional[int]       )    ->   List[Any]:






         """simple docstring"""







         pass





      def        _lowercase				(		self						:					Optional[Any]       )    ->   Any:






         """simple docstring"""







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

         # Test not batched input
         UpperCAmelCase							=     image_processing(image_inputs[0]					,       return_tensors="""pt"""       ).pixel_values

         UpperCAmelCase				,  UpperCAmelCase							=     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
         UpperCAmelCase				,  UpperCAmelCase							=     self.image_processor_tester.get_expected_values(_a					,       batched=_a       )

         UpperCAmelCase							=     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        _lowercase				(		self						:					Optional[Any]       )    ->   Optional[Any]:






         """simple docstring"""







         UpperCAmelCase							=     self.image_processing_class(**self.image_processor_dict       )
         # create random numpy tensors
         UpperCAmelCase							=     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
         UpperCAmelCase							=     image_processing(image_inputs[0]					,       return_tensors="""pt"""       ).pixel_values

         UpperCAmelCase				,  UpperCAmelCase							=     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
         UpperCAmelCase							=     image_processing(_a					,       return_tensors="""pt"""       ).pixel_values

         UpperCAmelCase				,  UpperCAmelCase							=     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        _lowercase				(		self						:					Any       )    ->   Union[str, Any]:






         """simple docstring"""







         UpperCAmelCase							=     self.image_processing_class(**self.image_processor_dict       )
         # create random PyTorch tensors
         UpperCAmelCase							=     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
         UpperCAmelCase							=     image_processing(image_inputs[0]					,       return_tensors="""pt"""       ).pixel_values

         UpperCAmelCase				,  UpperCAmelCase							=     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
         UpperCAmelCase							=     image_processing(_a					,       return_tensors="""pt"""       ).pixel_values

         UpperCAmelCase				,  UpperCAmelCase							=     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,
             )					,       )





      @slow
      def        _lowercase				(		self						:					Optional[int]       )    ->   List[Any]:






         """simple docstring"""







         UpperCAmelCase							=     Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""       )
         with open("""./tests/fixtures/tests_samples/COCO/coco_annotations.txt"""					,       """r"""       ) as f:
            UpperCAmelCase							=     json.loads(f.read()       )

         UpperCAmelCase							=     {"""image_id""": 3_9_7_6_9, """annotations""": target}

         # encode them
         UpperCAmelCase							=     DeformableDetrImageProcessor()
         UpperCAmelCase							=     image_processing(images=_a					,       annotations=_a					,       return_tensors="""pt"""       )

         # verify pixel values
         UpperCAmelCase							=     torch.Size([1, 3, 8_0_0, 1_0_6_6]       )
         self.assertEqual(encoding["""pixel_values"""].shape					,       _a       )

         UpperCAmelCase							=     torch.tensor([0.2_796, 0.3_138, 0.3_481]       )
         self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3]					,       _a					,       atol=1e-4       )       )

         # verify area
         UpperCAmelCase							=     torch.tensor([5_887.9_600, 1_1_2_5_0.2_0_6_1, 4_8_9_3_5_3.8_4_3_8, 8_3_7_1_2_2.7_5_0_0, 1_4_7_9_6_7.5_1_5_6, 1_6_5_7_3_2.3_4_3_8]       )
         self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""]					,       _a       )       )
         # verify boxes
         UpperCAmelCase							=     torch.Size([6, 4]       )
         self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape					,       _a       )
         UpperCAmelCase							=     torch.tensor([0.5_503, 0.2_765, 0.0_604, 0.2_215]       )
         self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0]					,       _a					,       atol=1e-3       )       )
         # verify image_id
         UpperCAmelCase							=     torch.tensor([3_9_7_6_9]       )
         self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""]					,       _a       )       )
         # verify is_crowd
         UpperCAmelCase							=     torch.tensor([0, 0, 0, 0, 0, 0]       )
         self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""]					,       _a       )       )
         # verify class_labels
         UpperCAmelCase							=     torch.tensor([7_5, 7_5, 6_3, 6_5, 1_7, 1_7]       )
         self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""]					,       _a       )       )
         # verify orig_size
         UpperCAmelCase							=     torch.tensor([4_8_0, 6_4_0]       )
         self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""]					,       _a       )       )
         # verify size
         UpperCAmelCase							=     torch.tensor([8_0_0, 1_0_6_6]       )
         self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""]					,       _a       )       )





      @slow
      def        _lowercase				(		self						:					Optional[Any]       )    ->   Union[str, Any]:






         """simple docstring"""







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

         UpperCAmelCase							=     {"""file_name""": """000000039769.png""", """image_id""": 3_9_7_6_9, """segments_info""": target}

         UpperCAmelCase							=     pathlib.Path("""./tests/fixtures/tests_samples/COCO/coco_panoptic"""       )

         # encode them
         UpperCAmelCase							=     DeformableDetrImageProcessor(format="""coco_panoptic"""       )
         UpperCAmelCase							=     image_processing(images=_a					,       annotations=_a					,       masks_path=_a					,       return_tensors="""pt"""       )

         # verify pixel values
         UpperCAmelCase							=     torch.Size([1, 3, 8_0_0, 1_0_6_6]       )
         self.assertEqual(encoding["""pixel_values"""].shape					,       _a       )

         UpperCAmelCase							=     torch.tensor([0.2_796, 0.3_138, 0.3_481]       )
         self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3]					,       _a					,       atol=1e-4       )       )

         # verify area
         UpperCAmelCase							=     torch.tensor([1_4_7_9_7_9.6_8_7_5, 1_6_5_5_2_7.0_4_6_9, 4_8_4_6_3_8.5_9_3_8, 1_1_2_9_2.9_3_7_5, 5_879.6_562, 7_634.1_147]       )
         self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""]					,       _a       )       )
         # verify boxes
         UpperCAmelCase							=     torch.Size([6, 4]       )
         self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape					,       _a       )
         UpperCAmelCase							=     torch.tensor([0.2_625, 0.5_437, 0.4_688, 0.8_625]       )
         self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0]					,       _a					,       atol=1e-3       )       )
         # verify image_id
         UpperCAmelCase							=     torch.tensor([3_9_7_6_9]       )
         self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""]					,       _a       )       )
         # verify is_crowd
         UpperCAmelCase							=     torch.tensor([0, 0, 0, 0, 0, 0]       )
         self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""]					,       _a       )       )
         # verify class_labels
         UpperCAmelCase							=     torch.tensor([1_7, 1_7, 6_3, 7_5, 7_5, 9_3]       )
         self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""]					,       _a       )       )
         # verify masks
         UpperCAmelCase							=     8_2_2_8_7_3
         self.assertEqual(encoding["""labels"""][0]["""masks"""].sum().item()					,       _a       )
         # verify orig_size
         UpperCAmelCase							=     torch.tensor([4_8_0, 6_4_0]       )
         self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""]					,       _a       )       )
         # verify size
         UpperCAmelCase							=     torch.tensor([8_0_0, 1_0_6_6]       )
         self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""]					,       _a       )       )







def   _UpperCamelCase				(						lowerCAmelCase_  ,						lowerCAmelCase_   )						->int:
   return int((input_a, input_a).count(0   ) == 0   )
def   _UpperCamelCase				(						)						->None:
   assert and_gate(0  ,						0   ) == 0
   assert and_gate(0  ,						1   ) == 0
   assert and_gate(1  ,						0   ) == 0
   assert and_gate(1  ,						1   ) == 1


if __name__ == "__main__":
 test_and_gate()
 print(and_gate(1, 0))
 print(and_gate(0, 0))
 print(and_gate(0, 1))
 print(and_gate(1, 1))



