
    g]                        d Z ddlZddlmZmZmZ ddlZddlZddlmZ ddl	m
Z
mZmZ ddlmZ ddlmZmZmZ dd	lmZ dd
lmZmZmZmZ ddlmZ  ej        e          ZdZdZ g dZ!dZ"dZ#dZ$dZ%dede&fdZ'd2dee&ef         de(fd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*                  Z0 G d$ d%ej*                  Z1 G d& d'ej*                  Z2 G d( d)ej*                  Z3 G d* d+e          Z4 ed,e$           G d- d.e4                      Z5 ed/e$           G d0 d1e4                      Z6dS )3zPyTorch EfficientNet model.    N)OptionalTupleUnion)nn)BCEWithLogitsLossCrossEntropyLossMSELoss   )ACT2FN)BaseModelOutputWithNoAttention(BaseModelOutputWithPoolingAndNoAttention$ImageClassifierOutputWithNoAttention)PreTrainedModel)add_code_sample_docstringsadd_start_docstrings%add_start_docstrings_to_model_forwardlogging   )EfficientNetConfigr   zgoogle/efficientnet-b7)r   i      r   ztabby, tabby cataN  
    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 ([`EfficientNetConfig`]): 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.
aB  
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See
            [`AutoImageProcessor.__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.
confignum_channelsc                     | j         }|| j        z  }t          |t          ||dz  z             |z  |z            }|d|z  k     r||z  }t          |          S )z<
    Round number of filters based on depth multiplier.
       g?)depth_divisorwidth_coefficientmaxint)r   r   divisornew_dims       r/var/www/html/ai-engine/env/lib/python3.11/site-packages/transformers/models/efficientnet/modeling_efficientnet.pyround_filtersr"   P   sk     "GF,,L'3|gk9::gEOPPG |###7w<<    Tkernel_sizeadjustc                     t          | t                    r| | f} | d         dz  | d         dz  f}|r$|d         dz
  |d         |d         dz
  |d         fS |d         |d         |d         |d         fS )aJ  
    Utility function to get the tuple padding value for the depthwise convolution.

    Args:
        kernel_size (`int` or `tuple`):
            Kernel size of the convolution layers.
        adjust (`bool`, *optional*, defaults to `True`):
            Adjusts padding value to apply to right and bottom sides of the input.
    r   r   r   )
isinstancer   )r$   r%   corrects      r!   correct_padr)   _   s     +s## 1"K01~"KNa$78G @
Q
GAJNGAJGG
GAJ
GAJ??r#   c                   L     e Zd ZdZdef fdZdej        dej        fdZ xZ	S )EfficientNetEmbeddingszL
    A module that corresponds to the stem module of the original work.
    r   c                 |   t                                                       t          |d          | _        t	          j        d          | _        t	          j        |j        | j        dddd          | _	        t	          j
        | j        |j        |j        	          | _        t          |j                 | _        d S )
N    )r   r   r   r   paddingr
   r   validFr$   strider/   bias)epsmomentum)super__init__r"   out_dimr   	ZeroPad2dr/   Conv2dr   convolutionBatchNorm2dbatch_norm_epsbatch_norm_momentum	batchnormr   
hidden_act
activationselfr   	__class__s     r!   r7   zEfficientNetEmbeddings.__init__x   s    $VR00|L99991QPW^c
 
 
 &:OZ`Ztuuu !23r#   pixel_valuesreturnc                     |                      |          }|                     |          }|                     |          }|                     |          }|S N)r/   r;   r?   rA   )rC   rE   featuress      r!   forwardzEfficientNetEmbeddings.forward   sM    <<--##H-->>(++??8,,r#   )
__name__
__module____qualname____doc__r   r7   torchTensorrJ   __classcell__rD   s   @r!   r+   r+   s   su         	41 	4 	4 	4 	4 	4 	4EL U\        r#   r+   c                   .     e Zd Z	 	 	 	 	 	 	 d fd	Z xZS )EfficientNetDepthwiseConv2dr   r
   r   Tzerosc	                 f    ||z  }	t                                          ||	|||||||	  	         d S )N)	in_channelsout_channelsr$   r2   r/   dilationgroupsr3   padding_mode)r6   r7   )rC   rW   depth_multiplierr$   r2   r/   rY   r3   r[   rX   rD   s             r!   r7   z$EfficientNetDepthwiseConv2d.__init__   sV     #%55#%#% 	 
	
 
	
 
	
 
	
 
	
r#   )r   r
   r   r   r   TrU   )rK   rL   rM   r7   rQ   rR   s   @r!   rT   rT      sT         
 
 
 
 
 
 
 
 
 
