a
    d1.                     @   s   d dl Zd dlZd dlmZ d dlmZ d dlmZ G dd dej	Z
G dd dej	ZG d	d
 d
ej	Ze G dd dej	ZdS )    N)nn)
functional)ARCH_REGISTRYc                       s*   e Zd ZdZd	 fdd	Zdd Z  ZS )
DenseBlocksTemporalReducea  A concatenation of 3 dense blocks with reduction in temporal dimension.

    Note that the output temporal dimension is 6 fewer the input temporal dimension, since there are 3 blocks.

    Args:
        num_feat (int): Number of channels in the blocks. Default: 64.
        num_grow_ch (int): Growing factor of the dense blocks. Default: 32
        adapt_official_weights (bool): Whether to adapt the weights translated from the official implementation.
            Set to false if you want to train from scratch. Default: False.
    @       Fc                    s  t t|   |rd}d}nd}d}ttj|||dtjddtj||ddddd	tj|||dtjddtj||d
dddd	| _ttj|| ||dtjddtj|| || ddddd	tj|| ||dtjddtj|| |d
dddd	| _	ttj|d|  ||dtjddtj|d|  |d|  ddddd	tj|d|  ||dtjddtj|d|  |d
dddd	| _
d S )NMbP?h㈵>皙?epsmomentumTZinplace   r   r   r   r   r   ZstridepaddingZbias   r   r   r   r   r      )superr   __init__r   
SequentialBatchNorm3dReLUConv3dtemporal_reduce1temporal_reduce2temporal_reduce3)selfnum_featnum_grow_chadapt_official_weightsr   r   	__class__ _/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/basicsr/archs/duf_arch.pyr      sN    
 

z"DenseBlocksTemporalReduce.__init__c              	   C   s   |  |}t|ddddddddddf |fd}| |}t|ddddddddddf |fd}| |}t|ddddddddddf |fd}|S )z
        Args:
            x (Tensor): Input tensor with shape (b, num_feat, t, h, w).

        Returns:
            Tensor: Output with shape (b, num_feat + num_grow_ch * 3, 1, h, w).
        Nr   )r   torchcatr   r    )r!   xx1Zx2Zx3r'   r'   r(   forward:   s    
2
2
2z!DenseBlocksTemporalReduce.forward)r   r   F__name__
__module____qualname____doc__r   r.   __classcell__r'   r'   r%   r(   r   	   s   %r   c                       s*   e Zd ZdZd	 fdd	Zdd Z  ZS )
DenseBlocksa(   A concatenation of N dense blocks.

    Args:
        num_feat (int): Number of channels in the blocks. Default: 64.
        num_grow_ch (int): Growing factor of the dense blocks. Default: 32.
        num_block (int): Number of dense blocks. The values are:
            DUF-S (16 layers): 3
            DUF-M (18 layers): 9
            DUF-L (52 layers): 21
        adapt_official_weights (bool): Whether to adapt the weights translated from the official implementation.
            Set to false if you want to train from scratch. Default: False.
    r      Fc                    s   t t|   |rd}d}nd}d}t | _td|D ]}| jttj	|||  ||dtj
ddtj|||  |||  ddd	dd
tj	|||  ||dtj
ddtj|||  |ddddd
 q8d S )Nr   r	   r
   r   r   Tr   r   r   r   r   )r   r5   r   r   Z
ModuleListdense_blocksrangeappendr   r   r   r   )r!   	num_blockr"   r#   r$   r   r   ir%   r'   r(   r   \   s:    
 


zDenseBlocks.__init__c                 C   s8   t dt| jD ]"}| j| |}t||fd}q|S )z
        Args:
            x (Tensor): Input tensor with shape (b, num_feat, t, h, w).

        Returns:
            Tensor: Output with shape (b, num_feat + num_block * num_grow_ch, t, h, w).
        r   r   )r8   lenr7   r*   r+   )r!   r,   r;   yr'   r'   r(   r.   x   s    zDenseBlocks.forward)r   r6   Fr/   r'   r'   r%   r(   r5   N   s   r5   c                       s*   e Zd ZdZd fdd	Zdd Z  ZS )DynamicUpsamplingFiltera%  Dynamic upsampling filter used in DUF.

    Ref: https://github.com/yhjo09/VSR-DUF.
    It only supports input with 3 channels. And it applies the same filters to 3 channels.

    Args:
        filter_size (tuple): Filter size of generated filters. The shape is (kh, kw). Default: (5, 5).
       r@   c                    s   t t|   t|ts&td| t|dkrFtdt| d|| _t	
