

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

lowerCAmelCase__       = logging.get_logger(__name__)






@add_end_docstrings(_snake_case    )
class 						__magic_name__    (       _snake_case    ):


	def __init__(						self		: str			,						*lowerCAmelCase__		: Any			,						**lowerCAmelCase__		: int    )    ->      Any:
			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		: Dict			,						lowerCAmelCase__		: Union[str, Any]=None    )    ->      int:
			UpperCAmelCase					      =  {}
			if top_k is not None:
					UpperCAmelCase					      =  top_k
			return {}, {}, postprocess_params


	def __call__(						self		: Tuple			,						lowerCAmelCase__		: Union[str, List[str], "Image.Image", List["Image.Image"]]			,						**lowerCAmelCase__		: str    )    ->      Union[str, Any]:
			return super().__call__(lowerCAmelCase__			,						**lowerCAmelCase__    )


	def _UpperCamelCase	(						self		: List[Any]			,						lowerCAmelCase__		: Any    )    ->      Optional[Any]:
			UpperCAmelCase					      =  load_image(lowerCAmelCase__    )
			UpperCAmelCase					      =  self.image_processor(images=lowerCAmelCase__			,						return_tensors=self.framework    )
			return model_inputs


	def _UpperCamelCase	(						self		: Any			,						lowerCAmelCase__		: Any    )    ->      Union[str, Any]:
			UpperCAmelCase					      =  self.model(**lowerCAmelCase__    )
			return model_outputs


	def _UpperCamelCase	(						self		: List[Any]			,						lowerCAmelCase__		: Any			,						lowerCAmelCase__		: Tuple=5    )    ->      Union[str, Any]:
			if top_k > self.model.config.num_labels:
					UpperCAmelCase					      =  self.model.config.num_labels

			if self.framework == "pt":
					UpperCAmelCase					      =  model_outputs.logits.softmax(-1    )[0]
					UpperCAmelCase      ,	UpperCAmelCase					      =  probs.topk(lowerCAmelCase__    )
			elif self.framework == "tf":
					UpperCAmelCase					      =  stable_softmax(model_outputs.logits			,						axis=-1    )[0]
					UpperCAmelCase					      =  tf.math.top_k(lowerCAmelCase__			,						k=lowerCAmelCase__    )
					UpperCAmelCase      ,	UpperCAmelCase					      =  topk.values.numpy(), topk.indices.numpy()
			else:
					raise ValueError(f"Unsupported framework: {self.framework}"    )

			UpperCAmelCase					      =  scores.tolist()
			UpperCAmelCase					      =  ids.tolist()
			return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(lowerCAmelCase__			,						lowerCAmelCase__    )]






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


lowerCAmelCase__       = logging.get_logger(__name__)

lowerCAmelCase__       = {
    "facebook/convnextv2-tiny-1k-224": "https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json",
}






class 						__magic_name__    (       _snake_case	,						_snake_case    ):
 UpperCAmelCase							    =						"""convnextv2"""


 def __init__(						self		: Optional[Any]			,						lowerCAmelCase__		: List[Any]=3			,						lowerCAmelCase__		: str=4			,						lowerCAmelCase__		: Dict=4			,						lowerCAmelCase__		: Optional[Any]=None			,						lowerCAmelCase__		: Optional[Any]=None			,						lowerCAmelCase__		: str="gelu"			,						lowerCAmelCase__		: Optional[int]=0.02			,						lowerCAmelCase__		: Dict=1e-1_2			,						lowerCAmelCase__		: str=0.0			,						lowerCAmelCase__		: str=2_2_4			,						lowerCAmelCase__		: int=None			,						lowerCAmelCase__		: List[Any]=None			,						**lowerCAmelCase__		: List[Any]			,						)    ->      List[Any]:
   super().__init__(**lowerCAmelCase__    )

   UpperCAmelCase					      =  num_channels
   UpperCAmelCase					      =  patch_size
   UpperCAmelCase					      =  num_stages
   UpperCAmelCase					      =  [9_6, 1_9_2, 3_8_4, 7_6_8] if hidden_sizes is None else hidden_sizes
   UpperCAmelCase					      =  [3, 3, 9, 3] if depths is None else depths
   UpperCAmelCase					      =  hidden_act
   UpperCAmelCase					      =  initializer_range
   UpperCAmelCase					      =  layer_norm_eps
   UpperCAmelCase					      =  drop_path_rate
   UpperCAmelCase					      =  image_size
   UpperCAmelCase					      =  ["stem"] + [f"stage{idx}" for idx in range(1			,						len(self.depths    ) + 1    )]
   UpperCAmelCase      ,	UpperCAmelCase					      =  get_aligned_output_features_output_indices(
       out_features=lowerCAmelCase__			,						out_indices=lowerCAmelCase__			,						stage_names=self.stage_names    )



