• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

geo-engine / geoengine / 12466344113

23 Dec 2024 11:27AM UTC coverage: 90.695% (+0.2%) from 90.509%
12466344113

push

github

web-flow
Merge pull request #1002 from geo-engine/update-deps-2024-12-06

update deps 2024-12-06

38 of 75 new or added lines in 14 files covered. (50.67%)

24 existing lines in 15 files now uncovered.

133182 of 146846 relevant lines covered (90.7%)

54823.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

55.93
/operators/src/machine_learning/metadata_from_file.rs
1
use super::MachineLearningError;
2
use crate::machine_learning::error::{
3
    InvalidInputDimensions, InvalidOutputDimensions, MultipleInputsNotSupported, Ort,
4
};
5
use geoengine_datatypes::{machine_learning::MlModelMetadata, raster::RasterDataType};
6
use snafu::{ensure, ResultExt};
7
use std::path::Path;
8

9
pub fn load_model_metadata(path: &Path) -> Result<MlModelMetadata, MachineLearningError> {
2✔
10
    // TODO: proper error if model file cannot be found
11
    let session = ort::session::Session::builder()
2✔
12
        .context(Ort)?
2✔
13
        .commit_from_file(path)
2✔
14
        .context(Ort)?;
2✔
15

16
    // Onnx model may have multiple inputs, but we only support one input (with multiple features/bands)
17
    ensure!(
2✔
18
        session.inputs.len() == 1,
2✔
19
        MultipleInputsNotSupported {
×
20
            num_inputs: session.inputs.len()
×
21
        }
×
22
    );
23

24
    // Onnx model input type must be a Tensor in order to accept a 2d ndarray as input
25
    let ort::value::ValueType::Tensor {
26
        ty: input_tensor_element_type,
2✔
27
        dimensions: input_dimensions,
2✔
28
        dimension_symbols: _dimension_symbols,
2✔
29
    } = &session.inputs[0].input_type
2✔
30
    else {
31
        return Err(MachineLearningError::InvalidInputType {
×
32
            input_type: session.inputs[0].input_type.clone(),
×
33
        });
×
34
    };
35

36
    // Input dimensions must be [-1, b] to accept a table of (arbitrarily many) single pixel features (rows) with `b` bands (columns)
37
    ensure!(
2✔
38
        input_dimensions.len() == 2 && input_dimensions[0] == -1 && input_dimensions[1] > 0,
2✔
39
        InvalidInputDimensions {
×
40
            dimensions: input_dimensions.clone()
×
41
        }
×
42
    );
43

44
    // Onnx model must output one prediction per pixel as
45
    // (1) a Tensor with a single dimension of unknown size (dim = [-1]), or
46
    // (2) a Tensor with two dimensions, the first of unknown size and the second of size 1 (dim = [-1, 1])
47
    let output_tensor_element_type = if let ort::value::ValueType::Tensor {
2✔
48
        ty,
2✔
49
        dimensions,
2✔
50
        dimension_symbols: _dimension_symbols,
2✔
51
    } = &session.outputs[0].output_type
2✔
52
    {
53
        ensure!(
2✔
54
            dimensions == &[-1] || dimensions == &[-1, 1],
2✔
NEW
55
            InvalidOutputDimensions {
×
NEW
56
                dimensions: dimensions.clone()
×
NEW
57
            }
×
58
        );
59

60
        ty
2✔
61
    } else {
NEW
62
        return Err(MachineLearningError::InvalidOutputType {
×
NEW
63
            output_type: session.outputs[0].output_type.clone(),
×
NEW
64
        });
×
65
    };
66

67
    Ok(MlModelMetadata {
68
        file_path: path.to_owned(),
2✔
69
        input_type: try_raster_datatype_from_tensor_element_type(*input_tensor_element_type)?,
2✔
70
        num_input_bands: input_dimensions[1] as u32,
2✔
71
        output_type: try_raster_datatype_from_tensor_element_type(*output_tensor_element_type)?,
2✔
72
    })
73
}
2✔
74

75
// can't implement `TryFrom` here because `RasterDataType` is in operators crate
76
fn try_raster_datatype_from_tensor_element_type(
4✔
77
    value: ort::tensor::TensorElementType,
4✔
78
) -> Result<RasterDataType, MachineLearningError> {
4✔
79
    match value {
4✔
80
        ort::tensor::TensorElementType::Float32 => Ok(RasterDataType::F32),
3✔
81
        ort::tensor::TensorElementType::Uint8 | ort::tensor::TensorElementType::Bool => {
NEW
82
            Ok(RasterDataType::U8)
×
83
        }
NEW
84
        ort::tensor::TensorElementType::Int8 => Ok(RasterDataType::I8),
×
NEW
85
        ort::tensor::TensorElementType::Uint16 => Ok(RasterDataType::U16),
×
NEW
86
        ort::tensor::TensorElementType::Int16 => Ok(RasterDataType::I16),
×
NEW
87
        ort::tensor::TensorElementType::Int32 => Ok(RasterDataType::I32),
×
88
        ort::tensor::TensorElementType::Int64 => Ok(RasterDataType::I64),
1✔
NEW
89
        ort::tensor::TensorElementType::Float64 => Ok(RasterDataType::F64),
×
NEW
90
        ort::tensor::TensorElementType::Uint32 => Ok(RasterDataType::U32),
×
NEW
91
        ort::tensor::TensorElementType::Uint64 => Ok(RasterDataType::U64),
×
92
        _ => Err(MachineLearningError::UnsupportedTensorElementType {
×
93
            element_type: value,
×
94
        }),
×
95
    }
96
}
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc