
    ڧg                     l   d dl mZmZmZmZ d dlZd dlmc 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 Zd Zd	 Zd
 Zd Zd Zej        j        d             Zd Zd Zd Zd Zd Z ej        j!        d             Z"d Z#d Z$d Z%ej        j        d             Z&ddZ' G d dej(                  Z)dS )    )DictListOptionalTupleN)nnTensor)boxes	roi_align   )_utilsc                    t          j        |d          }t          j        |d          }t          j        | |          }t          j        |dk              d         }||         }| j        \  }}|                    ||                    d          dz  d          }t          j        |||f         ||         dd          }	|	|	                                z  }	||	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)beta	reduction)
torchcatFcross_entropywhereshapereshapesizesmooth_l1_lossnumel)
class_logitsbox_regressionlabelsregression_targetsclassification_losssampled_pos_inds_subset
labels_posNnum_classesbox_losss
             b/var/www/html/ai-engine/env/lib/python3.11/site-packages/torchvision/models/detection/roi_heads.pyfastrcnn_lossr*      s      Yv1%%%F#51===/,??
 $k&1*55a8/0J!'NA{#++A~/B/B2/F/F!/KQOON.
:;23	  H &,,..(H((    c                    |                                  }| j        d         }d |D             }t          j        |          }t          j        ||j                  }|||f         dddf         }|                    |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                 (    g | ]}|j         d          S r   r   ).0labels     r)   
<listcomp>z&maskrcnn_inference.<locals>.<listcomp>I   s    :::%u{1~:::r+   deviceNr   )sigmoidr   r   r   aranger4   split)xr!   	mask_prob	num_masksboxes_per_imageindexs         r)   maskrcnn_inferencer=   4   s    " 		I 
I::6:::OYvFL6=999E%-(D1IQ77Ir+   c                     |                     |          }t          j        |dddf         |g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roiss        r)   project_masks_on_boxesrD   R   s      ??5))L9l111d7+U3;;;D4 ##D))HXtaVS11!!!Q$77r+   c                    | j         d         d t          ||          D             }fdt          |||          D             }t          j        |d          }t          j        |d          }|                                dk    r|                                 dz  S t          j        | t          j        |j         d         |j	                  |f         |          }|S )z
    Args:
        proposals (list[BoxList])
        mask_logits (Tensor)
        targets (list[BoxList])

    Return:
        mask_loss (Tensor): scalar tensor containing the loss
    r   c                 $    g | ]\  }}||         S  rG   )r0   gt_labelidxss      r)   r2   z!maskrcnn_loss.<locals>.<listcomp>n   s     UUU4htnUUUr+   c                 <    g | ]\  }}}t          |||          S rG   )rD   )r0   mpidiscretization_sizes       r)   r2   z!maskrcnn_loss.<locals>.<listcomp>o   s<       AHAqq!Q(;<<  r+   r   r   r3   )
r   zipr   r   r   r   r    binary_cross_entropy_with_logitsr6   r4   )	mask_logits	proposalsr@   	gt_labelsmask_matched_idxsr!   mask_targets	mask_lossrN   s	           @r)   maskrcnn_lossrW   a   s    &+B/UU3yBS3T3TUUUF   LOPXZcevLwLw  L Yv1%%%F9\q111L q    1$$2ELaGGGOPR^ I r+   c                     |d d df         }|d d df         }||d d df         |d d df         z
  z  }||d d df         |d d df         z
  z  }|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    }
||z
  |z  }|                                                                 }||z
  |z  }|                                                                 }|dz
  ||	<   |dz
  ||
