U
    ­Ê¹j„  ã                   @   s&  d dl mZmZmZmZ d dlZd dlm  mZ	 d dl
Z
d dlmZmZ d dlmZmZ ddlmZ dd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Zdd„ Zejjdd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Z ejj!dd „ ƒZ"d!d"„ Z#d#d$„ Z$d%d&„ Z%ejjd'd(„ ƒZ&d-d)d*„Z'G d+d,„ d,ej(ƒZ)dS ).é    )ÚDictÚListÚOptionalÚTupleN)ÚnnÚTensor)ÚboxesÚ	roi_aligné   )Ú_utilsc           
      C   s–   t j|dd�}t j|dd�}t | |¡}t  |dk¡d }|| }| j\}}| || d¡d d¡}tj|||f || ddd�}	|	| 	¡  }	||	fS )a  
    Computes the loss for Faster R-CNN.

    Args:
        class_logits (Tensor)
        box_regression (Tensor)
        labels (list[BoxList])
        regression_targets (Tensor)

    Returns:
        classification_loss (Tensor)
        box_loss (Tensor)
    r   ©Údiméÿÿÿÿé   gÇqÇq¼?Úsum)ÚbetaZ	reduction)
ÚtorchÚcatÚFÚcross_entropyÚwhereÚshapeÚreshapeÚsizeZsmooth_l1_lossÚnumel)
Úclass_logitsÚbox_regressionÚlabelsÚregression_targetsZclassification_lossZsampled_pos_inds_subsetZ
labels_posÚNÚnum_classesZbox_loss© r!   úe/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torchvision/models/detection/roi_heads.pyÚfastrcnn_loss   s    

ür#   c                 C   sd   |   ¡ }| jd }dd„ |D ƒ}t |¡}tj||jd�}|||f dd…df }|j|dd�}|S )a  
    From the results of the CNN, post process the masks
    by taking the mask corresponding to the class with max
    probability (which are of fixed size and directly output
    by the CNN) and return the masks in the mask field of the BoxList.

    Args:
        x (Tensor): the mask logits
        labels (list[BoxList]): bounding boxes that are used as
            reference, one for ech image

    Returns:
        results (list[BoxList]): one BoxList for each image, containing
            the extra field mask
    r   c                 S   s   g | ]}|j d  ‘qS ©r   ©r   )Ú.0Úlabelr!   r!   r"   Ú
<listcomp>I   s     z&maskrcnn_inference.<locals>.<listcomp>©ÚdeviceNr   )Zsigmoidr   r   r   Úaranger*   Úsplit)Úxr   Ú	mask_probZ	num_masksÚboxes_per_imageÚindexr!   r!   r"   Úmaskrcnn_inference4   s    

r1   c                 C   s\   |  |¡}tj|dd…df |gdd�}| dd…df   |¡} t| |||fdƒdd…df S )a%  
    Given segmentation masks and the bounding boxes corresponding
    to the location of the masks in the image, this function
    crops and resizes the masks in the position defined by the
    boxes. This prepares the masks for them to be fed to the
    loss computation as the targets.
    Nr
   r   g      ð?r   )Útor   r   r	   )Úgt_masksr   Úmatched_idxsÚMÚroisr!   r!   r"   Úproject_masks_on_boxesR   s    	
r7   c                    s–   | j d ‰ dd„ t||ƒD ƒ}‡ fdd„t|||ƒD ƒ}tj|dd�}tj|dd�}| ¡ dkrl|  ¡ d S t | tj|j d |j	d�|f |¡}|S )z¹
    Args:
        proposals (list[BoxList])
        mask_logits (Tensor)
        targets (list[BoxList])

    Return:
        mask_loss (Tensor): scalar tensor containing the loss
    r   c                 S   s   g | ]\}}|| ‘qS r!   r!   )r&   Zgt_labelZidxsr!   r!   r"   r(   n   s     z!maskrcnn_loss.<locals>.<listcomp>c                    s    g | ]\}}}t |||ˆ ƒ‘qS r!   )r7   )r&   ÚmÚpÚi©Údiscretization_sizer!   r"   r(   o   s    r   r   r)   )
