
    i/A                        d Z ddlZddlZddlmZmZ ddlmZ ddlm	Z	 ddl
mZmZ ddlmZmZmZmZ dd	lmZ dd
lmZmZ ddlmZ ddlmZ  ej6                  e      Z G d dej<                        Z G d dej<                        Z  G d dej<                        Z! G d dej<                        Z" G d dej<                        Z# G d dej<                        Z$ G d dej<                        Z%e G d de             Z&e G d de&             Z' ed !       G d" d#e&             Z( ed$!       G d% d&ee&             Z)g d'Z*y)(zPyTorch ResNet model.    N)Tensornn   )initialization)ACT2FN)BackboneMixinfilter_output_hidden_states)BackboneOutputBaseModelOutputWithNoAttention(BaseModelOutputWithPoolingAndNoAttention$ImageClassifierOutputWithNoAttention)PreTrainedModel)auto_docstringlogging)can_return_tuple   )ResNetConfigc                   H     e Zd Z	 d
dededededef
 fdZdedefd	Z xZS )ResNetConvLayerin_channelsout_channelskernel_sizestride
activationc                     t         |           t        j                  |||||dz  d      | _        t        j
                  |      | _        |t        |   | _	        y t        j                         | _	        y )N   F)r   r   paddingbias)
super__init__r   Conv2dconvolutionBatchNorm2dnormalizationr   Identityr   )selfr   r   r   r   r   	__class__s         {/var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/transformers/models/resnet/modeling_resnet.pyr    zResNetConvLayer.__init__(   sf     	99;vWbfgWgns
  ^^L90:0F&,BKKM    inputreturnc                 l    | j                  |      }| j                  |      }| j                  |      }|S N)r"   r$   r   r&   r*   hidden_states      r(   forwardzResNetConvLayer.forward2   s6    ''.)),7|4r)   )r   r   relu)	__name__
__module____qualname__intstrr    r   r0   __classcell__r'   s   @r(   r   r   '   sL    lrZZ.1Z@CZQTZfiZV  r)   r   c                   8     e Zd ZdZdef fdZdedefdZ xZS )ResNetEmbeddingszO
    ResNet Embeddings (stem) composed of a single aggressive convolution.
    configc                     t         |           t        |j                  |j                  dd|j
                        | _        t        j                  ddd      | _	        |j                  | _        y )N   r   )r   r   r   r   r   )r   r   r   )
r   r    r   num_channelsembedding_size
hidden_actembedderr   	MaxPool2dpoolerr&   r;   r'   s     r(   r    zResNetEmbeddings.__init__>   s\    '!6!6Aa\b\m\m
 llqAF"//r)   pixel_valuesr+   c                     |j                   d   }|| j                  k7  rt        d      | j                  |      }| j	                  |      }|S )Nr   zeMake sure that the channel dimension of the pixel values match with the one set in the configuration.)shaper>   
ValueErrorrA   rC   )r&   rE   r>   	embeddings       r(   r0   zResNetEmbeddings.forwardF   sT    #))!,4,,,w  MM,/	KK	*	r)   )	r2   r3   r4   __doc__r   r    r   r0   r7   r8   s   @r(   r:   r:   9   s'    0| 0F v r)   r:   c                   B     e Zd ZdZd	dededef fdZdedefdZ xZS )
ResNetShortCutz
    ResNet shortcut, used to project the residual features to the correct size. If needed, it is also used to
    downsample the input using `stride=2`.
    r   r   r   c                     t         |           t        j                  ||d|d      | _        t        j
                  |      | _        y )Nr   F)r   r   r   )r   r    r   r!   r"   r#   r$   )r&   r   r   r   r'   s       r(   r    zResNetShortCut.__init__W   s:    99[,AV\chi^^L9r)   r*   r+   c                 J    | j                  |      }| j                  |      }|S r-   )r"   r$   r.   s      r(   r0   zResNetShortCut.forward\   s(    ''.)),7r)   )r   )	r2   r3   r4   rJ   r5   r    r   r0   r7   r8   s   @r(   rL   rL   Q   s5    
:C :s :C :
V  r)   rL   c            	       <     e Zd ZdZddedededef fdZd Z xZS )	ResNetBasicLayerzO
    A classic ResNet's residual layer composed by two `3x3` convolutions.
    r   r   r   r   c                    t         |           ||k7  xs |dk7  }|rt        |||      nt        j                         | _        t        j                  t        |||      t        ||d             | _        t        |   | _
        y )Nr   r   r   r   r    rL   r   r%   shortcut
