





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



	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		,   ):
			# by setting size["longest_edge"] > max_resolution we're effectively not testing this :p
			lowerCAmelCase  :     List[str]       						= size if size is not None else {"shortest_edge": 18, "longest_edge": 1_333}
			lowerCAmelCase  :     Optional[int]       						= parent
			lowerCAmelCase  :     List[Any]       						= batch_size
			lowerCAmelCase  :     List[Any]       						= num_channels
			lowerCAmelCase  :     Tuple       						= min_resolution
			lowerCAmelCase  :     Optional[int]       						= max_resolution
			lowerCAmelCase  :     Tuple       						= do_resize
			lowerCAmelCase  :     Optional[int]       						= size
			lowerCAmelCase  :     int       						= do_normalize
			lowerCAmelCase  :     str       						= image_mean
			lowerCAmelCase  :     List[str]       						= image_std
			lowerCAmelCase  :     Union[str, Any]       						= do_rescale
			lowerCAmelCase  :     Tuple       						= rescale_factor
			lowerCAmelCase  :     Any       						= do_pad



	def 	_lowerCamelCase     ( self						):
			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 	_lowerCamelCase     ( self		,   a_		,   a_=False						):
			if not batched:
					lowerCAmelCase  :     str       						= image_inputs[0]
					if isinstance(a_		,   Image.Image						):
							lowerCAmelCase						,		lowerCAmelCase  :     Union[str, Any]       						= image.size
					else:
							lowerCAmelCase						,		lowerCAmelCase  :     Optional[int]       						= image.shape[1], image.shape[2]
					if w < h:
							lowerCAmelCase  :     Tuple       						= int(self.size["shortest_edge"] * h / w						)
							lowerCAmelCase  :     str       						= self.size["shortest_edge"]
					elif w > h:
							lowerCAmelCase  :     str       						= self.size["shortest_edge"]
							lowerCAmelCase  :     int       						= int(self.size["shortest_edge"] * w / h						)
					else:
							lowerCAmelCase  :     List[str]       						= self.size["shortest_edge"]
							lowerCAmelCase  :     Optional[Any]       						= self.size["shortest_edge"]

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

			return expected_height, expected_width






