Loading...
Searching...
No Matches
detection_head.h
Go to the documentation of this file.
1// SPDX-License-Identifier: AGPL-3.0-only
2// Copyright (C) 2026 Artificial Intelligence Techniques, S.L. and Roberto López González
3
4#pragma once
5
6#include <array>
7
9
10namespace opennn
11{
12
15
27
28inline float yolo_box_iou(const std::array<float, 6>& a, const std::array<float, 6>& b)
29{
30 const float a_left = a[0] - 0.5f * a[2];
31 const float a_top = a[1] - 0.5f * a[3];
32 const float a_right = a[0] + 0.5f * a[2];
33 const float a_bottom = a[1] + 0.5f * a[3];
34
35 const float b_left = b[0] - 0.5f * b[2];
36 const float b_top = b[1] - 0.5f * b[3];
37 const float b_right = b[0] + 0.5f * b[2];
38 const float b_bottom = b[1] + 0.5f * b[3];
39
40 const float inter_w = max(0.0f, min(a_right, b_right) - max(a_left, b_left));
41 const float inter_h = max(0.0f, min(a_bottom, b_bottom) - max(a_top, b_top));
42 const float inter = inter_w * inter_h;
43
44 const float area = a[2] * a[3] + b[2] * b[3] - inter;
45
46 return area > 0.0f ? inter / area : 0.0f;
47}
48
49inline float dfl_decode(const float* logits, Index reg_max)
50{
51 float max_l = *max_element(logits, logits + reg_max);
52 float sum = 0.0f;
53 for (Index i = 0; i < reg_max; ++i) sum += expf(logits[i] - max_l);
54 float d = 0.0f;
55 for (Index i = 0; i < reg_max; ++i) d += float(i) * expf(logits[i] - max_l) / sum;
56 return d;
57}
58
60{
61public:
62 virtual ~DetectionHeadEndpoint() = default;
63
65};
66
67}
Definition detection_head.h:60
virtual ~DetectionHeadEndpoint()=default
virtual DetectionHeadMetadata get_detection_head_metadata() const noexcept=0
Definition configuration.h:10
float dfl_decode(const float *logits, Index reg_max)
Definition detection_head.h:49
float yolo_box_iou(const std::array< float, 6 > &a, const std::array< float, 6 > &b)
Definition detection_head.h:28
DetectionClassActivation
Definition detection_head.h:14
@ Sigmoid
Definition detection_head.h:14
@ Softmax
Definition detection_head.h:14
DetectionHeadKind
Definition detection_head.h:13
@ AnchorBased
Definition detection_head.h:13
@ AnchorFree
Definition detection_head.h:13
@ Sigmoid
Definition configuration.h:39
@ Softmax
Definition configuration.h:39
Definition detection_head.h:17
Index classes_number
Definition detection_head.h:20
bool uses_sigmoid_classes() const noexcept
Definition detection_head.h:25
Index regression_bins
Definition detection_head.h:21
DetectionHeadKind kind
Definition detection_head.h:18
DetectionClassActivation class_activation
Definition detection_head.h:22
bool is_anchor_free() const noexcept
Definition detection_head.h:24
Index boxes_per_cell
Definition detection_head.h:19