<   |dk    |dk    z  ||k     z  ||k     z  }| d         dk    }||z                                  }||z  |z   }||z  }||fS )Nr   r         ).r   ).r   ).rY   )floorlong)	keypointsrC   heatmap_sizeoffset_xoffset_yscale_xscale_yr8   yx_boundary_indsy_boundary_inds	valid_locvisvalidlin_indheatmapss                   r)   keypoints_to_heatmaprk      s   AAAqDzHAAAqDzHd111a4j41:56Gd111a4j41:56G4 H4 HaaagGaaagG&A&A41:aaag..O41:aaag..O	
X A			A	
X A			A%)Ao%)AoaAF#q<'78A<LMI
F
a
C_""$$E,"GHU?r+   c                    t          j        |                     d          t           j                  }||z  }	||z  }
t	          j        |d d d f         t          |          t          |          fdd          d d df         }t          j        |                    d          t           j                  }|                    |d                              d	          }||z  }||z
  |z  }t          j	        d
t           j
                  |                    t           j
                  z   |	                    t           j
                  z  }t          j	        d
t           j
                  |                    t           j
                  z   |
                    t           j
                  z  }||                    t           j
                  z   }||                    t           j
                  z   }t          j        |j        t           j
                  }t          j        |                    t           j
                  |                    t           j
                  |                    t           j
                  gd          }||z  |z   dz   }t          j        |          }|                    t           j                  |z  }|                    d|                    t           j                                                d|                    t           j                                                d                              d|                    t           j                            }||fS )Nr   dtypebicubicFr   modealign_cornersr   rY   r   r         ?)r   scalar_tensorr   int64r   interpolateintr   argmaxtensorfloat32r?   onesr   stackr6   index_selectview)mapsmaps_iroi_map_widthroi_map_heightwidths_i	heights_i
offset_x_i
offset_y_inum_keypointswidth_correctionheight_correctionroi_mapwposx_inty_intr8   rc   xy_preds_i_0xy_preds_i_1xy_preds_i_2
xy_preds_ibaseindend_scores_is                            r)   _onnx_heatmaps_to_keypointsr      s    '		!EKHHHM-/!N2mqqq$ws>22C4F4FGigl  aadG 	GLLOO5;???A
//-
,
,
3
3
3
:
:C!GE5[QE	c	/	/	/%(((2O2O	OScSfSfm Tg T T 	A 
c	/	/	/%(((2O2O	OSdSgSgm Th T T 	A z}}5=}999Lz}}5=}999L:l0FFFLOO%-O00OO%-O00OO%-O00	

 	
 J =(=81<D
,}
%
%C
&&u{&
#
#d
*CQu{ ; ;<<	a44	5	5	b	aek22	3	3	  |##r+   c	                 f   t          j        ddt          |          ft           j        | j                  }	t          j        dt          |          ft           j        | j                  }
t          t          |                    d                              D ]}t          | | |         ||         ||         ||         ||         ||         ||                   \  }}t          j        |		                    t           j                  |
                    d          	                    t           j                  fd          }	t          j        |
	                    t           j                  |	                    t           j                  
                    d          fd          }
|	|
fS )Nr   rZ   rn   r4   rm   )r   zerosrw   rz   r4   ranger   r   r   r?   	unsqueeze)r   rC   widths_ceilheights_ceilwidthsheightsr_   r`   r   xy_preds
end_scoresrM   r   r   s                 r)    _onnx_heatmaps_to_keypoints_loopr      so    {Aq#m"4"45U]SWS^___Ha]!3!34EMRVR]^^^J3tyy||$$%% 
 
#>$q';q><?F1IwqzS[\]S^`hij`k$
 $
 
L 9hkkk>>
@T@TUV@W@W@Z@Zafan@Z@o@oprsttY]]]//u}1U1U1_1_`a1b1bcef
 


 Zr+   c                 d   |dddf         }|dddf         }|dddf         |dddf         z
  }|dddf         |dddf         z
  }|                     d          }|                     d          }|                                }|                                }| j        d         }t          j                    rQt          | |||||||t          j        |t          j                  	  	        \  }	}
|		                    ddd          |
fS t          j
        t          |          d|ft          j        | j                  }	t          j
        t          |          |ft          j        | j                  }
