
    RЦi;                     ^   S r SSKJr  SSKJrJrJrJrJr  SSK	r	SSK
Jr  SSKJrJr  SSKJrJr  SSKJr  SS	KJr  SS
KJrJr  S/r " S S\R4                  5      r " S S\R4                  5      r " S S\R4                  5      r " S S\R4                  5      r " S S\R4                  5      r " S S\R4                  5      r  " S S\R4                  5      r! " S S\R4                  5      r" " S S\R4                  5      r#S+S\#4S jjr$\" SS S!S"S#S$S%\\S&S'S(S).05      r%\S+S* j5       r&g),zPytorch Inception-V4 implementation
Sourced from https://github.com/Cadene/tensorflow-model-zoo.torch (MIT License) which is
based upon Google's Tensorflow implementation and pretrained weights (Apache 2.0 License)
    )partial)ListOptionalTupleUnionTypeN)IMAGENET_INCEPTION_MEANIMAGENET_INCEPTION_STD)create_classifierConvNormAct   )build_model_with_cfg)feature_take_indices)register_modelgenerate_default_cfgsInceptionV4c                   Z   ^  \ rS rSr\SS4S\\R                     4U 4S jjjrS r	Sr
U =r$ )Mixed3a   N
conv_blockc                 ~   > X#S.n[         TU ]  5         [        R                  " SSS9U l        U" SSSS.UD6U l        g )Ndevicedtype      stridekernel_sizer   @   `   )super__init__nn	MaxPool2dmaxpoolconvselfr   r   r   dd	__class__s        W/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/timm/models/inception_v4.pyr%   Mixed3a.__init__   s@     /||Aa0E1QE"E	    c                 z    U R                  U5      nU R                  U5      n[        R                  " X#4S5      nU$ Nr   )r(   r)   torchcatr+   xx0x1outs        r.   forwardMixed3a.forward    s3    \\!_YYq\ii!$
r0   r)   r(   __name__
__module____qualname____firstlineno__r   r   r&   Moduler%   r:   __static_attributes____classcell__r-   s   @r.   r   r      s5     +6		FRYY	F 	F r0   r   c                   Z   ^  \ rS rSr\SS4S\\R                     4U 4S jjjrS r	Sr
U =r$ )Mixed4a'   Nr   c                   > X#S.n[         TU ]  5         [        R                  " U" SSSS.UD6U" SSSS.UD65      U l        [        R                  " U" SSSS.UD6U" SSSSS.UD6U" SSSS	S.UD6U" SS
SS.UD65      U l        g )Nr   r   r   r   r      r   r   r    r   paddingrK   r   r   r   )r   r   )   r"   r!   )r"   r"   )r$   r%   r&   
Sequentialbranch0branch1r*   s        r.   r%   Mixed4a.__init__(   s     /}}>Aa>2>=1Q="=

 }}>Aa>2>R6!VRrRR6!VRrRB6!BrB	
r0   c                 z    U R                  U5      nU R                  U5      n[        R                  " X#4S5      nU$ r2   )rS   rT   r3   r4   r5   s        r.   r:   Mixed4a.forward=   s3    \\!_\\!_ii!$
r0   )rS   rT   r=   rE   s   @r.   rG   rG   '   s2     +6	
RYY
 
* r0   rG   c                   Z   ^  \ rS rSr\SS4S\\R                     4U 4S jjjrS r	Sr
U =r$ )Mixed5aD   Nr   c                 ~   > X#S.n[         TU ]  5         U" SSSS.UD6U l        [        R                  " SSS9U l        g )Nr   r   r   r   r      r]   )r$   r%   r)   r&   r'   r(   r*   s        r.   r%   Mixed5a.__init__E   s@     /GQqGBG	||Aa0r0   c                 z    U R                  U5      nU R                  U5      n[        R                  " X#4S5      nU$ r2   )r)   r(   r3   r4   r5   s        r.   r:   Mixed5a.forwardP   s3    YYq\\\!_ii!$
r0   r<   r=   rE   s   @r.   rY   rY   D   s2     +6		1RYY	1 	1 r0   rY   c                   Z   ^  \ rS rSr\SS4S\\R                     4U 4S jjjrS r	Sr
U =r$ )
InceptionAW   Nr   c                   > X#S.n[         TU ]  5         U" SSSS.UD6U l        [        R                  " U" S	SSS.UD6U" S
SSSS.UD65      U l        [        R                  " U" S	SSS.UD6U" S
SSSS.UD6U" SSSSS.UD65      U l        [        R                  " [        R                  " SSSSS9U" SSSS.UD65      U l        g )Nr   r   r   r   rM   Fr   rN   count_include_pad)  r#   )rg   r"   r!   )r#   r#   	r$   r%   rS   r&   rR   rT   branch2	AvgPool2dbranch3r*   s        r.   r%   InceptionA.__init__X   s     /!IqIbI}}>Aa>2>H1QHRH

 }}>Aa>2>H1QHRHH1QHRH
 }}LL1a5I>Aa>2>
