Loading...
Searching...
No Matches
yolo_dataset.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 <cstdio>
7
10
11namespace opennn
12{
13
15{
16 float center_x = 0.0f;
17 float center_y = 0.0f;
18 float width = 0.0f;
19 float height = 0.0f;
20 float score = 0.0f;
21 Index class_id = 0;
22};
23
24vector<YoloDetection> decode_yolo_detections(span<const float>,
25 Index,
26 Index,
27 Index,
28 Index);
29
31{
32 span<const float> data;
33 Index grid_size = 0;
34 Index boxes_per_cell = 0;
35 Index classes_number = 0;
36};
37
38vector<YoloDetection> decode_yolo_fpn_detections(const vector<YoloFpnHead>&,
39 Index,
40 Index,
41 Index,
42 Index,
43 float confidence_threshold = 0.25f,
44 float iou_threshold = 0.45f);
45
46vector<YoloDetection> decode_yolo_v8_fpn_detections(const vector<YoloFpnHead>&,
47 Index,
48 Index,
49 Index,
50 Index,
51 float confidence_threshold = 0.25f,
52 float iou_threshold = 0.45f,
53 Index reg_max = 1);
54
55class YoloDataset final : public Dataset
56{
57public:
58
59 struct Box
60 {
61 Index class_id = 0;
62 float x = 0.0f;
63 float y = 0.0f;
64 float w = 0.0f;
65 float h = 0.0f;
66 };
67
68 YoloDataset() = default;
69
70 YoloDataset(const filesystem::path&,
71 const filesystem::path&,
72 const Shape& input_shape = {416, 416, 3},
73 Index grid_size = 13,
74 Index boxes_per_cell = 5,
75 const vector<array<float, 2>>& anchors = {});
76
77 Index get_samples_number() const noexcept override { return samples_number; }
79
80 Index get_grid_size() const noexcept { return grid_size; }
81 Index get_boxes_per_cell() const noexcept { return boxes_per_cell; }
82 Index get_classes_number() const noexcept { return ssize(class_names); }
83 const vector<array<float, 2>>& get_anchors() const noexcept { return anchors; }
84 const vector<string>& get_class_names() const noexcept { return class_names; }
85 const filesystem::path& get_image_path(Index i) const { return image_filenames[size_t(i)]; }
86 const filesystem::path& get_images_directory() const { return images_directory; }
87 const filesystem::path& get_labels_directory() const { return labels_directory; }
88 const Shape& get_input_shape() const { return cache_input_shape; }
89
90 float get_display_confidence_threshold() const noexcept { return display_confidence_threshold; }
91 void set_display_confidence_threshold(float t) { display_confidence_threshold = t; }
92
93 bool is_multi_scale() const noexcept { return !head_grid_sizes.empty(); }
94 Index get_boxes_per_head() const noexcept { return boxes_per_head; }
95 void set_multi_scale_heads(const vector<Index>&,
96 const vector<vector<array<float, 2>>>&);
97
98 static constexpr Index MAX_GT_BOXES = 100;
99
100 void set_v8_mode(bool enabled);
101 bool get_v8_mode() const { return v8_mode; }
102
103 void set(const filesystem::path&,
104 const filesystem::path&,
105 const Shape& input_shape = {416, 416, 3},
106 Index grid_size = 13,
107 Index boxes_per_cell = 5,
108 const vector<array<float, 2>>& anchors = {});
111 void enable_device_residency() override;
112
113 void from_JSON(const JsonDocument&) override;
114 void to_JSON(JsonWriter&) const override;
115
116 // Detection inputs are normalized to [0, 1] in both training and inference.
117 void fill_inputs(const vector<Index>&,
118 const vector<Index>&,
119 float*,
120 FillMode,
121 ColumnContiguity column_contiguity = ColumnContiguity::Unknown) const override;
122
123 void fill_targets(const vector<Index>&,
124 const vector<Index>&,
125 float*,
126 FillMode,
127 ColumnContiguity column_contiguity = ColumnContiguity::Unknown) const override;
128
130 {
131 float jitter = 0.2f;
132 float exposure = 1.5f;
133 float saturation = 1.5f;
134 float hue = 0.1f;
135 bool flip = true;
136 bool enabled = true;
137 bool mosaic = false;
138 int cutout_count = 0; // 0 = disabled; TODO: wire editor UI
139 float cutout_max_size_ratio = 0.04f; // max patch side = max(H,W) * ratio
140 };
141
143 const AugmentationPolicy& get_augmentation_policy() const noexcept { return augmentation_policy; }
144
145 static Index convert_voc_to_yolo(const filesystem::path&,
146 const string&,
147 const filesystem::path&,
148 const vector<string>& class_filter = {});
149
150private:
151
152 filesystem::path images_directory;
153 filesystem::path labels_directory;
154 filesystem::path image_cache_path;
155 filesystem::path target_cache_path;
156 filesystem::path boxes_cache_path;
157
158 mutable FileReader image_cache_reader;
159 mutable FileReader target_cache_reader;
160 mutable FileReader boxes_cache_reader;
161
162 Index samples_number = 0;
163 Index grid_size = 13;
164 Index boxes_per_cell = 5;
165 Index classes_number = 0;
166 Index image_record_bytes = 0;
167 Index target_record_floats = 0;
168
169 Shape cache_input_shape;
170 Index cache_grid_size = 0;
171 Index cache_image_record_bytes = 0;
172 Index cache_target_record_floats = 0;
173 uint64_t target_data_offset = 0;
174 uint64_t boxes_data_offset = 0;
175 vector<uint64_t> boxes_offsets;
176
177 AugmentationPolicy augmentation_policy{};
178 float display_confidence_threshold = 0.25f;
179
180 vector<filesystem::path> image_filenames;
181 vector<array<float, 2>> anchors;
182 vector<string> class_names;
183
184 vector<Index> head_grid_sizes;
185 vector<vector<array<float, 2>>> head_anchors;
186 Index boxes_per_head = 0;
187
188 bool v8_mode = false;
189
190 void open_or_build_cache(const vector<array<float, 2>>&);
191 bool try_open_cache(const vector<array<float, 2>>&);
192 bool try_rebuild_target_from_boxes(const vector<array<float, 2>>&);
193 void build_cache(const vector<array<float, 2>>&);
194 void setup_metadata(Index);
195 void update_target_layout();
196 void load_cache_to_ram();
197
198 vector<uint8_t> images_ram;
199 vector<float> targets_ram;
200};
201
202}
Dataset()=default
virtual void set_storage_mode(StorageMode)
Shape input_shape
Definition dataset.h:358
virtual Index get_samples_number() const noexcept
Definition dataset.h:59
StorageMode
Definition dataset.h:57
Definition io_utilities.h:30
Definition json.h:75
Definition json.h:92
const AugmentationPolicy & get_augmentation_policy() const noexcept
Definition yolo_dataset.h:143
Index get_grid_size() const noexcept
Definition yolo_dataset.h:80
Index get_samples_number() const noexcept override
Definition yolo_dataset.h:77
bool get_v8_mode() const
Definition yolo_dataset.h:101
const filesystem::path & get_image_path(Index i) const
Definition yolo_dataset.h:85
const Shape & get_input_shape() const
Definition yolo_dataset.h:88
static Index convert_voc_to_yolo(const filesystem::path &, const string &, const filesystem::path &, const vector< string > &class_filter={})
void set_multi_scale_heads(const vector< Index > &, const vector< vector< array< float, 2 > > > &)
void set_augmentation_policy(const AugmentationPolicy &)
float get_display_confidence_threshold() const noexcept
Definition yolo_dataset.h:90
static constexpr Index MAX_GT_BOXES
Definition yolo_dataset.h:98
Index get_boxes_per_cell() const noexcept
Definition yolo_dataset.h:81
void set_storage_mode(StorageMode) override
void set(const filesystem::path &, const filesystem::path &, const Shape &input_shape={416, 416, 3}, Index grid_size=13, Index boxes_per_cell=5, const vector< array< float, 2 > > &anchors={})
void to_JSON(JsonWriter &) const override
Index get_boxes_per_head() const noexcept
Definition yolo_dataset.h:94
void set_display_confidence_threshold(float t)
Definition yolo_dataset.h:91
const vector< array< float, 2 > > & get_anchors() const noexcept
Definition yolo_dataset.h:83
YoloDataset(const filesystem::path &, const filesystem::path &, const Shape &input_shape={416, 416, 3}, Index grid_size=13, Index boxes_per_cell=5, const vector< array< float, 2 > > &anchors={})
const filesystem::path & get_labels_directory() const
Definition yolo_dataset.h:87
void fill_inputs(const vector< Index > &, const vector< Index > &, float *, FillMode, ColumnContiguity column_contiguity=ColumnContiguity::Unknown) const override
void enable_device_residency() override
Index get_classes_number() const noexcept
Definition yolo_dataset.h:82
const filesystem::path & get_images_directory() const
Definition yolo_dataset.h:86
const vector< string > & get_class_names() const noexcept
Definition yolo_dataset.h:84
void fill_targets(const vector< Index > &, const vector< Index > &, float *, FillMode, ColumnContiguity column_contiguity=ColumnContiguity::Unknown) const override
void set_v8_mode(bool enabled)
void from_JSON(const JsonDocument &) override
bool is_multi_scale() const noexcept
Definition yolo_dataset.h:93
Definition configuration.h:10
vector< YoloDetection > decode_yolo_v8_fpn_detections(const vector< YoloFpnHead > &, Index, Index, Index, Index, float confidence_threshold=0.25f, float iou_threshold=0.45f, Index reg_max=1)
FillMode
Definition batch.h:20
vector< YoloDetection > decode_yolo_fpn_detections(const vector< YoloFpnHead > &, Index, Index, Index, Index, float confidence_threshold=0.25f, float iou_threshold=0.45f)
Eigen::array< T, N > array
Definition tensor_types.h:736
vector< YoloDetection > decode_yolo_detections(span< const float >, Index, Index, Index, Index)
ColumnContiguity
Definition tensor_types.h:747
@ Unknown
Definition tensor_types.h:747
Definition tensor_types.h:172
Definition yolo_dataset.h:130
float exposure
Definition yolo_dataset.h:132
float jitter
Definition yolo_dataset.h:131
bool enabled
Definition yolo_dataset.h:136
bool mosaic
Definition yolo_dataset.h:137
float cutout_max_size_ratio
Definition yolo_dataset.h:139
float saturation
Definition yolo_dataset.h:133
bool flip
Definition yolo_dataset.h:135
float hue
Definition yolo_dataset.h:134
int cutout_count
Definition yolo_dataset.h:138
Definition yolo_dataset.h:60
float h
Definition yolo_dataset.h:65
Index class_id
Definition yolo_dataset.h:61
float w
Definition yolo_dataset.h:64
float y
Definition yolo_dataset.h:63
float x
Definition yolo_dataset.h:62
Definition yolo_dataset.h:15
float height
Definition yolo_dataset.h:19
float center_x
Definition yolo_dataset.h:16
float center_y
Definition yolo_dataset.h:17
float score
Definition yolo_dataset.h:20
float width
Definition yolo_dataset.h:18
Index class_id
Definition yolo_dataset.h:21
Definition yolo_dataset.h:31
Index grid_size
Definition yolo_dataset.h:33
Index classes_number
Definition yolo_dataset.h:35
span< const float > data
Definition yolo_dataset.h:32
Index boxes_per_cell
Definition yolo_dataset.h:34