





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


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

          '''simple docstring'''






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


     def 							SCREAMING_SNAKE_CASE  (						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 							SCREAMING_SNAKE_CASE  (						self						,     _UpperCAmelCase						,     _UpperCAmelCase=False):

          '''simple docstring'''






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

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

          return expected_height, expected_width




@require_torch
@require_vision
class 			SCREAMING_SNAKE_CASE   (a__ ,		unittest.TestCase							):
     lowerCAmelCase		      =	DeformableDetrImageProcessor if is_vision_available() else None


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						:			Tuple								=	DeformableDetrImageProcessingTester(self)


     @property
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          return self.image_processor_tester.prepare_image_processor_dict()


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						:			Optional[int]								=	self.image_processing_class(**self.image_processor_dict)
          self.assertTrue(hasattr(_UpperCAmelCase						,     'image_mean'))
          self.assertTrue(hasattr(_UpperCAmelCase						,     'image_std'))
          self.assertTrue(hasattr(_UpperCAmelCase						,     'do_normalize'))
          self.assertTrue(hasattr(_UpperCAmelCase						,     'do_resize'))
          self.assertTrue(hasattr(_UpperCAmelCase						,     'do_rescale'))
          self.assertTrue(hasattr(_UpperCAmelCase						,     'do_pad'))
          self.assertTrue(hasattr(_UpperCAmelCase						,     'size'))


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						:			List[Any]								=	self.image_processing_class.from_dict(self.image_processor_dict)
          self.assertEqual(image_processor.size						,     {'shortest_edge': 18, 'longest_edge': 1333})
          self.assertEqual(image_processor.do_pad						,     _UpperCAmelCase)

          __A						:			List[str]								=	self.image_processing_class.from_dict(
              self.image_processor_dict						,     size=42						,     max_size=84						,     pad_and_return_pixel_mask=_UpperCAmelCase)
          self.assertEqual(image_processor.size						,     {'shortest_edge': 42, 'longest_edge': 84})
          self.assertEqual(image_processor.do_pad						,     _UpperCAmelCase)


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          pass


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






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

          # Test not batched input
          __A						:			List[str]								=	image_processing(image_inputs[0]						,     return_tensors='pt').pixel_values

          __A			,__A						:			int								=	self.image_processor_tester.get_expected_values(_UpperCAmelCase)

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

          # Test batched
          __A			,__A						:			Any								=	self.image_processor_tester.get_expected_values(_UpperCAmelCase						,     batched=_UpperCAmelCase)

          __A						:			Dict								=	image_processing(_UpperCAmelCase						,     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 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






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

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

          __A			,__A						:			Tuple								=	self.image_processor_tester.get_expected_values(_UpperCAmelCase)

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

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

          __A			,__A						:			str								=	self.image_processor_tester.get_expected_values(_UpperCAmelCase						,     batched=_UpperCAmelCase)

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


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






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

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

          __A			,__A						:			Tuple								=	self.image_processor_tester.get_expected_values(_UpperCAmelCase)

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

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

          __A			,__A						:			Optional[Any]								=	self.image_processor_tester.get_expected_values(_UpperCAmelCase						,     batched=_UpperCAmelCase)

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


     @slow
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						:			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:
               __A						:			str								=	json.loads(f.read())

          __A						:			str								=	{'image_id': 3_9769, 'annotations': target}

          # encode them
          __A						:			Optional[int]								=	DeformableDetrImageProcessor()
          __A						:			Dict								=	image_processing(images=_UpperCAmelCase						,     annotations=_UpperCAmelCase						,     return_tensors='pt')

          # verify pixel values
          __A						:			Dict								=	torch.Size([1, 3, 800, 1066])
          self.assertEqual(encoding['pixel_values'].shape						,     _UpperCAmelCase)

          __A						:			str								=	torch.tensor([0.2796, 0.3138, 0.3481])
          self.assertTrue(torch.allclose(encoding['pixel_values'][0, 0, 0, :3]						,     _UpperCAmelCase						,     atol=1e-4))

          # verify area
          __A						:			int								=	torch.tensor([5887.9600, 11250.2061, 489353.8438, 837122.7500, 147967.5156, 165732.3438])
          self.assertTrue(torch.allclose(encoding['labels'][0]['area']						,     _UpperCAmelCase))
          # verify boxes
          __A						:			Union[str, Any]								=	torch.Size([6, 4])
          self.assertEqual(encoding['labels'][0]['boxes'].shape						,     _UpperCAmelCase)
          __A						:			int								=	torch.tensor([0.5503, 0.2765, 0.0604, 0.2215])
          self.assertTrue(torch.allclose(encoding['labels'][0]['boxes'][0]						,     _UpperCAmelCase						,     atol=1e-3))
          # verify image_id
          __A						:			Optional[Any]								=	torch.tensor([3_9769])
          self.assertTrue(torch.allclose(encoding['labels'][0]['image_id']						,     _UpperCAmelCase))
          # verify is_crowd
          __A						:			Tuple								=	torch.tensor([0, 0, 0, 0, 0, 0])
          self.assertTrue(torch.allclose(encoding['labels'][0]['iscrowd']						,     _UpperCAmelCase))
          # verify class_labels
          __A						:			List[Any]								=	torch.tensor([75, 75, 63, 65, 17, 17])
          self.assertTrue(torch.allclose(encoding['labels'][0]['class_labels']						,     _UpperCAmelCase))
          # verify orig_size
          __A						:			Union[str, Any]								=	torch.tensor([480, 640])
          self.assertTrue(torch.allclose(encoding['labels'][0]['orig_size']						,     _UpperCAmelCase))
          # verify size
          __A						:			Optional[Any]								=	torch.tensor([800, 1066])
          self.assertTrue(torch.allclose(encoding['labels'][0]['size']						,     _UpperCAmelCase))


     @slow
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






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

          __A						:			Union[str, Any]								=	{'file_name': '000000039769.png', 'image_id': 3_9769, 'segments_info': target}

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

          # encode them
          __A						:			List[str]								=	DeformableDetrImageProcessor(format='coco_panoptic')
          __A						:			List[str]								=	image_processing(images=_UpperCAmelCase						,     annotations=_UpperCAmelCase						,     masks_path=_UpperCAmelCase						,     return_tensors='pt')

          # verify pixel values
          __A						:			Dict								=	torch.Size([1, 3, 800, 1066])
          self.assertEqual(encoding['pixel_values'].shape						,     _UpperCAmelCase)

          __A						:			Any								=	torch.tensor([0.2796, 0.3138, 0.3481])
          self.assertTrue(torch.allclose(encoding['pixel_values'][0, 0, 0, :3]						,     _UpperCAmelCase						,     atol=1e-4))

          # verify area
          __A						:			int								=	torch.tensor([147979.6875, 165527.0469, 484638.5938, 11292.9375, 5879.6562, 7634.1147])
          self.assertTrue(torch.allclose(encoding['labels'][0]['area']						,     _UpperCAmelCase))
          # verify boxes
          __A						:			Union[str, Any]								=	torch.Size([6, 4])
          self.assertEqual(encoding['labels'][0]['boxes'].shape						,     _UpperCAmelCase)
          __A						:			int								=	torch.tensor([0.2625, 0.5437, 0.4688, 0.8625])
          self.assertTrue(torch.allclose(encoding['labels'][0]['boxes'][0]						,     _UpperCAmelCase						,     atol=1e-3))
          # verify image_id
          __A						:			Union[str, Any]								=	torch.tensor([3_9769])
          self.assertTrue(torch.allclose(encoding['labels'][0]['image_id']						,     _UpperCAmelCase))
          # verify is_crowd
          __A						:			str								=	torch.tensor([0, 0, 0, 0, 0, 0])
          self.assertTrue(torch.allclose(encoding['labels'][0]['iscrowd']						,     _UpperCAmelCase))
          # verify class_labels
          __A						:			Optional[int]								=	torch.tensor([17, 17, 63, 75, 75, 93])
          self.assertTrue(torch.allclose(encoding['labels'][0]['class_labels']						,     _UpperCAmelCase))
          # verify masks
          __A						:			Dict								=	82_2873
          self.assertEqual(encoding['labels'][0]['masks'].sum().item()						,     _UpperCAmelCase)
          # verify orig_size
          __A						:			Tuple								=	torch.tensor([480, 640])
          self.assertTrue(torch.allclose(encoding['labels'][0]['orig_size']						,     _UpperCAmelCase))
          # verify size
          __A						:			List[str]								=	torch.tensor([800, 1066])
          self.assertTrue(torch.allclose(encoding['labels'][0]['size']						,     _UpperCAmelCase))






'''simple docstring'''




import math






def 	_lowerCAmelCase      (  __snake_case  :			int      )					->					int:

     if not isinstance(__snake_case			,							__snake_case      ):
          __A						:			List[Any]								=	f'Input value of [number={number}] must be an integer'
          raise TypeError(__snake_case      )

     if number < 1:
          __A						:			Union[str, Any]								=	f'Input value of [number={number}] must be > 0'
          raise ValueError(__snake_case      )
     elif number == 1:
          return 3
     elif number == 2:
          return 5
     else:
          __A						:			Optional[Any]								=	int(math.log(number // 3			,							2      )      ) + 2

          __A						:			Union[str, Any]								=	[3, 5]
          __A						:			List[Any]								=	2
          __A						:			Optional[Any]								=	3
          for block in range(1			,							__snake_case      ):
               for _ in range(__snake_case      ):
                    proth_list.append(2 ** (block + 1) + proth_list[proth_index - 1]      )
                    proth_index += 1
               increment *= 2

     return proth_list[number - 1]


if __name__ == "__main__":
  import doctest

  doctest.testmod()

  for number in range(11):
    lowercase__   :				str      =   0
    try:
      lowercase__   :				List[str]      =   proth(number)
    except ValueError:
      print(f"""ValueError: there is no {number}th Proth number""")
      continue

    print(f"""The {number}th Proth number: {value}""")