r0   c                     U R                  U5      nU R                  U5      nU R                  U5      nU R                  U5      n[        R
                  " X#XE4S5      nU$ r2   rS   rT   ri   rk   r3   r4   r+   r6   r7   r8   x2x3r9   s          r.   r:   InceptionA.forwardr   P    \\!_\\!_\\!_\\!_ii(!,
r0   rS   rT   ri   rk   r=   rE   s   @r.   rb   rb   W   s2     +6	
RYY
 
4 r0   rb   c                   Z   ^  \ rS rSr\SS4S\\R                     4U 4S jjjrS r	Sr
U =r$ )
ReductionA{   Nr   c           
         > X#S.n[         TU ]  5         U" SSSS.UD6U l        [        R                  " U" S	SSS.UD6U" S
SSSS.UD6U" SSSS.UD65      U l        [        R                  " SSS9U l        g )Nr   r   r   r   r   rM   r   )rg   rg   )rg   r]   r]      rz      )r$   r%   rS   r&   rR   rT   r'   ri   r*   s        r.   r%   ReductionA.__init__|   s     /!J!JrJ}}?Qq?B?JQq!JrJ?Qq?B?
 ||Aa0r0   c                     U R                  U5      nU R                  U5      nU R                  U5      n[        R                  " X#U4S5      nU$ r2   rS   rT   ri   r3   r4   r+   r6   r7   r8   rp   r9   s         r.   r:   ReductionA.forward   B    \\!_\\!_\\!_iia(
r0   rS   rT   ri   r=   rE   s   @r.   rv   rv   {   s2     +6	1RYY1 1$ r0   rv   c                   Z   ^  \ rS rSr\SS4S\\R                     4U 4S jjjrS r	Sr
U =r$ )
InceptionB   Nr   c                   > X#S.n[         TU ]  5         U" SSSS.UD6U l        [        R                  " U" SSSS.UD6U" SSSSS.UD6U" SSSSS.UD65      U l        [        R                  " U" SSSS.UD6U" SSSSS.UD6U" SSSSS.UD6U" SSSSS.UD6U" SSSSS.UD65      U l        [        R                  " [        R                  " S	SSS
S9U" SSSS.UD65      U l        g )Nr   r   r   rJ   rL   rM   rO   rP   r   Fre   )   rg   r   r]   ry   r{   r\   )rz   rz   )r      rh   r*   s        r.   r%   InceptionB.__init__   s(    /!K1KK}}@a@R@TVAvTQSTTVAvTQST
 }}@a@R@TVAvTQSTTVAvTQSTTVAvTQSTTVAvTQST
 }}LL1a5I@a@R@
