


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




class 		a__    (							unittest.TestCase  ):





							'''simple docstring'''






							def __init__(		self ,   lowerCamelCase_ ,   lowerCamelCase_=7 ,   lowerCamelCase_=3 ,   lowerCamelCase_=30 ,   lowerCamelCase_=4_00 ,   lowerCamelCase_=True ,   lowerCamelCase_=None ,   lowerCamelCase_=True ,   lowerCamelCase_=1 / 2_55 ,   lowerCamelCase_=True ,   lowerCamelCase_=[0.5, 0.5, 0.5] ,   lowerCamelCase_=[0.5, 0.5, 0.5] ,   lowerCamelCase_=True ,   )	->							List[str]:
														# by setting size["longest_edge"] > max_resolution we're effectively not testing this :p
														lowerCAmelCase__	       =     size if size is not None else {'''shortest_edge''': 18, '''longest_edge''': 13_33}
														lowerCAmelCase__	       =     parent
														lowerCAmelCase__	       =     batch_size
														lowerCAmelCase__	       =     num_channels
														lowerCAmelCase__	       =     min_resolution
														lowerCAmelCase__	       =     max_resolution
														lowerCAmelCase__	       =     do_resize
														lowerCAmelCase__	       =     size
														lowerCAmelCase__	       =     do_rescale
														lowerCAmelCase__	       =     rescale_factor
														lowerCAmelCase__	       =     do_normalize
														lowerCAmelCase__	       =     image_mean
														lowerCAmelCase__	       =     image_std
														lowerCAmelCase__	       =     do_pad







							def   __SCREAMING_SNAKE_CASE							(		self       )	->							Optional[int]:
														return {
														    "do_resize": self.do_resize,
														    "size": self.size,
														    "do_rescale": self.do_rescale,
														    "rescale_factor": self.rescale_factor,
														    "do_normalize": self.do_normalize,
														    "image_mean": self.image_mean,
														    "image_std": self.image_std,
														    "do_pad": self.do_pad,
														}







							def   __SCREAMING_SNAKE_CASE							(		self ,   lowerCamelCase_ ,   lowerCamelCase_=False       )	->							Tuple:
														if not batched:
																					lowerCAmelCase__	       =     image_inputs[0]
																					if isinstance(lowerCamelCase_ ,   Image.Image       ):
																												lowerCAmelCase__       , lowerCAmelCase__	       =     image.size
																					else:
																												lowerCAmelCase__       , lowerCAmelCase__	       =     image.shape[1], image.shape[2]
																					if w < h:
																												lowerCAmelCase__	       =     int(self.size['''shortest_edge'''] * h / w       )
																												lowerCAmelCase__	       =     self.size['''shortest_edge''']
																					elif w > h:
																												lowerCAmelCase__	       =     self.size['''shortest_edge''']
																												lowerCAmelCase__	       =     int(self.size['''shortest_edge'''] * w / h       )
																					else:
																												lowerCAmelCase__	       =     self.size['''shortest_edge''']
																												lowerCAmelCase__	       =     self.size['''shortest_edge''']

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

														return expected_height, expected_width