Sequentialr   layerr   r   )r&   r   r   r   r   should_apply_shortcutr'   s         r(   r    zResNetBasicLayer.__init__g   s{     +| ; Jv{H]N;VDcecncncp 	 ]]KfEL,4H

 !,r)   c                 z    |}| j                  |      }| j                  |      }||z  }| j                  |      }|S r-   rW   rU   r   r&   r/   residuals      r(   r0   zResNetBasicLayer.forwards   A    zz,/==* |4r)   )r   r1   )	r2   r3   r4   rJ   r5   r6   r    r0   r7   r8   s   @r(   rP   rP   b   s/    
-C 
-s 
-C 
-Y\ 
-r)   rP   c                   L     e Zd ZdZ	 	 	 	 d
dedededededef fdZd	 Z xZ	S )ResNetBottleNeckLayera  
    A classic ResNet's bottleneck layer composed by three `3x3` convolutions.

    The first `1x1` convolution reduces the input by a factor of `reduction` in order to make the second `3x3`
    convolution faster. The last `1x1` convolution remaps the reduced features to `out_channels`. If
    `downsample_in_bottleneck` is true, downsample will be in the first layer instead of the second layer.
    r   r   r   r   	reductiondownsample_in_bottleneckc           
      F   t         	|           ||k7  xs |dk7  }||z  }|rt        |||      nt        j                         | _        t        j                  t        ||d|r|nd      t        |||s|nd      t        ||dd             | _        t        |   | _
        y )Nr   rR   )r   r   )r   r   rT   )
