IoU计算
I o U = p r e d i c t 检 测 框 与 g r o u n d _ t r u t h 检 测 框 的 交 集 p r e d i c t 检 测 框 与 g r o u n d _ t r u t h 检 测 框 的 的 并 集 IoU = \frac{predict检测框与ground\_truth检测框的交集}{predict检测框与ground\_truth检测框的的并集} IoU=predict检测框与ground_truth检测框的的并集predict检测框与ground_truth检测框的交集
源码
IoU就是caffe.ssd中的JaccardOverlap。
计算IoU
来自caffe.ssd/src/caffe/util/bbox_util.cpp
template <typename Dtype>
Dtype JaccardOverlap(const Dtype* bbox1, const Dtype* bbox2) {
// 若不重叠,返回0
if (bbox2[0] > bbox1[2] || bbox2[2] < bbox1[0] ||
bbox2[1] > bbox1[3] || bbox2[3] < bbox1[1]) {
return Dtype(0.);
} else {
const Dtype inter_xmin = std::max(bbox1[0], bbox2[0]);
const Dtype inter_ymin = std::max(bbox1[1], bbox2[1]);
const Dtype inter_xmax = std::min(bbox1[2], bbox2[2]);
const Dtype inter_ymax = std::min(bbox1[3], bbox2[3]);
const Dtype inter_width = inter_xmax - inter_xmin;
const Dtype inter_height = inter_ymax - inter_ymin;
const Dtype inter_size = inter_width * inter_height;
// 求bbox的面积
const Dtype bbox1_size = BBoxSize(bbox1);
const Dtype bbox2_size = BBoxSize(bbox2);
return inter_size / (bbox1_size + bbox2_size - inter_size);
}
}
求bbox面积
来自caffe.ssd/src/caffe/util/bbox_util.cpp
template <typename Dtype>
Dtype BBoxSize(const Dtype* bbox, const bool normalized = true);
template <typename Dtype>
Dtype BBoxSize(const Dtype* bbox, const bool normalized) {
if (bbox[2] < bbox[0] || bbox[3] < bbox[1]) {
// If bbox is invalid (e.g. xmax < xmin or ymax < ymin), return 0.
return Dtype(0.);
} else {
const Dtype width = bbox[2] - bbox[0];
const Dtype height = bbox[3] - bbox[1];
if (normalized) {
return width * height;
} else {
// If bbox is not within range [0, 1].
return (width + 1) * (height + 1);
}
}
}