@require_torch
@require_vision
class 		a__    (							a__    ,    unittest.TestCase  ):





							'''simple docstring'''






							lowercase__      :       Dict     						=				DetrImageProcessor if is_vision_available() else None







							def   __SCREAMING_SNAKE_CASE							(		self       )	->							Optional[int]:
														lowerCAmelCase__	       =     DetrImageProcessingTester(self       )







							@property
							def   __SCREAMING_SNAKE_CASE							(		self       )	->							Optional[Any]:
														return self.image_processor_tester.prepare_image_processor_dict()







							def   __SCREAMING_SNAKE_CASE							(		self       )	->							Optional[Any]:
														lowerCAmelCase__	       =     self.image_processing_class(**self.image_processor_dict       )
														self.assertTrue(hasattr(lowerCamelCase_ ,   '''image_mean'''       )       )
														self.assertTrue(hasattr(lowerCamelCase_ ,   '''image_std'''       )       )
														self.assertTrue(hasattr(lowerCamelCase_ ,   '''do_normalize'''       )       )
														self.assertTrue(hasattr(lowerCamelCase_ ,   '''do_rescale'''       )       )
														self.assertTrue(hasattr(lowerCamelCase_ ,   '''rescale_factor'''       )       )
														self.assertTrue(hasattr(lowerCamelCase_ ,   '''do_resize'''       )       )
														self.assertTrue(hasattr(lowerCamelCase_ ,   '''size'''       )       )
														self.assertTrue(hasattr(lowerCamelCase_ ,   '''do_pad'''       )       )







							def   __SCREAMING_SNAKE_CASE							(		self       )	->							str:
														lowerCAmelCase__	       =     self.image_processing_class.from_dict(self.image_processor_dict       )
														self.assertEqual(image_processor.size ,   {'''shortest_edge''': 18, '''longest_edge''': 13_33}       )
														self.assertEqual(image_processor.do_pad ,   lowerCamelCase_       )

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







							def   __SCREAMING_SNAKE_CASE							(		self       )	->							List[Any]:
														pass







							def   __SCREAMING_SNAKE_CASE							(		self       )	->							Optional[Any]:
														# Initialize image_processing
														lowerCAmelCase__	       =     self.image_processing_class(**self.image_processor_dict       )
														# create random PIL images
														lowerCAmelCase__	       =     prepare_image_inputs(self.image_processor_tester ,   equal_resolution=lowerCamelCase_       )
														for image in image_inputs:
																					self.assertIsInstance(lowerCamelCase_ ,   Image.Image       )

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

														lowerCAmelCase__       , lowerCAmelCase__	       =     self.image_processor_tester.get_expected_values(lowerCamelCase_       )

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

														# Test batched
														lowerCAmelCase__       , lowerCAmelCase__	       =     self.image_processor_tester.get_expected_values(lowerCamelCase_ ,   batched=lowerCamelCase_       )

														lowerCAmelCase__	       =     image_processing(lowerCamelCase_ ,   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       )	->							int:
														# Initialize image_processing
														lowerCAmelCase__	       =     self.image_processing_class(**self.image_processor_dict       )
														# create random numpy tensors
														lowerCAmelCase__	       =     prepare_image_inputs(self.image_processor_tester ,   equal_resolution=lowerCamelCase_ ,   numpify=lowerCamelCase_       )
														for image in image_inputs:
																					self.assertIsInstance(lowerCamelCase_ ,   np.ndarray       )

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

														lowerCAmelCase__       , lowerCAmelCase__	       =     self.image_processor_tester.get_expected_values(lowerCamelCase_       )

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

														# Test batched
														lowerCAmelCase__	       =     image_processing(lowerCamelCase_ ,   return_tensors='''pt'''       ).pixel_values

														lowerCAmelCase__       , lowerCAmelCase__	       =     self.image_processor_tester.get_expected_values(lowerCamelCase_ ,   batched=lowerCamelCase_       )

														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       )	->							Tuple:
														# Initialize image_processing
														lowerCAmelCase__	       =     self.image_processing_class(**self.image_processor_dict       )
														# create random PyTorch tensors
														lowerCAmelCase__	       =     prepare_image_inputs(self.image_processor_tester ,   equal_resolution=lowerCamelCase_ ,   torchify=lowerCamelCase_       )
														for image in image_inputs:
																					self.assertIsInstance(lowerCamelCase_ ,   torch.Tensor       )

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

														lowerCAmelCase__       , lowerCAmelCase__	       =     self.image_processor_tester.get_expected_values(lowerCamelCase_       )

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

														# Test batched
														lowerCAmelCase__	       =     image_processing(lowerCamelCase_ ,   return_tensors='''pt'''       ).pixel_values

														lowerCAmelCase__       , lowerCAmelCase__	       =     self.image_processor_tester.get_expected_values(lowerCamelCase_ ,   batched=lowerCamelCase_       )

														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       )	->							Union[str, Any]:
														# prepare image and target
														lowerCAmelCase__	       =     Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
														with open('''./tests/fixtures/tests_samples/COCO/coco_annotations.txt''' ,   '''r'''       ) as f:
																					lowerCAmelCase__	       =     json.loads(f.read()       )

														lowerCAmelCase__	       =     {'''image_id''': 3_97_69, '''annotations''': target}

														# encode them
														lowerCAmelCase__	       =     DetrImageProcessor.from_pretrained('''facebook/detr-resnet-50'''       )
														lowerCAmelCase__	       =     image_processing(images=lowerCamelCase_ ,   annotations=lowerCamelCase_ ,   return_tensors='''pt'''       )

														# verify pixel values
														lowerCAmelCase__	       =     torch.Size([1, 3, 8_00, 10_66]       )
														self.assertEqual(encoding['''pixel_values'''].shape ,   lowerCamelCase_       )

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

														# verify area
														lowerCAmelCase__	       =     torch.tensor([5_887.9_600, 11_250.2_061, 489_353.8_438, 837_122.7_500, 147_967.5_156, 165_732.3_438]       )
														self.assertTrue(torch.allclose(encoding['''labels'''][0]['''area'''] ,   lowerCamelCase_       )       )
														# verify boxes
														lowerCAmelCase__	       =     torch.Size([6, 4]       )
														self.assertEqual(encoding['''labels'''][0]['''boxes'''].shape ,   lowerCamelCase_       )
														lowerCAmelCase__	       =     torch.tensor([0.5_503, 0.2_765, 0.0_604, 0.2_215]       )
														self.assertTrue(torch.allclose(encoding['''labels'''][0]['''boxes'''][0] ,   lowerCamelCase_ ,   atol=1e-3       )       )
														# verify image_id
														lowerCAmelCase__	       =     torch.tensor([3_97_69]       )
														self.assertTrue(torch.allclose(encoding['''labels'''][0]['''image_id'''] ,   lowerCamelCase_       )       )
														# verify is_crowd
														lowerCAmelCase__	       =     torch.tensor([0, 0, 0, 0, 0, 0]       )
														self.assertTrue(torch.allclose(encoding['''labels'''][0]['''iscrowd'''] ,   lowerCamelCase_       )       )
														# verify class_labels
														lowerCAmelCase__	       =     torch.tensor([75, 75, 63, 65, 17, 17]       )
														self.assertTrue(torch.allclose(encoding['''labels'''][0]['''class_labels'''] ,   lowerCamelCase_       )       )
														# verify orig_size
														lowerCAmelCase__	       =     torch.tensor([4_80, 6_40]       )
														self.assertTrue(torch.allclose(encoding['''labels'''][0]['''orig_size'''] ,   lowerCamelCase_       )       )
														# verify size
														lowerCAmelCase__	       =     torch.tensor([8_00, 10_66]       )
														self.assertTrue(torch.allclose(encoding['''labels'''][0]['''size'''] ,   lowerCamelCase_       )       )







							@slow
							def   __SCREAMING_SNAKE_CASE							(		self       )	->							Optional[Any]:
														# prepare image, target and masks_path
														lowerCAmelCase__	       =     Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
														with open('''./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt''' ,   '''r'''       ) as f:
																					lowerCAmelCase__	       =     json.loads(f.read()       )

														lowerCAmelCase__	       =     {'''file_name''': '''000000039769.png''', '''image_id''': 3_97_69, '''segments_info''': target}

														lowerCAmelCase__	       =     pathlib.Path('''./tests/fixtures/tests_samples/COCO/coco_panoptic'''       )

														# encode them
														lowerCAmelCase__	       =     DetrImageProcessor.from_pretrained('''facebook/detr-resnet-50-panoptic'''       )
														lowerCAmelCase__	       =     image_processing(images=lowerCamelCase_ ,   annotations=lowerCamelCase_ ,   masks_path=lowerCamelCase_ ,   return_tensors='''pt'''       )

														# verify pixel values
														lowerCAmelCase__	       =     torch.Size([1, 3, 8_00, 10_66]       )
														self.assertEqual(encoding['''pixel_values'''].shape ,   lowerCamelCase_       )

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

														# verify area
														lowerCAmelCase__	       =     torch.tensor([147_979.6_875, 165_527.0_469, 484_638.5_938, 11_292.9_375, 5_879.6_562, 7_634.1_147]       )
														self.assertTrue(torch.allclose(encoding['''labels'''][0]['''area'''] ,   lowerCamelCase_       )       )
														# verify boxes
														lowerCAmelCase__	       =     torch.Size([6, 4]       )
														self.assertEqual(encoding['''labels'''][0]['''boxes'''].shape ,   lowerCamelCase_       )
														lowerCAmelCase__	       =     torch.tensor([0.2_625, 0.5_437, 0.4_688, 0.8_625]       )
														self.assertTrue(torch.allclose(encoding['''labels'''][0]['''boxes'''][0] ,   lowerCamelCase_ ,   atol=1e-3       )       )
														# verify image_id
														lowerCAmelCase__	       =     torch.tensor([3_97_69]       )
														self.assertTrue(torch.allclose(encoding['''labels'''][0]['''image_id'''] ,   lowerCamelCase_       )       )
														# verify is_crowd
														lowerCAmelCase__	       =     torch.tensor([0, 0, 0, 0, 0, 0]       )
														self.assertTrue(torch.allclose(encoding['''labels'''][0]['''iscrowd'''] ,   lowerCamelCase_       )       )
														# verify class_labels
														lowerCAmelCase__	       =     torch.tensor([17, 17, 63, 75, 75, 93]       )
														self.assertTrue(torch.allclose(encoding['''labels'''][0]['''class_labels'''] ,   lowerCamelCase_       )       )
														# verify masks
														lowerCAmelCase__	       =     82_28_73
														self.assertEqual(encoding['''labels'''][0]['''masks'''].sum().item() ,   lowerCamelCase_       )
														# verify orig_size
														lowerCAmelCase__	       =     torch.tensor([4_80, 6_40]       )
														self.assertTrue(torch.allclose(encoding['''labels'''][0]['''orig_size'''] ,   lowerCamelCase_       )       )
														# verify size
														lowerCAmelCase__	       =     torch.tensor([8_00, 10_66]       )
														self.assertTrue(torch.allclose(encoding['''labels'''][0]['''size'''] ,   lowerCamelCase_       )       )