r#   rT   c                   X     e Zd ZdZdedededef fdZdej        dej	        fd	Z
 xZS )
EfficientNetExpansionLayerz_
    This corresponds to the expansion phase of each block in the original implementation.
    r   in_dimr8   r2   c                     t                                                       t          j        ||ddd          | _        t          j        ||j                  | _        t          |j	                 | _
        d S )Nr   sameFrW   rX   r$   r/   r3   )num_featuresr4   )r6   r7   r   r:   expand_convr<   r=   	expand_bnr   r@   
expand_act)rC   r   r_   r8   r2   rD   s        r!   r7   z#EfficientNetExpansionLayer.__init__   so    9 
 
 
 W&BWXXX !23r#   hidden_statesrF   c                     |                      |          }|                     |          }|                     |          }|S rH   )rd   re   rf   rC   rg   s     r!   rJ   z"EfficientNetExpansionLayer.forward   s=    ((77}5566r#   )rK   rL   rM   rN   r   r   r7   rO   FloatTensorrP   rJ   rQ   rR   s   @r!   r^   r^      s         
41 
43 
4 
4VY 
4 
4 
4 
4 
4 
4U%6 5<        r#   r^   c            
       \     e Zd ZdZdededededef
 fdZdej	        d	ej
        fd
Z xZS )EfficientNetDepthwiseLayerzk
    This corresponds to the depthwise convolution phase of each block in the original implementation.
    r   r_   r2   r$   adjust_paddingc                 v   t                                                       || _        | j        dk    rdnd}t          ||          }t	          j        |          | _        t          ||||d          | _        t	          j	        ||j
        |j                  | _        t          |j                 | _        d S )	Nr   r0   ra   )r%   r.   Fr1   rc   r4   r5   )r6   r7   r2   r)   r   r9   depthwise_conv_padrT   depthwise_convr<   r=   r>   depthwise_normr   r@   depthwise_act)	rC   r   r_   r2   r$   rm   conv_padr/   rD   s	           r!   r7   z#EfficientNetDepthwiseLayer.__init__   s     	"kQ..77Fk.AAA"$,w"?"?"?9FHSX
 
 
 !nV%:VE_
 
 
 $F$56r#   rg   rF   c                     | j         dk    r|                     |          }|                     |          }|                     |          }|                     |          }|S )Nr   )r2   rp   rq   rr   rs   ri   s     r!   rJ   z"EfficientNetDepthwiseLayer.forward   sa    ;! 33MBBM++M::++M::**=99r#   rK   rL   rM   rN   r   r   boolr7   rO   rj   rP   rJ   rQ   rR   s   @r!   rl   rl      s         7"7 7 	7
 7 7 7 7 7 7 7,	U%6 	5< 	 	 	 	 	 	 	 	r#   rl   c            	       Z     e Zd ZdZddedededef fdZdej	        d	ej
        fd
Z xZS )EfficientNetSqueezeExciteLayerzl
    This corresponds to the Squeeze and Excitement phase of each block in the original implementation.
    Fr   r_   
