


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


snake_case    :   Any	    =					logging.get_logger(__name__)

snake_case    :   str	    =					{
    '''microsoft/swinv2-tiny-patch4-window8-256''': (
        '''https://huggingface.co/microsoft/swinv2-tiny-patch4-window8-256/resolve/main/config.json'''
    ),
}







class 		_snake_case  (				_snake_case							):
   SCREAMING_SNAKE_CASE__				=	'swinv2'

   SCREAMING_SNAKE_CASE__				=	{
       'num_attention_heads': 'num_heads',
       'num_hidden_layers': 'num_layers',
   }




   def __init__(		self ,  _lowerCamelCase=224 ,  _lowerCamelCase=4 ,  _lowerCamelCase=3 ,  _lowerCamelCase=96 ,  _lowerCamelCase=[2, 2, 6, 2] ,  _lowerCamelCase=[3, 6, 12, 24] ,  _lowerCamelCase=7 ,  _lowerCamelCase=4.0 ,  _lowerCamelCase=True ,  _lowerCamelCase=0.0 ,  _lowerCamelCase=0.0 ,  _lowerCamelCase=0.1 ,  _lowerCamelCase="gelu" ,  _lowerCamelCase=False ,  _lowerCamelCase=0.02 ,  _lowerCamelCase=1e-5 ,  _lowerCamelCase=32 ,  **_lowerCamelCase ,  ):
         super().__init__(**_lowerCamelCase   )

         a						:Optional[Any]										=	image_size
         a						:List[Any]										=	patch_size
         a						:Optional[int]										=	num_channels
         a						:Tuple										=	embed_dim
         a						:int										=	depths
         a						:Optional[int]										=	len(_lowerCamelCase   )
         a						:Optional[Any]										=	num_heads
         a						:Tuple										=	window_size
         a						:Optional[int]										=	mlp_ratio
         a						:int										=	qkv_bias
         a						:Optional[int]										=	hidden_dropout_prob
         a						:Union[str, Any]										=	attention_probs_dropout_prob
         a						:int										=	drop_path_rate
         a						:List[str]										=	hidden_act
         a						:Optional[Any]										=	use_absolute_embeddings
         a						:Union[str, Any]										=	layer_norm_eps
         a						:str										=	initializer_range
         a						:Dict										=	encoder_stride
         # we set the hidden_size attribute in order to make Swinv2 work with VisionEncoderDecoderModel
         # this indicates the channel dimension after the last stage of the model
         a						:Optional[Any]										=	int(embed_dim * 2 ** (len(_lowerCamelCase   ) - 1)   )
         a						:List[Any]										=	(0, 0, 0, 0)









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

snake_case    :   str	    =					logging.get_logger(__name__)







@add_end_docstrings(_snake_case							)
class 		_snake_case  (				_snake_case							):




 def __init__(		self ,  *_lowerCamelCase ,  **_lowerCamelCase   ):
       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 	SCREAMING_SNAKE_CASE__    (		self ,  _lowerCamelCase=None   ):
       a						:Tuple										=	{}
       if top_k is not None:
             a						:int										=	top_k
       return {}, {}, postprocess_params




 def __call__(		self ,  _lowerCamelCase ,  **_lowerCamelCase   ):
       return super().__call__(_lowerCamelCase ,  **_lowerCamelCase   )




 def 	SCREAMING_SNAKE_CASE__    (		self ,  _lowerCamelCase   ):
       a						:str										=	load_image(_lowerCamelCase   )
       a						:Any										=	self.image_processor(images=_lowerCamelCase ,  return_tensors=self.framework   )
       return model_inputs




 def 	SCREAMING_SNAKE_CASE__    (		self ,  _lowerCamelCase   ):
       a						:List[str]										=	self.model(**_lowerCamelCase   )
       return model_outputs




 def 	SCREAMING_SNAKE_CASE__    (		self ,  _lowerCamelCase ,  _lowerCamelCase=5   ):
       if top_k > self.model.config.num_labels:
             a						:List[Any]										=	self.model.config.num_labels

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

       a						:Optional[int]										=	scores.tolist()
       a						:str										=	ids.tolist()
       return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(_lowerCamelCase ,  _lowerCamelCase   )]





