Loading...
Searching...
No Matches
models.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
9
10namespace opennn
11{
12
14{
15
16public:
17
19 const Shape&,
20 const Shape&,
21 const string& hidden_activation = "Tanh");
22};
23
25{
26
27public:
28
30 const Shape&,
31 const Shape&,
32 const string& hidden_activation = "Tanh");
33};
34
36{
37
38public:
39
41 const Shape&,
42 const Shape&);
43};
44
46{
47
48public:
49
51 const Shape&,
52 const Shape&);
53};
54
55class Autoencoder : public Network
56{
57
58public:
59
61 const Shape&,
62 const string&,
63 const string&);
64};
65
66#ifndef OPENNN_NO_VISION
67
69{
70
71public:
72
74 const Shape&,
75 const Shape&);
76};
77
78class ResNet : public Network
79{
80
81public:
82
83 static unique_ptr<ResNet> from_pretrained(const filesystem::path&);
84
85 ResNet(const Shape&,
86 const vector<Index>&,
87 const Shape&,
88 const Shape&,
89 bool use_bottleneck = false);
90};
91
92class Yolo : public Network
93{
94public:
95
97
99
100 enum class HeadStyle { Single, FPN, PANet, FPNv8 };
101
103
104 enum class ModelSize { n, s, m, l, x };
105
106 Yolo(const Shape&,
107 Index,
108 const vector<array<float, 2>>&,
109 Index grid_size = 13,
110 Backbone backbone = Backbone::Vgg,
111 ClassActivation class_activation = ClassActivation::Softmax,
112 HeadStyle head_style = HeadStyle::Single,
113 BodyActivation body_activation = BodyActivation::ReLU,
114 bool use_sppf = false,
115 Index reg_max = 1,
116 ModelSize model_size = ModelSize::l);
117
118 Index load_pretrained_backbone(const filesystem::path&);
119
120private:
121
122 Backbone backbone;
123};
124
126{
127
128public:
129
131 {
132 string category;
133 float confidence = 0.0f;
134 };
135
136 static constexpr Index default_embedding_dimension = 64;
137 static constexpr Index default_heads_number = 4;
138 static constexpr Index default_hidden_neurons = 64;
139
141 const Shape&,
142 const Shape&,
144
145 Prediction classify(string_view);
146 MatrixR calculate_text_outputs(const Tensor<string, 1>&);
147
148 void set_tokenizer(unique_ptr<TokenizerOperator>);
150};
151
152class Transformer final : public Network
153{
154public:
155
157
159 Index,
160 Index,
161 Index,
162 Index,
163 Index,
164 Index,
165 Index);
166
167 explicit Transformer(const filesystem::path&);
168
169 Index get_input_sequence_length() const { return get_layer("encoder_embedding")->get_input_shape()[0]; }
170 Index get_decoder_sequence_length() const { return get_layer("decoder_embedding")->get_input_shape()[0]; }
171
172 void set_dropout_rate(const float);
174
175 void set_input_vocabulary(const vector<string>&);
176 void set_target_vocabulary(const vector<string>&);
179 const vector<string>& get_input_vocabulary() const;
180 const vector<string>& get_target_vocabulary() const;
181
182};
183
184class TextGenerationNetwork final : public Network
185{
186public:
187
189
191 Index,
192 Index,
193 Index,
194 Index,
195 Index,
196 bool pre_normalization = false,
197 bool scale_embedding = true,
198 bool learned_positional = false,
199 const string& feed_forward_activation = "ReLU");
200
201 explicit TextGenerationNetwork(const filesystem::path&);
202
203 void load_pretrained(const filesystem::path& data_directory);
204
205 Index get_sequence_length() const { return get_layer("embedding")->get_input_shape()[0]; }
206
207 void set_dropout_rate(const float);
208 void set_attention_sdpa_auto(bool new_sdpa_auto);
209
210 void set_tokenizer(unique_ptr<TokenizerOperator>);
211 void set_vocabulary(const vector<string>&);
213
214};
215
216class Qwen3 final : public Network
217{
218public:
219
220 enum class Variant { B0_6, B4 };
221
222 static unique_ptr<Qwen3> from_pretrained(
223 Variant,
224 const filesystem::path&,
225 Index sequence_length = 32768);
226
227 // Builds the network and loads a BF16 inference binary without ever
228 // allocating or initialising the fp32 parameter master.
229 static unique_ptr<Qwen3> from_binary(
230 const filesystem::path& weights_path,
231 Index sequence_length,
232 Index vocabulary_size,
233 Index hidden_size,
234 Index layers_number,
235 Index query_heads,
236 Index key_value_heads,
237 Index head_dimension,
238 Index intermediate_size,
239 float rope_theta = 1000000.0f,
240 float rms_epsilon = 1.0e-6f);
241
243
244 Qwen3(Index sequence_length,
245 Index vocabulary_size,
246 Index hidden_size,
247 Index layers_number,
248 Index query_heads,
249 Index key_value_heads,
250 Index head_dimension,
251 Index intermediate_size,
252 float rope_theta = 1000000.0f,
253 float rms_epsilon = 1.0e-6f);
254
255private:
256
257 void build(Index sequence_length,
258 Index vocabulary_size,
259 Index hidden_size,
260 Index layers_number,
261 Index query_heads,
262 Index key_value_heads,
263 Index head_dimension,
264 Index intermediate_size,
265 float rope_theta,
266 float rms_epsilon);
267};
268
269class Bert final : public Network
270{
271public:
272
274
275 Bert(Index sequence_length,
276 Index vocabulary_size,
277 Index hidden_size,
278 Index heads_number,
279 Index intermediate_size,
280 Index layers_number,
281 Index type_vocabulary_size = 2);
282};
283
285{
286public:
287
289 {
290 unique_ptr<BertForSequenceClassification> model;
291 filesystem::path vocabulary_path;
293 };
294
295 static Pretrained from_pretrained(const filesystem::path&);
296
298
299 BertForSequenceClassification(Index sequence_length,
300 Index vocabulary_size,
301 Index hidden_size,
302 Index heads_number,
303 Index intermediate_size,
304 Index layers_number,
305 Index labels_number,
306 Index type_vocabulary_size = 2);
307
308 void set_dropout_rate(const float);
309};
310
311#endif
312
313#ifndef OPENNN_NO_VISION
314Index load_darknet_backbone(Network&, const filesystem::path&, Index);
315Index load_darknet_backbone_v11(Network&, const filesystem::path&);
316Index load_yolov8s_onnx(Network&, const filesystem::path& onnx_path, Index n_classes);
317#endif
318
319}
ApproximationNetwork(const Shape &, const Shape &, const Shape &, const string &hidden_activation="Tanh")
Autoencoder(const Shape &, const Shape &, const string &, const string &)
static Pretrained from_pretrained(const filesystem::path &)
BertForSequenceClassification(Index sequence_length, Index vocabulary_size, Index hidden_size, Index heads_number, Index intermediate_size, Index layers_number, Index labels_number, Index type_vocabulary_size=2)
Bert(Index sequence_length, Index vocabulary_size, Index hidden_size, Index heads_number, Index intermediate_size, Index layers_number, Index type_vocabulary_size=2)
ClassificationNetwork(const Shape &, const Shape &, const Shape &, const string &hidden_activation="Tanh")
ForecastingLstmNetwork(const Shape &, const Shape &, const Shape &)
ForecastingNetwork(const Shape &, const Shape &, const Shape &)
ImageClassificationNetwork(const Shape &, const Shape &, const Shape &)
Definition network.h:87
const unique_ptr< Layer > & get_layer(const Index layer_index) const
Definition network.h:221
Qwen3(Index sequence_length, Index vocabulary_size, Index hidden_size, Index layers_number, Index query_heads, Index key_value_heads, Index head_dimension, Index intermediate_size, float rope_theta=1000000.0f, float rms_epsilon=1.0e-6f)
Variant
Definition models.h:220
@ B0_6
Definition models.h:220
@ B4
Definition models.h:220
static unique_ptr< Qwen3 > from_pretrained(Variant, const filesystem::path &, Index sequence_length=32768)
static unique_ptr< Qwen3 > from_binary(const filesystem::path &weights_path, Index sequence_length, Index vocabulary_size, Index hidden_size, Index layers_number, Index query_heads, Index key_value_heads, Index head_dimension, Index intermediate_size, float rope_theta=1000000.0f, float rms_epsilon=1.0e-6f)
static unique_ptr< ResNet > from_pretrained(const filesystem::path &)
ResNet(const Shape &, const vector< Index > &, const Shape &, const Shape &, bool use_bottleneck=false)
void set_tokenizer(unique_ptr< TokenizerOperator >)
static constexpr Index default_hidden_neurons
Definition models.h:138
const TokenizerOperator * get_tokenizer() const
Prediction classify(string_view)
static constexpr Index default_heads_number
Definition models.h:137
MatrixR calculate_text_outputs(const Tensor< string, 1 > &)
TextClassificationNetwork(const Shape &, const Shape &, const Shape &, PoolingMethod pooling_method=PoolingMethod::AveragePooling)
static constexpr Index default_embedding_dimension
Definition models.h:136
void load_pretrained(const filesystem::path &data_directory)
TextGenerationNetwork(const filesystem::path &)
void set_vocabulary(const vector< string > &)
void set_dropout_rate(const float)
void set_attention_sdpa_auto(bool new_sdpa_auto)
const TokenizerOperator * get_tokenizer() const
void set_tokenizer(unique_ptr< TokenizerOperator >)
TextGenerationNetwork(Index, Index, Index, Index, Index, Index, bool pre_normalization=false, bool scale_embedding=true, bool learned_positional=false, const string &feed_forward_activation="ReLU")
Index get_sequence_length() const
Definition models.h:205
Definition tokenizer_operator.h:35
const TokenizerOperator * get_target_tokenizer() const
void set_target_vocabulary(const vector< string > &)
Transformer(const filesystem::path &)
Transformer(Index, Index, Index, Index, Index, Index, Index, Index)
Index get_decoder_sequence_length() const
Definition models.h:170
void set_dropout_rate(const float)
void set_input_vocabulary(const vector< string > &)
const vector< string > & get_target_vocabulary() const
const TokenizerOperator * get_input_tokenizer() const
Index get_input_sequence_length() const
Definition models.h:169
void set_attention_sdpa_min_sequence_length(Index)
const vector< string > & get_input_vocabulary() const
HeadStyle
Definition models.h:100
@ PANet
Definition models.h:100
@ Single
Definition models.h:100
@ FPN
Definition models.h:100
@ FPNv8
Definition models.h:100
ModelSize
Definition models.h:104
@ s
Definition models.h:104
@ l
Definition models.h:104
@ m
Definition models.h:104
@ n
Definition models.h:104
@ x
Definition models.h:104
DetectionClassActivation ClassActivation
Definition models.h:98
BodyActivation
Definition models.h:102
@ LeakyReLU
Definition models.h:102
@ ReLU
Definition models.h:102
@ SiLU
Definition models.h:102
Backbone
Definition models.h:96
@ DarknetTiny
Definition models.h:96
@ CSPDarknet53v11
Definition models.h:96
@ CSPDarknet53
Definition models.h:96
@ Vgg
Definition models.h:96
@ Darknet53
Definition models.h:96
@ DarknetTinyV3
Definition models.h:96
Index load_pretrained_backbone(const filesystem::path &)
Yolo(const Shape &, Index, const vector< array< float, 2 > > &, Index grid_size=13, Backbone backbone=Backbone::Vgg, ClassActivation class_activation=ClassActivation::Softmax, HeadStyle head_style=HeadStyle::Single, BodyActivation body_activation=BodyActivation::ReLU, bool use_sppf=false, Index reg_max=1, ModelSize model_size=ModelSize::l)
Definition configuration.h:10
Eigen::array< T, N > array
Definition tensor_types.h:736
PoolingMethod
Definition pooling_layer.h:78
@ AveragePooling
Definition pooling_layer.h:80
Index load_darknet_backbone(Network &, const filesystem::path &, Index)
Index load_yolov8s_onnx(Network &, const filesystem::path &onnx_path, Index n_classes)
DetectionClassActivation
Definition detection_head.h:14
Index load_darknet_backbone_v11(Network &, const filesystem::path &)
Eigen::Matrix< float, Eigen::Dynamic, Eigen::Dynamic, Layout > MatrixR
Definition opennn_types.h:331
unique_ptr< BertForSequenceClassification > model
Definition models.h:290
Index sequence_length
Definition models.h:292
filesystem::path vocabulary_path
Definition models.h:291
Definition tensor_types.h:172
float confidence
Definition models.h:133
string category
Definition models.h:132