3
Á3W^bB  ã               @   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
 G dd„ dejƒZG d	d
„ d
ejƒZdd„ Zdd„ ZG dd„ de jjƒZdS )é    N)Ú
functional)Únn)Úboxesé   )Ú_utilsc                   sX   e Zd ZdZd‡ fdd	„	Zeddd„ƒZdd„ Zdd„ Zdd„ Z	dd„ Z
dd„ Z‡  ZS )ÚAnchorGeneratorae  
    Module that generates anchors for a set of feature maps and
    image sizes.

    The module support computing anchors at multiple sizes and aspect ratios
    per feature map.

    sizes and aspect_ratios should have the same number of elements, and it should
    correspond to the number of feature maps.

    sizes[i] and aspect_ratios[i] can have an arbitrary number of elements,
    and AnchorGenerator will output a set of sizes[i] * aspect_ratios[i] anchors
    per spatial location for feature map i.

    Arguments:
        sizes (Tuple[Tuple[int]]):
        aspect_ratios (Tuple[Tuple[float]]):
    é€   é   é   ç      à?ç      ð?ç       @c                s‚   t t| ƒjƒ  t|d ttfƒs2tdd„ |D ƒƒ}t|d ttfƒsR|ft|ƒ }t|ƒt|ƒksft‚|| _|| _	d | _
i | _d S )Nr   c             s   s   | ]}|fV  qd S )N© )Ú.0Úsr   r   úG/tmp/pip-build-7wfi2gtz/torchvision/torchvision/models/detection/rpn.pyú	<genexpr>(   s    z+AnchorGenerator.__init__.<locals>.<genexpr>)Úsuperr   Ú__init__Ú
isinstanceÚlistÚtupleÚlenÚAssertionErrorÚsizesÚaspect_ratiosÚcell_anchorsÚ_cache)Úselfr   r   )Ú	__class__r   r   r      s    zAnchorGenerator.__init__Úcpuc             C   s¨   t j| t j|d�} t j|t j|d�}t j|ƒ}d| }|d d …d f | d d d …f  jdƒ}|d d …d f | d d d …f  jdƒ}t j| | ||gdd�d }|jƒ S )N)ÚdtypeÚdevicer   )Údimé   éÿÿÿÿr%   )ÚtorchZ	as_tensorÚfloat32ÚsqrtÚviewÚstackÚround)Zscalesr   r"   Zh_ratiosZw_ratiosÚwsZhsÚbase_anchorsr   r   r   Úgenerate_anchors3   s    
&&z AnchorGenerator.generate_anchorsc                s8   ˆj d k	rˆj S ‡ ‡fdd„tˆjˆjƒD ƒ}|ˆ_ d S )Nc                s   g | ]\}}ˆj ||ˆ ƒ‘qS r   )r.   )r   r   r   )r"   r   r   r   ú
<listcomp>D   s   z4AnchorGenerator.set_cell_anchors.<locals>.<listcomp>)r   Úzipr   r   )r   r"   r   r   )r"   r   r   Úset_cell_anchors@   s
    
z AnchorGenerator.set_cell_anchorsc             C   s   dd„ t | j| jƒD ƒS )Nc             S   s    g | ]\}}t |ƒt |ƒ ‘qS r   )r   )r   r   Úar   r   r   r/   N   s    z<AnchorGenerator.num_anchors_per_location.<locals>.<listcomp>)r0   r   r   )r   r   r   r   Únum_anchors_per_locationM   s    z(AnchorGenerator.num_anchors_per_locationc             C   sÐ   g }xÆt ||| jƒD ]´\}}}|\}}|\}	}
|j}tjd|tj|d�|
 }tjd|tj|d�|	 }tj||ƒ\}}|jdƒ}|jdƒ}tj||||fdd�}|j	|j
dddƒ|j
dd	dƒ jd
dƒƒ qW |S )Nr   )r!   r"   r   )r#   é   r%   r%   r%   r%   r%   )r0   r   r"   r&   Úaranger'   ZmeshgridÚreshaper*   Úappendr)   )r   Ú
grid_sizesÚstridesÚanchorsÚsizeÚstrider-   Zgrid_heightZ
grid_widthZstride_heightZstride_widthr"   Zshifts_xZshifts_yZshift_yZshift_xZshiftsr   r   r   Úgrid_anchorsP   s&    

