3
Á3W^  ã               @   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 d dlm	Z	 ddl
mZ G d	d
„ d
eƒZddd„ZG dd„ dejƒZdS )é    N)Únn)ÚFunction)Úonce_differentiable)Ú_pair)Ú_lazy_importé   )Úconvert_boxes_to_roi_formatc               @   s(   e Zd Zedd„ ƒZeedd„ ƒƒZdS )Ú_RoIAlignFunctionc             C   sP   | j |ƒ t|ƒ| _|| _|| _|jƒ | _tƒ }|j||||d |d |ƒ}|S )Nr   r   )	Zsave_for_backwardr   Úoutput_sizeÚspatial_scaleÚsampling_ratioÚsizeÚinput_shaper   Zroi_align_forward)ÚctxÚinputZroir
   r   r   Ú_CÚoutput© r   ú@/tmp/pip-build-7wfi2gtz/torchvision/torchvision/ops/roi_align.pyÚforward   s    


z_RoIAlignFunction.forwardc             C   s`   | j \}| j}| j}| j}| j\}}}}	tƒ }
|
j||||d |d ||||	|ƒ
}|d d d d fS )Nr   r   )Zsaved_tensorsr
   r   r   r   r   Zroi_align_backward)r   Zgrad_outputÚroisr
   r   r   ÚbsÚchÚhÚwr   Z
grad_inputr   r   r   Úbackward   s    z_RoIAlignFunction.backwardN)Ú__name__Ú
__module__Ú__qualname__Ústaticmethodr   r   r   r   r   r   r   r	      s   r	   ç      ð?c             C   s*   |}t |tjƒst|ƒ}tj| ||||ƒS )aí  
    Performs Region of Interest (RoI) Align operator described in Mask R-CNN

    Arguments:
        input (Tensor[N, C, H, W]): input tensor
        boxes (Tensor[K, 5] or List[Tensor[L, 4]]): the box coordinates in (x1, y1, x2, y2)
            format where the regions will be taken from. If a single Tensor is passed,
            then the first column should contain the batch index. If a list of Tensors
            is passed, then each Tensor will correspond to the boxes for an element i
            in a batch
        output_size (int or Tuple[int, int]): the size of the output after the cropping
            is performed, as (height, width)
        spatial_scale (float): a scaling factor that maps the input coordinates to
            the box coordinates. Default: 1.0
        sampling_ratio (int): number of sampling points in the interpolation grid
            used to compute the output value of each pooled output bin. If > 0,
            then exactly sampling_ratio x sampling_ratio grid points are used. If
            <= 0, then an adaptive number of grid points are used (computed as
            ceil(roi_width / pooled_w), and likewise for height). Default: -1

    Returns:
        output (Tensor[K, C, output_size[0], output_size[1]])
    )Ú
isinstanceÚtorchZTensorr   r	   Úapply)r   Zboxesr
   r   r   r   r   r   r   Ú	roi_align*   s    r$   c                   s0   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Z‡  ZS )ÚRoIAlignz
    See roi_align
    c                s$   t t| ƒjƒ  || _|| _|| _d S )N)Úsuperr%   Ú__init__r
   r   r   )Úselfr
   r   r   )Ú	__class__r   r   r'   L   s    zRoIAlign.__init__c             C   s   t ||| j| j| jƒS )N)r$   r
   r   r   )r(   r   r   r   r   r   r   R   s    zRoIAlign.forwardc             C   sN   | j jd }|dt| jƒ 7 }|dt| jƒ 7 }|dt| jƒ 7 }|d7 }|S )Nú(zoutput_size=z, spatial_scale=z, sampling_ratio=ú))r)   r   Ústrr
   r   r   )r(   Ztmpstrr   r   r   Ú__repr__U   s    zRoIAlign.__repr__)r   r   r   Ú__doc__r'   r   r-   Ú__classcell__r   r   )r)   r   r%   H   s   r%   éÿÿÿÿ)r    r0   )r"   r   Ztorch.autogradr   Ztorch.autograd.functionr   Ztorch.nn.modules.utilsr   Ztorchvision.extensionr   Ú_utilsr   r	   r$   ÚModuler%   r   r   r   r   Ú<module>   s   
