





"""simple docstring"""



from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
       from PIL import Image

       from ..image_utils import load_image

if is_tf_available():
       import tensorflow as tf

       from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
       from ..tf_utils import stable_softmax

if is_torch_available():
       from ..models.auto.modeling_auto import MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING

a_			=      logging.get_logger(__name__)






@add_end_docstrings(SCREAMING_SNAKE_CASE__					)
class 		__snake_case (		SCREAMING_SNAKE_CASE__					):





 """simple docstring"""


 def __init__(   self ,						*__lowerCamelCase ,						**__lowerCamelCase  ):



     '''simple docstring'''




     super().__init__(*__lowerCamelCase ,						**__lowerCamelCase  )
     requires_backends(self ,						'''vision'''  )
     self.check_model_type(
         TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
         if self.framework == '''tf'''
         else MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING  )

 def  UpperCamelCase__(   self ,						__lowerCamelCase=None  ):



     '''simple docstring'''




     __A   :						Tuple									=					{}
     if top_k is not None:
         __A   :						List[str]									=					top_k
     return {}, {}, postprocess_params

 def __call__(   self ,						__lowerCamelCase ,						**__lowerCamelCase  ):



     '''simple docstring'''




     return super().__call__(__lowerCamelCase ,						**__lowerCamelCase  )

 def  UpperCamelCase__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						Any									=					load_image(__lowerCamelCase  )
     __A   :						Tuple									=					self.image_processor(images=__lowerCamelCase ,						return_tensors=self.framework  )
     return model_inputs

 def  UpperCamelCase__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						Any									=					self.model(**__lowerCamelCase  )
     return model_outputs







 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase=5  ):



     '''simple docstring'''




     if top_k > self.model.config.num_labels:
         __A   :						List[Any]									=					self.model.config.num_labels

     if self.framework == "pt":
         __A   :						Optional[int]									=					model_outputs.logits.softmax(-1  )[0]
         __A			,	__A   :						Any									=					probs.topk(__lowerCamelCase  )
     elif self.framework == "tf":
         __A   :						Optional[int]									=					stable_softmax(model_outputs.logits ,						axis=-1  )[0]
         __A   :						str									=					tf.math.top_k(__lowerCamelCase ,						k=__lowerCamelCase  )
         __A			,	__A   :						Union[str, Any]									=					topk.values.numpy(), topk.indices.numpy()
     else:
         raise ValueError(F"""Unsupported framework: {self.framework}"""  )

     __A   :						List[str]									=					scores.tolist()
     __A   :						List[Any]									=					ids.tolist()
     return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(__lowerCamelCase ,						__lowerCamelCase  )]











"""simple docstring"""



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


a_			=      logging.get_logger(__name__)

a_			=      {
    """shi-labs/dinat-mini-in1k-224""": """https://huggingface.co/shi-labs/dinat-mini-in1k-224/resolve/main/config.json""",
    # See all Dinat models at https://huggingface.co/models?filter=dinat
}






class 		__snake_case (		SCREAMING_SNAKE_CASE__							,							SCREAMING_SNAKE_CASE__					):





 """simple docstring"""


 _lowerCamelCase				=					"""dinat"""

 _lowerCamelCase				=					{
     """num_attention_heads""": """num_heads""",
     """num_hidden_layers""": """num_layers""",
 }

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



     '''simple docstring'''




     super().__init__(**__lowerCamelCase  )

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




