Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.26 KB | None | 0 0
  1. def motp_mota(obj, hyp, threshold=0.5):
  2.     """Calculate MOTP/MOTA
  3.  
  4.    obj: list
  5.        Ground truth frame detections.
  6.        detections: numpy int array Cx5 [[id, xmin, ymin, xmax, ymax]]
  7.  
  8.    hyp: list
  9.        Hypothetical frame detections.
  10.        detections: numpy int array Cx5 [[id, xmin, ymin, xmax, ymax]]
  11.  
  12.    threshold: IOU threshold
  13.    """
  14.  
  15.     dist_sum = 0  # a sum of IOU distances between matched objects and hypotheses
  16.     match_count = 0
  17.     missed_count = 0
  18.     false_positive = 0
  19.     mismatch_error = 0
  20.    
  21.     matches = {}  # matches between object IDs and hypothesis IDs
  22.  
  23.     # For every frame
  24.     for frame_obj, frame_hyp in zip(obj, hyp):
  25.         # Step 1: Convert frame detections to dict with IDs as keys
  26.         frame_obj = {el[0]: el[1:] for el in frame_obj}
  27.         frame_hyp = {el[0]: el[1:] for el in frame_hyp}
  28.        
  29.         # Step 2: Iterate over all previous matches
  30.         # If object is still visible, hypothesis still exists
  31.         # and IOU distance > threshold - we've got a match
  32.         # Update the sum of IoU distances and match count
  33.         # Delete matched detections from frame detections
  34.         for m_id in matches:
  35.             if m_id in frame_obj.keys() and m_id in frame_hyp.keys():
  36.                 cur_iou = iou_score(frame_obj[m_id], frame_hyp[m_id])
  37.                 if cur_iou > threshold:
  38.                     dist_sum += cur_iou
  39.                     match_count += 1
  40.                     del frame_obj[m_id]
  41.                     del frame_hyp[m_id]
  42.  
  43.         # Step 3: Calculate pairwise detection IOU between remaining frame detections
  44.         # Save IDs with IOU > threshold
  45.         iou_list = []
  46.         for gt_id in frame_obj:
  47.             for h_id in frame_hyp:
  48.                 cur_iou = iou_score(frame_obj[gt_id], frame_hyp[h_id])
  49.                 if cur_iou > threshold:
  50.                     iou_list.append([gt_id, h_id, cur_iou])
  51.  
  52.         # Step 4: Iterate over sorted pairwise IOU
  53.         # Update the sum of IoU distances and match count
  54.         # Delete matched detections from frame detections
  55.         del_gt = set()
  56.         del_h = set()
  57.        
  58.         for val in iou_list:
  59.             if val[0] not in del_gt and val[1] not in del_h:
  60.                 dist_sum += val[2]
  61.                 match_count += 1
  62.                 del_gt.add(val[0])
  63.                 del_h.add(val[1])
  64.                 if val[0] in matches.keys() and matches[val[0]] != val[1]:
  65.                     mismatch_error += 1
  66.                    
  67.                 matches[val[0]] = val[1]
  68.  
  69.         # Step 5: If matched IDs contradict previous matched IDs - increase mismatch error
  70.         pass
  71.        
  72.         # Step 6: Update matches with current matched IDs
  73.         pass
  74.  
  75.         # Step 7: Errors
  76.         # All remaining hypotheses are considered false positives
  77.         # All remaining objects are considered misses
  78.         for i in frame_obj:
  79.             if i not in del_gt:
  80.                 missed_count += 1
  81.         for i in frame_hyp:
  82.             if i not in del_h:
  83.                 false_positive += 1                
  84.  
  85.     # Step 8: Calculate MOTP and MOTA
  86.     MOTP = dist_sum / match_count
  87.     MOTA = 1 - (missed_count + mismatch_error + false_positive) / (match_count + missed_count)
  88.  
  89.     return MOTP, MOTA
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement