-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
271 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
ahnlich/ai/src/engine/ai/providers/processors/onnx_output_transform.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use ndarray::{Ix2, Ix3}; | ||
use crate::engine::ai::providers::processors::{Postprocessor, PostprocessorData}; | ||
use crate::error::AIProxyError; | ||
|
||
|
||
pub struct OnnxOutputTransform { | ||
output_key: String | ||
} | ||
|
||
impl OnnxOutputTransform { | ||
pub fn new(output_key: String) -> Self { | ||
Self { output_key } | ||
} | ||
} | ||
|
||
impl Postprocessor for OnnxOutputTransform { | ||
fn process(&self, data: PostprocessorData) -> Result<PostprocessorData, AIProxyError> { | ||
match data { | ||
PostprocessorData::OnnxOutput(onnx_output) => { | ||
let output = onnx_output.get(self.output_key.as_str()) | ||
.ok_or_else(|| AIProxyError::OnnxOutputTransformError { | ||
message: format!("Output key '{}' not found in the OnnxOutput.", self.output_key), | ||
})?; | ||
let output = output.try_extract_tensor::<f32>().map_err( | ||
|_| AIProxyError::OnnxOutputTransformError { | ||
message: "Failed to extract tensor from OnnxOutput.".to_string(), | ||
} | ||
)?; | ||
match output.ndim() { | ||
2 => { | ||
let output = output.into_dimensionality::<Ix2>().map_err( | ||
|_| AIProxyError::OnnxOutputTransformError { | ||
message: "Failed to convert Dyn tensor to 2D array.".to_string(), | ||
} | ||
)?; | ||
Ok(PostprocessorData::NdArray2(output.to_owned())) | ||
}, | ||
3 => { | ||
let output = output.into_dimensionality::<Ix3>().map_err( | ||
|_| AIProxyError::OnnxOutputTransformError { | ||
message: "Failed to convert Dyn tensor to 3D array.".to_string(), | ||
} | ||
)?; | ||
Ok(PostprocessorData::NdArray3(output.to_owned())) | ||
}, | ||
_ => Err(AIProxyError::OnnxOutputTransformError { | ||
message: "Only 2D and 3D tensors are supported.".to_string(), | ||
}), | ||
} | ||
} | ||
_ => Err(AIProxyError::OnnxOutputTransformError { | ||
message: "Only OnnxOutput is supported".to_string(), | ||
}), | ||
} | ||
} | ||
} |
Oops, something went wrong.