r   Úzipr   r   r   r   r   Z binary_cross_entropy_with_logitsr+   r*   )Úmask_logitsÚ	proposalsr3   Ú	gt_labelsZmask_matched_idxsr   Zmask_targetsZ	mask_lossr!   r;   r"   Úmaskrcnn_lossa   s    


ÿ ÿrA   c                 C   s”  |d d …df }|d d …df }||d d …df |d d …df   }||d d …df |d d …df   }|d d …d f }|d d …d f }|d d …d f }|d d …d f }| d }| d }||d d …df d d …d f k}	||d d …df d d …d f k}
|| | }|  ¡  ¡ }|| | }|  ¡  ¡ }|d ||	< |d ||
< |dk|dk@ ||k @ ||k @ }| d dk}||@  ¡ }|| | }|| }||fS )Nr   r
   é   é   ).r   ).r
   ).rB   )ÚfloorÚlong)Ú	keypointsr6   Zheatmap_sizeÚoffset_xÚoffset_yZscale_xZscale_yr-   ÚyZx_boundary_indsZy_boundary_indsZ	valid_locZvisÚvalidZlin_indÚheatmapsr!   r!   r"   Úkeypoints_to_heatmap�   s0    $$   rL   c                 C   sÌ  t j|  d¡t jd�}|| }	|| }
tj|d d …d f t|ƒt|ƒfddd�d d …df }t j| d¡t jd�}| |d¡jdd	�}|| }|| | }t j	d
t j
d�|jt j
d� |	jt j
d� }t j	d
t j
d�|jt j
d� |
jt j
d� }||jt j
d� }||jt j
d� }t j|jt j
d�}t  |jt j
d�|jt j
d�|jt j
d�gd¡}|| | d }t  |¡}|jt jd�| }| d|jt jd�¡ d|jt jd�¡ d¡ d|jt jd�¡}||fS )Nr
   ©ÚdtypeÚbicubicF©r   ÚmodeZalign_cornersr   rB   r   r   ç      à?)r   Úscalar_tensorr   Úint64r   ÚinterpolateÚintr   ÚargmaxÚtensorÚfloat32r2   Úonesr   Ústackr+   Zindex_selectÚview)ÚmapsZmaps_iÚroi_map_widthÚroi_map_heightZwidths_iZ	heights_iZ
offset_x_iZ
offset_y_iÚnum_keypointsÚwidth_correctionÚheight_correctionÚroi_mapÚwÚposÚx_intÚy_intr-   rI   Zxy_preds_i_0Zxy_preds_i_1Zxy_preds_i_2Ú
xy_preds_iÚbaseÚindÚend_scores_ir!   r!   r"   Ú_onnx_heatmaps_to_keypoints¥   sZ       ÿ
þ ÿ ÿýú

 ÿþ ýÿrl   c	                 C   sä   t jddt|ƒft j| jd�}	t jdt|ƒft j| jd�}
tt| d¡ƒƒD ]Ž}t| | | || || || || || || ƒ\}}t  |	j	t jd�| 
d¡j	t jd�fd¡}	t  |
j	t jd�|j	t jd� 
d¡fd¡}
qL|	|
fS )Nr   rC   ©rN   r*   rM   )r   ÚzerosrV   rY   r*   Úranger   rl   r   r2   Ú	unsqueeze)r]   r6   Úwidths_ceilÚheights_ceilÚwidthsÚheightsrG   rH   r`   Úxy_predsÚ
end_scoresr:   rh   rk   r!   r!   r"   Ú _onnx_heatmaps_to_keypoints_loopØ   s&           ÿ*  ÿrw   c                 C   sf  |dd…df }|dd…df }|dd…df |dd…df  }|dd…df |dd…df  }|j dd�}|j dd�}| ¡ }| ¡ }| jd }t ¡ rÔt| |||||||tj|tjd�ƒ	\}	}
|	 	ddd¡|
fS tj
t|ƒd|ftj| jd�}	tj
t|ƒ|ftj| jd�}
tt|ƒƒD �]6}t||  ¡ ƒ}t||  ¡ ƒ}|| | }|| | }tj| | dd…df ||fd	d
d�dd…df }|jd }| |d¡jdd�}|| }tj|| |dd�}| ¡ d | }| ¡ d | }|||  |	|ddd…f< |||  |	|ddd…f< d|	|ddd…f< |tj||jd�||f |
|dd…f< �q|	 	ddd¡|
fS )z°Extract predicted keypoint locations from heatmaps. Output has shape
    (#rois, 4, #keypoints) with the 4 rows corresponding to (x, y, logit, prob)
    for each keypoint.
    Nr   r
   rB   rC   ©ÚminrM   rm   rO   FrP   r   r   rD   )Zrounding_moderR   r)   )ÚclampÚceilr   ÚtorchvisionÚ_is_tracingrw   r   rS   rT   Zpermutern   ÚlenrY   r*   ro   rV   Úitemr   rU   r   rW   ÚdivÚfloatr+   )r]   r6   rG   rH   rs   rt   rq   rr   r`   ru   rv   r:   r^   r_   ra   rb   rc   rd   re   rf   rg   r-   rI   r!   r!   r"   Úheatmaps_to_keypointsê   s`    
  
