3
Á3W^ª  ã               @   s^   d dl Z d dljjZd dl mZ d dlmZ d dlmZ G dd„ de	ƒZ
G dd„ dejƒZdS )	é    N)Únn)Ú	roi_align)Úbox_areac               @   s"   e Zd ZdZd
dd„Zdd„ Zd	S )ÚLevelMapperzûDetermine which FPN level each RoI in a set of RoIs should map to based
    on the heuristic in the FPN paper.

    Arguments:
        k_min (int)
        k_max (int)
        canonical_scale (int)
        canonical_level (int)
        eps (float)
    éà   é   ç�íµ ÷Æ°>c             C   s"   || _ || _|| _|| _|| _d S )N)Úk_minÚk_maxÚs0Úlvl0Úeps)Úselfr	   r
   Zcanonical_scaleZcanonical_levelr   © r   ú>/tmp/pip-build-7wfi2gtz/torchvision/torchvision/ops/poolers.pyÚ__init__   s
    zLevelMapper.__init__c             C   sb   t jt jdd„ |D ƒƒƒ}t j| jt j|| j | j ƒ ƒ}t j|| j	| j
d�}|jt jƒ| j	 S )zA
        Arguments:
            boxlists (list[BoxList])
        c             S   s   g | ]}t |ƒ‘qS r   )r   )Ú.0Zboxlistr   r   r   ú
<listcomp>#   s    z(LevelMapper.__call__.<locals>.<listcomp>)ÚminÚmax)ÚtorchÚsqrtÚcatÚfloorr   Úlog2r   r   Úclampr	   r
   ZtoZint64)r   ZboxlistsÚsZtarget_lvlsr   r   r   Ú__call__   s    "zLevelMapper.__call__N)r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   
   s   

r   c                   s@   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Z‡  Z	S )ÚMultiScaleRoIAligna_  
    Multi-scale RoIAlign pooling, which is useful for detection with or without FPN.

    It infers the scale of the pooling via the heuristics present in the FPN paper.

    Arguments:
        featmap_names (List[str]): the names of the feature maps that will be used
            for the pooling.
        output_size (List[Tuple[int, int]] or List[int]): output size for the pooled region
        sampling_ratio (int): sampling ratio for ROIAlign

    Examples::

        >>> m = torchvision.ops.MultiScaleRoIAlign(['feat1', 'feat3'], 3, 2)
        >>> i = OrderedDict()
        >>> i['feat1'] = torch.rand(1, 5, 64, 64)
        >>> i['feat2'] = torch.rand(1, 5, 32, 32)  # this feature won't be used in the pooling
        >>> i['feat3'] = torch.rand(1, 5, 16, 16)
        >>> # create some random bounding boxes
        >>> boxes = torch.rand(6, 4) * 256; boxes[:, 2:] += boxes[:, :2]
        >>> # original image size, before computing the feature maps
        >>> image_sizes = [(512, 512)]
        >>> output = m(i, [boxes], image_sizes)
        >>> print(output.shape)
        >>> torch.Size([6, 5, 3, 3])

    c                sF   t t| ƒjƒ  t|tƒr ||f}|| _|| _t|ƒ| _d | _	d | _
d S )N)Úsuperr"   r   Ú
isinstanceÚintÚfeatmap_namesÚsampling_ratioÚtupleÚoutput_sizeÚscalesÚ
map_levels)r   r&   r)   r'   )Ú	__class__r   r   r   H   s    

