o
    ߥi                     @   s^   d dl mZ G dd dejZ			dddZd	d
 ZG dd dejZG dd dejZdS )    Nc                       s*   e Zd ZdZd	 fdd	Zdd Z  ZS )
FPNFusionModulez8 This is a fpn-style cross-scale feature fusion module"       Fc                    s4   t    	 || _|| _|| _t||||d| _d S )N)use_bnn_block)super__init__
embed_dimsfuse_dimr   _make_multi_scale_layersmulti_scaler)selfr	   r
   r   r   	__class__ X/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/cv/vidt/fpn_fusion.pyr      s   
zFPNFusionModule.__init__c                 C   s   |}t | jd ddD ]!}t| jd| d|| ||< t| jd| || ||< qg }t | jd ddD ]6}|| jd krQt| jd| || gd }nt| jd| ||| g||  dd  }|| q8|S )N   layer__rnp_norm_
refinenet_   )ranger   getattrr   sizeappend)r   x_blocksidxrefined_embedspathr   r   r   forward   s0   


zFPNFusionModule.forward)r   r   F__name__
__module____qualname____doc__r   r!   __classcell__r   r   r   r   r      s    r   r   r   Fc           	   	      s    fddt |D }t }t |d ddD ]W}	 d| d}||tj| | || dd d| }||td	||  d
| }||t | tjjt	|d| dj
dd tjt	|d| djd q|S )Nc                    s   g | ]} qS r   r   ).0_	out_shaper   r   
<listcomp>=   s    z,_make_multi_scale_layers.<locals>.<listcomp>r   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_shaper+   r   groupsr   
out_shapesr   r   
layer_namer   r*   r   r   7   s(   

 r   c                 C   s   t | td|dddS )z. We use a resnet bottleneck structure for fpn FT)bnexpandalign_corners)FeatureFusionBlockr0   ReLU)featuresr   r   r   r   r5   X   s   r5   c                       s0   e Zd ZdZ			d fdd	Zdd Z  ZS )	rB   z Feature fusion block FTc                    sx   t t|   || _d| _|| _|}| jdu r|d }tj||dddd| _t	|||| _
t	|||| _tj | _dS )a-  Init.
        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   )r-   r:   r<   N)r   rB   r   rA   r<   r@   r0   r3   	smoothingResidualConvUnitresConfUnit1resConfUnit2	quantizedFloatFunctionalskip_add)r   rD   
activationr?   r@   rA   out_featuresr   r   r   r   g   s"   
zFeatureFusionBlock.__init__c                 C   s^   |d }t |dkr(tjj||d| jd}| |}| |d }| j||}| 	|}|S )aR   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   moderA   r   )
lenr0   
functionalinterpolaterA   rE   rG   rK   addrH   )r   xsup_sizeoutputresr   r   r   r!      s   	

zFeatureFusionBlock.forward)FFTr"   r   r   r   r   rB   d   s    $rB   c                       s(   e Zd ZdZ fddZdd Z  ZS )rF   z Residual convolution module. c              	      s   t    || _d| _tj|ddd| j | jd| _tjddddd| j | jd| _tjd|dd| j | jd| _| jdu rPt	|| _
t	|| _t	|| _|| _tj | _dS )zInit.
        Args:
            features (int): channel dim of the input
            activation: activation function
            bn: whether to use bn
        r   @   )r-   strider:   r<      )r-   rY   paddingr:   r<   TN)r   r   r?   r<   r0   r3   conv1conv2conv3BatchNorm2dbn1bn2bn3rL   rI   rJ   rK   )r   rD   rL   r?   r   r   r   r      sD   
	
zResidualConvUnit.__init__c                 C   s   |  |}| |}| jdu r| |}|  |}| |}| jdu r(| |}|  |}| |}| jdu r<| |}| jdkrF| 	|}| j
||S )z Forward pass

        Args:
            x (tensor): input feature

        Returns:
            tensor: output feature
        Tr   )rL   r\   r?   r`   r]   ra   r^   rb   r<   
conv_mergerK   rS   )r   xoutr   r   r   r!      s   














zResidualConvUnit.forwardr"   r   r   r   r   rF      s    .rF   )r   r   F)torch.nnr0   r1   r   r   r5   rB   rF   r   r   r   r   <module>   s   1
!G