






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

UpperCamelCase__     =	logging.get_logger(__name__)

@add_end_docstrings(_a    )
class     __SCREAMING_SNAKE_CASE   (       _a    ):






							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 			_lowerCamelCase						(  self					,	__lowerCAmelCase=None					):
														UpperCamelCase__			       =     {}
														if top_k is not None:
																					UpperCamelCase__			       =     top_k
														return {}, {}, postprocess_params






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






							def 			_lowerCamelCase						(  self					,	__lowerCAmelCase					):
														UpperCamelCase__			       =     load_image(__lowerCAmelCase					)
														UpperCamelCase__			       =     self.image_processor(images=__lowerCAmelCase					,	return_tensors=self.framework					)
														return model_inputs






							def 			_lowerCamelCase						(  self					,	__lowerCAmelCase					):
														UpperCamelCase__			       =     self.model(**__lowerCAmelCase					)
														return model_outputs







							def 			_lowerCamelCase						(  self					,	__lowerCAmelCase					,	__lowerCAmelCase=5					):
														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					)]











import copy

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


UpperCamelCase__     =	logging.get_logger(__name__)

class     __SCREAMING_SNAKE_CASE   (       _a    ):
							snake_case       : List[Any]         =    """upernet"""






							def __init__(  self					,	__lowerCAmelCase=None					,	__lowerCAmelCase=512					,	__lowerCAmelCase=0.02					,	__lowerCAmelCase=[1, 2, 3, 6]					,	__lowerCAmelCase=True					,	__lowerCAmelCase=0.4					,	__lowerCAmelCase=384					,	__lowerCAmelCase=256					,	__lowerCAmelCase=1					,	__lowerCAmelCase=False					,	__lowerCAmelCase=255					,	**__lowerCAmelCase					,	):
														super().__init__(**__lowerCAmelCase					)

														if backbone_config is None:
																					logger.info("""`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone."""					)
																					UpperCamelCase__			       =     CONFIG_MAPPING["""resnet"""](out_features=["""stage1""", """stage2""", """stage3""", """stage4"""]					)
														elif isinstance(__lowerCAmelCase					,	__lowerCAmelCase					):
																					UpperCamelCase__			       =     backbone_config.get("""model_type"""					)
																					UpperCamelCase__			       =     CONFIG_MAPPING[backbone_model_type]
																					UpperCamelCase__			       =     config_class.from_dict(__lowerCAmelCase					)

														UpperCamelCase__			       =     backbone_config
														UpperCamelCase__			       =     hidden_size
														UpperCamelCase__			       =     initializer_range
														UpperCamelCase__			       =     pool_scales
														UpperCamelCase__			       =     use_auxiliary_head
														UpperCamelCase__			       =     auxiliary_loss_weight
														UpperCamelCase__			       =     auxiliary_in_channels
														UpperCamelCase__			       =     auxiliary_channels
														UpperCamelCase__			       =     auxiliary_num_convs
														UpperCamelCase__			       =     auxiliary_concat_input
														UpperCamelCase__			       =     loss_ignore_index







							def 			_lowerCamelCase						(  self					):
														UpperCamelCase__			       =     copy.deepcopy(self.__dict__					)
														UpperCamelCase__			       =     self.backbone_config.to_dict()
														UpperCamelCase__			       =     self.__class__.model_type
														return output