÷   ÿ
þ
*r‚   c                 C   s
  | j \}}}}||kr*td|› d|› �ƒ‚|}g }	g }
t|||ƒD ]B\}}}|| }t|||ƒ\}}|	 | d¡¡ |
 | d¡¡ qBtj|	dd�}tj|
dd�jtj	d�}
t 
|
¡d }
| ¡ dksÒt|
ƒdkrÞ|  ¡ d S |  || || ¡} t | |
 ||
 ¡}|S )Nz_keypoint_logits height and width (last two elements of shape) should be equal. Instead got H = z	 and W = r   r   r   rM   )r   Ú
ValueErrorr=   rL   Úappendr\   r   r   r2   Zuint8r   r   r~   r   r   r   )Úkeypoint_logitsr?   Úgt_keypointsZkeypoint_matched_idxsr   ÚKÚHÚWr<   rK   rJ   Zproposals_per_imageZgt_kp_in_imageZmidxÚkpZheatmaps_per_imageZvalid_per_imageZkeypoint_targetsZkeypoint_lossr!   r!   r"   Úkeypointrcnn_loss*  s*    ÿr‹   c           
      C   sb   g }g }dd„ |D ƒ}| j |dd�}t||ƒD ]*\}}t||ƒ\}}	| |¡ | |	¡ q.||fS )Nc                 S   s   g | ]}|  d ¡‘qS r$   )r   )r&   Úboxr!   r!   r"   r(   N  s     z*keypointrcnn_inference.<locals>.<listcomp>r   r   )r,   r=   r‚   r„   )
r-   r   Zkp_probsÚ	kp_scoresr/   Zx2ZxxZbbZkp_probÚscoresr!   r!   r"   Úkeypointrcnn_inferenceI  s    
r�   c                 C   sì   | d d …df | d d …df  d }| d d …df | d d …df  d }| d d …df | d d …df  d }| d d …df | d d …df  d }|j tjd�| }|j tjd�| }|| }|| }|| }|| }	t ||||	fd¡}
|
S )NrB   r   rR   rC   r
   rM   )r2   r   rY   r[   )r   ÚscaleÚw_halfÚh_halfÚx_cÚy_cZ
boxes_exp0Z
boxes_exp1Z
boxes_exp2Z
boxes_exp3Ú	boxes_expr!   r!   r"   Ú_onnx_expand_boxesY  s    $$$$r–   c                 C   s  t  ¡ rt| |ƒS | d d …df | d d …df  d }| d d …df | d d …df  d }| d d …df | d d …df  d }| d d …df | d d …df  d }||9 }||9 }t | ¡}|| |d d …df< || |d d …df< || |d d …df< || |d d …df< |S )NrB   r   rR   rC   r
   )r|   r}   r–   r   Z
