





from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


__lowerCamelCase	:					Any			  =					logging.get_logger(__name__)

__lowerCamelCase	:					Union[str, Any]			  =					{
    """shi-labs/nat-mini-in1k-224""": """https://huggingface.co/shi-labs/nat-mini-in1k-224/resolve/main/config.json""",
    # See all Nat models at https://huggingface.co/models?filter=nat
}



class  A__       (	__snake_case	,       __snake_case      ):
      _UpperCAmelCase     :Union[str, Any]						   =       'nat'

      _UpperCAmelCase     :Tuple						   =       {
          'num_attention_heads': 'num_heads',
          'num_hidden_layers': 'num_layers',
      }





      def __init__(			self	,							A_=4	,							A_=3	,							A_=64	,							A_=[3, 4, 6, 5]	,							A_=[2, 4, 8, 16]	,							A_=7	,							A_=3.0	,							A_=True	,							A_=0.0	,							A_=0.0	,							A_=0.1	,							A_="gelu"	,							A_=0.02	,							A_=1e-5	,							A_=0.0	,							A_=None	,							A_=None	,							**A_	,							):
       '''simple docstring'''




       super().__init__(**A_       )

       UpperCamelCase     : Any	     =       patch_size
       UpperCamelCase     : Dict	     =       num_channels
       UpperCamelCase     : Dict	     =       embed_dim
       UpperCamelCase     : Optional[int]	     =       depths
       UpperCamelCase     : List[str]	     =       len(A_       )
       UpperCamelCase     : Dict	     =       num_heads
       UpperCamelCase     : Optional[int]	     =       kernel_size
       UpperCamelCase     : Optional[int]	     =       mlp_ratio
       UpperCamelCase     : Dict	     =       qkv_bias
       UpperCamelCase     : Optional[int]	     =       hidden_dropout_prob
       UpperCamelCase     : Dict	     =       attention_probs_dropout_prob
       UpperCamelCase     : Union[str, Any]	     =       drop_path_rate
       UpperCamelCase     : Dict	     =       hidden_act
       UpperCamelCase     : str	     =       layer_norm_eps
       UpperCamelCase     : int	     =       initializer_range
       # we set the hidden_size attribute in order to make Nat work with VisionEncoderDecoderModel
       # this indicates the channel dimension after the last stage of the model
       UpperCamelCase     : Optional[int]	     =       int(embed_dim * 2 ** (len(A_       ) - 1)       )
       UpperCamelCase     : List[str]	     =       layer_scale_init_value
       UpperCamelCase     : Tuple	     =       ["stem"] + [F"""stage{idx}""" for idx in range(1	,							len(A_       ) + 1       )]
       UpperCamelCase     ,   UpperCamelCase     : Dict	     =       get_aligned_output_features_output_indices(
           out_features=A_	,							out_indices=A_	,							stage_names=self.stage_names       )







from pathlib import Path

import numpy as np
from PIL import Image



def 		A_    (					_lowerCAmelCase					)						->			np.ndarray:
 UpperCamelCase     ,   UpperCamelCase     ,   UpperCamelCase     : Optional[Any]	     =       rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2]
 return 0.2_989 * r + 0.5_870 * g + 0.1_140 * b



def 		A_    (					_lowerCAmelCase					)						->			np.ndarray:
 return (gray > 127) & (gray <= 255)



def 		A_    (					_lowerCAmelCase							,       _lowerCAmelCase					)						->			np.ndarray:
 UpperCamelCase     : Dict	     =       np.zeros_like(_lowerCAmelCase					)
 UpperCamelCase     : List[str]	     =       np.zeros(
     (image.shape[0] + kernel.shape[0] - 1, image.shape[1] + kernel.shape[1] - 1)					)

 # Copy image to padded image
 UpperCamelCase     : Tuple	     =       image

 # Iterate over image & apply kernel
 for x in range(image.shape[1]					):
  for y in range(image.shape[0]					):
   UpperCamelCase     : List[str]	     =       (
       kernel * image_padded[y : y + kernel.shape[0], x : x + kernel.shape[1]]
   ).sum()
   UpperCamelCase     : int	     =       int(summation > 0					)
 return output


if __name__ == "__main__":
    # read original image
    __lowerCamelCase	:					int			  =					Path(__file__).resolve().parent / """image_data""" / """lena.jpg"""
    __lowerCamelCase	:					Tuple			  =					np.array(Image.open(lena_path))
    # kernel to be applied
    __lowerCamelCase	:					Optional[Any]			  =					np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]])
    __lowerCamelCase	:					Union[str, Any]			  =					dilation(gray_to_binary(rgb_to_gray(lena)), structuring_element)
    # Save the output image
    __lowerCamelCase	:					Any			  =					Image.fromarray(output).convert("""RGB""")
    pil_img.save("""result_dilation.png""")