r&   r   r   r   r   r`   ra   rX   reduces_channelsr'   s
            r(   r    zResNetBottleNeckLayer.__init__   s     	 +| ; Jv{'94H]N;VDcecncncp 	 ]]-1OgVmn ,.>Umvstu,lVZ[

 !,r)   c                 z    |}| j                  |      }| j                  |      }||z  }| j                  |      }|S r-   rZ   r[   s      r(   r0   zResNetBottleNeckLayer.forward   r]   r)   )r   r1      F)
r2   r3   r4   rJ   r5   r6   boolr    r0   r7   r8   s   @r(   r_   r_   |   sZ      ).-- - 	-
 - - #'-0r)   r_   c                   N     e Zd ZdZ	 	 ddededededef
 fdZded	efd
Z xZ	S )ResNetStagez4
    A ResNet stage composed by stacked layers.
    r;   r   r   r   depthc                    t         	|           |j                  dk(  rt        nt        }|j                  dk(  r" |||||j
                  |j                        }n |||||j
                        }t        j                  |gt        |dz
        D cg c]  } ||||j
                         c} | _
        y c c}w )N
bottleneck)r   r   ra   )r   r   r   rS   )r   r    
layer_typer_   rP   r@   ra   r   rV   rangelayers)
r&   r;   r   r   r   ri   rW   first_layer_r'   s
            r(   r    zResNetStage.__init__   s     	)/):):l)J%P`,!,,)/)H)HK  \&U[UfUfgKmm
dijorsjsdtu_`5|HYHYZu
us   B;
r*   r+   c                 <    |}| j                   D ]
  } ||      } |S r-   )rn   )r&   r*   r/   rW   s       r(   r0   zResNetStage.forward   s*    [[ 	/E .L	/r)   )r   r   )
r2   r3   r4   rJ   r   r5   r    r   r0   r7   r8   s   @r(   rh   rh      sX     

 
 	

 
 
4V  r)   rh   c            	       @     e Zd Zdef fdZ	 ddedededefdZ xZ	S )	ResNetEncoderr;   c           
         t         |           t        j                  g       | _        | j                  j                  t        ||j                  |j                  d   |j                  rdnd|j                  d                t        |j                  |j                  dd        }t        ||j                  dd        D ]0  \  \  }}}| j                  j                  t        ||||             2 y )Nr   r   r   )r   ri   )ri   )r   r    r   
ModuleListstagesappendrh   r?   hidden_sizesdownsample_in_first_stagedepthszip)r&   r;   in_out_channelsr   r   ri   r'   s         r(   r    zResNetEncoder.__init__   s    mmB'%%##A&"<<q!mmA&	
 f1163F3Fqr3JK25ov}}UVUWGX2Y 	\.'[,KK{6;TYZ[	\r)   r/   output_hidden_statesreturn_dictr+   c                     |rdnd }| j                   D ]  }|r||fz   } ||      } |r||fz   }|st        d ||fD              S t        ||      S )N c              3   &   K   | ]	  }||  y wr-   r   ).0vs     r(   	<genexpr>z(ResNetEncoder.forward.<locals>.<genexpr>   s     SqQ]Ss   )last_hidden_statehidden_states)rv   tupler   )r&   r/   r}   r~   r   stage_modules         r(   r0   zResNetEncoder.forward   sv     3 KK 	6L# - ?'5L		6  )\O;MS\=$ASSS-*'
 	
r)   )FT)
r2   r3   r4   r   r    r   rf   r   r0   r7   r8   s   @r(   rs   rs      s=    \| \$ ]a
"
:>
UY
	'
r)   rs   c                   Z    e Zd ZU eed<   dZdZdZddgZ e	j                         d        Zy)	ResNetPreTrainedModelr;   resnetrE   )imager   rL   c                    t        |t        j                        r#t        j                  |j
                  dd       y t        |t        j                        rt        j                  |j
                  t        j                  d             |j                  xt        j                  j                  j                  |j
                        \  }}|dkD  rdt        j                  |      z  nd}t        j                  |j                  | |       y y d|j                  j                  v rt        j                   |j
                         t        j"                  |j                         t        j"                  |j$                         t        j                   |j&                         t)        |d	d        t        j"                  |j*                         y y y )
Nfan_outr1   )modenonlinearity   )ar   r   	BatchNormnum_batches_tracked)
isinstancer   r!   initkaiming_normal_weightLinearkaiming_uniform_mathsqrtr   torch_calculate_fan_in_and_fan_outuniform_r'   r2   ones_zeros_running_meanrunning_vargetattrr   )r&   modulefan_inrp   bounds        r(   _init_weightsz#ResNetPreTrainedModel._init_weights   s7   fbii(  YVT		*!!&--499Q<@{{&!HHMMGGV	17!DIIf--fkkE659 '
 F,,555JJv}}%KK$KK++,JJv))*v4d;GF667 H 6r)   N)r2   r3   r4   r   __annotations__base_model_prefixmain_input_nameinput_modalities_no_split_modulesr   no_gradr   r   r)   r(   r   r      s?     $O!*,<=U]]_8 8r)   r   c            
       R     e Zd Z fdZe	 	 ddededz  dedz  defd       Z xZ	S )	ResNetModelc                     t         |   |       || _        t        |      | _        t        |      | _        t        j                  d      | _	        | j                          y )N)r   r   )r   r    r;   r:   rA   rs   encoderr   AdaptiveAvgPool2drC   	post_initrD   s     r(   r    zResNetModel.__init__  sK     (0$V,**62r)   NrE   r}   r~   r+   c                 (   ||n| j                   j                  }||n| j                   j                  }| j                  |      }| j	                  |||      }|d   }| j                  |      }|s
||f|dd  z   S t        |||j                        S )Nr}   r~   r   r   )r   pooler_outputr   )r;   r}   r~   rA   r   rC   r   r   )	r&   rE   r}   r~   kwargsembedding_outputencoder_outputsr   pooled_outputs	            r(   r0   zResNetModel.forward  s     %9$D $++JjJj 	 &1%<k$++BYBY==6,,3GU` ' 
 ,A.$56%}58KKK7/')77
 	
r)   NN)
r2   r3   r4   r    r   r   rf   r   r0   r7   r8   s   @r(   r   r     sR      -1#'	

 #Tk
 D[	
 
2
 
r)   r   z
    ResNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for
    ImageNet.
    )custom_introc                        e Zd Z fdZe	 	 	 	 d	dej                  dz  dej                  dz  dedz  dedz  de	f
d       Z
 xZS )