zeros_like)r   r�   r‘   r’   r“   r”   r•   r!   r!   r"   Úexpand_boxesn  s    
$$$$
r—   c                 C   s,   t  | d|  ¡ t j¡t  | ¡ t j¡ S )NrB   )r   rX   r2   rY   )r5   Úpaddingr!   r!   r"   Úexpand_masks_tracing_scale‚  s    r™   c                 C   sN   | j d }tj ¡ r t||ƒ}nt|d|  ƒ| }t | |fd ¡}||fS )Nr   rB   r   )r   r   Z_CZ_get_tracing_stater™   r�   r   Úpad)Úmaskr˜   r5   r�   Zpadded_maskr!   r!   r"   Úexpand_masksˆ  s    

rœ   c                 C   s  d}t |d |d  | ƒ}t |d |d  | ƒ}t|dƒ}t|dƒ}|  d¡} tj| ||fddd�} | d d } tj||f| j| jd	�}t|d dƒ}t	|d d |ƒ}	t|d dƒ}
t	|d d |ƒ}| |
|d  ||d  …||d  |	|d  …f ||
|…||	…f< |S )
Nr
   rB   r   rC   )r
   r
   r   r   ÚbilinearFrP   rm   )
rV   ÚmaxÚexpandr   rU   r   rn   rN   r*   ry   )r›   rŒ   Úim_hÚim_wZ	TO_REMOVErd   ÚhÚim_maskÚx_0Úx_1Úy_0Úy_1r!   r!   r"   Úpaste_mask_in_image“  s    


Dr¨   c                 C   s  t jdt jd�}t jdt jd�}|d |d  | }|d |d  | }t  t  ||f¡¡}t  t  ||f¡¡}|  dd|  d¡|  d¡f¡} tj	| t
|ƒt
|ƒfddd�} | d d } t  t  |d  d¡|f¡¡}t  t  |d  d¡| | d¡f¡¡}	t  t  |d  d¡|f¡¡}
t  t  |d  d¡| | d¡f¡¡}| |
|d  ||d  …||d  |	|d  …f }t  |
| d¡¡}t  || | d¡¡}t  ||jt jd�|fd¡d|…d d …f }t  | d¡|¡}t  | d¡||	 ¡}t  |||fd¡d d …d |…f }|S )	Nr
   rM   rB   r   rC   r�   FrP   )r   rZ   rT   rn   rž   r   rŸ   r   r   rU   rV   rp   ry   r2   rY   )r›   rŒ   r    r¡   ÚoneÚzerord   r¢   r¤   r¥   r¦   r§   Zunpaded_im_maskZzeros_y0Zzeros_y1Zconcat_0Zzeros_x0Zzeros_x1r£   r!   r!   r"   Ú_onnx_paste_mask_in_image¬  s*    ((4,"r«   c                 C   sX   t  d||¡}t|  d¡ƒD ]6}t| | d || ||ƒ}| d¡}t  ||f¡}q|S ©Nr   )r   rn   ro   r   r«   rp   r   )Úmasksr   r    r¡   Z
res_appendr:   Zmask_resr!   r!   r"   Ú_onnx_paste_masks_in_image_loopÐ  s    
r®   c                    s¾   t | |d�\} }t||ƒjtjd�}|\‰ ‰t ¡ rft| |tjˆ tjd�tjˆtjd�ƒd d …d f S ‡ ‡fdd„t	| |ƒD ƒ}t
|ƒdkr¨tj|dd�d d …d f }n|  ddˆ ˆf¡}|S )N)r˜   rM   c                    s"   g | ]\}}t |d  |ˆ ˆƒ‘qS r$   )r¨   )r&   r8   Úb©r    r¡   r!   r"   r(   ä  s     z(paste_masks_in_image.<locals>.<listcomp>r   r   r
   )rœ   r—   r2   r   rT   r|   r}   r®   rS   r=   r~   r[   Z	new_empty)r­   r   Z	img_shaper˜   r�   ÚresÚretr!   r°   r"   Úpaste_masks_in_imageÚ  s"       ÿþr³   c                       sz   e Zd ZejejejdœZd‡ fdd„	Zdd„ Z	dd„ Z
