



from typing import Optional

import torch
import torch.utils.checkpoint
from torch import Tensor, nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...modeling_outputs import (
    BackboneOutput,
    BaseModelOutputWithNoAttention,
    BaseModelOutputWithPoolingAndNoAttention,
    ImageClassifierOutputWithNoAttention,
)
from ...modeling_utils import PreTrainedModel
from ...utils import (
    add_code_sample_docstrings,
    add_start_docstrings,
    add_start_docstrings_to_model_forward,
    logging,
    replace_return_docstrings,
)
from ...utils.backbone_utils import BackboneMixin
from .configuration_resnet import ResNetConfig


UpperCAmelCase_		 =					logging.get_logger(__name__)

# General docstring
UpperCAmelCase_		 =					"""ResNetConfig"""

# Base docstring
UpperCAmelCase_		 =					"""microsoft/resnet-50"""
UpperCAmelCase_		 =					[1, 2_0_4_8, 7, 7]

# Image classification docstring
UpperCAmelCase_		 =					"""microsoft/resnet-50"""
UpperCAmelCase_		 =					"""tiger cat"""

UpperCAmelCase_		 =					[
    """microsoft/resnet-50""",
    # See all resnet models at https://huggingface.co/models?filter=resnet
]


class 	lowerCamelCase__	(       nn.Module):



 """simple docstring"""

 def __init__(    self					:					Union[str, Any]			,						__lowerCAmelCase					:					int			,						__lowerCAmelCase					:					int			,						__lowerCAmelCase					:					int = 3			,						__lowerCAmelCase					:					int = 1			,						__lowerCAmelCase					:					str = "relu"      )   ->	Tuple:
    super().__init__()
    _A      =       nn.Convad(
        __lowerCAmelCase			,						__lowerCAmelCase			,						kernel_size=__lowerCAmelCase			,						stride=__lowerCAmelCase			,						padding=kernel_size // 2			,						bias=__lowerCAmelCase      )
    _A      =       nn.BatchNormad(__lowerCAmelCase      )
    _A      =       ACTaFN[activation] if activation is not None else nn.Identity()

 def     snake_case_      (    self					:					Tuple			,						__lowerCAmelCase					:					Tensor      )   ->	Tensor:
    _A      =       self.convolution(__lowerCAmelCase      )
    _A      =       self.normalization(__lowerCAmelCase      )
    _A      =       self.activation(__lowerCAmelCase      )
    return hidden_state


class 	lowerCamelCase__	(       nn.Module):



 """simple docstring"""

 def __init__(    self					:					Optional[int]			,						__lowerCAmelCase					:					ResNetConfig      )   ->	List[Any]:
    super().__init__()
    _A      =       ResNetConvLayer(
        config.num_channels			,						config.embedding_size			,						kernel_size=7			,						stride=2			,						activation=config.hidden_act      )
    _A      =       nn.MaxPoolad(kernel_size=3			,						stride=2			,						padding=1      )
    _A      =       config.num_channels

 def     snake_case_      (    self					:					List[str]			,						__lowerCAmelCase					:					Tensor      )   ->	Tensor:
    _A      =       pixel_values.shape[1]
    if num_channels != self.num_channels:
       raise ValueError(
           '''Make sure that the channel dimension of the pixel values match with the one set in the configuration.'''      )
    _A      =       self.embedder(__lowerCAmelCase      )
    _A      =       self.pooler(__lowerCAmelCase      )
    return embedding


class 	lowerCamelCase__	(       nn.Module):



 """simple docstring"""

 def __init__(    self					:					str			,						__lowerCAmelCase					:					int			,						__lowerCAmelCase					:					int			,						__lowerCAmelCase					:					int = 2      )   ->	List[str]:
    super().__init__()
    _A      =       nn.Convad(__lowerCAmelCase			,						__lowerCAmelCase			,						kernel_size=1			,						stride=__lowerCAmelCase			,						bias=__lowerCAmelCase      )
    _A      =       nn.BatchNormad(__lowerCAmelCase      )

 def     snake_case_      (    self					:					List[Any]			,						__lowerCAmelCase					:					Tensor      )   ->	Tensor:
    _A      =       self.convolution(__lowerCAmelCase      )
    _A      =       self.normalization(__lowerCAmelCase      )
    return hidden_state


class 	lowerCamelCase__	(       nn.Module):



 """simple docstring"""

 def __init__(    self					:					Optional[int]			,						__lowerCAmelCase					:					int			,						__lowerCAmelCase					:					int			,						__lowerCAmelCase					:					int = 1			,						__lowerCAmelCase					:					str = "relu"      )   ->	Dict:
    super().__init__()
    _A      =       in_channels != out_channels or stride != 1
    _A      =       (
        ResNetShortCut(__lowerCAmelCase			,						__lowerCAmelCase			,						stride=__lowerCAmelCase      ) if should_apply_shortcut else nn.Identity()
    )
    _A      =       nn.Sequential(
        ResNetConvLayer(__lowerCAmelCase			,						__lowerCAmelCase			,						stride=__lowerCAmelCase      )			,						ResNetConvLayer(__lowerCAmelCase			,						__lowerCAmelCase			,						activation=__lowerCAmelCase      )			,						)
    _A      =       ACTaFN[activation]

 def     snake_case_      (    self					:					Any			,						__lowerCAmelCase					:					Any      )   ->	List[str]:
    _A      =       hidden_state
    _A      =       self.layer(__lowerCAmelCase      )
    _A      =       self.shortcut(__lowerCAmelCase      )
    hidden_state += residual
    _A      =       self.activation(__lowerCAmelCase      )
    return hidden_state


class 	lowerCamelCase__	(       nn.Module):



 """simple docstring"""

 def __init__(    self					:					Tuple			,						__lowerCAmelCase					:					int			,						__lowerCAmelCase					:					int			,						__lowerCAmelCase					:					int = 1			,						__lowerCAmelCase					:					str = "relu"			,						__lowerCAmelCase					:					int = 4      )   ->	Optional[Any]:
    super().__init__()
    _A      =       in_channels != out_channels or stride != 1
    _A      =       out_channels // reduction
    _A      =       (
        ResNetShortCut(__lowerCAmelCase			,						__lowerCAmelCase			,						stride=__lowerCAmelCase      ) if should_apply_shortcut else nn.Identity()
    )
    _A      =       nn.Sequential(
        ResNetConvLayer(__lowerCAmelCase			,						__lowerCAmelCase			,						kernel_size=1      )			,						ResNetConvLayer(__lowerCAmelCase			,						__lowerCAmelCase			,						stride=__lowerCAmelCase      )			,						ResNetConvLayer(__lowerCAmelCase			,						__lowerCAmelCase			,						kernel_size=1			,						activation=__lowerCAmelCase      )			,						)
    _A      =       ACTaFN[activation]

 def     snake_case_      (    self					:					Any			,						__lowerCAmelCase					:					List[str]      )   ->	Optional[Any]:
    _A      =       hidden_state
    _A      =       self.layer(__lowerCAmelCase      )
    _A      =       self.shortcut(__lowerCAmelCase      )
    hidden_state += residual
    _A      =       self.activation(__lowerCAmelCase      )
    return hidden_state


class 	lowerCamelCase__	(       nn.Module):



 """simple docstring"""

 def __init__(    self					:					str			,						__lowerCAmelCase					:					ResNetConfig			,						__lowerCAmelCase					:					int			,						__lowerCAmelCase					:					int			,						__lowerCAmelCase					:					int = 2			,						__lowerCAmelCase					:					int = 2			,						)   ->	Tuple:
    super().__init__()

    _A      =       ResNetBottleNeckLayer if config.layer_type == '''bottleneck''' else ResNetBasicLayer

    _A      =       nn.Sequential(
        # downsampling is done in the first layer with stride of 2
        layer(__lowerCAmelCase			,						__lowerCAmelCase			,						stride=__lowerCAmelCase			,						activation=config.hidden_act      )			,						*[layer(__lowerCAmelCase			,						__lowerCAmelCase			,						activation=config.hidden_act      ) for _ in range(depth - 1      )]			,						)

 def     snake_case_      (    self					:					Any			,						__lowerCAmelCase					:					Tensor      )   ->	Tensor:
    _A      =       input
    for layer in self.layers:
       _A      =       layer(__lowerCAmelCase      )
    return hidden_state


class 	lowerCamelCase__	(       nn.Module):



 """simple docstring"""

 def __init__(    self					:					Dict			,						__lowerCAmelCase					:					ResNetConfig      )   ->	Optional[Any]:
    super().__init__()
    _A      =       nn.ModuleList([]      )
    # based on `downsample_in_first_stage` the first layer of the first stage may or may not downsample the input
    self.stages.append(
        ResNetStage(
            __lowerCAmelCase			,						config.embedding_size			,						config.hidden_sizes[0]			,						stride=2 if config.downsample_in_first_stage else 1			,						depth=config.depths[0]			,						)      )
    _A      =       zip(config.hidden_sizes			,						config.hidden_sizes[1:]      )
    for (in_channels, out_channels), depth in zip(__lowerCAmelCase			,						config.depths[1:]      ):
       self.stages.append(ResNetStage(__lowerCAmelCase			,						__lowerCAmelCase			,						__lowerCAmelCase			,						depth=__lowerCAmelCase      )      )

 def     snake_case_      (    self					:					List[Any]			,						__lowerCAmelCase					:					Tensor			,						__lowerCAmelCase					:					bool = False			,						__lowerCAmelCase					:					bool = True      )   ->	BaseModelOutputWithNoAttention:
    _A      =       () if output_hidden_states else None

    for stage_module in self.stages:
       if output_hidden_states:
          _A      =       hidden_states + (hidden_state,)

       _A      =       stage_module(__lowerCAmelCase      )

    if output_hidden_states:
       _A      =       hidden_states + (hidden_state,)

    if not return_dict:
       return tuple(v for v in [hidden_state, hidden_states] if v is not None      )

    return BaseModelOutputWithNoAttention(
        last_hidden_state=__lowerCAmelCase			,						hidden_states=__lowerCAmelCase			,						)


class 	lowerCamelCase__	(       _A):



 """simple docstring"""

 a__					: Dict =  ResNetConfig
 a__					: Union[str, Any] =  "resnet"
 a__					: Dict =  "pixel_values"
 a__					: Dict =  True

 def     snake_case_      (    self					:					List[Any]			,						__lowerCAmelCase					:					List[str]      )   ->	Optional[Any]:
    if isinstance(__lowerCAmelCase			,						nn.Convad      ):
       nn.init.kaiming_normal_(module.weight			,						mode='''fan_out'''			,						nonlinearity='''relu'''      )
    elif isinstance(__lowerCAmelCase			,						(nn.BatchNormad, nn.GroupNorm)      ):
       nn.init.constant_(module.weight			,						1      )
       nn.init.constant_(module.bias			,						0      )

 def     snake_case_      (    self					:					List[str]			,						__lowerCAmelCase					:					str			,						__lowerCAmelCase					:					Tuple=False      )   ->	Any:
    if isinstance(__lowerCAmelCase			,						__lowerCAmelCase      ):
       _A      =       value
UpperCAmelCase_		 =					r"""
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it
    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.

    Parameters:
        config ([`ResNetConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
"""

UpperCAmelCase_		 =					r"""
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See
            [`ConvNextImageProcessor.__call__`] for details.

        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
"""


@add_start_docstrings(
    "The bare ResNet model outputting raw features without any specific head on top."							,  _A							,  )
class 	lowerCamelCase__	(       _A):



 """simple docstring"""

 def __init__(    self					:					str			,						__lowerCAmelCase					:					int      )   ->	Tuple:
    super().__init__(__lowerCAmelCase      )
    _A      =       config
    _A      =       ResNetEmbeddings(__lowerCAmelCase      )
    _A      =       ResNetEncoder(__lowerCAmelCase      )
    _A      =       nn.AdaptiveAvgPoolad((1, 1)      )
    # Initialize weights and apply final processing
    self.post_init()

 @add_start_docstrings_to_model_forward(__lowerCAmelCase      )
 @add_code_sample_docstrings(
     checkpoint=_CHECKPOINT_FOR_DOC			,						output_type=__lowerCAmelCase			,						config_class=_CONFIG_FOR_DOC			,						modality='''vision'''			,						expected_output=_EXPECTED_OUTPUT_SHAPE			,						)
 def     snake_case_      (    self					:					List[str]			,						__lowerCAmelCase					:					Tensor			,						__lowerCAmelCase					:					Optional[bool] = None			,						__lowerCAmelCase					:					Optional[bool] = None      )   ->	BaseModelOutputWithPoolingAndNoAttention:
    _A      =       (
        output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
    )
    _A      =       return_dict if return_dict is not None else self.config.use_return_dict

    _A      =       self.embedder(__lowerCAmelCase      )

    _A      =       self.encoder(
        __lowerCAmelCase			,						output_hidden_states=__lowerCAmelCase			,						return_dict=__lowerCAmelCase      )

    _A      =       encoder_outputs[0]

    _A      =       self.pooler(__lowerCAmelCase      )

    if not return_dict:
       return (last_hidden_state, pooled_output) + encoder_outputs[1:]

    return BaseModelOutputWithPoolingAndNoAttention(
        last_hidden_state=__lowerCAmelCase			,						pooler_output=__lowerCAmelCase			,						hidden_states=encoder_outputs.hidden_states			,						)


@add_start_docstrings(
    "\n    ResNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n    ImageNet.\n    "							,  _A							,  )
class 	lowerCamelCase__	(       _A):



 """simple docstring"""

 def __init__(    self					:					Optional[Any]			,						__lowerCAmelCase					:					Dict      )   ->	Optional[Any]:
    super().__init__(__lowerCAmelCase      )
    _A      =       config.num_labels
    _A      =       ResNetModel(__lowerCAmelCase      )
    # classification head
    _A      =       nn.Sequential(
        nn.Flatten()			,						nn.Linear(config.hidden_sizes[-1]			,						config.num_labels      ) if config.num_labels > 0 else nn.Identity()			,						)
    # initialize weights and apply final processing
    self.post_init()

 @add_start_docstrings_to_model_forward(__lowerCAmelCase      )
 @add_code_sample_docstrings(
     checkpoint=_IMAGE_CLASS_CHECKPOINT			,						output_type=__lowerCAmelCase			,						config_class=_CONFIG_FOR_DOC			,						expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT			,						)
 def     snake_case_      (    self					:					Union[str, Any]			,						__lowerCAmelCase					:					Optional[torch.FloatTensor] = None			,						__lowerCAmelCase					:					Optional[torch.LongTensor] = None			,						__lowerCAmelCase					:					Optional[bool] = None			,						__lowerCAmelCase					:					Optional[bool] = None			,						)   ->	ImageClassifierOutputWithNoAttention:
    _A      =       return_dict if return_dict is not None else self.config.use_return_dict

    _A      =       self.resnet(__lowerCAmelCase			,						output_hidden_states=__lowerCAmelCase			,						return_dict=__lowerCAmelCase      )

    _A      =       outputs.pooler_output if return_dict else outputs[1]

    _A      =       self.classifier(__lowerCAmelCase      )

    _A      =       None

    if labels is not None:
       if self.config.problem_type is None:
          if self.num_labels == 1:
             _A      =       '''regression'''
          elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
             _A      =       '''single_label_classification'''
          else:
             _A      =       '''multi_label_classification'''
       if self.config.problem_type == "regression":
          _A      =       MSELoss()
          if self.num_labels == 1:
             _A      =       loss_fct(logits.squeeze()			,						labels.squeeze()      )
          else:
             _A      =       loss_fct(__lowerCAmelCase			,						__lowerCAmelCase      )
       elif self.config.problem_type == "single_label_classification":
          _A      =       CrossEntropyLoss()
          _A      =       loss_fct(logits.view(-1			,						self.num_labels      )			,						labels.view(-1      )      )
       elif self.config.problem_type == "multi_label_classification":
          _A      =       BCEWithLogitsLoss()
          _A      =       loss_fct(__lowerCAmelCase			,						__lowerCAmelCase      )

    if not return_dict:
       _A      =       (logits,) + outputs[2:]
       return (loss,) + output if loss is not None else output

    return ImageClassifierOutputWithNoAttention(loss=__lowerCAmelCase			,						logits=__lowerCAmelCase			,						hidden_states=outputs.hidden_states      )


@add_start_docstrings(
    "\n    ResNet backbone, to be used with frameworks like DETR and MaskFormer.\n    "							,  _A							,  )
class 	lowerCamelCase__	(       _A							,  _A):



 """simple docstring"""

 def __init__(    self					:					int			,						__lowerCAmelCase					:					str      )   ->	Union[str, Any]:
    super().__init__(__lowerCAmelCase      )
    super()._init_backbone(__lowerCAmelCase      )

    _A      =       [config.embedding_size] + config.hidden_sizes
    _A      =       ResNetEmbeddings(__lowerCAmelCase      )
    _A      =       ResNetEncoder(__lowerCAmelCase      )

    # initialize weights and apply final processing
    self.post_init()

 @add_start_docstrings_to_model_forward(__lowerCAmelCase      )
 @replace_return_docstrings(output_type=__lowerCAmelCase			,						config_class=_CONFIG_FOR_DOC      )
 def     snake_case_      (    self					:					Dict			,						__lowerCAmelCase					:					Tensor			,						__lowerCAmelCase					:					Optional[bool] = None			,						__lowerCAmelCase					:					Optional[bool] = None      )   ->	BackboneOutput:
    _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      =       self.embedder(__lowerCAmelCase      )

    _A      =       self.encoder(__lowerCAmelCase			,						output_hidden_states=__lowerCAmelCase			,						return_dict=__lowerCAmelCase      )

    _A      =       outputs.hidden_states

    _A      =       ()
    for idx, stage in enumerate(self.stage_names      ):
       if stage in self.out_features:
          feature_maps += (hidden_states[idx],)

    if not return_dict:
       _A      =       (feature_maps,)
       if output_hidden_states:
          output += (outputs.hidden_states,)
       return output

    return BackboneOutput(
        feature_maps=__lowerCAmelCase			,						hidden_states=outputs.hidden_states if output_hidden_states else None			,						attentions=__lowerCAmelCase			,						)








def 	SCREAMING_SNAKE_CASE_							(	_snake_case	:bytes				)  ->     str:
   return "".join([hex(_snake_case				)[2:].zfill(2				).upper() for byte in list(_snake_case				)]				)

def 	SCREAMING_SNAKE_CASE_							(	_snake_case	:str				)  ->     bytes:
   # Check data validity, following RFC3548
   # https://www.ietf.org/rfc/rfc3548.txt
   if (len(_snake_case				) % 2) != 0:
      raise ValueError(
          '''Base16 encoded data is invalid:
Data does not have an even number of hex digits.'''				)
   # Check the character set - the standard base16 alphabet
   # is uppercase according to RFC3548 section 6
   if not set(_snake_case				) <= set('''0123456789ABCDEF'''				):
      raise ValueError(
          '''Base16 encoded data is invalid:
Data is not uppercase hex or it contains invalid characters.'''				)
   # For every two hexadecimal digits (= a byte), turn it into an integer.
   # Then, string the result together into bytes, and return it.
   return bytes(int(data[i] + data[i + 1]	,   16				) for i in range(0	,   len(_snake_case				)	,   2				)				)


if __name__ == "__main__":
   import doctest

   doctest.testmod()