*zAnchorGenerator.grid_anchorsc             C   s>   t |ƒt |ƒ }|| jkr$| j| S | j||ƒ}|| j|< |S )N)r   r   r=   )r   r8   r9   Úkeyr:   r   r   r   Úcached_grid_anchorsi   s    


z#AnchorGenerator.cached_grid_anchorsc                s¬   t dd„ |D ƒƒ}|jjdd … ‰ t ‡ fdd„|D ƒƒ}| j|d jƒ | j||ƒ}g }x@t|jƒD ]2\}\}}	g }
x|D ]}|
j|ƒ qzW |j|
ƒ qdW dd„ |D ƒ}|S )	Nc             S   s   g | ]}|j dd… ‘qS )r$   Néþÿÿÿ)Úshape)r   Zfeature_mapr   r   r   r/   r   s    z+AnchorGenerator.forward.<locals>.<listcomp>r$   c             3   s.   | ]&}ˆ d  |d   ˆ d |d  fV  qdS )r   r   Nr   )r   Úg)Ú
image_sizer   r   r   t   s    z*AnchorGenerator.forward.<locals>.<genexpr>r   c             S   s   g | ]}t j|ƒ‘qS r   )r&   Úcat)r   Úanchors_per_imager   r   r   r/   }   s    r@   )	r   ZtensorsrA   r1   r"   r?   Ú	enumerateÚimage_sizesr7   )r   Z
image_listZfeature_mapsr8   r9   Zanchors_over_all_feature_mapsr:   ÚiZimage_heightZimage_widthZanchors_in_imageZanchors_per_feature_mapr   )rC   r   Úforwardq   s    
zAnchorGenerator.forward©r   r	   r
   ©r   r   r   )rJ   rK   )r    )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ústaticmethodr.   r1   r3   r=   r?   rI   Ú__classcell__r   r   )r   r   r      s    r   c                   s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )ÚRPNHeadzÙ
    Adds a simple RPN Head with classification and regression heads

    Arguments:
        in_channels (int): number of channels of the input feature
        num_anchors (int): number of anchors to be predicted
    c                sŒ   t t| ƒjƒ  tj||dddd�| _tj||ddd�| _tj||d ddd�| _x6| jƒ D ]*}t	jj
j|jdd� t	jj
j|jdƒ qZW d S )	Né   r   )Úkernel_sizer<   Úpadding)rT   r<   r4   g{®Gáz„?)Zstdr   )r   rR   r   r   ZConv2dÚconvÚ
cls_logitsÚ	bbox_predÚchildrenr&   ÚinitZnormal_ZweightZ	constant_Zbias)r   Zin_channelsÚnum_anchorsÚl)r   r   r   r   Š   s    zRPNHead.__init__c             C   sN   g }g }x<|D ]4}t j| j|ƒƒ}|j| j|ƒƒ |j| j|ƒƒ qW ||fS )N)ÚFZrelurV   r7   rW   rX   )r   ÚxZlogitsZbbox_regZfeatureÚtr   r   r   rI   ˜   s    
zRPNHead.forward)rL   rM   rN   rO   r   rI   rQ   r   r   )r   r   rR   �   s   rR   c             C   s6   | j |d|||ƒ} | jdddddƒ} | j|d|ƒ} | S )Nr   r   rS   r4   r$   r%   r%   )r)   Zpermuter6   )ZlayerÚNÚAÚCÚHÚWr   r   r   Úpermute_and_flatten¢   s    re   c             C   s´   g }g }xvt | |ƒD ]h\}}|j\}}}}	|jd }
|
d }|| }t||||||	ƒ}|j|ƒ t|||d||	ƒ}|j|ƒ qW tj|dd�jd|ƒ} tj|dd�jddƒ}| |fS )Nr   r4   )r#   r%   r%   )r0   rA   re   r7   r&   rD   r6   )Zbox_clsZbox_regressionZbox_cls_flattenedZbox_regression_flattenedZbox_cls_per_levelZbox_regression_per_levelr`   ZAxCrc   rd   ZAx4ra   rb   r   r   r   Úconcat_box_prediction_layers©   s"    