t          t          |                    D ]}t          ||                                                   }t          ||                                                   }||         |z  }||         |z  }t#          j        | |         dddf         ||fd	d
          dddf         }|j        d         }|                    |d                              d          }||z  }t          j        ||z
  |d          }|                                dz   |z  }|                                dz   |z  }|||         z   |	|dddf<   |||         z   |	|dddf<   d|	|dddf<   |t          j        ||j                  ||f         |
|ddf<   |		                    ddd          |
fS )zExtract 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   rY   rZ   minrm   r   ro   Frp   r   r   r[   )rounding_moders   r3   )clampceilr   torchvision_is_tracingr   r   rt   ru   permuter   lenrz   r4   r   rw   itemr   rv   r   rx   divfloatr6   )r   rC   r_   r`   r   r   r   r   r   r   r   rM   r   r   r   r   r   r   r   r   r   r8   rc   s                          r)   heatmaps_to_keypointsr      sW    AAAqDzHAAAqDzH!!!Q$Z$qqq!t*$F111a4j41:%G\\a\  Fmmm""G++--K<<>>LJqMM   5?U[AAA
 
 
 
* 1a((*44{CIIq-8VZVabbbHc$ii7u}UYU`aaaJ3t99 e eKN//1122\!_113344!!9}4#AJ7-GAAAtGNM#Bbg
 
 

!!Q$ M!oomR0077A7>>a	#+q@@@ [[]]S $44[[]]S $55OAqqqOAqqqAqqq"5<gn#U#U#UW\^c#cd
1aaa4Aq!$$j00r+   c                 .   | j         \  }}}}||k    rt          d| d|           |}g }	g }
t          |||          D ]r\  }}}||         }t          |||          \  }}|	                    |                    d                     |
                    |                    d                     st          j        |	d          }t          j        |
d                              t          j	                  }
t          j
        |
          d         }
|                                dk    st          |
          dk    r|                                 dz  S |                     ||z  ||z            } t          j        | |
         ||
                   }|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   
ValueErrorrO   rk   appendr~   r   r   r?   uint8r   r   r   r   r   r   )keypoint_logitsrR   gt_keypointskeypoint_matched_idxsr&   KHWrN   rj   rh   proposals_per_imagegt_kp_in_imagemidxkpheatmaps_per_imagevalid_per_imagekeypoint_targetskeypoint_losss                      r)   keypointrcnn_lossr   *  s    &JAq!QAvv}no}}z{}}
 
 	
 HE58LRg5h5h / /1^TD!.B2GZ\o.p.p+O*//33444_))"--....yq111Ie###&&U[&99EKq!E 1$$E

a""$$q((%**1q5!a%88OOOE$:<LU<STTMr+   c                     g }g }d |D             }|                      |d          }t          ||          D ]B\  }}t          ||          \  }}	|                    |           |                    |	           C||fS )Nc                 8    g | ]}|                     d           S r.   )r   )r0   boxs     r)   r2   z*keypointrcnn_inference.<locals>.<listcomp>N  s"    444ssxx{{444r+   r   r   )r7   rO   r   r   )
r8   r	   kp_probs	kp_scoresr;   x2xxbbkp_probscoress
             r)   keypointrcnn_inferencer   I  s    HI44e444O	
a	(	(Bb%.. ! !B/B77       Yr+   c                    | d d df         | d d df         z
  dz  }| d d df         | d d df         z
  dz  }| d d df         | d d df         z   dz  }| d d df         | d d df         z   dz  }|                     t          j                  |z  }|                     t          j                  |z  }||z
  }||z
  }||z   }||z   }	t          j        ||||	fd          }
|
S )NrY   r   rs   rZ   r   rm   )r?   r   rz   r|   )r	   scalew_halfh_halfx_cy_c
boxes_exp0
boxes_exp1
boxes_exp2
boxes_exp3	boxes_exps              r)   _onnx_expand_boxesr   Y  s   AAAqDkE!!!Q$K'3.FAAAqDkE!!!Q$K'3.FA;qqq!t$
+CA;qqq!t$
+CYYU]Y++e3FYYU]Y++e3FvJvJvJvJZZLaPPIr+   c                    t          j                    rt          | |          S | d d df         | d d df         z
  dz  }| d d df         | d d df         z
  dz  }| d d df         | d d df         z   dz  }| d d df         | d d df         z   dz  }||z  }||z  }t          j        |           }||z
  |d d df<   ||z   |d d df<   ||z
  |d d df<   ||z   |d d df<   |S )NrY   r   rs   rZ   r   )r   r   r   r   
