1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//! Error types for OxiGDAL ML operations
//!
//! This module provides ML-specific error types that integrate with
//! the core OxiGDAL error hierarchy.
use thiserror::Error;
/// The main result type for ML operations
pub type Result<T> = core::result::Result<T, MlError>;
/// ML-specific errors
#[derive(Debug, Error)]
pub enum MlError {
/// Model loading error
#[error("Model error: {0}")]
Model(#[from] ModelError),
/// Inference error
#[error("Inference error: {0}")]
Inference(#[from] InferenceError),
/// Preprocessing error
#[error("Preprocessing error: {0}")]
Preprocessing(#[from] PreprocessingError),
/// Postprocessing error
#[error("Postprocessing error: {0}")]
Postprocessing(#[from] PostprocessingError),
/// OxiGDAL core error
#[error("OxiGDAL error: {0}")]
OxiGdal(#[from] oxigdal_core::OxiGdalError),
/// ONNX Runtime error
#[error("ONNX Runtime error: {0}")]
Ort(String),
/// I/O error
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
/// Serialization error
#[error("Serialization error: {0}")]
Serialization(#[from] serde_json::Error),
/// Invalid configuration
#[error("Invalid configuration: {0}")]
InvalidConfig(String),
/// Feature not available
#[error("Feature not available: {feature}. Enable with feature flag: {flag}")]
FeatureNotAvailable {
/// The feature name
feature: String,
/// The required feature flag
flag: String,
},
}
/// Model-related errors
#[derive(Debug, Error)]
pub enum ModelError {
/// Model file not found
#[error("Model file not found: {path}")]
NotFound {
/// The model file path
path: String,
},
/// Model loading failed
#[error("Failed to load model: {reason}")]
LoadFailed {
/// The reason for failure
reason: String,
},
/// Invalid model format
#[error("Invalid model format: {message}")]
InvalidFormat {
/// Error message
message: String,
},
/// Model initialization failed
#[error("Model initialization failed: {reason}")]
InitializationFailed {
/// The reason for failure
reason: String,
},
/// Incompatible model version
#[error("Incompatible model version: expected {expected}, got {actual}")]
IncompatibleVersion {
/// Expected version
expected: String,
/// Actual version
actual: String,
},
/// Missing required input
#[error("Missing required model input: {input_name}")]
MissingInput {
/// Input name
input_name: String,
},
/// Missing required output
#[error("Missing required model output: {output_name}")]
MissingOutput {
/// Output name
output_name: String,
},
}
/// Inference-related errors
#[derive(Debug, Error)]
pub enum InferenceError {
/// Invalid input shape
#[error("Invalid input shape: expected {expected:?}, got {actual:?}")]
InvalidInputShape {
/// Expected shape
expected: Vec<usize>,
/// Actual shape
actual: Vec<usize>,
},
/// Invalid input type
#[error("Invalid input type: expected {expected}, got {actual}")]
InvalidInputType {
/// Expected type
expected: String,
/// Actual type
actual: String,
},
/// Band (channel) count mismatch between the input buffer and the model.
///
/// A [`RasterBuffer`](oxigdal_core::buffer::RasterBuffer) is single-band, so
/// a single buffer can only ever supply one channel. This error is raised
/// when a model declares more (or fewer) input channels than the supplied
/// buffer provides, instead of silently building a wrong-shaped tensor.
#[error(
"Band count mismatch: model expects {expected} channel(s) but the input buffer supplies {actual}"
)]
InvalidBandCount {
/// Channel count the model expects
expected: usize,
/// Channel count the buffer supplies
actual: usize,
},
/// Batch size mismatch
#[error("Batch size mismatch: expected {expected}, got {actual}")]
BatchSizeMismatch {
/// Expected batch size
expected: usize,
/// Actual batch size
actual: usize,
},
/// Inference failed
#[error("Inference failed: {reason}")]
Failed {
/// The reason for failure
reason: String,
},
/// Output parsing failed
#[error("Failed to parse output: {reason}")]
OutputParsingFailed {
/// The reason for failure
reason: String,
},
/// GPU not available
#[error("GPU acceleration requested but not available: {message}")]
GpuNotAvailable {
/// Error message
message: String,
},
}
/// Preprocessing-related errors
#[derive(Debug, Error)]
pub enum PreprocessingError {
/// Invalid normalization parameters
#[error("Invalid normalization parameters: {message}")]
InvalidNormalization {
/// Error message
message: String,
},
/// Tiling failed
#[error("Tiling failed: {reason}")]
TilingFailed {
/// The reason for failure
reason: String,
},
/// Padding failed
#[error("Padding failed: {reason}")]
PaddingFailed {
/// The reason for failure
reason: String,
},
/// Invalid tile size
#[error("Invalid tile size: width={width}, height={height}")]
InvalidTileSize {
/// Tile width
width: usize,
/// Tile height
height: usize,
},
/// Channel mismatch
#[error("Channel mismatch: expected {expected}, got {actual}")]
ChannelMismatch {
/// Expected channels
expected: usize,
/// Actual channels
actual: usize,
},
/// Augmentation failed
#[error("Data augmentation failed: {reason}")]
AugmentationFailed {
/// The reason for failure
reason: String,
},
}
/// Postprocessing-related errors
#[derive(Debug, Error)]
pub enum PostprocessingError {
/// Tile merging failed
#[error("Tile merging failed: {reason}")]
MergingFailed {
/// The reason for failure
reason: String,
},
/// Threshold out of range
#[error("Threshold out of range: must be between 0.0 and 1.0, got {value}")]
InvalidThreshold {
/// The invalid threshold value
value: f32,
},
/// Polygon conversion failed
#[error("Polygon conversion failed: {reason}")]
PolygonConversionFailed {
/// The reason for failure
reason: String,
},
/// NMS failed
#[error("Non-maximum suppression failed: {reason}")]
NmsFailed {
/// The reason for failure
reason: String,
},
/// Export failed
#[error("Export failed: {reason}")]
ExportFailed {
/// The reason for failure
reason: String,
},
/// Invalid class ID
#[error("Invalid class ID: {class_id}")]
InvalidClassId {
/// The invalid class ID
class_id: usize,
},
}
impl From<oxionnx::OnnxError> for MlError {
fn from(err: oxionnx::OnnxError) -> Self {
MlError::Ort(err.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let err = ModelError::NotFound {
path: "/path/to/model.onnx".to_string(),
};
assert!(err.to_string().contains("Model file not found"));
assert!(err.to_string().contains("/path/to/model.onnx"));
}
#[test]
fn test_error_conversion() {
let model_err = ModelError::LoadFailed {
reason: "test".to_string(),
};
let ml_err: MlError = model_err.into();
assert!(matches!(ml_err, MlError::Model(_)));
}
#[test]
fn test_invalid_threshold() {
let err = PostprocessingError::InvalidThreshold { value: 1.5 };
assert!(err.to_string().contains("1.5"));
}
}