d	d
„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zddd„Z‡  ZS )ÚRoIHeads)Ú	box_coderÚproposal_matcherÚfg_bg_samplerNc                    s–   t ƒ  ¡  tj| _tj||dd�| _t ||¡| _	|d kr>d}t 
|¡| _|| _|| _|| _|	| _|
| _|| _|| _|| _|| _|| _|| _|| _d S )NF)Zallow_low_quality_matches)ç      $@r¸   ç      @r¹   )ÚsuperÚ__init__Úbox_opsÚbox_iouZbox_similarityÚ	det_utilsÚMatcherr¶   ÚBalancedPositiveNegativeSamplerr·   ÚBoxCoderrµ   Úbox_roi_poolÚbox_headÚbox_predictorÚscore_threshÚ
nms_threshÚdetections_per_imgÚmask_roi_poolÚ	mask_headÚmask_predictorÚkeypoint_roi_poolÚkeypoint_headÚkeypoint_predictor)ÚselfrÂ   rÃ   rÄ   Zfg_iou_threshZbg_iou_threshZbatch_size_per_imageZpositive_fractionZbbox_reg_weightsrÅ   rÆ   rÇ   rÈ   rÉ   rÊ   rË   rÌ   rÍ   ©Ú	__class__r!   r"   r»   ó  s&    
zRoIHeads.__init__c                 C   s.   | j d krdS | jd krdS | jd kr*dS dS ©NFT)rÈ   rÉ   rÊ   ©rÎ   r!   r!   r"   Úhas_mask&  s    


zRoIHeads.has_maskc                 C   s.   | j d krdS | jd krdS | jd kr*dS dS rÑ   )rË   rÌ   rÍ   rÒ   r!   r!   r"   Úhas_keypoint/  s    


