o
    ߥiw                     @   sp   d dl Z d dlmZ d dlm  mZ G dd dejZG dd dejZ			dd	d
Z	dd Z
dddZdS )    Nc                       s*   e Zd ZdZd fdd	Zdd Z  ZS )	ResidualBlockNoBNa?  Residual block without BN.
    It has a style of:
    ::
        ---Conv-ReLU-Conv-+-
         |________________|
    Args:
        mid_channels (int): Channel number of intermediate features.
            Default: 64.
        res_scale (float): Used to scale the residual before addition.
            Default: 1.0.
    @         ?c                    sR   t    || _tj||ddddd| _tj||ddddd| _tjdd| _d S )N      T)bias)inplace)	super__init__	res_scalennConv2dconv1conv2ReLUrelu)selfmid_channelsr   	__class__ f/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/cv/video_super_resolution/common.pyr
      s
   
zResidualBlockNoBN.__init__c                 C   s(   |}|  | | |}||| j  S )zForward function.
        Args:
            x (Tensor): Input tensor with shape (n, c, h, w).
        Returns:
            Tensor: Forward results.
        )r   r   r   r   )r   xidentityoutr   r   r   forward   s   zResidualBlockNoBN.forward)r   r   __name__
__module____qualname____doc__r
   r   __classcell__r   r   r   r   r   
   s    r   c                       s(   e Zd ZdZ fddZdd Z  ZS )PixelShufflePacka=  Pixel Shuffle upsample layer.
    Args:
        in_channels (int): Number of input channels.
        out_channels (int): Number of output channels.
        scale_factor (int): Upsample ratio.
        upsample_kernel (int): Kernel size of Conv layer to expand channels.
    Returns:
        Upsampled feature map.
    c                    sR   t    || _|| _|| _|| _tj| j| j| | | j| jd d d| _d S )Nr      )padding)	r	   r
   in_channelsout_channelsscale_factorupsample_kernelr   r   upsample_conv)r   r%   r&   r'   r(   r   r   r   r
   7   s   
zPixelShufflePack.__init__c                 C   s   |  |}t|| j}|S )zForward function for PixelShufflePack.
        Args:
            x (Tensor): Input tensor with shape (n, c, h, w).
        Returns:
            Tensor: Forward results.
        )r)   Fpixel_shuffler'   )r   r   r   r   r   r   D   s   
zPixelShufflePack.forwardr   r   r   r   r   r"   ,   s    
r"   bilinearzerosTc              	   C   s@  |   dd |  dd kr'td|   dd  d|  dd  d|   \}}}}|j}ttjd||| jd	tjd||| jd	\}	}
t|
|	fd
}d|_|| }d|dddddddf  t	|d d d }d|dddddddf  t	|d d d }tj||fdd}t
j| ||||d}|S )a  Warp an image or a feature map with optical flow.
    Args:
        x (Tensor): Tensor with size (n, c, h, w).
        flow (Tensor): Tensor with size (n, h, w, 2). The last dimension is
            a two-channel, denoting the width and height relative offsets.
            Note that the values are not normalized to [-1, 1].
        interpolation (str): Interpolation mode: 'nearest' or 'bilinear'.
            Default: 'bilinear'.
        padding_mode (str): Padding mode: 'zeros' or 'border' or 'reflection'.
            Default: 'zeros'.
        align_corners (bool): Whether align corners. Default: True.
    Returns:
        Tensor: Warped image or feature map.
    Nr   r   zThe spatial sizes of input (z) and flow (z) are not the same.r   )devicedtyper#   Fg       @r   )dim)modepadding_modealign_corners)size
ValueErrorr/   torchmeshgridaranger0   stackrequires_gradmaxr*   grid_sample)r   flowinterpolationr3   r4   _hwr/   grid_ygrid_xgrid	grid_flowgrid_flow_xgrid_flow_youtputr   r   r   	flow_warpP   s0    
22rJ   c                 K   s0   g }t |D ]}|| di | qtj| S )zMake layers by stacking the same blocks.
    Args:
        block (nn.module): nn.module class for basic block.
        num_blocks (int): number of blocks.
    Returns:
        nn.Sequential: Stacked blocks in nn.Sequential.
    Nr   )rangeappendr   
Sequential)block
num_blockskwarglayersr@   r   r   r   
make_layer}   s   
rR   -q=c                 C   s   t | | d | S )Nr#   )r7   sqrt)predtargetepsr   r   r   charbonnier_loss   s   rX   )r,   r-   T)rS   )r7   torch.nnr   torch.nn.functional
functionalr*   Moduler   r"   rJ   rR   rX   r   r   r   r   <module>   s   "&
-