




'''simple docstring'''





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       _A				(		__lowercase       ,     __lowercase     ):
		__a					    =       """pixel_values"""
		__a					    =       False
		__a					    =       TimmBackboneConfig






		def __init__(    self						,		_SCREAMING_SNAKE_CASE						,		**_SCREAMING_SNAKE_CASE			):
								requires_backends(self						,		"""timm"""			)
								super().__init__(_SCREAMING_SNAKE_CASE			)
								_UpperCAmelCase      =			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(_SCREAMING_SNAKE_CASE						,		"""out_features"""			) and config.out_features is not None:
														raise ValueError("""out_features is not supported by TimmBackbone. Please use out_indices instead."""			)

								_UpperCAmelCase      =			getattr(_SCREAMING_SNAKE_CASE						,		"""use_pretrained_backbone"""						,		_SCREAMING_SNAKE_CASE			)
								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.
								_UpperCAmelCase      =			config.out_indices if getattr(_SCREAMING_SNAKE_CASE						,		"""out_indices"""						,		_SCREAMING_SNAKE_CASE			) is not None else (-1,)

								_UpperCAmelCase      =			timm.create_model(
								    config.backbone						,		pretrained=_SCREAMING_SNAKE_CASE						,		features_only=config.features_only						,		in_chans=config.num_channels						,		out_indices=_SCREAMING_SNAKE_CASE						,		**_SCREAMING_SNAKE_CASE						,		)
								# 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.
								_UpperCAmelCase      =			self._backbone.return_layers
								_UpperCAmelCase      =			{layer["""module"""]: str(_SCREAMING_SNAKE_CASE			) for i, layer in enumerate(self._backbone.feature_info.info			)}
								super()._init_backbone(_SCREAMING_SNAKE_CASE			)






		@classmethod
		def 				UpperCAmelCase (    cls						,		_SCREAMING_SNAKE_CASE						,		*_SCREAMING_SNAKE_CASE						,		**_SCREAMING_SNAKE_CASE			):
								requires_backends(cls						,		["""vision""", """timm"""]			)
								from ...models.timm_backbone import TimmBackboneConfig

								_UpperCAmelCase      =			kwargs.pop("""config"""						,		TimmBackboneConfig()			)

								_UpperCAmelCase      =			kwargs.pop("""use_timm_backbone"""						,		_SCREAMING_SNAKE_CASE			)
								if not use_timm:
														raise ValueError("""use_timm_backbone must be True for timm backbones"""			)

								_UpperCAmelCase      =			kwargs.pop("""num_channels"""						,		config.num_channels			)
								_UpperCAmelCase      =			kwargs.pop("""features_only"""						,		config.features_only			)
								_UpperCAmelCase      =			kwargs.pop("""use_pretrained_backbone"""						,		config.use_pretrained_backbone			)
								_UpperCAmelCase      =			kwargs.pop("""out_indices"""						,		config.out_indices			)
								_UpperCAmelCase      =			TimmBackboneConfig(
								    backbone=_SCREAMING_SNAKE_CASE						,		num_channels=_SCREAMING_SNAKE_CASE						,		features_only=_SCREAMING_SNAKE_CASE						,		use_pretrained_backbone=_SCREAMING_SNAKE_CASE						,		out_indices=_SCREAMING_SNAKE_CASE						,		)
								return super()._from_config(_SCREAMING_SNAKE_CASE						,		**_SCREAMING_SNAKE_CASE			)






		def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE			):
								pass






		def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE=None						,		_SCREAMING_SNAKE_CASE=None						,		_SCREAMING_SNAKE_CASE=None						,		**_SCREAMING_SNAKE_CASE			):
								_UpperCAmelCase      =			return_dict if return_dict is not None else self.config.use_return_dict
								_UpperCAmelCase      =			(
								    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
								)
								_UpperCAmelCase      =			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
														_UpperCAmelCase      =			self._all_layers
														_UpperCAmelCase      =			self._backbone(_SCREAMING_SNAKE_CASE						,		**_SCREAMING_SNAKE_CASE			)
														_UpperCAmelCase      =			self._return_layers
														_UpperCAmelCase      =			tuple(hidden_states[i] for i in self.out_indices			)
								else:
														_UpperCAmelCase      =			self._backbone(_SCREAMING_SNAKE_CASE						,		**_SCREAMING_SNAKE_CASE			)
														_UpperCAmelCase      =			None

								_UpperCAmelCase      =			tuple(_SCREAMING_SNAKE_CASE			)
								_UpperCAmelCase      =			tuple(_SCREAMING_SNAKE_CASE			) if hidden_states is not None else None

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

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





# flake8: noqa
# Lint as: python3
a													=			[
    "VerificationMode",
    "Version",
    "disable_progress_bar",
    "enable_progress_bar",
    "is_progress_bar_enabled",
    "experimental",
]

from .info_utils import VerificationMode
from .logging import disable_progress_bar, enable_progress_bar, is_progress_bar_enabled
from .version import Version
from .experimental import experimental