expand_dimexpandc                    t                                                       |r|n|| _        t          dt	          ||j        z                      | _        t          j        d          | _	        t          j
        | j        | j        dd          | _        t          j
        | j        | j        dd          | _        t          |j                 | _        t          j                    | _        d S )Nr   )output_sizera   )rW   rX   r$   r/   )r6   r7   dimr   r   squeeze_expansion_ratiodim_ser   AdaptiveAvgPool2dsqueezer:   reducer{   r   r@   
act_reduceSigmoid
act_expand)rC   r   r_   rz   r{   rD   s        r!   r7   z'EfficientNetSqueezeExciteLayer.__init__   s    !'3::V!S&*H!HIIJJ+:::i	
 
 
 i	
 
 
 !!23*,,r#   rg   rF   c                    |}|                      |          }|                     |          }|                     |          }|                     |          }|                     |          }t          j        ||          }|S rH   )r   r   r   r{   r   rO   mul)rC   rg   inputss      r!   rJ   z&EfficientNetSqueezeExciteLayer.forward  ss    ]33M2266M2266	&-88r#   )Frv   rR   s   @r!   ry   ry      s         ' '1 '3 'C 'Y] ' ' ' ' ' '*
U%6 
5< 
 
 
 
 
 
 
 
r#   ry   c                   n     e Zd ZdZdedededededef fdZd	e	j
        d
e	j
        de	j        fdZ xZS )EfficientNetFinalBlockLayerz[
    This corresponds to the final phase of each block in the original implementation.
    r   r_   r8   r2   	drop_rateid_skipc                     t                                                       |dk    o| | _        t          j        ||ddd          | _        t          j        ||j        |j                  | _	        t          j
        |          | _        d S )Nr   ra   Frb   ro   p)r6   r7   apply_dropoutr   r:   project_convr<   r=   r>   