r0   c                     U R                  U5      nU R                  U5      nU R                  U5      nU R                  U5      n[        R
                  " X#XE4S5      nU$ r2   rn   ro   s          r.   r:   InceptionB.forward   rs   r0   rt   r=   rE   s   @r.   r   r      s2     +6	
RYY
 
: r0   r   c                   Z   ^  \ rS rSr\SS4S\\R                     4U 4S jjjrS r	Sr
U =r$ )
ReductionB   Nr   c                 B  > X#S.n[         TU ]  5         [        R                  " U" SSSS.UD6U" SSSS.UD65      U l        [        R                  " U" SSSS.UD6U" SSSSS.UD6U" SS	SS
S.UD6U" SSSS.UD65      U l        [        R                  " SSS9U l        g )Nr   r   r   r   r   rJ   rL   rM   rO   rP   r   r   r\   )r   r|   )r|   r|   )r|   @  )r   r   )r$   r%   r&   rR   rS   rT   r'   ri   r*   s        r.   r%   ReductionB.__init__   s     /}}@a@R@?Qq?B?

 }}@a@R@TVAvTQSTTVAvTQST?Qq?B?	
 ||Aa0r0   c                     U R                  U5      nU R                  U5      nU R                  U5      n[        R                  " X#U4S5      nU$ r2   r   r   s         r.   r:   ReductionB.forward   r   r0   r   r=   rE   s   @r.   r   r      s2     +6	1RYY1 1. r0   r   c                   Z   ^  \ rS rSr\SS4S\\R                     4U 4S jjjrS r	Sr
U =r$ )
InceptionC   Nr   c           	        > X#S.n[         TU ]  5         U" SSSS.UD6U l        U" SSSS.UD6U l        U" SSSSS.UD6U l        U" SSSSS.UD6U l        U" SSSS.UD6U l        U" SSSSS.UD6U l        U" SSSSS.UD6U l        U" SSSSS.UD6U l	        U" SSSSS.UD6U l
        [        R                  " [        R                  " S	SSS
S9U" SSSS.UD65      U l        g )Nr   r   r   )r   r   )r   r   rM   )r   r   )r   r   r   Fre   )   r|   )r   rg   )rg   r|   )rg     )r      )r   r|   )r$   r%   rS   	branch1_0
branch1_1a
branch1_1b	branch2_0	branch2_1	branch2_2
branch2_3a
branch2_3br&   rR   rj   rk   r*   s        r.   r%   InceptionC.__init__   s*    /!K1KK#M1QM"M$b6!U[b_ab$b6!U[b_ab#M1QM"M#a&TZa^`a#a&TZa^`a$b6!U[b_ab$b6!U[b_ab}}LL1a5I@a@R@
r0   c                    U R                  U5      nU R                  U5      nU R                  U5      nU R                  U5      n[        R
                  " XE4S5      nU R                  U5      nU R                  U5      nU R                  U5      n	U R                  U	5      n
U R                  U	5      n[        R
                  " X4S5      nU R                  U5      n[        R
                  " X&X4S5      nU$ r2   )rS   r   r   r   r3   r4   r   r   r   r   r   rk   )r+   r6   r7   x1_0x1_1ax1_1br8   x2_0x2_1x2_2x2_3ax2_3brp   rq   r9   s                  r.   r:   InceptionC.forward   s    \\!_~~a %%YY~q)~~a ~~d#~~d#%%YY~q)\\!_ii(!,
r0   )
rS   r   r   r   r   r   r   r   r   rk   r=   rE   s   @r.   r   r      s2     +6	
RYY
 
4 r0   r   c                   R  ^  \ rS rSr          S S\S\S\S\S\S\S	\S
\SS4U 4S jjjr\R                  R                  S!S j5       r\R                  R                  S"S j5       r\R                  R                  S\R                  4S j5       rS#S\S\4S jjr     S$S\R"                  S\\\\\   4      S\S\S\S\S\\\R"                     \\R"                  \\R"                     4   4   4S jjr   S%S\\\\   4   S\S\4S jjrS rS!S\4S jjrS rSrU =r$ )&r   i  Nnum_classesin_chansoutput_stride	drop_rateglobal_pool
norm_layernorm_eps	act_layerreturnc                   > XS.n[         TU ]  5         US:X  d   eXl        X l        S=U l        U l        [        [        SUU[        US9[        SS9S9nU" US4S	S
S.UD6U" SS	SS.UD6U" SS	SSS.UD6[        U40 UD6[        U40 UD6[        U40 UD6/nU[        S5       Vs/ s H  n[        U40 UD6PM     sn-  nU[        U40 UD6/-  nU[        S5       Vs/ s H  n[        U40 UD6PM     sn-  nU[!        U40 UD6/-  nU[        S	5       Vs/ s H  n[#        U40 UD6PM     sn-  n[$        R&                  " U6 U l        [        SS
SS9[        SSSS9[        SSSS9[        SSSS9[        SSSS9/U l        [-        U R                  U R                  4UUS.UD6u  U l        U l        U l        g s  snf s  snf s  snf )Nr       r   r   )epsT)inplace)rN   r   r   norm_kwargs
act_kwargsr   r   r   r   r"   rM      rK   z
features.2)num_chs	reductionmodulerQ   z
features.3rg      z
features.9r      zfeatures.17zfeatures.21)	pool_typer   )r   r   )r   r"   )r$   r%   r   r   num_featureshead_hidden_sizer   r   dictr   rG   rY   rangerb   rv   r   r   r   r&   rR   featuresfeature_infor   r   	head_droplast_linear)r+   r   r   r   r   r   r   r   r   r   r   r,   r   r   _r-   s                  r.   r%   InceptionV4.__init__  s    /"""& 488D1!*D)

 xC1CC=1Q="=H1QHRHJ%"%J%"%J%"%
 	58D8aZ
1b18DDZ
1b12258D8aZ
1b18DDZ
1b12258D8aZ
1b18DDx0q>,?,?MBMB
 >O>
 "	>

 >
:$.$*: EDDs   (F>!GGc                     [        SSS9$ )Nz^features\.[012]\.z^features\.(\d+))stemblocks)r   )r+   coarses     r.   group_matcherInceptionV4.group_matcherG  s    &&
 	
r0   c                      U(       a   S5       eg )Nz$gradient checkpointing not supported )r+   enables     r.   set_grad_checkpointing"InceptionV4.set_grad_checkpointingN  s    AAAz6r0   c                     U R                   $ N)r   )r+   s    r.   get_classifierInceptionV4.get_classifierR  s    r0   c                 h    Xl         [        U R                  U R                   US9u  U l        U l        g )N)r   )r   r   r   r   r   )r+   r   r   s      r.   reset_classifierInceptionV4.reset_classifierV  s1    &->t//;.H*$*r0   r6   indicesnorm
stop_early
output_fmtintermediates_onlyc                    US;   d   S5       e/ nU R                    Vs/ s H#  n[        US   R                  S5      S   5      PM%     n	n[        [	        U	5      U5      u  pU
 Vs/ s H  oU   PM	     n
nX   n[
        R                  R                  5       (       d  U(       d  U R                  nOU R                  SUS-    n[        U5       H%  u  pU" U5      nX;   d  M  UR                  U5        M'     U(       a  U$ X4$ s  snf s  snf )a  Forward features that returns intermediates.

Args:
    x: Input image tensor
    indices: Take last n blocks if int, all if None, select matching indices if sequence
    norm: Apply norm layer to compatible intermediates
    stop_early: Stop iterating over blocks when last desired intermediate hit
    output_fmt: Shape of intermediate feature outputs
    intermediates_only: Only return intermediate features
Returns:

)NCHWzOutput shape must be NCHW.r   .Nr   )r   intsplitr   lenr3   jitis_scriptingr   	enumerateappend)r+   r6   r   r   r   r   r   intermediatesinfo
stage_endstake_indices	max_indexistagesfeat_idxstages                   r.   forward_intermediates!InceptionV4.forward_intermediates[  s   * Y&D(DD&EIEVEVWEVTc$x...s3B78EV
W"6s:"P/;<|!1|<)	 99!!##:]]F]]>IM2F(0OHaA'$$Q'  1
   ' X<s   *D%D
prune_norm
prune_headc                    U R                    Vs/ s H#  n[        US   R                  S5      S   5      PM%     nn[        [	        U5      U5      u  pgXW   nU R
                  SUS-    U l        U(       a  U R                  SS5        U$ s  snf )z?Prune layers not required for specified intermediates.
        r   r   r   Nr   r    )r   r   r   r   r   r   r   )r+   r   r   r   r   r   r   r   s           r.   prune_intermediate_layers%InceptionV4.prune_intermediate_layers  s     FJEVEVWEVTc$x...s3B78EV
W"6s:"P)	ny1}5!!!R( Xs   *Bc                 $    U R                  U5      $ r   )r   r+   r6   s     r.   forward_featuresInceptionV4.forward_features  s    }}Qr0   
pre_logitsc                 z    U R                  U5      nU R                  U5      nU(       a  U$ U R                  U5      $ r   )r   r   r   )r+   r6   r  s      r.   forward_headInceptionV4.forward_head  s8    QNN1q7D$4$4Q$77r0   c                 J    U R                  U5      nU R                  U5      nU$ r   )r  r
  r  s     r.   r:   InceptionV4.forward  s'    !!!$a r0   )	r   r   r   r   r   r   r   r   r   )
  r   r   g        avgbatchnorm2dgMbP?reluNNF)T)r  )NFFr   F)r   FT)r>   r?   r@   rA   r   floatstrr%   r3   r   ignorer   r   r&   rB   r   r   Tensorr   r   r   boolr   r   r  r  r
  r:   rC   rD   rE   s   @r.   r   r     s     $!#!$+"#7
