3
Á3W^R  ã               @   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 )Ú_RoIPoolFunctionc             C   sN   t |ƒ| _|| _|jƒ | _tƒ }|j||||d |d ƒ\}}| j||ƒ |S )Nr   r   )r   Úoutput_sizeÚspatial_scaleÚsizeÚinput_shaper   Zroi_pool_forwardZsave_for_backward)ÚctxÚinputÚroisr
   r   Ú_CÚoutputÚargmax© r   ú?/tmp/pip-build-7wfi2gtz/torchvision/torchvision/ops/roi_pool.pyÚforward   s    

z_RoIPoolFunction.forwardc             C   sZ   | j \}}| j}| j}| j\}}}}	tƒ }
|
j|||||d |d ||||	ƒ
}|d d d fS )Nr   r   )Zsaved_tensorsr
   r   r   r   Zroi_pool_backward)r   Zgrad_outputr   r   r
   r   ÚbsÚchÚhÚwr   Z
grad_inputr   r   r   Úbackward   s    
z_RoIPoolFunction.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) Pool operator described in Fast 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

    Returns:
        output (Tensor[K, C, output_size[0], output_size[1]])
    )Ú
isinstanceÚtorchZTensorr   r	   Úapply)r   Zboxesr
   r   r   r   r   r   Úroi_pool(   s    r$   c                   s0   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Z‡  ZS )ÚRoIPoolz
    See roi_pool
    c                s   t t| ƒjƒ  || _|| _d S )N)Úsuperr%   Ú__init__r
   r   )Úselfr
   r   )Ú	__class__r   r   r'   E   s    zRoIPool.__init__c             C   s   t ||| j| jƒS )N)r$   r
   r   )r(   r   r   r   r   r   r   J   s    zRoIPool.forwardc             C   s<   | j jd }|dt| jƒ 7 }|dt| jƒ 7 }|d7 }|S )Nú(zoutput_size=z, spatial_scale=ú))r)   r   Ústrr
   r   )r(   Ztmpstrr   r   r   Ú__repr__M   s
    zRoIPool.__repr__)r   r   r   Ú__doc__r'   r   r-   Ú__classcell__r   r   )r)   r   r%   A   s   r%   )r    )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   