'''simple docstring'''






from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


__UpperCAmelCase       =							logging.get_logger(__name__)

__UpperCAmelCase       =							{
    '''distilbert-base-uncased''': '''https://huggingface.co/distilbert-base-uncased/resolve/main/config.json''',
    '''distilbert-base-uncased-distilled-squad''': (
        '''https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/config.json'''
    ),
    '''distilbert-base-cased''': '''https://huggingface.co/distilbert-base-cased/resolve/main/config.json''',
    '''distilbert-base-cased-distilled-squad''': (
        '''https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/config.json'''
    ),
    '''distilbert-base-german-cased''': '''https://huggingface.co/distilbert-base-german-cased/resolve/main/config.json''',
    '''distilbert-base-multilingual-cased''': (
        '''https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/config.json'''
    ),
    '''distilbert-base-uncased-finetuned-sst-2-english''': (
        '''https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json'''
    ),
}




class 		a__    (							a__  ):





							'''simple docstring'''






							lowercase__      :       Optional[Any]     						=				"distilbert"
							lowercase__      :       Tuple     						=				{
							    "hidden_size": "dim",
							    "num_attention_heads": "n_heads",
							    "num_hidden_layers": "n_layers",
							}







							def __init__(		self ,   lowerCamelCase_=3_05_22 ,   lowerCamelCase_=5_12 ,   lowerCamelCase_=False ,   lowerCamelCase_=6 ,   lowerCamelCase_=12 ,   lowerCamelCase_=7_68 ,   lowerCamelCase_=4 * 7_68 ,   lowerCamelCase_=0.1 ,   lowerCamelCase_=0.1 ,   lowerCamelCase_="gelu" ,   lowerCamelCase_=0.02 ,   lowerCamelCase_=0.1 ,   lowerCamelCase_=0.2 ,   lowerCamelCase_=0 ,   **lowerCamelCase_ ,   )	->							Any:
														lowerCAmelCase__	       =     vocab_size
														lowerCAmelCase__	       =     max_position_embeddings
														lowerCAmelCase__	       =     sinusoidal_pos_embds
														lowerCAmelCase__	       =     n_layers
														lowerCAmelCase__	       =     n_heads
														lowerCAmelCase__	       =     dim
														lowerCAmelCase__	       =     hidden_dim
														lowerCAmelCase__	       =     dropout
														lowerCAmelCase__	       =     attention_dropout
														lowerCAmelCase__	       =     activation
														lowerCAmelCase__	       =     initializer_range
														lowerCAmelCase__	       =     qa_dropout
														lowerCAmelCase__	       =     seq_classif_dropout
														super().__init__(**lowerCamelCase_ ,   pad_token_id=lowerCamelCase_       )




class 		a__    (							a__  ):





							'''simple docstring'''






							@property
							def   __SCREAMING_SNAKE_CASE							(		self       )	->							Mapping[str, Mapping[int, str]]:
														if self.task == "multiple-choice":
																					lowerCAmelCase__	       =     {0: '''batch''', 1: '''choice''', 2: '''sequence'''}
														else:
																					lowerCAmelCase__	       =     {0: '''batch''', 1: '''sequence'''}
														return OrderedDict(
														    [
														        ('''input_ids''', dynamic_axis),
														        ('''attention_mask''', dynamic_axis),
														    ]       )