






'''simple docstring'''
import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING


a_ 		=       logging.get_logger(__name__)

a_ 		=       {
    "ut/deta": "https://huggingface.co/ut/deta/resolve/main/config.json",
}






class    __SCREAMING_SNAKE_CASE					(	A__ ):
 snake_case_     							=							"""deta"""
 snake_case_     							=							{
     """hidden_size""": """d_model""",
     """num_attention_heads""": """encoder_attention_heads""",
 }




 def __init__(					self		:							Tuple    ,					__lowercase		:							Optional[int]=None    ,					__lowercase		:							int=9_00    ,					__lowercase		:							Optional[Any]=20_48    ,					__lowercase		:							int=6    ,					__lowercase		:							Tuple=20_48    ,					__lowercase		:							Optional[int]=8    ,					__lowercase		:							Any=6    ,					__lowercase		:							str=10_24    ,					__lowercase		:							int=8    ,					__lowercase		:							int=0.0    ,					__lowercase		:							Optional[Any]=True    ,					__lowercase		:							Tuple="relu"    ,					__lowercase		:							Union[str, Any]=2_56    ,					__lowercase		:							Tuple=0.1    ,					__lowercase		:							str=0.0    ,					__lowercase		:							Dict=0.0    ,					__lowercase		:							Tuple=0.02    ,					__lowercase		:							Union[str, Any]=1.0    ,					__lowercase		:							Any=True    ,					__lowercase		:							Tuple=False    ,					__lowercase		:							List[Any]="sine"    ,					__lowercase		:							str=5    ,					__lowercase		:							List[Any]=4    ,					__lowercase		:							str=4    ,					__lowercase		:							Union[str, Any]=True    ,					__lowercase		:							Optional[int]=3_00    ,					__lowercase		:							Dict=True    ,					__lowercase		:							List[Any]=True    ,					__lowercase		:							List[Any]=1    ,					__lowercase		:							List[str]=5    ,					__lowercase		:							int=2    ,					__lowercase		:							Dict=1    ,					__lowercase		:							str=1    ,					__lowercase		:							Optional[Any]=5    ,					__lowercase		:							Union[str, Any]=2    ,					__lowercase		:							List[str]=0.1    ,					__lowercase		:							List[Any]=0.25    ,					**__lowercase		:							Union[str, Any]    ,					)		->  List[str]:






       if backbone_config is None:
             logger.info('''`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone.'''					)
             SCREAMING_SNAKE_CASE__   : Dict													=CONFIG_MAPPING['''resnet'''](out_features=['''stage2''', '''stage3''', '''stage4''']					)
       else:
             if isinstance(_a    ,					_a					):
                   SCREAMING_SNAKE_CASE__   : Dict													=backbone_config.pop('''model_type'''					)
                   SCREAMING_SNAKE_CASE__   : str													=CONFIG_MAPPING[backbone_model_type]
                   SCREAMING_SNAKE_CASE__   : List[str]													=config_class.from_dict(_a					)

       SCREAMING_SNAKE_CASE__   : Union[str, Any]													=backbone_config
       SCREAMING_SNAKE_CASE__   : Tuple													=num_queries
       SCREAMING_SNAKE_CASE__   : Optional[int]													=max_position_embeddings
       SCREAMING_SNAKE_CASE__   : int													=d_model
       SCREAMING_SNAKE_CASE__   : str													=encoder_ffn_dim
       SCREAMING_SNAKE_CASE__   : int													=encoder_layers
       SCREAMING_SNAKE_CASE__   : List[Any]													=encoder_attention_heads
       SCREAMING_SNAKE_CASE__   : Dict													=decoder_ffn_dim
       SCREAMING_SNAKE_CASE__   : List[Any]													=decoder_layers
       SCREAMING_SNAKE_CASE__   : int													=decoder_attention_heads
       SCREAMING_SNAKE_CASE__   : List[Any]													=dropout
       SCREAMING_SNAKE_CASE__   : Any													=attention_dropout
       SCREAMING_SNAKE_CASE__   : Tuple													=activation_dropout
       SCREAMING_SNAKE_CASE__   : Dict													=activation_function
       SCREAMING_SNAKE_CASE__   : Union[str, Any]													=init_std
       SCREAMING_SNAKE_CASE__   : Tuple													=init_xavier_std
       SCREAMING_SNAKE_CASE__   : str													=encoder_layerdrop
       SCREAMING_SNAKE_CASE__   : str													=auxiliary_loss
       SCREAMING_SNAKE_CASE__   : Dict													=position_embedding_type
       # deformable attributes
       SCREAMING_SNAKE_CASE__   : Optional[Any]													=num_feature_levels
       SCREAMING_SNAKE_CASE__   : Union[str, Any]													=encoder_n_points
       SCREAMING_SNAKE_CASE__   : str													=decoder_n_points
       SCREAMING_SNAKE_CASE__   : Optional[int]													=two_stage
       SCREAMING_SNAKE_CASE__   : int													=two_stage_num_proposals
       SCREAMING_SNAKE_CASE__   : Union[str, Any]													=with_box_refine
       SCREAMING_SNAKE_CASE__   : List[str]													=assign_first_stage
       if two_stage is True and with_box_refine is False:
             raise ValueError('''If two_stage is True, with_box_refine must be True.'''					)
       # Hungarian matcher
       SCREAMING_SNAKE_CASE__   : Tuple													=class_cost
       SCREAMING_SNAKE_CASE__   : Tuple													=bbox_cost
       SCREAMING_SNAKE_CASE__   : Union[str, Any]													=giou_cost
       # Loss coefficients
       SCREAMING_SNAKE_CASE__   : Dict													=mask_loss_coefficient
       SCREAMING_SNAKE_CASE__   : str													=dice_loss_coefficient
       SCREAMING_SNAKE_CASE__   : Optional[Any]													=bbox_loss_coefficient
       SCREAMING_SNAKE_CASE__   : List[Any]													=giou_loss_coefficient
       SCREAMING_SNAKE_CASE__   : Any													=eos_coefficient
       SCREAMING_SNAKE_CASE__   : Optional[int]													=focal_alpha
       super().__init__(is_encoder_decoder=_a    ,					**_a					)




 @property
 def    __magic_name__		(					self		:							Dict					)		->  int:
       return self.encoder_attention_heads




 @property
 def    __magic_name__		(					self		:							List[Any]					)		->  int:
       return self.d_model




 def    __magic_name__		(					self		:							Optional[int]					)		->  List[str]:
       SCREAMING_SNAKE_CASE__   : List[str]													=copy.deepcopy(self.__dict__					)
       SCREAMING_SNAKE_CASE__   : List[str]													=self.backbone_config.to_dict()
       SCREAMING_SNAKE_CASE__   : Tuple													=self.__class__.model_type
       return output