project_bnDropoutdropout)rC   r   r_   r8   r2   r   r   rD   s          r!   r7   z$EfficientNetFinalBlockLayer.__init__  s     	#q[8[I 
 
 
 . f&;fF`
 
 
 zI...r#   
embeddingsrg   rF   c                     |                      |          }|                     |          }| j        r|                     |          }||z   }|S rH   )r   r   r   r   )rC   r   rg   s      r!   rJ   z#EfficientNetFinalBlockLayer.forward$  sR    ))-8866 	7 LL77M)J6Mr#   rK   rL   rM   rN   r   r   floatrw   r7   rO   rj   rP   rJ   rQ   rR   s   @r!   r   r     s         /(/25/@C/MP/]b/mq/ / / / / /"%"3 EDU Z_Zf        r#   r   c                   l     e Zd ZdZdededededededed	ed
ef fdZde	j
        de	j        fdZ xZS )EfficientNetBlocka  
    This corresponds to the expansion and depthwise convolution phase of each block in the original implementation.

    Args:
        config ([`EfficientNetConfig`]):
            Model configuration class.
        in_dim (`int`):
            Number of input channels.
        out_dim (`int`):
            Number of output channels.
        stride (`int`):
            Stride size to be used in convolution layers.
        expand_ratio (`int`):
            Expand ratio to set the output dimensions for the expansion and squeeze-excite layers.
        kernel_size (`int`):
            Kernel size for the depthwise convolution layer.
        drop_rate (`float`):
            Dropout rate to be used in the final phase of each block.
        id_skip (`bool`):
            Whether to apply dropout and sum the final hidden states with the input embeddings during the final phase
            of each block. Set to `True` for the first block of each stage.
        adjust_padding (`bool`):
            Whether to apply padding to only right and bottom side of the input kernel before the depthwise convolution
            operation, set to `True` for inputs with odd input sizes.
    r   r_   r8   r2   expand_ratior$   r   r   rm   c
                    t                                                       || _        | j        dk    rdnd| _        ||z  }
| j        rt	          |||
|          | _        t          || j        r|
n||||	          | _        t          |||
| j                  | _	        t          || j        r|
n|||||          | _        d S )Nr   TF)r   r_   r8   r2   )r   r_   r2   r$   rm   )r   r_   rz   r{   )r   r_   r8   r2   r   r   )r6   r7   r   r{   r^   	expansionrl   rq   ry   squeeze_exciter   
projection)rC   r   r_   r8   r2   r   r$   r   r   rm   expand_in_dimrD   s              r!   r7   zEfficientNetBlock.__init__J  s     	("/144dd%-; 	7fmF  DN 9$(K;==V#)
 
 
 =&]4;
 
 
 6$(K;==V
 
 
r#   rg   rF   c                     |}| j         dk    r|                     |          }|                     |          }|                     |          }|                     ||          }|S )Nr   )r   r   rq   r   r   )rC   rg   r   s      r!   rJ   zEfficientNetBlock.forwards  sg    "
!! NN=99M++M:: ++M::
MBBr#   r   rR   s   @r!   r   r   /  s         4'
"'
 '
 	'

 '
 '
 '
 '
 '
 '
 '
 '
 '
 '
 '
R
U%6 
5< 
 
 
 
 
 
 
 
r#   r   c            	       h     e Zd ZdZdef fdZ	 	 ddej        dee	         dee	         d	e
fd
Z xZS )EfficientNetEncoderz
    Forward propogates the embeddings through each EfficientNet block.

    Args:
        config ([`EfficientNetConfig`]):
            Model configuration class.
    r   c                     t                                                       | _        |j         _         fdt	          |j                  }t          fd|j        D                       }d}g }t          |          D ]}t          ||j        |                   }t          ||j
        |                   }|j        |         }	|j        |         }
|j        |         }t           |j        |                             D ]l}|dk    rdnd}|dk    rdn|	}	|dk    r|n|}||j        v rdnd}|j        |z  |z  }t!          ||||	|
||||	  	        }|                    |           |dz  }mt%          j        |           _        t%          j        |t          |d          dd	d
           _        t%          j        |j        |j        |j                   _        t8          |j                  _        d S )Nc                 V    t          t          j        j        | z                      S rH   )r   mathceildepth_coefficient)repeatsrC   s    r!   round_repeatsz3EfficientNetEncoder.__init__.<locals>.round_repeats  s#    ty!7'!ABBCCCr#   c              3   .   K   | ]} |          V  d S rH    ).0nr   s     r!   	<genexpr>z/EfficientNetEncoder.__init__.<locals>.<genexpr>  s-      LLaq))LLLLLLr#   r   TFr   )	r   r_   r8   r2   r$   r   r   r   rm   i   ra   rb   ro   )r6   r7   r   r   lenrW   sumnum_block_repeatsranger"   rX   strideskernel_sizesexpand_ratiosdepthwise_paddingdrop_connect_rater   appendr   
ModuleListblocksr:   top_convr<   
hidden_dimr=   r>   top_bnr   r@   top_activation)rC   r   num_base_blocks
num_blockscurr_block_numr   ir_   r8   r2   r$   r   jr   rm   r   blockr   rD   s   `                @r!   r7   zEfficientNetEncoder.__init__  sW   !'!9	D 	D 	D 	D 	D f011LLLL63KLLLLL
'' 	$ 	$A"66+=a+@AAF#FF,?,BCCG^A&F -a0K!/2L==)A!)DEEFF $ $"#q&&$$e!ee$%EEv*8F<T*T*TZ^"4~E
R	)!!#! +!-'##1
 
 
 e$$$!#'$* mF++	&vt44
 
 
 n*0EPVPj
 
 
 %V%67r#   FTrg   output_hidden_statesreturn_dictrF   c                 $   |r|fnd }| j         D ]} ||          }|r||fz  }|                     |          }|                     |          }|                     |          }|st	          d ||fD                       S t          ||          S )Nc              3      K   | ]}||V  	d S rH   r   )r   vs     r!   r   z.EfficientNetEncoder.forward.<locals>.<genexpr>  s"      XXq!-----XXr#   )last_hidden_staterg   )r   r   r   r   tupler   )rC   rg   r   r   all_hidden_statesr   s         r!   rJ   zEfficientNetEncoder.forward  s     1EN],,$[ 	6 	6E!E-00M# 6!m%55!m44M22++M:: 	YXX]4E$FXXXXXX-++
 
 
 	
