U
    h                     @   s|   d dl mZ d dlZd dlm  mZ d dlmZ deeeee	 dddZ
deeeed	d
dZdee edddZdS )    )OptionalN)Tensorbilinear)imgabsolute_gridmodealign_cornersc           	      C   s|   | j dd \}}|jddgdd\}}d| |d  d }|dkrXd| |d  d }tj||gdd}tj| |||dS )z_Same as torch's grid_sample, with absolute pixel coordinates instead of normalized coordinates.N   dim   )r   r   )shapesplittorchcatFgrid_sample)	r   r   r   r   hwZxgridZygridZnormalized_grid r   X/var/www/html/venv/lib/python3.8/site-packages/torchvision/models/optical_flow/_utils.pyr      s    r   cpu)
batch_sizer   r   devicec                 C   s^   t |}t jt j||dt j||ddd}t j|d d d dd }|d  | dddS )N)r   Zij)Zindexingr   r   r   r
   )r   r   ZmeshgridZarangestackfloatrepeat)r   r   r   r   Zcoordsr   r   r   make_coords_grid   s    
$r      )up_maskfactorc           
   	   C   s   | j \}}}}|| ||  }}|dkrB|tj| ||fddd S ||dd||||}tj|dd}tj||  d	dd
||ddd||}	tj||	 dd}	|	dddddd		||||S )a,  Upsample flow by the input factor (default 8).

    If up_mask is None we just interpolate.
    If up_mask is specified, we upsample using a convex combination of its weights. See paper page 8 and appendix B.
    Note that in appendix B the picture assumes a downsample factor of 4 instead of 8.
    Nr   T)sizer   r   r
   	   r   r      )Zkernel_sizepaddingr         )
r   r   Zinterpolateviewr   ZsoftmaxZunfoldsumZpermuteZreshape)
Zflowr!   r"   r   Znum_channelsr   r   Znew_hZnew_wZupsampled_flowr   r   r   upsample_flow   s    &r+   )r   N)r   )Nr    )typingr   r   Ztorch.nn.functionalnnZ
functionalr   r   strboolr   intr   r+   r   r   r   r   <module>   s   