
    9i                         S SK Jr   " S S\R                  5      r   S
S jrS r " S S\R                  5      r " S S	\R                  5      rg)    Nc                   6   ^  \ rS rSrSrSU 4S jjrS rSrU =r$ )FPNFusionModule   z7This is a fpn-style cross-scale feature fusion module" c                 f   > [         TU ]  5          Xl        X l        X0l        [        XXCS9U l        g )N)use_bnn_block)super__init__
embed_dimsfuse_dimr   _make_multi_scale_layersmulti_scaler)selfr   r   r   r   	__class__s        d/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/models/cv/vidt/fpn_fusion.pyr
   FPNFusionModule.__init__   s9    	 %  5B    c                    Un[        U R                  S-
  SS5       HJ  n[        U R                  SU S35      " X   5      X'   [        U R                  SU 35      " X   5      X'   ML     / n[        U R                  S-
  SS5       H  nX R                  S-
  :X  a$  [        U R                  SU 35      " X   /S 5      nO7[        U R                  SU 35      " WX   /X   R	                  5       SS  5      nUR                  U5        M     U$ )N   layer__rnp_norm_
refinenet_   )ranger   getattrr   sizeappend)r   x_blocksidxrefined_embedspaths        r   forwardFPNFusionModule.forward   s(    )2r2C#D$5$5uC7HIHM#D$5$5GHM 3 )2r2CllQ&&t00!+C5134<M?DJ t00!+C51348(-3H3;=3E3E3G3KM !!$' 3 r   )r   r   r   r   )      F	__name__
__module____qualname____firstlineno____doc__r
   r$   __static_attributes____classcell__r   s   @r   r   r      s    BB$ r   r   c           
      v   [        U5       Vs/ s H  oQPM     nn[        R                  " 5       n[        US-
  SS5       H  n SU S3n	UR                  U	[        R                  " X   Xh   SS95        SU 3n	UR                  U	[        R
                  " SXh   5      5        SU 3n	UR                  U	[        X5      5        [        R                  R                  [        USU S35      R                  SS	9  [        R                  R                  [        USU S35      R                  S
5        M     U$ s  snf )Nr   r   r   r   )kernel_sizer       r   )gainr   )r   nnModule
add_moduleConv2d	GroupNorm_make_fusion_blockinitxavier_uniform_r   weight	constant_bias)
in_shape	out_shaper   groupsr   _
out_shapesr   r!   
layer_names
             r   r   r   7   s5    &+7^4^)^J499;LWq["b)	 sWC(
IIhmZ_!D	F g&

BLLZ_,MN!#'

 29 E	G 	LF3%s"34;;! 	  	E
',&S0ABGGK' ** 1 5s   D6c                 D    [        U [        R                  " S5      USSS9$ )z-We use a resnet bottleneck structure for fpn FT)bnexpandalign_corners)FeatureFusionBlockr5   ReLU)featuresr   s     r   r:   r:   X   s(     
 r   c                   <   ^  \ rS rSrSr   SU 4S jjrS rSrU =r$ )rJ   d   zFeature fusion block c                 @  > [         [        U ]  5         XPl        SU l        X@l        UnU R
                  SL a  US-  n[        R                  " UUSSSS9U l        [        XU5      U l
        [        XU5      U l        [        R                  R                  5       U l        g)zInit.
Args:
    features (int): channel dim of the input feature
    activation: activation function to use
    bn: whether to use bn
    expand: whether to expand feature or not
    align_corners: whether to use align_corners for interpolation
r   Tr   )r2   r?   rB   N)r	   rJ   r
   rI   rB   rH   r5   r8   	smoothingResidualConvUnitresConfUnit1resConfUnit2	quantizedFloatFunctionalskip_add)r   rL   
activationrG   rH   rI   out_featuresr   s          r   r
   FeatureFusionBlock.__init__g   s     	 $02*;;$#q=L
 -X2F,X2F446r   c                 $   US   n[        U5      S:X  aj  [        R                  R                  UUSU R                  S9nU R                  U5      nU R                  US   5      nU R                  R                  X45      nU R                  U5      nU$ )a!  Forward pass.
Args
    xs: xs[0]: the feature refined from the previous step, xs[1]: the next scale features to fuse
    up_size: the size for upsampling; xs[0] is upsampled before merging with xs[1]
Returns:
    output: the fused feature, which is fed to the next fusion step as an input
r   r   bilinear)r   moderI   r   )
lenr5   
functionalinterpolaterI   rP   rR   rV   addrS   )r   xsup_sizeoutputress        r   r$   FeatureFusionBlock.forward   s     Ar7a<]].."00	 / 2F ^^F+F ##BqE*C ]]&&v3F ""6*r   )rI   rH   rB   rR   rS   rV   rP   )FFTr(   r0   s   @r   rJ   rJ   d   s!     
 #"7H r   rJ   c                   2   ^  \ rS rSrSrU 4S jrS rSrU =r$ )rQ      zResidual convolution module. c           
        > [         TU ]  5         X0l        SU l        [        R
                  " USSSU R                  (       + U R                  S9U l        [        R
                  " SSSSSU R                  (       + U R                  S9U l        [        R
                  " SUSSU R                  (       + U R                  S9U l        U R                  SL aQ  [        R                  " U5      U l
        [        R                  " U5      U l        [        R                  " U5      U l        X l        [        R                  R                  5       U l        g)zwInit.
Args:
    features (int): channel dim of the input
    activation: activation function
    bn: whether to use bn
r   @   )r2   strider?   rB      )r2   rj   paddingr?   rB   TN)r	   r
   rG   rB   r5   r8   conv1conv2conv3BatchNorm2dbn1bn2bn3rW   rT   rU   rV   )r   rL   rW   rG   r   s       r   r
   ResidualConvUnit.__init__   s     	YYWW;;

 YYWW;;

 YYWW;;

 77d?~~h/DH~~h/DH~~h/DH$446r   c                    U R                  U5      nU R                  U5      nU R                  SL a  U R                  U5      nU R                  U5      nU R	                  U5      nU R                  SL a  U R                  U5      nU R                  U5      nU R                  U5      nU R                  SL a  U R                  U5      nU R                  S:  a  U R                  U5      nU R                  R                  X!5      $ )zWForward pass

Args:
    x (tensor): input feature

Returns:
    tensor: output feature
Tr   )rW   rm   rG   rq   rn   rr   ro   rs   rB   
conv_mergerV   r`   )r   xouts      r   r$   ResidualConvUnit.forward   s     ooa jjo77d?((3-Cooc"jjo77d?((3-Cooc"jjo77d?((3-C;;?//#&C}}  ((r   )
rW   rG   rq   rr   rs   rm   rn   ro   rB   rV   r(   r0   s   @r   rQ   rQ      s    (,7\) )r   rQ   )r'   r   F)torch.nnr5   r6   r   r   r:   rJ   rQ    r   r   <module>r|      sV   
 ,bii ,b &'$%$)	B	D DNM)ryy M)r   