r#   )FT)rK   rL   rM   rN   r   r7   rO   rj   r   rw   r   rJ   rQ   rR   s   @r!   r   r     s         581 58 58 58 58 58 58t 05&*	
 
(
 'tn
 d^	

 
(
 
 
 
 
 
 
 
r#   r   c                   (    e Zd ZdZeZdZdZg Zd Z	dS )EfficientNetPreTrainedModelz
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    efficientnetrE   c                    t          |t          j        t          j        f          rT|j        j                            d| j        j                   |j	         |j	        j        
                                 dS dS t          |t          j                  r?|j	        j        
                                 |j        j                            d           dS dS )zInitialize the weightsg        )meanstdNg      ?)r'   r   Linearr:   weightdatanormal_r   initializer_ranger3   zero_	LayerNormfill_)rC   modules     r!   _init_weightsz)EfficientNetPreTrainedModel._init_weights  s    fry")455 	* M&&CT[5R&SSS{& &&((((( '&-- 	*K""$$$M$$S)))))	* 	*r#   N)
rK   rL   rM   rN   r   config_classbase_model_prefixmain_input_name_no_split_modulesr   r   r#   r!   r   r     sE         
 &L&$O
* 
* 
* 
* 
*r#   r   zUThe bare EfficientNet model outputting raw features without any specific head on top.c                        e Zd Zdef fdZ ee           eee	e
de          	 	 	 ddej        dee         dee         d	eee	f         fd
                        Z xZS )EfficientNetModelr   c                    t                                          |           || _        t          |          | _        t          |          | _        |j        dk    r!t          j	        |j
        d          | _        nC|j        dk    r!t          j        |j
        d          | _        nt          d|j                   |                                  d S )Nr   T)	ceil_moder   z2config.pooling must be one of ['mean', 'max'] got )r6   r7   r   r+   r   r   encoderpooling_typer   	AvgPool2dr   pooler	MaxPool2d