'''simple docstring'''
import unittest

import numpy as np
import requests

from transformers.testing_utils import require_torch, require_vision
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

     from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
     lowerCamelCase :      int					 =     False

if is_vision_available():
     from PIL import Image

     from transformers import PixaStructImageProcessor







class 							A__    (       unittest.TestCase  ):





  def __init__(		self      :	List[str]							,					_a      :	List[Any]							,					_a      :	List[str]=7							,					_a      :	List[str]=3							,					_a      :	Tuple=18							,					_a      :	Tuple=30							,					_a      :	str=400							,					_a      :	Tuple=None							,					_a      :	Union[str, Any]=True							,					_a      :	List[str]=True							,					_a      :	Optional[int]=None							,					)	->    List[str]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =size if size is not None else {'height': 20, 'width': 20}
       _SCREAMING_SNAKE_CASE							    =parent
       _SCREAMING_SNAKE_CASE							    =batch_size
       _SCREAMING_SNAKE_CASE							    =num_channels
       _SCREAMING_SNAKE_CASE							    =image_size
       _SCREAMING_SNAKE_CASE							    =min_resolution
       _SCREAMING_SNAKE_CASE							    =max_resolution
       _SCREAMING_SNAKE_CASE							    =size
       _SCREAMING_SNAKE_CASE							    =do_normalize
       _SCREAMING_SNAKE_CASE							    =do_convert_rgb
       _SCREAMING_SNAKE_CASE							    =[512, 1024, 2048, 4096]
       _SCREAMING_SNAKE_CASE							    =patch_size if patch_size is not None else {'height': 16, 'width': 16}





  def 						A  (		self      :	Any )	->    List[str]:





       '''simple docstring'''






       return {"do_normalize": self.do_normalize, "do_convert_rgb": self.do_convert_rgb}





  def 						A  (		self      :	int )	->    str:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    ='https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/australia.jpg'
       _SCREAMING_SNAKE_CASE							    =Image.open(requests.get(_a							,					stream=_a ).raw ).convert('RGB' )
       return raw_image







