a
    d31                     @   s   d dl Z d dl mZ d dlmZ d dlmZ ddlmZm	Z	m
Z
 ddlmZmZ ddlmZ e G d	d
 d
ejZG dd dejZe G dd dejZG dd dejZdS )    N)nn)
functional)ARCH_REGISTRY   )ResidualBlockNoBN	flow_warp
make_layer)PCDAlignment	TSAFusion)SpyNetc                       s2   e Zd ZdZd fdd	Zdd Zd	d
 Z  ZS )BasicVSRa(  A recurrent network for video SR. Now only x4 is supported.

    Args:
        num_feat (int): Number of channels. Default: 64.
        num_block (int): Number of residual blocks for each branch. Default: 15
        spynet_path (str): Path to the pretrained weights of SPyNet. Default: None.
    @      Nc                    s   t    || _t|| _t|d ||| _t|d ||| _tj	|d |ddddd| _
tj	||d ddddd| _tj	|dddddd| _t	d	d	ddd| _t	d	dddd| _td| _tjd
dd| _d S )N      r   r   TZbias      r   皙?Znegative_slopeZinplace)super__init__num_featr   spynetConvResidualBlocksbackward_trunkforward_trunkr   Conv2dfusionupconv1upconv2conv_hr	conv_lastPixelShufflepixel_shuffle	LeakyReLUlrelu)selfr   	num_blockspynet_path	__class__ d/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/basicsr/archs/basicvsr_arch.pyr      s    

zBasicVSR.__init__c                 C   s   |  \}}}}}|d d d dd d d d d d f d|||}|d d dd d d d d d d f d|||}| ||||d d||}	| ||||d d||}
|
|	fS Nr   r   sizeZreshaper   viewr'   xbnchwZx_1Zx_2flows_backwardflows_forwardr,   r,   r-   get_flow,   s    22zBasicVSR.get_flowc              	   C   s  |  |\}}| \}}}}}g }	||| j||}
t|d ddD ]}|dd|ddddddf }||d k r|dd|ddddddf }t|
|dddd}
tj||
gdd}
| 	|
}
|	
d|
 qFt|
}
td|D ]}|dd|ddddddf }|dkr\|dd|d ddddddf }t|
|dddd}
tj||
gdd}
| |
}
tj|	| |
gdd}| | |}| | | |}| | | |}| | |}| |}tj|dd	d
d}||7 }||	|< qtj|	ddS )zForward function of BasicVSR.

        Args:
            x: Input frames with shape (b, n, c, h, w). n is the temporal dimension / number of frames.
        r   r/   Nr   r   r   Zdimr   bilinearFZscale_factormodeZalign_corners)r<   r1   	new_zerosr   ranger   permutetorchcatr   insert
zeros_liker   r&   r   r$   r   r    r!   r"   Finterpolatestack)r'   r4   r;   r:   r5   r6   _r8   r9   out_l	feat_propix_iflowoutbaser,   r,   r-   forward7   s<    ""

"
&


zBasicVSR.forward)r   r   N)__name__
__module____qualname____doc__r   r<   rS   __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 )
r   zConv and residual block used in BasicVSR.

    Args:
        num_in_ch (int): Number of input channels. Default: 3.
        num_out_ch (int): Number of output channels. Default: 64.
        num_block (int): Number of residual blocks. Default: 15.
    r   r   r   c              
      sD   t    ttj||dddddtjdddtt||d| _d S )Nr   r   Tr   r   r   r   )	r   r   r   Z
Sequentialr   r%   r   r   main)r'   Z	num_in_chZ
num_out_chr(   r*   r,   r-   r   n   s
    
 zConvResidualBlocks.__init__c                 C   s
   |  |S N)rZ   )r'   Zfear,   r,   r-   rS   t   s    zConvResidualBlocks.forward)r   r   r   rT   rU   rV   rW   r   rS   rX   r,   r,   r*   r-   r   e   s   r   c                       sB   e Zd ZdZd fdd	Zd	d
 Zdd Zdd Zdd Z  Z	S )IconVSRa  IconVSR, proposed also in the BasicVSR paper.

    Args:
        num_feat (int): Number of channels. Default: 64.
        num_block (int): Number of residual blocks for each branch. Default: 15.
        keyframe_stride (int): Keyframe stride. Default: 5.
        temporal_padding (int): Temporal padding. Default: 2.
        spynet_path (str): Path to the pretrained weights of SPyNet. Default: None.
        edvr_path (str): Path to the pretrained EDVR model. Default: None.
    r   r      r   Nc                    s  t    || _|| _|| _t|d d ||| _t|| _t	j
d| |ddddd| _t|d ||| _t	j
d| |ddddd| _td| d ||| _t	j
||d ddddd| _t	j
|dddddd| _t	
ddddd| _t	
ddddd| _t	d| _t	jd	dd
| _d S )Nr   r   r   Tr   r   r   r   r   r   )r   r   r   temporal_paddingkeyframe_strideEDVRFeatureExtractoredvrr   r   r   r   backward_fusionr   r   forward_fusionr   r   r    r!   r"   r#   r$   r%   r&   )r'   r   r(   r`   r_   r)   Z	edvr_pathr*   r,   r-   r      s     