zeros_like)r	   r   r   r   r   r   r   s          r)   expand_boxesr   n  s;      0!%///AAAqDkE!!!Q$K'3.FAAAqDkE!!!Q$K'3.FA;qqq!t$
+CA;qqq!t$
+C
eOF
eOF ''IFlIaaadOFlIaaadOFlIaaadOFlIaaadOr+   c                     t          j        | d|z  z                                 t           j                  t          j        |                               t           j                  z  S )NrY   )r   ry   r?   rz   )rB   paddings     r)   expand_masks_tracing_scaler     sK     <AK((++EM::U\!__=O=OPUP]=^=^^^r+   c                     | j         d         }t          j                                        rt	          ||          }nt          |d|z  z             |z  }t          j        | |fdz            }||fS )Nr   rY   r   )r   r   _C_get_tracing_stater   r   r   pad)maskr   rB   r   padded_masks        r)   expand_masksr     sp    
2Ax""$$ +*1g66a!g+o&&*%wj1n--Kr+   c                    d}t          |d         |d         z
  |z             }t          |d         |d         z
  |z             }t          |d          }t          |d          }|                     d          } t          j        | ||fdd          } | d         d         } t          j        ||f| j        | j        	          }t          |d         d          }t          |d         dz   |          }	t          |d         d          }
t          |d         dz   |          }| |
|d         z
  ||d         z
  ||d         z
  |	|d         z
  f         ||
|||	f<   |S )
Nr   rY   r   rZ   )r   r   r   r   bilinearFrp   r   )
rw   maxexpandr   rv   r   r   rn   r4   r   )r   r   im_him_w	TO_REMOVEr   him_maskx_0x_1y_0y_1s               r)   paste_mask_in_imager     s_   ICFSVOi'((ACFSVOi'((AAq		AAq		A ;;~&&D =QF5QQQD71:Dk4,djMMMG
c!fa..C
c!fqj$

C
c!fa..C
c!fqj$

C $cCFlsSV|%DsSQRV|X[^abc^dXdFe%e fGCGSWNr+   c                 T   t          j        dt           j                  }t          j        dt           j                  }|d         |d         z
  |z   }|d         |d         z
  |z   }t          j        t          j        ||f                    }t          j        t          j        ||f                    }|                     dd|                     d          |                     d          f          } t          j	        | t          |          t          |          fdd          } | d         d         } t          j        t          j        |d                             d          |f                    }t          j        t          j        |d                             d          |z   |                    d          f                    }	t          j        t          j        |d                             d          |f                    }
t          j        t          j        |d                             d          |z   |                    d          f                    }| |
|d         z
  ||d         z
  ||d         z
  |	|d         z
  f         }t          j        |
|                    d                    }t          j        ||z
  |                    d                    }t          j        ||                    t           j                  |fd          d|d d f         }t          j        |                    d          |          }t          j        |                    d          ||	z
            }t          j        |||fd          d d d |f         }|S )	Nr   rm   rY   r   rZ   r   Frp   )r   r{   ru   r   r   r   r   r   r   rv   rw   r   r   r?   rz   )r   r   r   r   onezeror   r   r   r   r   r   unpaded_im_maskzeros_y0zeros_y1concat_0zeros_x0zeros_x1r   s                      r)   _onnx_paste_mask_in_imager     s   