rf   c                   sb   e Zd ZdZ‡ fdd„Zedd„ ƒZedd„ ƒZdd	„ Zd
d„ Z	dd„ Z
dd„ Zddd„Z‡  ZS )ÚRegionProposalNetworkac  
    Implements Region Proposal Network (RPN).

    Arguments:
        anchor_generator (AnchorGenerator): module that generates the anchors for a set of feature
            maps.
        head (nn.Module): module that computes the objectness and regression deltas
        fg_iou_thresh (float): minimum IoU between the anchor and the GT box so that they can be
            considered as positive during training of the RPN.
        bg_iou_thresh (float): maximum IoU between the anchor and the GT box so that they can be
            considered as negative during training of the RPN.
        batch_size_per_image (int): number of anchors that are sampled during training of the RPN
            for computing the loss
        positive_fraction (float): proportion of positive anchors in a mini-batch during training
            of the RPN
        pre_nms_top_n (Dict[int]): number of proposals to keep before applying NMS. It should
            contain two fields: training and testing, to allow for different values depending
            on training or evaluation
        post_nms_top_n (Dict[int]): number of proposals to keep after applying NMS. It should
            contain two fields: training and testing, to allow for different values depending
            on training or evaluation
        nms_thresh (float): NMS threshold used for postprocessing the RPN proposals

    c
       
         sl   t t| ƒjƒ  || _|| _tjdd�| _tj	| _
tj||dd�| _tj||ƒ| _|| _|| _|	| _d| _d S )Nç      ð?)ÚweightsT)Zallow_low_quality_matchesgü©ñÒMbP?)rh   rh   rh   rh   )r   rg   r   Úanchor_generatorÚheadÚ	det_utilsZBoxCoderÚ	box_coderÚbox_opsZbox_iouÚbox_similarityZMatcherÚproposal_matcherZBalancedPositiveNegativeSamplerÚfg_bg_samplerÚ_pre_nms_top_nÚ_post_nms_top_nÚ
nms_threshÚmin_size)
r   rj   rk   Zfg_iou_threshZbg_iou_threshZbatch_size_per_imageZpositive_fractionÚpre_nms_top_nÚpost_nms_top_nrt   )r   r   r   r   â   s    

zRegionProposalNetwork.__init__c             C   s   | j r| jd S | jd S )NÚtrainingÚtesting)rx   rr   )r   r   r   r   rv     s    
z#RegionProposalNetwork.pre_nms_top_nc             C   s   | j r| jd S | jd S )Nrx   ry   )rx   rs   )r   r   r   r   rw     s    
z$RegionProposalNetwork.post_nms_top_nc             C   s¨   g }g }x–t ||ƒD ]ˆ\}}|d }| j||ƒ}| j|ƒ}	||	jdd� }
|	dk}|jtjd�}|	| jjk}d||< |	| jjk}d||< |j	|ƒ |j	|
ƒ qW ||fS )Nr   r   )Úmin)r!   r   r%   )
r0   ro   rp   ÚclampZtor&   r'   ZBELOW_LOW_THRESHOLDZBETWEEN_THRESHOLDSr7   )r   r:   ÚtargetsÚlabelsÚmatched_gt_boxesrE   Ztargets_per_imageZgt_boxesZmatch_quality_matrixZmatched_idxsZmatched_gt_boxes_per_imageZlabels_per_imageZ
bg_indicesZinds_to_discardr   r   r   Úassign_targets_to_anchors  s     

z/RegionProposalNetwork.assign_targets_to_anchorsc       
      C   sj   g }d}xR|j |dƒD ]B}|jd }t| j|ƒ}|j|dd�\}}	|j|	| ƒ ||7 }qW tj|dd�S )Nr   r   )r#   )ÚsplitrA   rz   rv   Ztopkr7   r&   rD   )