zMultiScaleRoIAlign.__init__c                sT   t j|dd�}|j|j ‰ ‰t j‡ ‡fdd„t|ƒD ƒdd�}t j||gdd�}|S )Nr   )Zdimc                s*   g | ]"\}}t jt|ƒd f|ˆˆ d�‘qS )é   )ÚdtypeÚdevice)r   ÚfullÚlen)r   ÚiÚb)r/   r.   r   r   r   W   s   z<MultiScaleRoIAlign.convert_to_roi_format.<locals>.<listcomp>r-   )r   r   r/   r.   Ú	enumerate)r   ÚboxesZconcat_boxesÚidsÚroisr   )r/   r.   r   Úconvert_to_roi_formatR   s    
z(MultiScaleRoIAlign.convert_to_roi_formatc       	      C   sv   |j dd … }g }xFt||ƒD ]8\}}t|ƒ| }dtj|ƒjƒ jƒ jƒ  }|j|ƒ qW |d |d ksnt	‚|d S )Né   r   r-   éþÿÿÿ)
ÚshapeÚzipÚfloatr   Útensorr   ÚroundÚitemÚappendÚAssertionError)	r   ZfeatureZoriginal_sizeÚsizeZpossible_scalesÚs1Ús2Zapprox_scaleÚscaler   r   r   Úinfer_scale_   s    zMultiScaleRoIAlign.infer_scalec                s€   t dd„ t|Ž D ƒƒ‰ ‡ ‡fdd„|D ƒ}tjtj|d tjd�ƒjƒ  }tjtj|d tjd�ƒjƒ  }|ˆ_t||ƒˆ_	d S )	Nc             s   s   | ]}t |ƒV  qd S )N)r   )r   r   r   r   r   ú	<genexpr>k   s    z2MultiScaleRoIAlign.setup_scales.<locals>.<genexpr>c                s   g | ]}ˆj |ˆ ƒ‘qS r   )rG   )r   Zfeat)Úoriginal_input_shaper   r   r   r   l   s    z3MultiScaleRoIAlign.setup_scales.<locals>.<listcomp>r   )r.   r-   éÿÿÿÿ)
r(   r<   r   r   r>   Zfloat32r@   r*   r   r+   )r   ÚfeaturesÚimage_shapesr*   Zlvl_minZlvl_maxr   )rI   r   r   Úsetup_scalesj   s      zMultiScaleRoIAlign.setup_scalesc                s  ‡ fdd„|j ƒ D ƒ}t|ƒ}ˆ j|ƒ}ˆ jdkr>ˆ j||ƒ |dkrft|d |ˆ jˆ jd ˆ jd�S ˆ j|ƒ}t|ƒ}|d j	d }|d j
|d j }	}
tj||fˆ j |	|
d�}xVtt|ˆ jƒƒD ]B\}\}}tj||kƒjdƒ}|| }t||ˆ j|ˆ jd�||< qÈW |S )aÊ  
        Arguments:
            x (OrderedDict[Tensor]): feature maps for each level. They are assumed to have
                all the same number of channels, but they can have different sizes.
            boxes (List[Tensor[N, 4]]): boxes to be used to perform the pooling operation, in
                (x1, y1, x2, y2) format and in the image reference size, not the feature map
                reference.
            image_shapes (List[Tuple[height, width]]): the sizes of each image before they
                have been fed to a CNN to obtain feature maps. This allows us to infer the
                scale factor for each one of the levels to be pooled.
        Returns:
            result (Tensor)
        c                s   g | ]\}}|ˆ j kr|‘qS r   )r&   )r   ÚkÚv)r   r   r   r   ‚   s    z.MultiScaleRoIAlign.forward.<locals>.<listcomp>Nr-   r   )r)   Zspatial_scaler'   )r.   r/   )Úitemsr1   r8   r*   rM   r   r)   r'   r+   r;   r.   r/   r   Úzerosr4   r<   ZnonzeroZsqueeze)r   Úxr5   rL   Z
num_levelsr7   ZlevelsZnum_roisZnum_channelsr.   r/   ÚresultÚlevelZper_level_featurerF   Zidx_in_levelZrois_per_levelr   )r   r   Úforwardt   s6    



zMultiScaleRoIAlign.forward)
r   r   r    r!   r   r8   rG   rM   rU   Ú__classcell__r   r   )r,   r   r"   +   s   

r"   )r   Ztorch.nn.functionalr   Z
functionalÚFZtorchvision.opsr   Ztorchvision.ops.boxesr   Úobjectr   ÚModuler"   r   r   r   r   Ú<module>   s   !