@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11     ,   reason='`Pix2StructImageProcessor` requires `torch>=1.11.0`.'     ,   )
@require_torch
@require_vision
class 							A__    (       A__     ,   unittest.TestCase  ):
  A__		 =				PixaStructImageProcessor if is_vision_available() else None





  def 						A  (		self      :	Dict )	->    Tuple:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =PixaStructImageProcessingTester(self )





  @property
  def 						A  (		self      :	Optional[Any] )	->    int:





       '''simple docstring'''






       return self.image_processor_tester.prepare_image_processor_dict()





  def 						A  (		self      :	Any )	->    Tuple:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =self.image_processing_class(**self.image_processor_dict )
       self.assertTrue(hasattr(_a							,					'do_normalize' ) )
       self.assertTrue(hasattr(_a							,					'do_convert_rgb' ) )





  def 						A  (		self      :	Any )	->    List[str]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =self.image_processor_tester.prepare_dummy_image()

       _SCREAMING_SNAKE_CASE							    =self.image_processing_class(**self.image_processor_dict )
       _SCREAMING_SNAKE_CASE							    =2048

       _SCREAMING_SNAKE_CASE							    =image_processor(_a							,					return_tensors='pt'							,					max_patches=_a )
       self.assertTrue(torch.allclose(inputs.flattened_patches.mean()							,					torch.tensor(0.06_06 )							,					atol=1e-3							,					rtol=1e-3 ) )





  def 						A  (		self      :	Any )	->    Any:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =self.image_processing_class(**self.image_processor_dict )
       # create random PIL images
       _SCREAMING_SNAKE_CASE							    =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							    =(
           (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
           * self.image_processor_tester.num_channels
       ) + 2

       for max_patch in self.image_processor_tester.max_patches:
            # Test not batched input
            _SCREAMING_SNAKE_CASE							    =image_processor(
                image_inputs[0]							,					return_tensors='pt'							,					max_patches=_a ).flattened_patches
            self.assertEqual(
                encoded_images.shape							,					(1, max_patch, expected_hidden_dim)							,					)

            # Test batched
            _SCREAMING_SNAKE_CASE							    =image_processor(
                _a							,					return_tensors='pt'							,					max_patches=_a ).flattened_patches
            self.assertEqual(
                encoded_images.shape							,					(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)							,					)





  def 						A  (		self      :	List[str] )	->    Optional[Any]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =self.image_processing_class(**self.image_processor_dict )
       # create random PIL images
       _SCREAMING_SNAKE_CASE							    =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							    =(
           (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
           * self.image_processor_tester.num_channels
       ) + 2

       _SCREAMING_SNAKE_CASE							    =True

       for max_patch in self.image_processor_tester.max_patches:
            # Test not batched input
            with self.assertRaises(_a ):
                 _SCREAMING_SNAKE_CASE							    =image_processor(
                     image_inputs[0]							,					return_tensors='pt'							,					max_patches=_a ).flattened_patches

            _SCREAMING_SNAKE_CASE							    ='Hello'

            _SCREAMING_SNAKE_CASE							    =image_processor(
                image_inputs[0]							,					return_tensors='pt'							,					max_patches=_a							,					header_text=_a ).flattened_patches
            self.assertEqual(
                encoded_images.shape							,					(1, max_patch, expected_hidden_dim)							,					)

            # Test batched
            _SCREAMING_SNAKE_CASE							    =image_processor(
                _a							,					return_tensors='pt'							,					max_patches=_a							,					header_text=_a ).flattened_patches
            self.assertEqual(
                encoded_images.shape							,					(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)							,					)





  def 						A  (		self      :	List[Any] )	->    Any:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =self.image_processing_class(**self.image_processor_dict )
       # create random numpy tensors
       _SCREAMING_SNAKE_CASE							    =prepare_image_inputs(self.image_processor_tester							,					equal_resolution=_a							,					numpify=_a )
       for image in image_inputs:
            self.assertIsInstance(_a							,					np.ndarray )

       _SCREAMING_SNAKE_CASE							    =(
           (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
           * self.image_processor_tester.num_channels
       ) + 2

       for max_patch in self.image_processor_tester.max_patches:
            # Test not batched input
            _SCREAMING_SNAKE_CASE							    =image_processor(
                image_inputs[0]							,					return_tensors='pt'							,					max_patches=_a ).flattened_patches
            self.assertEqual(
                encoded_images.shape							,					(1, max_patch, expected_hidden_dim)							,					)

            # Test batched
            _SCREAMING_SNAKE_CASE							    =image_processor(
                _a							,					return_tensors='pt'							,					max_patches=_a ).flattened_patches
            self.assertEqual(
                encoded_images.shape							,					(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)							,					)





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





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =self.image_processing_class(**self.image_processor_dict )
       # create random PyTorch tensors
       _SCREAMING_SNAKE_CASE							    =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							    =(
           (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
           * self.image_processor_tester.num_channels
       ) + 2

       for max_patch in self.image_processor_tester.max_patches:
            # Test not batched input
            _SCREAMING_SNAKE_CASE							    =image_processor(
                image_inputs[0]							,					return_tensors='pt'							,					max_patches=_a ).flattened_patches
            self.assertEqual(
                encoded_images.shape							,					(1, max_patch, expected_hidden_dim)							,					)

            # Test batched
            _SCREAMING_SNAKE_CASE							    =image_processor(
                _a							,					return_tensors='pt'							,					max_patches=_a ).flattened_patches
            self.assertEqual(
                encoded_images.shape							,					(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)							,					)







@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11     ,   reason='`Pix2StructImageProcessor` requires `torch>=1.11.0`.'     ,   )
@require_torch
@require_vision
class 							A__    (       A__     ,   unittest.TestCase  ):
  A__		 =				PixaStructImageProcessor if is_vision_available() else None





  def 						A  (		self      :	str )	->    Dict:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =PixaStructImageProcessingTester(self							,					num_channels=4 )
       _SCREAMING_SNAKE_CASE							    =3





  @property
  def 						A  (		self      :	List[str] )	->    Optional[Any]:





       '''simple docstring'''






       return self.image_processor_tester.prepare_image_processor_dict()





  def 						A  (		self      :	List[str] )	->    Tuple:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =self.image_processing_class(**self.image_processor_dict )
       self.assertTrue(hasattr(_a							,					'do_normalize' ) )
       self.assertTrue(hasattr(_a							,					'do_convert_rgb' ) )





  def 						A  (		self      :	Dict )	->    int:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE							    =self.image_processing_class(**self.image_processor_dict )
       # create random PIL images
       _SCREAMING_SNAKE_CASE							    =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							    =(
           (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
           * (self.image_processor_tester.num_channels - 1)
       ) + 2

       for max_patch in self.image_processor_tester.max_patches:
            # Test not batched input
            _SCREAMING_SNAKE_CASE							    =image_processor(
                image_inputs[0]							,					return_tensors='pt'							,					max_patches=_a ).flattened_patches
            self.assertEqual(
                encoded_images.shape							,					(1, max_patch, expected_hidden_dim)							,					)

            # Test batched
            _SCREAMING_SNAKE_CASE							    =image_processor(
                _a							,					return_tensors='pt'							,					max_patches=_a ).flattened_patches
            self.assertEqual(
                encoded_images.shape							,					(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)							,					)
