import numpy as np
def ap_per_class(tp, conf, pred_cls, target_cls, plot=False, save_dir='.', names=()):
""" Compute the average precision, given the recall and precision curves.
Source: https://github.com/rafaelpadilla/Object-Detection-Metrics.
# Arguments
tp: True positives (nparray, nx1 or nx10).
conf: Objectness value from 0-1 (nparray).
pred_cls: Predicted object classes (nparray).
target_cls: True object classes (nparray).
plot: Plot precision-recall curve at mAP@0.5
save_dir: Plot save directory
# Returns
The average precision as computed in py-faster-rcnn.
"""
# Sort by objectness
i = np.argsort(-conf)
tp, conf, pred_cls = tp[i], conf[i], pred_cls[i]
# Find unique classes
unique_classes = np.unique(target_cls)
nc = unique_classes.shape[0] # number of classes, number of detections
# Create Precision-Recall curve and compute AP for each class
px, py = np.linspace(0, 1, 1000), [] # for plotting
ap, p, r = np.zeros((nc, tp.shape[1])), np.zeros((nc, 1000)), np.zeros((nc, 1000))
for ci, c in enumerate(unique_classes):
i = pred_cls == c
n_l = (target_cls == c).sum() # number of labels
n_p = i.sum() # number of predictions
if n_p == 0 or n_l == 0:
continue
else:
# Accumulate FPs and TPs
fpc = (1 - tp[i]).cumsum(0)
tpc = tp[i].cumsum(0)
# Recall
recall = tpc / (n_l + 1e-16) # recall curve
r[ci] = np.interp(-px, -conf[i], recall[:, 0], left=0) # negative x, xp because xp decreases
# Precision
precision = tpc / (tpc + fpc) # precision curve
p[ci] = np.interp(-px, -conf[i], precision[:, 0], left=1) # p at pr_score
# AP from recall-precision curve
for j in range(tp.shape[1]):
ap[ci, j], mpre, mrec = compute_ap(recall[:, j], precision[:, j])
# Compute F1 (harmonic mean of precision and recall)
f1 = 2 * p * r / (p + r + 1e-16)
i = f1.mean(0).argmax() # max F1 index
return p[:, i], r[:, i], ap, f1[:, i], unique_classes.astype('int32')
def compute_ap(recall, precision):
""" Compute the average precision, given the recall and precision curves
# Arguments
recall: The recall curve (list)
precision: The precision curve (list)
# Returns
Average precision, precision curve, recall curve
"""
# Append sentinel values to beginning and end
mrec = np.concatenate(([0.0], recall, [1.0]))
mpre = np.concatenate(([1.0], precision, [0.0]))
# Compute the precision envelope
mpre = np.flip(np.maximum.accumulate(np.flip(mpre)))
# Integrate area under curve
method = 'interp' # methods: 'continuous', 'interp'
if method == 'interp':
x = np.linspace(0, 1, 101) # 101-point interp (COCO)
ap = np.trapz(np.interp(x, mrec, mpre), x) # integrate
else: # 'continuous'
i = np.where(mrec[1:] != mrec[:-1])[0] # points where x axis (recall) changes
ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1]) # area under curve
return ap, mpre, mrec
def box_iou(box1, box2):
# https://github.com/pytorch/vision/blob/master/torchvision/ops/boxes.py
"""
Return intersection-over-union (Jaccard index) of boxes.
Both sets of boxes are expected to be in (x1, y1, x2, y2) format.
Arguments:
box1 (Tensor[N, 4])
box2 (Tensor[M, 4])
Returns:
iou (Tensor[N, M]): the NxM matrix containing the pairwise
IoU values for every element in boxes1 and boxes2
"""
def box_area(box):
# box = 4xn
return (box[2] - box[0]) * (box[3] - box[1])
area1 = box_area(box1.T)
area2 = box_area(box2.T)
# inter(N,M) = (rb(N,M,2) - lt(N,M,2)).clamp(0).prod(2)
inter = (np.minimum(box1[:, None, 2:], box2[:, 2:]) - np.maximum(box1[:, None, :2], box2[:, :2])).clip(0).prod(2)
return inter / (area1[:, None] + area2 - inter) # iou = inter / (area1 + area2 - inter)
def process_batch(detections, labels, iouv):
"""
Return correct predictions matrix. Both sets of boxes are in (x1, y1, x2, y2) format.
Arguments:
detections (Array[N, 6]), x1, y1, x2, y2, conf, class
labels (Array[M, 5]), class, x1, y1, x2, y2
Returns:
correct (Array[N, 10]), for 10 IoU levels
"""
correct = np.zeros((detections.shape[0], iouv.shape[0]), dtype=np.bool)
iou = box_iou(labels[:, 1:], detections[:, :4])
x = np.where((iou >= iouv[0]) & (labels[:, 0:1] == detections[:, 5])) # IoU above threshold and classes match
if x[0].shape[0]:
matches = np.concatenate((np.stack(x, 1), iou[x[0], x[1]][:, None]), 1) # [label, detection, iou]
if x[0].shape[0] > 1:
matches = matches[matches[:, 2].argsort()[::-1]]
matches = matches[np.unique(matches[:, 1], return_index=True)[1]]
# matches = matches[matches[:, 2].argsort()[::-1]]
matches = matches[np.unique(matches[:, 0], return_index=True)[1]]
correct[np.floor(matches[:, 1]).astype(np.int8)] = matches[:, 2:3] >= iouv
return correct
predn = np.array([[ 171.77, 240.05, 195.48, 274.49, 0.93848, 3],
[ 21.572, 51.537, 47.377, 86.898, 0.9333, 11],
[ 209.37, 52.834, 234.48, 86.861, 0.92738, 6],
[ 158.85, 53.388, 178.66, 86.812, 0.92486, 1],
[ 52.979, 53.726, 74.956, 87.647, 0.90863, 30],
[ 199.26, 239.44, 219.24, 274.93, 0.90682, 1],
[ 183.99, 53.799, 205.99, 86.792, 0.89765, 2],
[ 183.97, 52.928, 205.89, 87.249, 0.26055, 1],
[ 199.31, 239.66, 219.37, 274.57, 0.013927, 3],
[ 53.337, 53.867, 75.058, 88.076, 0.0075426, 23],
[ 183.87, 53.502, 205.93, 86.706, 0.0064968, 6],
[ 53.384, 53.45, 75.153, 87.949, 0.0061097, 19],
[ 21.375, 51.966, 47.245, 87.098, 0.0048851, 26],
[ 184.01, 53.078, 206.01, 87.207, 0.0039936, 4],
[ 52.943, 53.725, 74.989, 87.645, 0.0028298, 26],
[ 209.84, 52.922, 234.25, 86.502, 0.00276, 2],
[ 209.8, 51.267, 234.56, 87.55, 0.0024511, 5],
[ 209.84, 52.922, 234.25, 86.502, 0.002151, 0],
[ 21.572, 51.537, 47.377, 86.898, 0.0020529, 27],
[ 21.572, 51.537, 47.377, 86.898, 0.0017623, 23],
[ 52.943, 53.725, 74.989, 87.645, 0.0016926, 13],
[ 209.86, 52.984, 234.26, 86.499, 0.0016361, 27],
[ 21.557, 51.488, 47.374, 86.9, 0.0015562, 17],
[ 209.8, 51.267, 234.56, 87.55, 0.001505, 1],
[ 209.8, 51.267, 234.56, 87.55, 0.001483, 7],
[ 209.79, 51.285, 234.57, 87.517, 0.0014728, 9],
[ 209.37, 52.834, 234.48, 86.861, 0.0014664, 8],
[ 53.341, 53.449, 75.175, 87.967, 0.0012464, 21],
[ 21.517, 51.855, 47.466, 87.171, 0.0012186, 25],
[ 209.86, 52.984, 234.26, 86.499, 0.001037, 26]], dtype=np.float32)
labelsn= np.array([[ 1, 158.95, 54, 177.94, 86],
[ 1, 198.94, 240, 217.93, 273],
[ 2, 183.94, 55, 204.93, 86],
[ 3, 171.95, 241, 194.94, 273],
[ 6, 208.93, 52, 233.93, 86],
[ 11, 21.993, 53, 46.985, 87],
[ 30, 53.983, 54, 74.976, 87]], dtype=np.float32)
iouv = np.array([ 0.5,0.55, 0.6,0.65, 0.7,0.75,0.8,0.85,0.9,0.95], dtype=np.float32)
tp = process_batch(predn, labelsn, iouv)
conf = predn[:,4]
pred_cls = predn[:, 5]
target_cls = labelsn[:,0]
stats=[]
stats.append((tp, predn[:, 4], predn[:, 5], target_cls)) # (correct, conf, pcls, tcls)
stats_temp = [np.concatenate(x, 0) for x in zip(*stats)] # to numpy
p, r, ap, f1, ap_class = ap_per_class(*stats_temp)
ap50, ap = ap[:, 0], ap.mean(1) # AP@0.5, AP@0.5:0.95
mp, mr, map50, map = p.mean(), r.mean(), ap50.mean(), ap.mean()
print(p)
YOLO5单独提取代码计算预测框与类别的Map0.5-0.95
最新推荐文章于 2024-07-07 10:33:56 发布