|}tt|j|dg|R  }|dddd| _d S )Nz3The type of filter_size must be tuple, but got typer   z-The length of filter size must be 2, but got .r   r   )r   r>   r   
isinstancetuple	TypeErrorr<   
ValueErrorfilter_sizenpprodr*   Zeyeintviewrepeatexpansion_filter)r!   rF   filter_prodrL   r%   r'   r(   r      s    

z DynamicUpsamplingFilter.__init__c                 C   s   |  \}}}}}| j\}}	tj|| j||d |	d fdd}
|
|d|||ddddd}
|ddddd}t	|
|}|ddddd|d| ||S )a  Forward function for DynamicUpsamplingFilter.

        Args:
            x (Tensor): Input image with 3 channels. The shape is (n, 3, h, w).
            filters (Tensor): Generated dynamic filters.
                The shape is (n, filter_prod, upsampling_square, h, w).
                filter_prod: prod of filter kernel size, e.g., 1*5*5=25.
                upsampling_square: similar to pixel shuffle,
                    upsampling_square = upsampling * upsampling
                    e.g., for x 4 upsampling, upsampling_square= 4*4 = 16

        Returns:
            Tensor: Filtered image with shape (n, 3*upsampling_square, h, w)
        r   r   )r   groupsr      r   )
sizerF   FZconv2drL   torJ   permuter*   matmul)r!   r,   filtersnrM   Zupsampling_squarehwZkhkwZexpanded_inputoutr'   r'   r(   r.      s    
zDynamicUpsamplingFilter.forward)r?   r/   r'   r'   r%   r(   r>      s   	r>   c                       s*   e Zd ZdZd	 fdd	Zdd Z  ZS )
DUFa  Network architecture for DUF

    Paper: Jo et.al. Deep Video Super-Resolution Network Using Dynamic
            Upsampling Filters Without Explicit Motion Compensation, CVPR, 2018
    Code reference:
        https://github.com/yhjo09/VSR-DUF
    For all the models below, 'adapt_official_weights' is only necessary when
    loading the weights converted from the official TensorFlow weights.
    Please set it to False if you are training the model from scratch.

    There are three models with different model size: DUF16Layers, DUF28Layers,
    and DUF52Layers. This class is the base class for these models.

    Args:
        scale (int): The upsampling factor. Default: 4.
        num_layer (int): The number of layers. Default: 52.
        adapt_official_weights_weights (bool): Whether to adapt the weights
            translated from the official implementation. Set to false if you
            want to train from scratch. Default: False.
    rO   4   Fc           	         sn  t t|   || _|r"d}d}nd}d}tjdddddd	d
| _td| _|dkr^d}d}n4|dkrpd}d}n"|dkrd}d}nt	d| dt
|d||d| _td||  ||d| _d||  |d  }tj|||d| _tj|ddddd	d
| _tjdddddd	d
| _tjdd|d  dddd	d
| _tjdddddd	d
| _tjdd|d  dddd	d
| _d S )Nr   r	   r
   r   r   )r   r   r   r   r   Tr   r?   r6   r      	   r\      z,Only supported (16, 28, 52) layers, but got rA   )r:   r"   r#   r$   )r$   r      r   r   i      )r   r[   r   scaler   r   conv3d1r>   dynamic_filterrE   r5   dense_block1r   dense_block2r   bn3d2conv3d2	conv3d_r1	conv3d_r2	conv3d_f1	conv3d_f2)	r!   rb   Z	num_layerr$   r   r   r:   r#   Zchannelsr%   r'   r(   r      sF    
 zDUF.__init__c                 C   s  |  \}}}}}|ddddd}|dddd|d ddddf }| |}| |}| |}tj| |dd}tj| |dd}| 	tj| 
|dd}| tj| |dd}	tj|	|d	| jd ||dd
}	| ||	}
|
|d7 }
t|
| j}
|
S )z
        Args:
            x (Tensor): Input with shape (b, 7, c, h, w)

        Returns:
            Tensor: Output with shape (b, c, h * scale, w * scale)
        r   r   r   r   rO   NTr   ra   )Zdim)rP   rS   rc   re   rf   rQ   Zrelurg   rh   rj   ri   rl   rk   ZsoftmaxrJ   rb   rd   Zsqueeze_Zpixel_shuffle)r!   r,   Znum_batchesZnum_imgs_rW   rX   Zx_centerresZfilter_rZ   r'   r'   r(   r.      s    &


"zDUF.forward)rO   r\   Fr/   r'   r'   r%   r(   r[      s   )r[   )numpyrG   r*   r   Ztorch.nnr   rQ   Zbasicsr.utils.registryr   Moduler   r5   r>   registerr[   r'   r'   r'   r(   <module>   s   E80