@require_torch
@require_vision
class 		lowerCamelCase	(   _A   , unittest.TestCase							):
	snake_case_   = DeformableDetrImageProcessor if is_vision_available() else None



	def 	_lowerCamelCase     ( self						):
			lowerCAmelCase  :     Union[str, Any]       						= DeformableDetrImageProcessingTester(self						)



	@property
	def 	_lowerCamelCase     ( self						):
			return self.image_processor_tester.prepare_image_processor_dict()



	def 	_lowerCamelCase     ( self						):
			lowerCAmelCase  :     int       						= 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 	_lowerCamelCase     ( self						):
			lowerCAmelCase  :     List[str]       						= 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_						)

			lowerCAmelCase  :     Tuple       						= 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 	_lowerCamelCase     ( self						):
			pass



	def 	_lowerCamelCase     ( self						):
			# Initialize image_processing
			lowerCAmelCase  :     List[str]       						= self.image_processing_class(**self.image_processor_dict						)
			# create random PIL images
			lowerCAmelCase  :     List[str]       						= prepare_image_inputs(self.image_processor_tester		,   equal_resolution=a_						)
			for image in image_inputs:
					self.assertIsInstance(a_		,   Image.Image						)

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

			lowerCAmelCase						,		lowerCAmelCase  :     Tuple       						= 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
			lowerCAmelCase						,		lowerCAmelCase  :     Optional[int]       						= self.image_processor_tester.get_expected_values(a_		,   batched=a_						)

			lowerCAmelCase  :     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 	_lowerCamelCase     ( self						):
			# Initialize image_processing
			lowerCAmelCase  :     int       						= self.image_processing_class(**self.image_processor_dict						)
			# create random numpy tensors
			lowerCAmelCase  :     Tuple       						= 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
			lowerCAmelCase  :     List[Any]       						= image_processing(image_inputs[0]		,   return_tensors="pt"						).pixel_values

			lowerCAmelCase						,		lowerCAmelCase  :     Union[str, 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
			lowerCAmelCase  :     List[str]       						= image_processing(a_		,   return_tensors="pt"						).pixel_values

			lowerCAmelCase						,		lowerCAmelCase  :     Optional[int]       						= 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 	_lowerCamelCase     ( self						):
			# Initialize image_processing
			lowerCAmelCase  :     str       						= self.image_processing_class(**self.image_processor_dict						)
			# create random PyTorch tensors
			lowerCAmelCase  :     Union[str, Any]       						= 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
			lowerCAmelCase  :     int       						= image_processing(image_inputs[0]		,   return_tensors="pt"						).pixel_values

			lowerCAmelCase						,		lowerCAmelCase  :     Dict       						= 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
			lowerCAmelCase  :     Tuple       						= image_processing(a_		,   return_tensors="pt"						).pixel_values

			lowerCAmelCase						,		lowerCAmelCase  :     List[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,
			    )		,   )



	@slow
	def 	_lowerCamelCase     ( self						):
			# prepare image and target
			lowerCAmelCase  :     List[Any]       						= Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"						)
			with open("./tests/fixtures/tests_samples/COCO/coco_annotations.txt"		,   "r"						) as f:
					lowerCAmelCase  :     List[Any]       						= json.loads(f.read()						)

			lowerCAmelCase  :     Union[str, Any]       						= {"image_id": 39_769, "annotations": target}

			# encode them
			lowerCAmelCase  :     List[str]       						= DeformableDetrImageProcessor()
			lowerCAmelCase  :     List[str]       						= image_processing(images=a_		,   annotations=a_		,   return_tensors="pt"						)

			# verify pixel values
			lowerCAmelCase  :     int       						= torch.Size([1, 3, 800, 1_066]						)
			self.assertEqual(encoding["pixel_values"].shape		,   a_						)

			lowerCAmelCase  :     List[Any]       						= torch.tensor([0.2796, 0.3138, 0.3481]						)
			self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3]		,   a_		,   atol=1e-4						)						)

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





	@slow
	def 	_lowerCamelCase     ( self						):
			# prepare image, target and masks_path
			lowerCAmelCase  :     Union[str, Any]       						= Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"						)
			with open("./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt"		,   "r"						) as f:
					lowerCAmelCase  :     Tuple       						= json.loads(f.read()						)

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

			lowerCAmelCase  :     List[str]       						= pathlib.Path("./tests/fixtures/tests_samples/COCO/coco_panoptic"						)

			# encode them
			lowerCAmelCase  :     Optional[Any]       						= DeformableDetrImageProcessor(format="coco_panoptic"						)
			lowerCAmelCase  :     Any       						= image_processing(images=a_		,   annotations=a_		,   masks_path=a_		,   return_tensors="pt"						)

			# verify pixel values
			lowerCAmelCase  :     Tuple       						= torch.Size([1, 3, 800, 1_066]						)
			self.assertEqual(encoding["pixel_values"].shape		,   a_						)

			lowerCAmelCase  :     List[Any]       						= torch.tensor([0.2796, 0.3138, 0.3481]						)
			self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3]		,   a_		,   atol=1e-4						)						)

			# verify area
			lowerCAmelCase  :     Dict       						= torch.tensor([147979.6875, 165527.0469, 484638.5938, 11292.9375, 5879.6562, 7634.1147]						)
			self.assertTrue(torch.allclose(encoding["labels"][0]["area"]		,   a_						)						)
			# verify boxes
			lowerCAmelCase  :     Tuple       						= torch.Size([6, 4]						)
			self.assertEqual(encoding["labels"][0]["boxes"].shape		,   a_						)
			lowerCAmelCase  :     int       						= torch.tensor([0.2625, 0.5437, 0.4688, 0.8625]						)
			self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0]		,   a_		,   atol=1e-3						)						)
			# verify image_id
			lowerCAmelCase  :     int       						= torch.tensor([39_769]						)
			self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"]		,   a_						)						)
			# verify is_crowd
			lowerCAmelCase  :     Any       						= torch.tensor([0, 0, 0, 0, 0, 0]						)
			self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"]		,   a_						)						)
			# verify class_labels
			lowerCAmelCase  :     List[Any]       						= torch.tensor([17, 17, 63, 75, 75, 93]						)
			self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"]		,   a_						)						)
			# verify masks
			lowerCAmelCase  :     Tuple       						= 822_873
			self.assertEqual(encoding["labels"][0]["masks"].sum().item()		,   a_						)
			# verify orig_size
			lowerCAmelCase  :     Optional[int]       						= torch.tensor([480, 640]						)
			self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"]		,   a_						)						)
			# verify size
			lowerCAmelCase  :     List[Any]       						= torch.tensor([800, 1_066]						)
			self.assertTrue(torch.allclose(encoding["labels"][0]["size"]		,   a_						)						)










'''simple docstring'''






import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase									=   logging.get_logger(__name__)

