a e.@sddlZddlZddlmZddlmZddlmZGdddej Z Gdddej Z Gd d d ej Z e Gd d d ej ZdS) N)nn) functional) ARCH_REGISTRYcs*eZdZdZd fdd ZddZZS) DenseBlocksTemporalReduceaA 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. @ Fcstt||rd}d}nd}d}ttj|||dtjddtj||ddddd tj|||dtjddtj||d dd dd |_ttj||||dtjddtj||||ddddd tj||||dtjddtj|||d dd dd |_ ttj|d |||dtjddtj|d ||d |ddddd tj|d |||dtjddtj|d ||d dd dd |_ dS) NMbP?h㈵>皙?epsmomentumTinplacerrrrrstridepaddingbiasrrrrr) superr__init__r Sequential BatchNorm3dReLUConv3dtemporal_reduce1temporal_reduce2temporal_reduce3)selfnum_feat num_grow_chadapt_official_weightsr r  __class__&D:\face swap\basicsr\archs\duf_arch.pyrsN     z"DenseBlocksTemporalReduce.__init__c Cs||}t|ddddddddddf|fd}||}t|ddddddddddf|fd}||}t|ddddddddddf|fd}|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!torchcatr"r#)r$xx1x2x3r*r*r+forward:s 2 2 2z!DenseBlocksTemporalReduce.forward)rrF__name__ __module__ __qualname____doc__rr3 __classcell__r*r*r(r+r s %rcs*eZdZdZd fdd ZddZZS) 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. rFcstt||rd}d}nd}d}t|_td|D]}|jttj |||||dtj ddtj ||||||ddd dd tj |||||dtj ddtj ||||d dddd q8dS) Nrr r rr Trrrrr) rr:rr ModuleList dense_blocksrangeappendrrrr )r$ num_blockr%r&r'r r ir(r*r+r\s:      zDenseBlocks.__init__cCs8tdt|jD]"}|j||}t||fd}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). rr)r>lenr=r-r.)r$r/rAyr*r*r+r3xszDenseBlocks.forward)rr;Fr4r*r*r(r+r:Ns r:cs*eZdZdZdfdd ZddZZS)DynamicUpsamplingFiltera+Dynamic upsampling filter used in DUF. Reference: 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). rFcstt|t|ts&td|t|dkrFtdt|d||_t |}t t |j|dg|R}|dddd|_dS)Nz3The type of filter_size must be tuple, but got typerz-The length of filter size must be 2, but got .rr)rrDr isinstancetuple TypeErrorrB ValueError filter_sizenpprodr-eyeintviewrepeatexpansion_filter)r$rL filter_prodrSr(r*r+rs   z DynamicUpsamplingFilter.__init__c Cs|\}}}}}|j\}} tj||j||d| dfdd} | |d|||ddddd} |ddddd}t | |} | ddddd|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) rr)rgroupsrr) sizerLFconv2drStorQpermuter-matmul) r$r/filtersnrTZupsampling_squarehwkhkwexpanded_inputoutr*r*r+r3s   zDynamicUpsamplingFilter.forward)rEr4r*r*r(r+rDs  rDcs*eZdZdZd fdd ZddZZS) DUFaNetwork architecture for DUF ``Paper: Deep Video Super-Resolution Network Using Dynamic Upsampling Filters Without Explicit Motion Compensation`` 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. rV4Fc sntt|||_|r"d}d}nd}d}tjdddddd d |_td |_|d kr^d}d }n4|dkrpd}d }n"|dkrd}d }nt d|dt |d||d|_ t d||||d|_ d|||d}tj|||d|_tj|ddddd d |_tjdddddd d |_tjdd|ddddd d |_tjdddddd d |_tjdd|ddddd d |_dS)Nrr r rr)rrrrrTrrEr;r rfz,Only supported (16, 28, 52) layers, but got rG)r@r%r&r')r'r rri)rrerscalerr conv3d1rDdynamic_filterrKr: dense_block1r dense_block2rbn3d2conv3d2 conv3d_r1 conv3d_r2 conv3d_f1 conv3d_f2) r$rlZ num_layerr'r r r@r&channelsr(r*r+rsF  z DUF.__init__c Cs|\}}}}}|ddddd}|dddd|dddddf}||}||}||}tj||dd}tj||dd}| tj| |dd}| tj| |dd} tj | |d |jd||dd } ||| } | |d7} 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) rrrrrVNTrrk)dim)rWr[rmrorprXrelurqrrrtrsrvrusoftmaxrQrlrnsqueeze_ pixel_shuffle) r$r/Z num_batchesnum_imgs_r_r`x_centerresfilter_rdr*r*r+r3s&   " z DUF.forward)rVrfFr4r*r*r(r+res)re)numpyrMr-rtorch.nnrrXbasicsr.utils.registryrModulerr:rDregisterrer*r*r*r+s   E8/