a e-@s8ddlZddlZddlZddlZddlZddlmZddlm Z ddlm Z ddl m Z ddl mZddlmZddlmZmZdd lmZed,d d Zd dZGddde jZGddde jZd-ddZd.ddZddZGdddeZ dd Z!d/d%d&Z"d'd(Z#e#d Z$e#d)Z%e#d*Z&e#d+Z'e#Z(dS)0N) LooseVersion)repeat)nn) functional)init) _BatchNorm)ModulatedDeformConvPackmodulated_deform_conv)get_root_loggercKst|ts|g}|D]}|D]}t|tjrntj|jfi||jj|9_|j dur|j j |q t|tj rtj|jfi||jj|9_|j dur|j j |q t|t r t |jd|j dur |j j |q qdS)aeInitialize network weights. Args: module_list (list[nn.Module] | nn.Module): Modules to be initialized. scale (float): Scale initialized weights, especially for residual blocks. Default: 1. bias_fill (float): The value to fill bias. Default: 0 kwargs (dict): Other arguments for initialization function. Nr ) isinstancelistmodulesrConv2drkaiming_normal_weightdatabiasfill_Linearr constant_) module_listscaleZ bias_fillkwargsmodulemr'D:\face swap\basicsr\archs\arch_util.pydefault_init_weightss$        rcKs0g}t|D]}||fi|q tj|S)zMake layers by stacking the same blocks. Args: basic_block (nn.module): nn.module class for basic block. num_basic_block (int): number of blocks. Returns: nn.Sequential: Stacked blocks in nn.Sequential. )rangeappendr Sequential)Z basic_blockZnum_basic_blockkwarglayers_rrr make_layer0s  r%cs*eZdZdZd fdd ZddZZS) ResidualBlockNoBNaCResidual block without BN. Args: num_feat (int): Channel number of intermediate features. Default: 64. res_scale (float): Residual scale. Default: 1. pytorch_init (bool): If set to True, use pytorch default init, otherwise, use default_init_weights. Default: False. @r Fcsltt|||_tj||ddddd|_tj||ddddd|_tjdd|_ |sht |j|jgddS)Nr T)r)inplaceg?) superr&__init__ res_scalerrconv1conv2ReLUrelur)selfnum_featr,Z pytorch_init __class__rrr+KszResidualBlockNoBN.__init__cCs(|}||||}|||jSN)r.r0r-r,)r1xidentityoutrrrforwardUszResidualBlockNoBN.forward)r'r F)__name__ __module__ __qualname____doc__r+r9 __classcell__rrr3rr&@s  r&cs eZdZdZfddZZS)UpsamplezUpsample module. Args: scale (int): Scale factor. Supported scales: 2^n and 3. num_feat (int): Channel number of intermediate features. c sg}||d@dkr\ttt|dD]0}|t|d|ddd|tdq(nF|dkr|t|d|ddd|tdntd|dt t |j |dS) Nr rr( zscale z/ is not supported. Supported scales: 2^n and 3.) rintmathlogr rr PixelShuffle ValueErrorr*r?r+)r1rr2rr$r3rrr+cszUpsample.__init__)r:r;r<r=r+r>rrr3rr?[sr?bilinearzerosTcCs|dd|ddks$J|\}}}}ttd||td||\}} t| |fd} d| _| |} d| dddddddft|ddd } d| dddddddft|ddd } tj| | fdd }t j |||||d }|S) akWarp an image or feature map with optical flow. Args: x (Tensor): Tensor with size (n, c, h, w). flow (Tensor): Tensor with size (n, h, w, 2), normal value. interp_mode (str): 'nearest' or 'bilinear'. Default: 'bilinear'. padding_mode (str): 'zeros' or 'border' or 'reflection'. Default: 'zeros'. align_corners (bool): Before pytorch 1.3, the default value is align_corners=True. After pytorch 1.3, the default value is align_corners=False. Here, we use the True as default. Returns: Tensor: Warped image or feature map. Nr r(rr@F@?dim)mode padding_mode align_corners) sizetorchmeshgridarangetype_asstackfloat requires_gradmaxF grid_sample)r6flow interp_moderPrQr$hwgrid_ygrid_xgridZvgridZvgrid_xZvgrid_yZ vgrid_scaledoutputrrr flow_warpqs$,22reFcCs|\}}}}|dkr 1.0). 2) The order of output_size should be [out_h, out_w]. interp_mode (str): The mode of interpolation for resizing. Default: 'bilinear'. align_corners (bool): Whether align corners. Default: False. Returns: Tensor: Resized flow. ratiorr shapez1Size type should be ratio or shape, but got type .N)inputrRrOrQ)rRrCrGcloner[ interpolate)r]Z size_typesizesr^rQr$Zflow_hZflow_woutput_houtput_wZ input_flowratio_hratio_wZ resized_flowrrr resize_flows$$$ rqc Cs||\}}}}||d}||dkr4||dks8J||}||}|||||||} | dddddd||||S)z Pixel unshuffle. Args: x (Tensor): Input feature with shape (b, c, hh, hw). scale (int): Downsample ratio. Returns: Tensor: the pixel unshuffled feature. r@rr r(rA)rRviewpermutereshape) r6rbchhhw out_channelr_r`x_viewrrrpixel_unshuffles  r|c@seZdZdZddZdS) DCNv2PackaKModulated deformable conv for deformable alignment. Different from the official DCNv2Pack, which generates offsets and masks from the preceding features, this DCNv2Pack takes another different features to generate offsets and masks. ``Paper: Delving Deep into Deformable Alignment in Video Super-Resolution`` c Cs||}tj|ddd\}}}tj||fdd}t|}tt|}|dkrlt} | d|dt t j t dkrt j |||j|j|j|j|j|St||||j|j|j|j|j|j|j SdS)Nr(r rM2zOffset abs mean is z, larger than 50.z0.9.0) conv_offsetrSchunkcatsigmoidmeanabsr warningr torchvision __version__ops deform_conv2drrstridepaddingdilationr groupsdeformable_groups) r1r6featr8o1o2maskoffsetZoffset_absmeanloggerrrrr9s   zDCNv2Pack.forwardN)r:r;r<r=r9rrrrr}s r}cCsdd}||d|ks(||d|kr6tjdddt||||}||||}|d|dd|d|||td| ||j ||d|WdS1s0YdS) NcSsdt|tddS)NrLrK)rDerfsqrtr6rrrnorm_cdfsz(_no_grad_trunc_normal_..norm_cdfr@zjmean is more than 2 std from [a, b] in nn.init.trunc_normal_. The distribution of values may be incorrect.) stacklevelr rK)minrZ) warningswarnrSno_graduniform_erfinv_mul_rDradd_clamp_)tensorrstdarvrlowuprrr_no_grad_trunc_normal_s   rrLrKcCst|||||S)a?Fills the input Tensor with values drawn from a truncated normal distribution. From: https://github.com/rwightman/pytorch-image-models/blob/master/timm/models/layers/weight_init.py The values are effectively drawn from the normal distribution :math:`\mathcal{N}(\text{mean}, \text{std}^2)` with values outside :math:`[a, b]` redrawn until they are within the bounds. The method used for generating the random values works best when :math:`a \leq \text{mean} \leq b`. Args: tensor: an n-dimensional `torch.Tensor` mean: the mean of the normal distribution std: the standard deviation of the normal distribution a: the minimum cutoff value b: the maximum cutoff value Examples: >>> w = torch.empty(3, 5) >>> nn.init.trunc_normal_(w) )r)rrrrrvrrr trunc_normal_srcsfdd}|S)Ncs t|tjjr|Stt|Sr5)r collectionsabcIterabletuplerrnrrparse-sz_ntuple..parser)rrrrr_ntuple+s rr@r(rA)r r)rHrIT)rHF)rrLrrK))collections.abcrrDrSrrZdistutils.versionr itertoolsrrtorch.nnrr[rZtorch.nn.modules.batchnormrbasicsr.ops.dcnrr basicsr.utilsr rrr%Moduler&r!r?rerqr|r}rrrZ to_1tupleZ to_2tupleZ to_3tupleZ to_4tupleZ to_ntuplerrrrs:         " '&