o
    *j                     @   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__ e/var/www/html/Deteccion_Ine/venv/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   Zx_blocksidxZ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   )Zgainr   )r   nnModuleZ
add_moduleConv2dZ	GroupNorm_make_fusion_blockinitZxavier_uniform_r   weightZ	constant_bias)	Zin_shaper)   r   groupsr   Z
out_shapesr   r   Z
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)FeatureFusionBlockr-   ZReLU)featuresr   r   r   r   r0   X   s   r0   c                       s0   e Zd ZdZ			d fdd	Zdd Z  ZS )	r8   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+   r3   r4   N)r   r8   r   r7   r4   r6   r-   r/   	smoothingResidualConvUnitresConfUnit1resConfUnit2	quantizedFloatFunctionalskip_add)r   r9   
activationr5   r6   r7   Z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   Zbilinear)r   moder7   r   )
lenr-   Z
functionalZinterpolater7   r:   r<   r@   addr=   )r   ZxsZup_sizeoutputresr   r   r   r      s   	

zFeatureFusionBlock.forward)FFTr    r   r   r   r   r8   d   s    $r8   c                       s(   e Zd ZdZ fddZdd Z  ZS )r;   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+   strider3   r4      )r+   rH   paddingr3   r4   TN)r   r   r5   r4   r-   r/   conv1conv2conv3ZBatchNorm2dbn1bn2bn3rA   r>   r?   r@   )r   r9   rA   r5   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   )rA   rK   r5   rN   rL   rO   rM   rP   r4   Z
conv_merger@   rD   )r   xoutr   r   r   r      s   














zResidualConvUnit.forwardr    r   r   r   r   r;      s    .r;   )r   r   F)Ztorch.nnr-   r.   r   r   r0   r8   r;   r   r   r   r   <module>   s   1
!G