7
 7
 	7

 7
 7
 7
 7
 7
 
7
 7
r YY
 
 YYB B YY 		    HC Hc H 8<$$',* ||*  eCcN34*  	* 
 *  *  !%*  
tELL!5tELL7I)I#JJ	K* \ ./$#	3S	>*  	  8$ 8
 r0   r   c                 8    [        [        U U4S[        SS90UD6$ )Nfeature_cfgT)flatten_sequential)r   r   r   )variant
pretrainedkwargss      r.   _create_inception_v4r    s1     D1	
  r0   zinception_v4.tf_in1kztimm/r  )r   +  r  )r   r   g      ?bicubiczfeatures.0.convr   z
apache-2.0)	hf_hub_idr   
input_size	pool_sizecrop_pctinterpolationmeanstd
first_conv
classifierlicensec                     [        SU 40 UD6$ )Ninception_v4)r  )r  r  s     r.   r,  r,    s    
EfEEr0   r  )'__doc__	functoolsr   typingr   r   r   r   r   r3   torch.nnr&   	timm.datar	   r
   timm.layersr   r   _builderr   	_featuresr   	_registryr   r   __all__rB   r   rG   rY   rb   rv   r   r   r   r   r  default_cfgsr,  r   r0   r.   <module>r8     s     5 5   E 6 * + </bii &bii :bii &! !H 6$ $N @- -`U")) Up  %=vI'0F'}	& 	 F Fr0   