ValueErrorpooling	post_initrB   s     r!   r7   zEfficientNetModel.__init__  s       088*622 &((,v'8DIIIDKK E)),v'8DIIIDKKbRXR`bbccc 	r#   vision)
checkpointoutput_typer   modalityexpected_outputNrE   r   r   rF   c                    ||n| j         j        }||n| j         j        }|t          d          |                     |          }|                     |||          }|d         }|                     |          }|                    |j        d d                   }|s||f|dd          z   S t          |||j
                  S )Nz You have to specify pixel_valuesr   r   r   r   r   )r   pooler_outputrg   )r   r   use_return_dictr   r   r   r   reshapeshaper   rg   )rC   rE   r   r   embedding_outputencoder_outputsr   pooled_outputs           r!   rJ   zEfficientNetModel.forward  s     %9$D  $+Jj 	 &1%<kk$+B]?@@@??<88,,!5# ' 
 
 ,A.$566%--m.A"1".EFF 	L%}58KKK7/')7
 
 
 	
r#   )NNN)rK   rL   rM   r   r7   r   EFFICIENTNET_INPUTS_DOCSTRINGr   _CHECKPOINT_FOR_DOCr   _CONFIG_FOR_DOC_EXPECTED_OUTPUT_SHAPErO   rj   r   rw   r   r   rJ   rQ   rR   s   @r!   r   r     s        
1      " +*+HII&<$.   +//3&*	"
 "
'"
 'tn"
 d^	"

 
u>>	?"
 "
 "
  JI"
 "
 "
 "
 "
r#   r   z
    EfficientNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g.
    for ImageNet.
    c                        e Zd Z fdZ ee           eeee	e
          	 	 	 	 d
dej        deej                 dee         dee         deeef         f
d	                        Z xZS )"EfficientNetForImageClassificationc                    t                                          |           |j        | _        || _        t	          |          | _        t          j        |j                  | _	        | j        dk    rt          j
        |j        | j                  nt          j                    | _        |                                  d S )Nr   r   )r6   r7   
num_labelsr   r   r   r   r   dropout_rater   r   r   Identity
classifierr   rB   s     r!   r7   z+EfficientNetForImageClassification.__init__=  s        +-f55zF$7888KO?]^K^K^")F$5tGGGdfdodqdq 	r#   )r   r   r   r   NrE   labelsr   r   rF   c                    ||n| j         j        }|                     |||          }|r|j        n|d         }|                     |          }|                     |          }d}|Z| j         j        f| j        dk    rd| j         _        nN| j        dk    r7|j        t          j
        k    s|j        t          j        k    rd| j         _        nd| j         _        | j         j        dk    rWt                      }	| j        dk    r1 |	|                                |                                          }n |	||          }n| j         j        dk    rGt                      }	 |	|                    d| j                  |                    d                    }n*| j         j        dk    rt!                      }	 |	||          }|s|f|dd         z   }
||f|
z   n|
S t#          |||j        	          S )
a  
        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 regression loss is computed (Mean-Square loss), If
            `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
        Nr   r   
regressionsingle_label_classificationmulti_label_classificationr   )losslogitsrg   )r   r   r   r   r   r  problem_typer  dtyperO   longr   r	   r   r   viewr   r   rg   )rC   rE   r  r   r   outputsr   r  r  loss_fctoutputs              r!   rJ   z*EfficientNetForImageClassification.forwardI  s
   ( &1%<kk$+B]##LG[it#uu1<L--'!*]33//{'/?a''/;DK,,_q((flej.H.HFL\a\eLeLe/LDK,,/KDK,{'<77"99?a''#8FNN$4$4fnn6F6FGGDD#8FF33DD)-JJJ+--xB @ @&++b//RR)-III,..x// 	FY,F)-)9TGf$$vE3!/
 
 
 	
r#   )NNNN)rK   rL   rM   r7   r   r   r   _IMAGE_CLASS_CHECKPOINTr   r   _IMAGE_CLASS_EXPECTED_OUTPUTrO   rj   r   
LongTensorrw   r   r   rJ   rQ   rR   s   @r!   r  r  5  s        
 
 
 
 
 +*+HII*8$4	   +/-1/3&*4
 4
'4
 )*4
 'tn	4

 d^4
 
u::	;4
 4
 4
  JI4
 4
 4
 4
 4
r#   r  )T)7rN   r   typingr   r   r   rO   torch.utils.checkpointr   torch.nnr   r   r	   activationsr   modeling_outputsr   r   r   modeling_utilsr   utilsr   r   r   r   configuration_efficientnetr   
get_loggerrK   loggerr   r   r   r  r  EFFICIENTNET_START_DOCSTRINGr   r   r"   rw   r)   Moduler+   r:   rT   r^   rl   ry   r   r   r   r   r   r  r   r#   r!   <module>r&     s   " !  ) ) ) ) ) ) ) ) ) )            A A A A A A A A A A ! ! ! ! ! !         
 . - - - - -            ; : : : : : 
	H	%	% ' / '  3 1 	  ! , C    @ @U3:. @ @ @ @ @(    RY   2
 
 
 
 
") 
 
 
4       4$ $ $ $ $ $ $ $N$ $ $ $ $RY $ $ $N    ")   BN N N N N	 N N NbW
 W
 W
 W
 W
") W
 W
 W
t* * * * */ * * *0 [  <
 <
 <
 <
 <
3 <
 <
	 <
~  ! H
 H
 H
 H
 H
)D H
 H
 H
 H
 H
r#   