在目标检测任务中,检测框过滤是后处理的关键环节。本文将从实现方式、性能表现和适用场景三个维度,对比分析Python与C++在检测框过滤中的差异。
检测框过滤的核心是非极大值抑制(NMS)算法,其数学表达式为:
NMS ( B , S , θ ) = { b i ∣ ∀ b j , area ( b i ∩ b j ) area ( b i ∪ b j ) < θ } \text{NMS}(B,S,\theta) = \{ b_i \mid \forall b_j, \frac{\text{area}(b_i \cap b_j)}{\text{area}(b_i \cup b_j)} < \theta \} NMS(B,S,θ)={bi∣∀bj,area(bi∪bj)area(bi∩bj)<θ}
其中 B B B是候选框集合, S S S是置信度得分, θ \theta θ是IoU阈值。算法目标是在保留高分检测框的同时,去除重叠度过高的冗余框。
import numpy as np
def nms_python(boxes, scores, threshold):
x1 = boxes[:, 0]
y1 = boxes[:, 1]
x2 = boxes[:, 2]
y2 = boxes[:, 3]
areas = (x2 - x1 + 1) * (y2 - y1 + 1)
order = scores.argsort()[::-1]
keep = []
while order.size > 0:
i = order[0]
keep.append(i)
xx1 = np.maximum(x1[i], x1[order[1:]])
yy1 = np.maximum(y1[i], y1[order[1:]])
xx2 = np.minimum(x2[i], x2[order[1:]])
yy2 = np.minimum(y2[i], y2[order[1:]])
w = np.maximum(0.0, xx2 - xx1 + 1)
h = np.maximum(0.0, yy2 - yy1 + 1)
inter = w * h
iou = inter / (areas[i] + areas[order[1:]] - inter)
inds = np.where(iou <= threshold)[0]
order = order[inds + 1]
return keep
#include
#include
struct Box {
float x1, y1, x2, y2, score;
};
std::vector<int> nms_cpp(std::vector<Box>& boxes, float threshold) {
std::sort(boxes.begin(), boxes.end(),
[](const Box& a, const Box& b) { return a.score > b.score; });
std::vector<int> keep;
for (int i = 0; i < boxes.size(); ++i) {
if (boxes[i].score < 0) continue;
keep.push_back(i);
for (int j = i + 1; j < boxes.size(); ++j) {
if (boxes[j].score < 0) continue;
float xx1 = std::max(boxes[i].x1, boxes[j].x1);
float yy1 = std::max(boxes[i].y1, boxes[j].y1);
float xx2 = std::min(boxes[i].x2, boxes[j].x2);
float yy2 = std::min(boxes[i].y2, boxes[j].y2);
float w = std::max(0.0f, xx2 - xx1);
float h = std::max(0.0f, yy2 - yy1);
float inter = w * h;
float area_i = (boxes[i].x2 - boxes[i].x1) * (boxes[i].y2 - boxes[i].y1);
float area_j = (boxes[j].x2 - boxes[j].x1) * (boxes[j].y2 - boxes[j].y1);
float iou = inter / (area_i + area_j - inter);
if (iou > threshold) {
boxes[j].score = -1; // 标记为抑制
}
}
}
return keep;
}
指标 | Python(NumPy) | C++ | 差异倍数 |
---|---|---|---|
1000框耗时(ms) | 8.2 | 0.4 | × 20 \times20 ×20 |
内存峰值(MB) | 45 | 8 | × 5.6 \times5.6 ×5.6 |
代码行数 | 25 | 35 | × 0.7 \times0.7 ×0.7 |
选择Python的场景
选择C++的场景
Python优化方向:
C++优化方向:
Python与C++在检测框过滤中各有优势:Python胜在开发效率,适合快速迭代;C++强在执行性能,适合生产部署。实际项目中常采用混合架构:用Python开发原型,C++实现核心模块,达到开发效率与运行性能的最佳平衡。