



from typing import Tuple, Union

from ...modeling_outputs import BackboneOutput
from ...modeling_utils import PreTrainedModel
from ...utils import is_timm_available, is_torch_available, requires_backends
from ...utils.backbone_utils import BackboneMixin
from .configuration_timm_backbone import TimmBackboneConfig


if is_timm_available():
				import timm


if is_torch_available():
				from torch import Tensor



class        lowerCAmelCase__					(  _lowerCamelCase ,				_lowerCamelCase       ):
				A_   :     Optional[int]								=     'pixel_values'
				A_   :     List[str]								=     False
				A_   :     Optional[int]								=     TimmBackboneConfig


				def __init__(							self      :  List[str]						, __UpperCamelCase      :  int						, **__UpperCamelCase      :  int       )		->     Any:
											requires_backends(self						, 'timm'       )
											super().__init__(__UpperCamelCase       )
											A      						=    config

											if config.backbone is None:
																		raise ValueError('backbone is not set in the config. Please set it to a timm model name.'       )

											if config.backbone not in timm.list_models():
																		raise ValueError(f'''backbone {config.backbone} is not supported by timm.'''       )

											if hasattr(__UpperCamelCase						, 'out_features'       ) and config.out_features is not None:
																		raise ValueError('out_features is not supported by TimmBackbone. Please use out_indices instead.'       )

											A      						=    getattr(__UpperCamelCase						, 'use_pretrained_backbone'						, __UpperCamelCase       )
											if pretrained is None:
																		raise ValueError('use_pretrained_backbone is not set in the config. Please set it to True or False.'       )

											# We just take the final layer by default. This matches the default for the transformers models.
											A      						=    config.out_indices if getattr(__UpperCamelCase						, 'out_indices'						, __UpperCamelCase       ) is not None else (-1,)

											A      						=    timm.create_model(
											    config.backbone						, pretrained=__UpperCamelCase						, features_only=config.features_only						, in_chans=config.num_channels						, out_indices=__UpperCamelCase						, **__UpperCamelCase						, )
											# These are used to control the output of the model when called. If output_hidden_states is True, then
											# return_layers is modified to include all layers.
											A      						=    self._backbone.return_layers
											A      						=    {layer['module']: str(__UpperCamelCase       ) for i, layer in enumerate(self._backbone.feature_info.info       )}
											super()._init_backbone(__UpperCamelCase       )


				@classmethod
				def __UpperCamelCase						(							cls      :  Optional[int]						, __UpperCamelCase      :  Union[str, Any]						, *__UpperCamelCase      :  Dict						, **__UpperCamelCase      :  List[str]       )		->     str:
											requires_backends(cls						, ['vision', 'timm']       )
											from ...models.timm_backbone import TimmBackboneConfig

											A      						=    kwargs.pop('config'						, TimmBackboneConfig()       )

											A      						=    kwargs.pop('use_timm_backbone'						, __UpperCamelCase       )
											if not use_timm:
																		raise ValueError('use_timm_backbone must be True for timm backbones'       )

											A      						=    kwargs.pop('num_channels'						, config.num_channels       )
											A      						=    kwargs.pop('features_only'						, config.features_only       )
											A      						=    kwargs.pop('use_pretrained_backbone'						, config.use_pretrained_backbone       )
											A      						=    kwargs.pop('out_indices'						, config.out_indices       )
											A      						=    TimmBackboneConfig(
											    backbone=__UpperCamelCase						, num_channels=__UpperCamelCase						, features_only=__UpperCamelCase						, use_pretrained_backbone=__UpperCamelCase						, out_indices=__UpperCamelCase						, )
											return super()._from_config(__UpperCamelCase						, **__UpperCamelCase       )


				def __UpperCamelCase						(							self      :  List[Any]						, __UpperCamelCase      :  Optional[int]       )		->     Optional[Any]:
											pass


				def __UpperCamelCase						(							self      :  Union[str, Any]						, __UpperCamelCase      :  int						, __UpperCamelCase      :  List[str]=None						, __UpperCamelCase      :  Optional[Any]=None						, __UpperCamelCase      :  Tuple=None						, **__UpperCamelCase      :  List[str]       )		->     Union[BackboneOutput, Tuple[Tensor, ...]]:
											A      						=    return_dict if return_dict is not None else self.config.use_return_dict
											A      						=    (
											    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
											)
											A      						=    output_attentions if output_attentions is not None else self.config.output_attentions

											if output_attentions:
																		raise ValueError('Cannot output attentions for timm backbones at the moment'       )

											if output_hidden_states:
																		# We modify the return layers to include all the stages of the backbone
																		A      						=    self._all_layers
																		A      						=    self._backbone(__UpperCamelCase						, **__UpperCamelCase       )
																		A      						=    self._return_layers
																		A      						=    tuple(hidden_states[i] for i in self.out_indices       )
											else:
																		A      						=    self._backbone(__UpperCamelCase						, **__UpperCamelCase       )
																		A      						=    None

											A      						=    tuple(__UpperCamelCase       )
											A      						=    tuple(__UpperCamelCase       ) if hidden_states is not None else None

											if not return_dict:
																		A      						=    (feature_maps,)
																		if output_hidden_states:
																									A      						=    output + (hidden_states,)
																		return output

											return BackboneOutput(feature_maps=__UpperCamelCase						, hidden_states=__UpperCamelCase						, attentions=__UpperCamelCase       )


from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


_lowerCAmelCase  :      Tuple			 ={
    """configuration_mobilenet_v2""": [
        """MOBILENET_V2_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """MobileNetV2Config""",
        """MobileNetV2OnnxConfig""",
    ],
}

try:
					if not is_vision_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					_lowerCAmelCase  :      Tuple			 =["""MobileNetV2FeatureExtractor"""]
					_lowerCAmelCase  :      Optional[int]			 =["""MobileNetV2ImageProcessor"""]


try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					_lowerCAmelCase  :      List[str]			 =[
					    """MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST""",
					    """MobileNetV2ForImageClassification""",
					    """MobileNetV2ForSemanticSegmentation""",
					    """MobileNetV2Model""",
					    """MobileNetV2PreTrainedModel""",
					    """load_tf_weights_in_mobilenet_v2""",
					]


if TYPE_CHECKING:
					from .configuration_mobilenet_va import (
					    MOBILENET_V2_PRETRAINED_CONFIG_ARCHIVE_MAP,
					    MobileNetVaConfig,
					    MobileNetVaOnnxConfig,
					)

					try:
										if not is_vision_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .feature_extraction_mobilenet_va import MobileNetVaFeatureExtractor
										from .image_processing_mobilenet_va import MobileNetVaImageProcessor

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_mobilenet_va import (
										    MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST,
										    MobileNetVaForImageClassification,
										    MobileNetVaForSemanticSegmentation,
										    MobileNetVaModel,
										    MobileNetVaPreTrainedModel,
										    load_tf_weights_in_mobilenet_va,
										)


else:
					import sys

					_lowerCAmelCase  :      Any			 =_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)