ResNetForImageClassificationc                 |   t         |   |       |j                  | _        t        |      | _        t        j                  t        j                         |j                  dkD  r-t        j                  |j                  d   |j                        nt        j                               | _        | j                          y )Nr   )r   r    
num_labelsr   r   r   rV   Flattenr   rx   r%   
classifierr   rD   s     r(   r    z%ResNetForImageClassification.__init__E  s      ++!&)--JJLEKEVEVYZEZBIIf))"-v/@/@A`b`k`k`m

 	r)   NrE   labelsr}   r~   r+   c                 B   ||n| j                   j                  }| j                  |||      }|r|j                  n|d   }| j	                  |      }d}	|| j                  ||| j                         }	|s|f|dd z   }
|	|	f|
z   S |
S t        |	||j                        S )a0  
        labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the image classification/regression loss. Indices should be in `[0, ...,
            config.num_labels - 1]`. If `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
        Nr   r   r   )losslogitsr   )r;   r~   r   r   r   loss_functionr   r   )r&   rE   r   r}   r~   r   outputsr   r   r   outputs              r(   r0   z$ResNetForImageClassification.forwardQ  s     &1%<k$++BYBY++lAUcn+o1<--'!*/%%ffdkkBDY,F'+'7D7V#CVC3f\c\q\qrrr)   )NNNN)r2   r3   r4   r    r   r   FloatTensor
LongTensorrf   r   r0   r7   r8   s   @r(   r   r   >  s    
  26*.,0#'s''$.s   4's #Tk	s
 D[s 
.s sr)   r   zO
    ResNet backbone, to be used with frameworks like DETR and MaskFormer.
    c                   j     e Zd ZdZ fdZeee	 	 d	dede	dz  de	dz  de
fd                     Z xZS )
ResNetBackboneFc                     t         |   |       |j                  g|j                  z   | _        t        |      | _        t        |      | _        | j                          y r-   )
r   r    r?   rx   num_featuresr:   rA   rs   r   r   rD   s     r(   r    zResNetBackbone.__init__{  sP     #223f6I6II(0$V, 	r)   NrE   r}   r~   r+   c                    ||n| j                   j                  }||n| j                   j                  }| j                  |      }| j	                  |dd      }|j
                  }d}t        | j                        D ]  \  }	}
|
| j                  v s|||	   fz  } |s|f}|r||j
                  fz  }|S t        ||r|j
                  d      S dd      S )ar  
        Examples:

        ```python
        >>> from transformers import AutoImageProcessor, AutoBackbone
        >>> import torch
        >>> from PIL import Image
        >>> import httpx
        >>> from io import BytesIO

        >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
        >>> with httpx.stream("GET", url) as response:
        ...     image = Image.open(BytesIO(response.read()))

        >>> processor = AutoImageProcessor.from_pretrained("microsoft/resnet-50")
        >>> model = AutoBackbone.from_pretrained(
        ...     "microsoft/resnet-50", out_features=["stage1", "stage2", "stage3", "stage4"]
        ... )

        >>> inputs = processor(image, return_tensors="pt")

        >>> outputs = model(**inputs)
        >>> feature_maps = outputs.feature_maps
        >>> list(feature_maps[-1].shape)
        [1, 2048, 7, 7]
        ```NTr   r   )feature_mapsr   
attentions)
r;   r~   r}   rA   r   r   	enumeratestage_namesout_featuresr
   )r&   rE   r}   r~   r   r   r   r   r   idxstager   s               r(   r0   zResNetBackbone.forward  s    H &1%<k$++BYBY$8$D $++JjJj 	  ==6,,/dX\,]--#D$4$45 	6JC)))s!3 55	6 "_F#70022M%3G'//
 	
MQ
 	
r)   r   )r2   r3   r4   has_attentionsr    r   r	   r   r   rf   r
   r0   r7   r8   s   @r(   r   r   s  si     N   -1#'	;
;
 #Tk;
 D[	;
 
;
  ! ;
r)   r   )r   r   r   r   )+rJ   r   r   r   r    r   r   activationsr   backbone_utilsr   r	   modeling_outputsr
   r   r   r   modeling_utilsr   utilsr   r   utils.genericr   configuration_resnetr   
get_loggerr2   loggerModuler   r:   rL   rP   r_   rh   rs   r   r   r   r   __all__r   r)   r(   <module>r      sQ       & ! H  . , - . 
		H	%bii $ryy 0RYY "ryy 4'BII 'T#")) #L&
BII &
R 8O 8 88 (
' (
 (
V ,s#8 ,s,s^ 
K
]$9 K

K
\ er)   