zIconVSR.__init__c           	      C   st   |  \}}}}}d|d  d }d|d  d }|d|||}tj|d|d|gdd}|||||| || S )a  Apply padding spatially.

        Since the PCD module in EDVR requires that the resolution is a multiple
        of 4, we apply padding to the input LR images if their resolution is
        not divisible by 4.

        Args:
            x (Tensor): Input LR sequence with shape (n, t, c, h, w).
        Returns:
            Tensor: Padded LR sequence with shape (n, t, c, h_pad, w_pad).
        r   r/   r   Zreflect)r@   )r1   r2   rH   pad)	r'   r4   r6   tr7   r8   r9   Zpad_hZpad_wr,   r,   r-   pad_spatial   s    zIconVSR.pad_spatialc                 C   s   |  \}}}}}|d d d dd d d d d d f d|||}|d d dd d d d d d d f d|||}| ||||d d||}	| ||||d d||}
|
|	fS r.   r0   r3   r,   r,   r-   r<      s    22zIconVSR.get_flowc                 C   s   | j dkr6|d d ddgf ||d d ddgf g}n4| j dkrj|d d g df ||d d g df g}tj|dd	}d| j  d }i }|D ]*}| |d d ||| f  ||< q|S )
Nr   r   r   )   r^   r   )ri   iir   r=   )r_   rD   rE   rb   
contiguous)r'   r4   keyframe_idxZ
num_framesfeats_keyframerN   r,   r,   r-   get_keyframe_feature   s    
,
*(zIconVSR.get_keyframe_featurec              	   C   s  |  \}}}}}| |}|jdd  \}}ttd|| j}	|	d |d kr^|	|d  | |\}
}| ||	}g }|	|| j
||}t|d ddD ]}|d d |d d d d d d f }||d k r
|d d |d d d d d d f }t||dddd}||	v r4tj||| gdd}| |}tj||gdd}| |}|d| qt|}td|D ]"}|d d |d d d d d d f }|dkr|
d d |d d d d d d d f }t||dddd}||	v r
tj||| gdd}| |}tj||| |gdd}| |}| | | |}| | | |}| | |}| |}tj|ddd	d
}||7 }|||< qrtj|dddd d| d d| f S )Nr   r   r/   r   r   r=   r   r>   Fr?   .)r1   rg   shapelistrB   r`   appendr<   rn   rA   r   r   rC   rD   rE   rc   r   rF   rG   rd   r   r&   r$   r   r    r!   r"   rH   rI   rJ   )r'   r4   r5   r6   rK   Zh_inputZw_inputr8   r9   rl   r;   r:   rm   rL   rM   rN   rO   rP   rQ   rR   r,   r,   r-   rS      sP    
""



"
&



zIconVSR.forward)r   r   r^   r   NN)
rT   rU   rV   rW   r   rg   r<   rn   rS   rX   r,   r,   r*   r-   r]   x   s         $r]   c                       s(   e Zd ZdZ fddZdd Z  ZS )ra   zEDVR feature extractor used in IconVSR.

    Args:
        num_input_frame (int): Number of input frames.
        num_feat (int): Number of feature channels
        load_path (str): Path to the pretrained weights of EDVR. Default: None.
    c                    s   t t|   |d | _td|ddd| _ttd|d| _	t||ddd| _
t||ddd| _t||ddd| _t||ddd| _t|dd| _t||| jd| _tjd	d
d| _|r| tj|dd dd  d S )Nr   r   r   r^   rY      )r   Zdeformable_groups)r   Z	num_framecenter_frame_idxr   Tr   c                 S   s   | S r[   r,   )Zstoragelocr,   r,   r-   <lambda>.      z/EDVRFeatureExtractor.__init__.<locals>.<lambda>)Zmap_locationparams)r   ra   r   rs   r   r   
conv_firstr   r   feature_extraction	conv_l2_1	conv_l2_2	conv_l3_1	conv_l3_2r	   	pcd_alignr
   r   r%   r&   Zload_state_dictrD   load)r'   Znum_input_framer   Z	load_pathr*   r,   r-   r     s    
zEDVRFeatureExtractor.__init__c              
   C   s  |  \}}}}}| | |d|||}| |}| | |}| | |}| | |}	| | |	}	|||d||}|||d|d |d }|	||d|d |d }	|d d | j	d d d d d d f 
 |d d | j	d d d d d d f 
 |	d d | j	d d d d d d f 
 g}
g }t|D ]}|d d |d d d d d d f 
 |d d |d d d d d d f 
 |	d d |d d d d d d f 
 g}|| ||
 q@tj|dd}| |S )Nr/   r   r   r   r=   )r1   r&   rx   r2   ry   rz   r{   r|   r}   rs   clonerB   rq   r~   rD   rJ   r   )r'   r4   r5   r6   r7   r8   r9   Zfeat_l1Zfeat_l2Zfeat_l3Z
ref_feat_lZaligned_featrN   Z
nbr_feat_lr,   r,   r-   rS   0  s(    
L&lzEDVRFeatureExtractor.forwardr\   r,   r,   r*   r-   ra     s   ra   )rD   r   Ztorch.nnr   rH   Zbasicsr.utils.registryr   Z	arch_utilr   r   r   Z	edvr_archr	   r
   Zspynet_archr   registerModuler   r   r]   ra   r,   r,   r,   r-   <module>   s   Y 