18 constexpr
int box_elements = 4;
20 constexpr
int confidence_elements = 1;
29 float iou(
const Box& box1,
const Box& box2)
34 if (area1 <= 0 || area2 <= 0)
40 const auto y_min_intersection = std::max<float>(box1.
ymin, box2.
ymin);
41 const auto x_min_intersection = std::max<float>(box1.
xmin, box2.
xmin);
42 const auto y_max_intersection = std::min<float>(box1.
ymax, box2.
ymax);
43 const auto x_max_intersection = std::min<float>(box1.
xmax, box2.
xmax);
44 const auto area_intersection =
45 std::max<float>(y_max_intersection - y_min_intersection, 0.0f) *
46 std::max<float>(x_max_intersection - x_min_intersection, 0.0f);
47 overlap = area_intersection / (area1 + area2 - area_intersection);
52 std::vector<Detection> convert_to_detections(
const NMSConfig& config,
53 const std::vector<float>& detected_boxes)
55 const size_t element_step =
static_cast<size_t>(
56 box_elements + confidence_elements + config.
num_classes);
57 std::vector<Detection> detections;
59 for (
unsigned int i = 0; i < config.
num_boxes; ++i)
61 const float* cur_box = &detected_boxes[i * element_step];
65 det.
box = {cur_box[0], cur_box[0] + cur_box[2], cur_box[1],
66 cur_box[1] + cur_box[3]};
69 for (
unsigned int c = 0; c < config.
num_classes; ++c)
71 const float class_prob = det.
confidence * cur_box[5 + c];
77 detections.emplace_back(std::move(det));
85 const std::vector<float>& expected)
87 float tolerance = 0.001f;
88 return (std::fabs(detection.
classes[0] - expected[0]) < tolerance &&
89 std::fabs(detection.
box.
xmin - expected[1]) < tolerance &&
90 std::fabs(detection.
box.
ymin - expected[2]) < tolerance &&
91 std::fabs(detection.
box.
xmax - expected[3]) < tolerance &&
92 std::fabs(detection.
box.
ymax - expected[4]) < tolerance &&
93 std::fabs(detection.
confidence - expected[5]) < tolerance );
97 const std::vector<Detection>& detections)
99 for (
const auto& detection : detections)
101 for (
unsigned int c = 0; c < detection.classes.size(); ++c)
103 if (detection.classes[c] != 0.0f)
105 os << c <<
" " << detection.classes[c] <<
" " << detection.box.xmin
106 <<
" " << detection.box.ymin <<
" " << detection.box.xmax <<
" " 107 << detection.box.ymax << std::endl;
114 const std::vector<float>& detected_boxes) {
116 std::vector<Detection> detections =
117 convert_to_detections(config, detected_boxes);
119 const unsigned int num_detections =
static_cast<unsigned int>(detections.size());
120 for (
unsigned int c = 0; c < config.
num_classes; ++c)
123 std::sort(detections.begin(), detections.begin() +
static_cast<std::ptrdiff_t
>(num_detections),
129 for (
unsigned int d = 0; d < num_detections; ++d)
132 if (detections[d].classes[c] == 0.f)
138 const Box& box1 = detections[d].box;
139 for (
unsigned int b = d + 1; b < num_detections; ++b)
141 const Box& box2 = detections[b].box;
144 detections[b].classes[c] = 0.f;
float confidence
Confidence of detection.
float ymin
Y-pos position of the low left coordinate.
unsigned int num_boxes
Number of detected boxes.
float xmin
X-pos position of the low left coordinate.
float xmax
X-pos position of the top right coordinate.
void print_detection(std::ostream &os, const std::vector< Detection > &detections)
Print identified yolo detections.
std::vector< float > classes
Probability of classes.
Box representation structure.
float ymax
Y-pos position of the top right coordinate.
float iou_threshold
Inclusion threshold for Intersection-Over-Union.
std::vector< Detection > nms(const NMSConfig &config, const std::vector< float > &detected_boxes)
Perform Non-Maxima Supression on a list of given detections.
Non Maxima Suprresion configuration meta-data.
float confidence_threshold
Inclusion confidence threshold for a box.
bool compare_detection(const yolov3::Detection &detection, const std::vector< float > &expected)
Compare a detection object with a vector of float values.
unsigned int num_classes
Number of classes in the detected boxes.