├── figures ├── iou.jpg ├── concepts.jpg ├── different_iou.jpg └── precision_recall_curve.jpg ├── example.py ├── utils.py ├── example_data_generator.py ├── README.md └── ap.py /figures/iou.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ao-Lee/AveragePrecision/HEAD/figures/iou.jpg -------------------------------------------------------------------------------- /figures/concepts.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ao-Lee/AveragePrecision/HEAD/figures/concepts.jpg -------------------------------------------------------------------------------- /figures/different_iou.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ao-Lee/AveragePrecision/HEAD/figures/different_iou.jpg -------------------------------------------------------------------------------- /figures/precision_recall_curve.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ao-Lee/AveragePrecision/HEAD/figures/precision_recall_curve.jpg -------------------------------------------------------------------------------- /example.py: -------------------------------------------------------------------------------- 1 | from example_data_generator import GenFakeData 2 | from ap import AveragePrecisionOnImages 3 | 4 | gts, predictions, num_gt = GenFakeData(size=20) 5 | ap, _, _ = AveragePrecisionOnImages(gts, predictions, num_gt, min_overlap=0.5, validate_input=True) 6 | print(ap) 7 | 8 | -------------------------------------------------------------------------------- /utils.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | ''' 4 | Compute IoU between detect box and ground truth boxes 5 | 6 | Parameters: 7 | box: detection box, numpy array, shape (4,): x1, y1, x2, y2 8 | 9 | boxes: ground truth boxes, shape (n, 4): x1, y1, x2, y2 10 | 11 | Returns: 12 | iou: numpy array, shape (n, ) 13 | ''' 14 | def IoU(box, boxes): 15 | 16 | box_area = (box[2] - box[0] + 1) * (box[3] - box[1] + 1) 17 | area = (boxes[:, 2] - boxes[:, 0] + 1) * (boxes[:, 3] - boxes[:, 1] + 1) 18 | xx1 = np.maximum(box[0], boxes[:, 0]) 19 | yy1 = np.maximum(box[1], boxes[:, 1]) 20 | xx2 = np.minimum(box[2], boxes[:, 2]) 21 | yy2 = np.minimum(box[3], boxes[:, 3]) 22 | 23 | # compute the width and height of the bounding box 24 | w = np.maximum(0, xx2 - xx1 + 1) 25 | h = np.maximum(0, yy2 - yy1 + 1) 26 | 27 | inter = w * h 28 | iou = inter / (box_area + area - inter) 29 | return iou -------------------------------------------------------------------------------- /example_data_generator.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from utils import IoU 3 | 4 | def _GenRandomBoxes(size=10): 5 | def _GenRandomBox(): 6 | rand = np.random.randint(low=0, high=101, size=4) 7 | box = np.array([rand[0], rand[1], rand[0]+rand[2], rand[1]+rand[3]]) 8 | box[box<0]=0 9 | return box 10 | 11 | arrays = [] 12 | for _ in range(size): 13 | arrays.append(_GenRandomBox()) 14 | return np.stack(arrays) 15 | 16 | def _GenPredictionBox(box, fp=False): 17 | w = box[2] - box[0] 18 | h = box[3] - box[1] 19 | dw = int(w*0.7) if fp else int(w*0.3) 20 | dh = int(h*0.7) if fp else int(h*0.3) 21 | rand_x = np.random.randint(low=-dw, high=dw+1, size=2) 22 | rand_y = np.random.randint(low=-dh, high=dh+1, size=2) 23 | new_box = [box[0]+rand_x[0], box[1]+rand_y[0], box[2]+rand_x[1], box[3]+rand_y[1]] 24 | 25 | new_box = np.array(new_box) 26 | new_box[new_box<0]=0 27 | if new_box[2]= min_overlap and not used: 130 | result.append({'TP':True, 'confidence':confidence}) 131 | used_info[file_id][idx] = True 132 | else: 133 | result.append({'TP':False, 'confidence':confidence}) 134 | 135 | ap, recall, precision = VocAveragePrecision(result, num_gt_example, validate=validate_input) 136 | return ap, recall, precision 137 | 138 | 139 | 140 | 141 | --------------------------------------------------------------------------------