lowerCAmelCase									=   {
    """asapp/sew-d-tiny-100k""": """https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json""",
    # See all SEW-D models at https://huggingface.co/models?filter=sew-d
}
class 		lowerCamelCase	(   _A							):
	snake_case_   = "sew-d"



	def __init__( self		,   a_=32		,   a_=768		,   a_=12		,   a_=12		,   a_=3_072		,   a_=2		,   a_=512		,   a_=256		,   a_=True		,   a_=True		,   a_=("p2c", "c2p")		,   a_="layer_norm"		,   a_="gelu_python"		,   a_=0.1		,   a_=0.1		,   a_=0.1		,   a_=0.0		,   a_=0.1		,   a_=0.02		,   a_=1e-7		,   a_=1e-5		,   a_="group"		,   a_="gelu"		,   a_=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512)		,   a_=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1)		,   a_=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1)		,   a_=False		,   a_=128		,   a_=16		,   a_=True		,   a_=0.05		,   a_=10		,   a_=2		,   a_=0.0		,   a_=10		,   a_=0		,   a_="mean"		,   a_=False		,   a_=False		,   a_=256		,   a_=0		,   a_=1		,   a_=2		,   **a_		,   ):
			super().__init__(**a_		,   pad_token_id=a_		,   bos_token_id=a_		,   eos_token_id=a_						)
			lowerCAmelCase  :     Optional[int]       						= hidden_size
			lowerCAmelCase  :     Optional[int]       						= feat_extract_norm
			lowerCAmelCase  :     str       						= feat_extract_activation
			lowerCAmelCase  :     Dict       						= list(a_						)
			lowerCAmelCase  :     int       						= list(a_						)
			lowerCAmelCase  :     Any       						= list(a_						)
			lowerCAmelCase  :     Optional[Any]       						= conv_bias
			lowerCAmelCase  :     Tuple       						= num_conv_pos_embeddings
			lowerCAmelCase  :     int       						= num_conv_pos_embedding_groups
			lowerCAmelCase  :     Optional[int]       						= len(self.conv_dim						)
			lowerCAmelCase  :     Dict       						= num_hidden_layers
			lowerCAmelCase  :     str       						= intermediate_size
			lowerCAmelCase  :     Any       						= squeeze_factor
			lowerCAmelCase  :     Optional[int]       						= max_position_embeddings
			lowerCAmelCase  :     Tuple       						= position_buckets
			lowerCAmelCase  :     Union[str, Any]       						= share_att_key
			lowerCAmelCase  :     str       						= relative_attention
			lowerCAmelCase  :     List[str]       						= norm_rel_ebd
			lowerCAmelCase  :     int       						= list(a_						)
			lowerCAmelCase  :     str       						= hidden_act
			lowerCAmelCase  :     Dict       						= num_attention_heads
			lowerCAmelCase  :     Optional[Any]       						= hidden_dropout
			lowerCAmelCase  :     int       						= attention_dropout
			lowerCAmelCase  :     Optional[int]       						= activation_dropout
			lowerCAmelCase  :     Any       						= feat_proj_dropout
			lowerCAmelCase  :     Optional[int]       						= final_dropout
			lowerCAmelCase  :     Union[str, Any]       						= layer_norm_eps
			lowerCAmelCase  :     str       						= feature_layer_norm_eps
			lowerCAmelCase  :     Tuple       						= initializer_range
			lowerCAmelCase  :     int       						= vocab_size

			if (
			    (len(self.conv_stride						) != self.num_feat_extract_layers)
			    or (len(self.conv_kernel						) != self.num_feat_extract_layers)
			    or (len(self.conv_dim						) != self.num_feat_extract_layers)
			):
					raise ValueError(
					    "Configuration for convolutional layers is incorrect."
					    "It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,"
					    F'''but is `len(config.conv_dim) = {len(self.conv_dim						)}`, `len(config.conv_stride)'''
					    F'''= {len(self.conv_stride						)}`, `len(config.conv_kernel) = {len(self.conv_kernel						)}`.'''						)

			# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
			lowerCAmelCase  :     List[str]       						= apply_spec_augment
			lowerCAmelCase  :     List[Any]       						= mask_time_prob
			lowerCAmelCase  :     Tuple       						= mask_time_length
			lowerCAmelCase  :     List[Any]       						= mask_time_min_masks
			lowerCAmelCase  :     List[str]       						= mask_feature_prob
			lowerCAmelCase  :     Union[str, Any]       						= mask_feature_length
			lowerCAmelCase  :     List[Any]       						= mask_feature_min_masks

			# ctc loss
			lowerCAmelCase  :     Any       						= ctc_loss_reduction
			lowerCAmelCase  :     str       						= ctc_zero_infinity

			# sequence classification
			lowerCAmelCase  :     Dict       						= use_weighted_layer_sum
			lowerCAmelCase  :     Tuple       						= classifier_proj_size





	@property
	def 	_lowerCamelCase     ( self						):
			return functools.reduce(operator.mul		,   self.conv_stride		,   1						)