*Qek
*
*
*C;q,,,DAQ#AAQ#A	%)QH%%&&A	%)QH%%&&A ;;1diillDIIaLL9::D =SVVSVV$4:UZ[[[D71:D
)EIs1v//22D9::
;
;C
)EIs1v//22S8$..:K:KLMM
N
NC
)EIs1v//22D9::
;
;C
)EIs1v//22S8$..:K:KLMM
N
NCC#a&LS3q6\:S3q6\cTWXYTZl<[[\O
 {3 4 4Q 7 788H{4#:';';A'>'>??Hy(O$6$6U]$6$K$KXVXYZZ[\]a[acdcdcd[deH{8==++S11H{8==++TCZ88Hi8X6::111ete8DGNr+   c                    t          j        d||          }t          |                     d                    D ]Q}t	          | |         d         ||         ||          }|                    d          }t          j        ||f          }R|S Nr   )r   r   r   r   r   r   r   )masksr	   r   r   
res_appendrM   mask_ress          r)   _onnx_paste_masks_in_image_loopr    s    Qd++J5::a==!! 7 7,U1Xa[%(D$OO%%a((Y
H566

r+   c           	      B   t          | |          \  } }t          ||                              t          j                  }|\  t          j                    rXt          | |t          j        t          j                  t          j        t          j                            d d d f         S fdt          | |          D             }t          |          dk    r!t          j        |d          d d d f         }n|                     ddf          }|S )N)r   rm   c                 F    g | ]\  }}t          |d          |          S r.   )r   )r0   rK   br   r   s      r)   r2   z(paste_masks_in_image.<locals>.<listcomp>  s0    
R
R
R1qtQd33
R
R
Rr+   r   r   r   )r   r   r?   r   ru   r   r   r  rt   rO   r   r|   	new_empty)	r  r	   	img_shaper   r   resretr   r   s	          @@r)   paste_masks_in_imager    s"   w777LE5&&)))<<EJD$   .5%-d%+FFFH[\`hmhsHtHtHt
 

!!T' 	 S
R
R
R
RE5@Q@Q
R
R
RC
3xx!||k#1%%%aaag.ooq!T4011Jr+   c                        e Zd Zej        ej        ej        dZ	 	 	 	 	 	 d fd	Zd Z	d Z
d Zd Zd Zd	 Zd
 Zd Z	 ddZ xZS )RoIHeads)	box_coderproposal_matcherfg_bg_samplerNc                    t                                                       t          j        | _        t          j        ||d          | _        t          j        ||          | _	        |d}t          j
        |          | _        || _        || _        || _        |	| _        |
| _        || _        || _        || _        || _        || _        || _        || _        d S )NF)allow_low_quality_matches)      $@r        @r  )super__init__box_opsbox_iou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"  fg_iou_threshbg_iou_threshbatch_size_per_imagepositive_fractionbbox_reg_weightsr#  r$  r%  r&  r'  r(  r)  r*  r+  	__class__s                     r)   r  zRoIHeads.__init__  s    . 	%o ) 1-jo p p p&FG[]noo#5"+,<==( *($"4*",!2*"4r+   c                 <    | j         dS | j        dS | j        dS dS NFT)r&  r'  r(  r,  s    r)   has_maskzRoIHeads.has_mask&  s0    %5>!5&5tr+   c                 <    | j         dS | j        dS | j        dS dS r4  )r)  r*  r+  r5  s    r)   has_keypointzRoIHeads.has_keypoint/  s1    !)5%5"*5tr+   c                    g }g }t          |||          D ]=\  }}}|                                dk    rb|j        }	t          j        |j        d         ft          j        |	          }
t          j        |j        d         ft          j        |	          }nt          j        ||          }| 	                    |          }|
                    d          }