r   Ú
objectnessÚnum_anchors_per_levelÚrÚoffsetÚobr[   rv   Ú_Ú	top_n_idxr   r   r   Ú_get_top_n_idx)  s    
z$RegionProposalNetwork._get_top_n_idxc                sN  |j d }|j‰ |jƒ }|j|dƒ}‡ fdd„t|ƒD ƒ}tj|dƒ}|jddƒj|ƒ}| j||ƒ}tj	|ˆ d�d d …d f }|||f }|||f }|||f }g }	g }
xšt
||||ƒD ]ˆ\}}}}tj||ƒ}tj|| jƒ}|| || ||   }}}tj|||| jƒ}|d | j… }|| ||  }}|	j|ƒ |
j|ƒ qºW |	|
fS )Nr   r   c                s&   g | ]\}}t j|f|t jˆ d �‘qS ))r!   r"   )r&   ÚfullZint64)r   ÚidxÚn)r"   r   r   r/   <  s   z:RegionProposalNetwork.filter_proposals.<locals>.<listcomp>)r"   r%   r%   )rA   r"   Údetachr6   rF   r&   rD   Z	expand_asrˆ   r5   r0   rn   Zclip_boxes_to_imageZremove_small_boxesru   Zbatched_nmsrt   rw   r7   )r   Ú	proposalsr�   Zimage_shapesr‚   Ú
num_imagesZlevelsr‡   Z	batch_idxZfinal_boxesZfinal_scoresr   ÚscoresZlvlZ	img_shapeZkeepr   )r"   r   Úfilter_proposals4  s2    


z&RegionProposalNetwork.filter_proposalsc       
      C   s´   | j |ƒ\}}tjtj|dd�ƒjdƒ}tjtj|dd�ƒjdƒ}tj||gdd�}|jƒ }tj|dd�}tj|dd�}tj|| || dd�|jƒ  }tj	|| || ƒ}	|	|fS )a  
        Arguments:
            objectness (Tensor)
            pred_bbox_deltas (Tensor)
            labels (List[Tensor])
            regression_targets (List[Tensor])

        Returns:
            objectness_loss (Tensor)
            box_loss (Tensor
        r   )r#   r   Úsum)Z	reduction)
rq   r&   ZnonzerorD   ZsqueezeÚflattenr]   Zl1_lossÚnumelZ binary_cross_entropy_with_logits)
r   r�   Úpred_bbox_deltasr}   Úregression_targetsZsampled_pos_indsZsampled_neg_indsZsampled_indsZbox_lossZobjectness_lossr   r   r   Úcompute_lossX  s    
z"RegionProposalNetwork.compute_lossNc             C   sÎ   t |jƒ ƒ}| j|ƒ\}}| j||ƒ}t|ƒ}dd„ |D ƒ}t||ƒ\}}| jj|jƒ |ƒ}	|	j	|ddƒ}	| j
|	||j|ƒ\}
}i }| jrÆ| j||ƒ\}}| jj||ƒ}| j||||ƒ\}}||dœ}|
|fS )a2  
        Arguments:
            images (ImageList): images for which we want to compute the predictions
            features (List[Tensor]): features computed from the images that are
                used for computing the predictions. Each tensor in the list
                correspond to different feature levels
            targets (List[Dict[Tensor]): ground-truth boxes present in the image (optional).
                If provided, each element in the dict should contain a field `boxes`,
                with the locations of the ground-truth boxes.

        Returns:
            boxes (List[Tensor]): the predicted boxes from the RPN, one Tensor per
                image.
            losses (Dict[Tensor]): the losses for the model during training. During
                testing, it is an empty dict.
        c             S   s   g | ]}|d  j ƒ ‘qS )r   )r“   )r   Úor   r   r   r/   “  s    z1RegionProposalNetwork.forward.<locals>.<listcomp>r   r4   )Úloss_objectnessÚloss_rpn_box_regr%   )r   Úvaluesrk   rj   r   rf   rm   ÚdecoderŒ   r)   r�   rG   rx   r   Úencoder–   )r   ZimagesÚfeaturesr|   r�   r”   r:   rŽ   r‚   r�   r   r�   Zlossesr}   r~   r•   r˜   r™   r   r   r   rI   |  s$    zRegionProposalNetwork.forward)N)rL   rM   rN   rO   r   Úpropertyrv   rw   r   rˆ   r�   r–   rI   rQ   r   r   )r   r   rg   È   s   $$rg   )r&   Ztorch.nnr   r]   r   Ztorchvision.opsr   rn   Ú r   rl   ÚModuler   rR   re   rf   rg   r   r   r   r   Ú<module>   s   v!