
"""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      _UpperCAmelCase							(		SCREAMING_SNAKE_CASE_     ,			SCREAMING_SNAKE_CASE_							):
						__SCREAMING_SNAKE_CASE   :							str							      =	"pixel_values"
						__SCREAMING_SNAKE_CASE   :							int							      =	False
						__SCREAMING_SNAKE_CASE   :							List[str]							      =	TimmBackboneConfig




						def __init__(							self   ,       lowercase_   ,       **lowercase_							)						->					List[str]:
										requires_backends(self   ,       'timm'							)
										super().__init__(lowercase_							)
										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(lowercase_   ,       '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(lowercase_   ,       'use_pretrained_backbone'   ,       lowercase_							)
										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(lowercase_   ,       'out_indices'   ,       lowercase_							) is not None else (-1,)

										UpperCAmelCase									=					timm.create_model(
										    config.backbone   ,       pretrained=lowercase_   ,       features_only=config.features_only   ,       in_chans=config.num_channels   ,       out_indices=lowercase_   ,       **lowercase_   ,       )
										# 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(lowercase_							) for i, layer in enumerate(self._backbone.feature_info.info							)}
										super()._init_backbone(lowercase_							)




						@classmethod
						def     a_      (							cls   ,       lowercase_   ,       *lowercase_   ,       **lowercase_							)						->					Any:
										requires_backends(cls   ,       ['vision', 'timm']							)
										from ...models.timm_backbone import TimmBackboneConfig

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

										UpperCAmelCase									=					kwargs.pop('use_timm_backbone'   ,       lowercase_							)
										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=lowercase_   ,       num_channels=lowercase_   ,       features_only=lowercase_   ,       use_pretrained_backbone=lowercase_   ,       out_indices=lowercase_   ,       )
										return super()._from_config(lowercase_   ,       **lowercase_							)




						def     a_      (							self   ,       lowercase_							)						->					Optional[int]:
										pass




						def     a_      (							self   ,       lowercase_   ,       lowercase_=None   ,       lowercase_=None   ,       lowercase_=None   ,       **lowercase_							)						->					Union[BackboneOutput, Tuple[Tensor, ...]]:
										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(lowercase_   ,       **lowercase_							)
														UpperCAmelCase									=					self._return_layers
														UpperCAmelCase									=					tuple(hidden_states[i] for i in self.out_indices							)
										else:
														UpperCAmelCase									=					self._backbone(lowercase_   ,       **lowercase_							)
														UpperCAmelCase									=					None

										UpperCAmelCase									=					tuple(lowercase_							)
										UpperCAmelCase									=					tuple(lowercase_							) 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=lowercase_   ,       hidden_states=lowercase_   ,       attentions=lowercase_							)





"""simple docstring"""

from abc import ABC, abstractmethod
from argparse import ArgumentParser
class      _UpperCAmelCase							(		SCREAMING_SNAKE_CASE_							):




      @staticmethod
      @abstractmethod
      def     a_      (							lowercase_							)						->					Optional[Any]:
          raise NotImplementedError()




      @abstractmethod
      def     a_      (							self							)						->					Any:
          raise NotImplementedError()