||
         }|                    t          j                  }|| j	        j        k    }d||<   || j	        j        k    }d||<   |                    |
           |                    |           ?||fS )Nr   r   r   rm   r   )rO   r   r4   r   r   r   ru   r  r  r  r   r?   BELOW_LOW_THRESHOLDBETWEEN_THRESHOLDSr   )r,  rR   gt_boxesrS   rA   r!   proposals_in_imagegt_boxes_in_imagegt_labels_in_imager4   clamped_matched_idxs_in_imagelabels_in_imagematch_quality_matrixmatched_idxs_in_imagebg_indsignore_indss                   r)   assign_targets_to_proposalsz$RoIHeads.assign_targets_to_proposals8  s{   ILYX`bkIlIl 	+ 	+E 13E &&((A--+205'-a02%+f1 1 1- #(+/A/G/J.LTYT_hn"o"o"o (/7HJ\']']$(,(=(=>R(S(S%0E0K0KPQ0K0R0R-"45R"S"1"4"45;"4"G"G 043H3\\+,( 4t7L7__/1, =>>>MM/****V##r+   c                     |                      |          \  }}g }t          t          ||                    D ]:\  }\  }}t          j        ||z            d         }|                    |           ;|S r  )r  	enumeraterO   r   r   r   )	r,  r!   sampled_pos_indssampled_neg_indssampled_indsimg_idxpos_inds_imgneg_inds_imgimg_sampled_indss	            r)   	subsamplezRoIHeads.subsample[  s    -1-?-?-G-G**5>sCSUe?f?f5g5g 	2 	21G1lL${<,+FGGJ 01111r+   c                 :    d t          ||          D             }|S )Nc                 @    g | ]\  }}t          j        ||f          S rG   )r   r   )r0   proposalgt_boxs      r)   r2   z-RoIHeads.add_gt_proposals.<locals>.<listcomp>f  s,    ddd7GxUY&122dddr+   )rO   )r,  rR   r<  s      r)   add_gt_proposalszRoIHeads.add_gt_proposalsd  s&    dd3yZbKcKcddd	r+   c                 D   |t          d          t          d |D                       st          d          t          d |D                       st          d          |                                 r(t          d |D                       st          d          d S d S )Ntargets should not be Nonec                     g | ]}d |v S r	   rG   r0   ts     r)   r2   z*RoIHeads.check_targets.<locals>.<listcomp>n  s    222QGqL222r+   z0Every element of targets should have a boxes keyc                     g | ]}d |v S r!   rG   rZ  s     r)   r2   z*RoIHeads.check_targets.<locals>.<listcomp>p  s    333aHM333r+   z1Every element of targets should have a labels keyc                     g | ]}d |v S r  rG   rZ  s     r)   r2   z*RoIHeads.check_targets.<locals>.<listcomp>s  s    6661666r+   z0Every element of targets should have a masks key)r   allr6  )r,  targetss     r)   check_targetszRoIHeads.check_targetsj  s    ?9:::22'22233 	QOPPP33733344 	RPQQQ==?? 	U66g66677 U !STTT	U 	UU Ur+   c                    |                      |           |t          d          |d         j        |d         j        }fd|D             }d |D             }|                     ||          }|                     |||          \  }}|                     |          }g }	t          |          }
t          |
          D ]}||         }||         |         ||<   ||         |         ||<   ||         |         ||<   ||         }|	                                dk    rt          j        d|          }|	                    |||                             | j                            |	|          }||||fS )NrW  r   c                 F    g | ]}|d                                         S rY  )r?   )r0   r[  rn   s     r)   r2   z4RoIHeads.select_training_samples.<locals>.<listcomp>  s)    :::QAgJMM%((:::r+   c                     g | ]
}|d          S r]  rG   rZ  s     r)   r2   z4RoIHeads.select_training_samples.<locals>.<listcomp>  s    222QQx[222r+   )r   r   r   )rb  r   rn   r4   rU  rF  rP  r   r   r   r   r   r   r  encode)r,  rR   ra  r4   r<  rS   rA   r!   rK  matched_gt_boxes
num_imagesimg_idrO  r>  r"   rn   s                  @r)   select_training_samplesz RoIHeads.select_training_samplesv  s    	7###?9:::!"1$::::':::22'222	 )))X>>	  $??	8U^__f~~f--^^
J'' 		M 		MF+F3 )& 12B CIf#F^,<=F6N#/#78H#IL  ( 0 &&((A--$)KeF$S$S$S!##$5l66J$KLLLL!^223CYOO,0BBBr+   c                    |j         }|j        d         }d |D             }| j                            ||          }t	          j        |d          }	|                    |d          }
|	                    |d          }g }g }g }t          |
||          D ]\  }}}t          j	        ||          }t          j        ||          }|                    dd                              |          }|d d dd f         }|d d dd f         }|d d dd f         }|                    dd          }|                    d          }|                    d          }t          j        || j        k              d         }||         ||         ||         }}}t          j        |d          }||         ||         ||         }}}t          j        |||| j                  }|d | j                 }||         ||         ||         }}}|                    |           |                    |           |                    |           |||fS )	Nr   c                 (    g | ]}|j         d          S r.   r/   )r0   boxes_in_images     r)   r2   z3RoIHeads.postprocess_detections.<locals>.<listcomp>  s     SSS~>/2SSSr+   r   r3   r   r   g{Gz?)min_size)r4   r   r  decoder   softmaxr7   rO   r  clip_boxes_to_imager   r6   r~   	expand_asr   r   r#  remove_small_boxesbatched_nmsr$  r%  r   )r,  r   r    rR   image_shapesr4   r'   r;   
pred_boxespred_scorespred_boxes_listpred_scores_list	all_boxes
all_scores
all_labelsr	   r   image_shaper!   indskeeps                        r)   postprocess_detectionszRoIHeads.postprocess_detections  sl    $"(,SSSSS^**>9EE
ib11$**?A>>&,,_a@@	

*-o?OQ]*^*^ !	& !	&&E6;/{CCE \+f===F[[B''11&99F !!!QRR%LEAAAqrrE]FAAAqrrE]F MM"a((E^^B''F^^B''F ;v(99::1=D$)$Kvd|66E -edCCCD$)$Kvd|66E &uffdoNND1$112D$)$Kvd|66EU###f%%%f%%%%*j00r+   c                 
   ||D ]}t           j        t           j        t           j        f}|d         j        |vrt          d|d         j                   |d         j        t           j        k    st          d|d         j                   |                                 r8|d         j        t           j        k    st          d|d         j                   | j	        r| 
                    ||          \  }}}}	nd}d}	d}|                     |||          }
|                     |
          }
|                     |
          \  }}g }i }| j	        r=|t          d          |	t          d	          t          ||||	          \  }}||d
}nh|                     ||||          \  }}}t#          |          }t%          |          D ]-}|                    ||         ||         ||         d           .|                                 rd |D             }| j	        r|t          d          t#          |          }g }g }t%          |          D ]h}t          j        ||         dk              d         }|                    ||         |                    |                    ||         |                    ind}| j        B|                     |||          }|                     |          }|                     |          }nt3          d          i }| j	        rE|||t          d          d |D             }d |D             }t5          |||||          }d|i}n7d |D             }t7          ||          }t9          ||          D ]
\  } }!| |!d<   |                    |           | j        | j        | j         d |D             }"| j	        rt#          |          }g }"g }|t          d          t%          |          D ]h}t          j        ||         dk              d         }|"                    ||         |                    |                    ||         |                    ind}|                     ||"|          }#|                     |#          }#|                      |#          }$i }%| j	        r6||t          d          d |D             }&tC          |$|"|&|          }'d|'i}%nH|$|"t          d          tE          |$|"          \  }(})t9          |(|)|          D ]\  }*}+}!|*|!d<   |+|!d<   |                    |%           ||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 r]   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                     g | ]
}|d          S rY  rG   r0   rL   s     r)   r2   z$RoIHeads.forward.<locals>.<listcomp>  s    999Qaj999r+   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                     g | ]
}|d          S r_  rG   rZ  s     r)   r2   z$RoIHeads.forward.<locals>.<listcomp>/  s    8881AgJ888r+   c                     g | ]
}|d          S r]  rG   rZ  s     r)   r2   z$RoIHeads.forward.<locals>.<listcomp>0  s    :::QQx[:::r+   	loss_maskc                     g | ]
}|d          S r]  rG   )r0   rs     r)   r2   z$RoIHeads.forward.<locals>.<listcomp>4  s    666!!H+666r+   r  c                     g | ]
}|d          S rY  rG   r  s     r)   r2   z$RoIHeads.forward.<locals>.<listcomp>B  s    !=!=!=!G*!=!=!=r+   z0if in trainning, matched_idxs should not be NonezJboth targets and pos_matched_idxs should not be None when in training modec                     g | ]
}|d          S )r]   rG   rZ  s     r)   r2   z$RoIHeads.forward.<locals>.<listcomp>[  s    @@@1+@@@r+   loss_keypointzXboth keypoint_logits and keypoint_proposals should not be None when not in training modekeypoints_scores)#r   r   doublehalfrn   	TypeErrorru   r8  rz   trainingrj  r   r!  r"  r   r*   r  r   r   r   r6  r   r&  r'  r(  	ExceptionrW   r=   rO   updater)  r*  r+  r   r   ),r,  featuresrR   ru  ra  r[  floating_point_typesrA   r!   r"   box_featuresr   r    resultlossesr  r  r	   r   rh  rM   mask_proposalspos_matched_idxsri  r   mask_featuresrQ   r  r@   rS   rcnn_loss_maskmasks_probsr9   r  keypoint_proposalskeypoint_featuresr   r  r   rcnn_loss_keypointkeypoints_probsr   keypoint_probkpss,                                               r)   forwardzRoIHeads.forward  sl     	t 	t(-U\5:'N$z'+???#$fTUV]T^Td$f$fggg{(EK77#$hUVW_U`Uf$h$hiii$$&& t[>/5=@@'(r\]^i\j\p(r(rsss= 	 BFB^B^_hjqBrBr?I|V-?-?F!%L((9lKK}}\22'+'9'9,'G'G$n02= 	~ !8999!) !DEEE,9,X^`r,s,s)O\)8,WWFF$($?$?n^giu$v$v!E66UJ:&&  !&q"()"()     ==?? '	%99&999N} ('$%VWWW !^^
!##% #J// G GF+fVnq&899!<C")))F*;C*@AAA$++L,@,EFFFFG
 $( !- $ 2 28^\ Z Z $} = ="11-@@ GHHHI} +?&6&>+BU$%jkkk88888::':::	!.{NHV_aq!r!r(.9		66v6660fEE$'V$<$< + +LIq!*AgJJMM)$$$
 ".".'3!=!=f!=!=!=} ( ^^
%'"#% '$%WXXX#J// G GF+fVnq&899!<C&--i.?.DEEE$++L,@,EFFFFG
 $(  $ 6 6xASUa b b $ 2 23D E E"556GHHOM} 0?&6&>$%qrrr@@@@@%6#%7GW& &" "12D E"*.@.H$r   .DOUg-h-h*-0)V-T-T 0 0)M3%2AkN,/A())MM-(((v~r+   )NNNNNN)N)__name__
__module____qualname__r  r  r  r  __annotations__r  r6  r8  rF  rP  rU  rb  rj  r  r  __classcell__)r2  s   @r)   r  r    s       '%-"B O, +15 15 15 15 15 15f    !$ !$ !$F    
U 
U 
U$C $C $CL91 91 91@ U U U U U U U Ur+   r  )r   )*typingr   r   r   r   r   torch.nn.functionalr   
functionalr   r   r   torchvision.opsr	   r  r
    r   r  r*   r=   rD   rW   rk   r   jit_script_if_tracingr   r   r   r   r   r   unusedr   r   r   r   r  r  Moduler  rG   r+   r)   <module>r     s-   . . . . . . . . . . . .                       7 7 7 7 7 7 7 7 ! ! ! ! ! !%) %) %)P  <8 8 8  @! ! !H0$ 0$ 0$f      "=1 =1 =1@  >     *  ( _ _ _
    2! ! !H      $@ @ @ @ @ry @ @ @ @ @r+   