zRoIHeads.has_keypointc                 C   sä   g }g }t |||ƒD ]Æ\}}}| ¡ dkrf|j}	tj|jd ftj|	d�}
tj|jd ftj|	d�}n`t ||¡}|  	|¡}|j
dd�}
||
 }|jtjd�}|| j	jk}d||< || j	jk}d||< | |
¡ | |¡ q||fS )Nr   rm   rx   rM   r   )r=   r   r*   r   rn   r   rT   r¼   r½   r¶   rz   r2   ZBELOW_LOW_THRESHOLDZBETWEEN_THRESHOLDSr„   )rÎ   r?   Úgt_boxesr@   r4   r   Zproposals_in_imageÚgt_boxes_in_imageZgt_labels_in_imager*   Zclamped_matched_idxs_in_imageZlabels_in_imageZmatch_quality_matrixZmatched_idxs_in_imageZbg_indsZignore_indsr!   r!   r"   Úassign_targets_to_proposals8  s.    
  ÿ

z$RoIHeads.assign_targets_to_proposalsc           	      C   sN   |   |¡\}}g }tt||ƒƒD ](\}\}}t ||B ¡d }| |¡ q |S r¬   )r·   Ú	enumerater=   r   r   r„   )	rÎ   r   Zsampled_pos_indsZsampled_neg_indsÚsampled_indsZimg_idxZpos_inds_imgZneg_inds_imgÚimg_sampled_indsr!   r!   r"   Ú	subsample[  s    zRoIHeads.subsamplec                 C   s   dd„ t ||ƒD ƒ}|S )Nc                 S   s   g | ]\}}t  ||f¡‘qS r!   )r   r   )r&   ZproposalZgt_boxr!   r!   r"   r(   f  s     z-RoIHeads.add_gt_proposals.<locals>.<listcomp>)r=   )rÎ   r?   rÕ   r!   r!   r"   Úadd_gt_proposalsd  s    zRoIHeads.add_gt_proposalsc                 C   sj   |d krt dƒ‚tdd„ |D ƒƒs*t dƒ‚tdd„ |D ƒƒsDt dƒ‚|  ¡ rftdd„ |D ƒƒsft dƒ‚d S )	Nútargets should not be Nonec                 S   s   g | ]}d |k‘qS ©r   r!   ©r&   Útr!   r!   r"   r(   n  s     z*RoIHeads.check_targets.<locals>.<listcomp>z0Every element of targets should have a boxes keyc                 S   s   g | ]}d |k‘qS ©r   r!   rß   r!   r!   r"   r(   p  s     z1Every element of targets should have a labels keyc                 S   s   g | ]}d |k‘qS ©r­   r!   rß   r!   r!   r"   r(   s  s     z0Every element of targets should have a masks key)rƒ   ÚallrÓ   )rÎ   Útargetsr!   r!   r"   Úcheck_targetsj  s    zRoIHeads.check_targetsc                    s  |   |¡ |d krtdƒ‚|d j‰ |d j}‡ fdd„|D ƒ}dd„ |D ƒ}|  ||¡}|  |||¡\}}|  |¡}g }	t|ƒ}
t|
ƒD ]r}|| }|| | ||< || | ||< || | ||< || }| 	¡ dkrêt
jdˆ |d�}|	 |||  ¡ qŠ| j |	|¡}||||fS )NrÝ   r   c                    s   g | ]}|d    ˆ ¡‘qS rÞ   )r2   rß   rM   r!   r"   r(   ‚  s     z4RoIHeads.select_training_samples.<locals>.<listcomp>c                 S   s   g | ]}|d  ‘qS rá   r!   rß   r!   r!   r"   r(   ƒ  s     )r
   r   rm   )rå   rƒ   rN   r*   rÜ   r×   rÛ   r~   ro   r   r   rn   r„   rµ   Úencode)rÎ   r?   rä   r*   rÕ   r@   r4   r   rÙ   Zmatched_gt_boxesÚ
num_imagesÚimg_idrÚ   rÖ   r   r!   rM   r"   Úselect_training_samplesv  s.    



z RoIHeads.select_training_samplesc                 C   s¼  |j }|jd }dd„ |D ƒ}| j ||¡}t |d¡}	| |d¡}
|	 |d¡}g }g }g }t|
||ƒD �]F\}}}t 	||¡}t
j||d�}| dd¡ |¡}|d d …dd …f }|d d …dd …f }|d d …dd …f }| dd¡}| d¡}| d¡}t
 || jk¡d }|| || ||   }}}tj|dd	�}|| || ||   }}}t |||| j¡}|d | j… }|| || ||   }}}| |¡ | |¡ | |¡ qh|||fS )
Nr   c                 S   s   g | ]}|j d  ‘qS r$   r%   )r&   Zboxes_in_imager!   r!   r"   r(   §  s     z3RoIHeads.postprocess_detections.<locals>.<listcomp>r   r)   r
   r   g{®Gáz„?)Zmin_size)r*   r   rµ   Údecoder   Zsoftmaxr,   r=   r¼   Zclip_boxes_to_imager   r+   r\   Z	expand_asr   r   rÅ   Zremove_small_boxesZbatched_nmsrÆ   rÇ   r„   )rÎ   r   r   r?   Úimage_shapesr*   r    r/   Z
pred_boxesZpred_scoresZpred_boxes_listZpred_scores_listZ	all_boxesZ
all_scoresZ
all_labelsr   rŽ   Zimage_shaper   ZindsZkeepr!   r!   r"   Úpostprocess_detectionsœ  s>    




zRoIHeads.postprocess_detectionsc           ,      C   st  |dk	r”|D ]†}t jt jt jf}|d j|krBtd|d j› �ƒ‚|d jt jksftd|d j› �ƒ‚|  ¡ r|d jt jkstd|d j› �ƒ‚q| j	r°|  
||¡\}}}}	nd}d}	d}|  |||¡}
|  |
¡}
|  |
¡\}}g }i }| j	�r4|dk�rtdƒ‚|	dk�rtd	ƒ‚t||||	ƒ\}}||d
œ}nL|  ||||¡\}}}t|ƒ}t|ƒD ]$}| || || || dœ¡ �qZ|  ¡ �ròdd„ |D ƒ}| j	�r|dk�r²tdƒ‚t|ƒ}g }g }t|ƒD ]@}t  || dk¡d }| || | ¡ | || | ¡ �qÊnd}| jdk	�rB|  |||¡}|  |¡}|  |¡}ntdƒ‚i }| j	�r²|dk�st|dk�st|dk�r|tdƒ‚dd„ |D ƒ}dd„ |D ƒ}t|||||ƒ}d|i}n6dd„ |D ƒ}t||ƒ}t||ƒD ]\} }!| |!d< �qÔ| |¡ | jdk	�rl| jdk	�rl| j dk	�rldd„ |D ƒ}"| j	�ršt|ƒ}g }"g }|dk�rNtdƒ‚t|ƒD ]@}t  || dk¡d }|" || | ¡ | || | ¡ �qVnd}|  ||"|¡}#|  |#¡}#|   |#¡}$i }%| j	�r|dk�sà|dk�rètdƒ‚dd„ |D ƒ}&t!|$|"|&|ƒ}'d|'i}%nT|$dk�s"|"dk�r*tdƒ‚t"|$|"ƒ\}(})t|(|)|ƒD ]\}*}+}!|*|!d< |+|!d< �qD| |%¡ ||fS )z´
        Args:
            features (List[Tensor])
            proposals (List[Tensor[N, 4]])
            image_shapes (List[Tuple[H, W]])
            targets (List[Dict])
        Nr   z-target boxes must of float type, instead got r   z.target labels must of int64 type, instead got rF   z1target keypoints must of float type, instead got zlabels cannot be Nonez!regression_targets cannot be None)Úloss_classifierÚloss_box_reg)r   r   rŽ   c                 S   s   g | ]}|d  ‘qS rÞ   r!   ©r&   r9   r!   r!   r"   r(     s     z$RoIHeads.forward.<locals>.<listcomp>z/if in training, matched_idxs should not be Noner   z%Expected mask_roi_pool to be not NonezCtargets, pos_matched_idxs, mask_logits cannot be None when trainingc                 S   s   g | ]}|d  ‘qS râ   r!   rß   r!   r!   r"   r(   /  s     c                 S   s   g | ]}|d  ‘qS rá   r!   rß   r!   r!   r"   r(   0  s     Ú	loss_maskc                 S   s   g | ]}|d  ‘qS rá   r!   )r&   Úrr!   r!   r"   r(   4  s     r­   c                 S   s   g | ]}|d  ‘qS rÞ   r!   rï   r!   r!   r"   r(   B  s     z0if in trainning, matched_idxs should not be NonezJboth targets and pos_matched_idxs should not be None when in training modec                 S   s   g | ]}|d  ‘qS )rF   r!   rß   r!   r!   r"   r(   [  s     Úloss_keypointzXboth keypoint_logits and keypoint_proposals should not be None when not in training modeZkeypoints_scores)#r   r�   ÚdoubleZhalfrN   Ú	TypeErrorrT   rÔ   rY   Ztrainingré   rÂ   rÃ   rÄ   rƒ   r#   rì   r~   ro   r„   rÓ   r   rÈ   rÉ   rÊ   Ú	ExceptionrA   r1   r=   ÚupdaterË   rÌ   rÍ   r‹   r�   ),rÎ   Úfeaturesr?   rë   rä   rà   Zfloating_point_typesr4   r   r   Zbox_featuresr   r   ÚresultZlossesrí   rî   r   rŽ   rç   r:   Zmask_proposalsZpos_matched_idxsrè   re   Zmask_featuresr>   rð   r3   r@   Zrcnn_loss_maskZmasks_probsr.   rñ   Zkeypoint_proposalsZkeypoint_featuresr…   rò   r†   Zrcnn_loss_keypointZkeypoints_probsr�   Zkeypoint_probZkpsr!   r!   r"   Úforward×  sÜ    


ýÿ





ÿþý


   ÿ
ÿ
zRoIHeads.forward)NNNNNN)N)Ú__name__Ú
__module__Ú__qualname__r¾   rÁ   r¿   rÀ   Ú__annotations__r»   rÓ   rÔ   r×   rÛ   rÜ   rå   ré   rì   rù   Ú__classcell__r!   r!   rÏ   r"   r´   ì  s*   ý      ë3		#	&@ ûr´   )r
   )*Útypingr   r   r   r   r   Ztorch.nn.functionalr   Z
functionalr   r|   r   Ztorchvision.opsr   r¼   r	   Ú r   r¾   r#   r1   r7   rA   rL   rl   ZjitZ_script_if_tracingrw   r‚   r‹   r�   r–   r—   Zunusedr™   rœ   r¨   r«   r®   r³   ÚModuler´   r!   r!   r!   r"   Ú<module>   s8   ( $3
@
$
	
