Class OnnxModelLoader
- java.lang.Object
-
- com.codedstream.otterstream.onnx.OnnxModelLoader
-
- All Implemented Interfaces:
ModelLoader<InferenceSession>
public class OnnxModelLoader extends Object implements ModelLoader<InferenceSession>
ModelLoaderimplementation for loading ONNX models.This loader handles the specifics of loading ONNX models from various sources (file system, input streams) and configuring ONNX Runtime sessions with optimized settings. It extracts model metadata and validates loaded models.
Loading Sources:
- File Path: Load models from local filesystem or network paths
- InputStream: Load models from memory streams or network streams
Session Configuration:
Supports configuration through
ModelConfigmodel options:- interOpThreads: Number of threads for inter-operation parallelism
- intraOpThreads: Number of threads for intra-operation parallelism
- optimizationLevel: "disable", "basic", "extended", or "all"
- useGpu: Boolean flag to enable GPU execution (CUDA)
Usage Example:
OnnxModelLoader loader = new OnnxModelLoader(); // Configure with options ModelConfig config = ModelConfig.builder() .modelPath("model.onnx") .modelOption("interOpThreads", 2) .modelOption("intraOpThreads", 4) .modelOption("optimizationLevel", "all") .modelOption("useGpu", true) .build(); // Load model InferenceSession session = loader.loadModel(config); // Validate and get metadata if (loader.validateModel(session, config)) { ModelMetadata metadata = loader.getModelMetadata(session); }Supported Formats:
This loader only supports
ModelFormat.ONNXformat.Model Validation:
Validates that loaded models have at least one input and one output. More comprehensive validation can be added by extending this class.
Error Handling:
All loading failures throw
ModelLoadExceptionwith detailed error messages and root causes.- Since:
- 1.0.0
- Author:
- Nestor Martourez, Sr Software and Data Streaming Engineer @ CodedStreams
- See Also:
ModelLoader,InferenceSession,OnnxInferenceEngine
-
-
Constructor Summary
Constructors Constructor Description OnnxModelLoader()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ModelMetadatagetModelMetadata(InferenceSession model)Extracts metadata from a loaded ONNX model.ModelFormat[]getSupportedFormats()Gets the model formats supported by this loader.InferenceSessionloadModel(ModelConfig config)Loads an ONNX model from the path specified in configuration.InferenceSessionloadModel(InputStream inputStream, ModelConfig config)Loads an ONNX model from an input stream.booleanvalidateModel(InferenceSession model, ModelConfig config)Validates that a loaded ONNX model matches the configuration.
-
-
-
Method Detail
-
loadModel
public InferenceSession loadModel(ModelConfig config) throws ModelLoadException
Loads an ONNX model from the path specified in configuration.- Specified by:
loadModelin interfaceModelLoader<InferenceSession>- Parameters:
config- model configuration containing path and options- Returns:
- loaded
InferenceSessioninstance - Throws:
ModelLoadException- if loading fails
-
loadModel
public InferenceSession loadModel(InputStream inputStream, ModelConfig config) throws ModelLoadException
Loads an ONNX model from an input stream.Useful for loading models from memory or network sources.
- Specified by:
loadModelin interfaceModelLoader<InferenceSession>- Parameters:
inputStream- stream containing ONNX model dataconfig- model configuration- Returns:
- loaded
InferenceSessioninstance - Throws:
ModelLoadException- if loading fails
-
validateModel
public boolean validateModel(InferenceSession model, ModelConfig config)
Validates that a loaded ONNX model matches the configuration.Basic validation checks that the model has at least one input and one output.
- Specified by:
validateModelin interfaceModelLoader<InferenceSession>- Parameters:
model- the loadedInferenceSessionconfig- model configuration (not used in basic validation)- Returns:
- true if model has both inputs and outputs
-
getSupportedFormats
public ModelFormat[] getSupportedFormats()
Gets the model formats supported by this loader.- Specified by:
getSupportedFormatsin interfaceModelLoader<InferenceSession>- Returns:
- array containing only
ModelFormat.ONNX
-
getModelMetadata
public ModelMetadata getModelMetadata(InferenceSession model)
Extracts metadata from a loaded ONNX model.Extracts input and output schemas from model metadata. If extraction fails, returns minimal metadata with empty schemas.
- Specified by:
getModelMetadatain interfaceModelLoader<InferenceSession>- Parameters:
model- the loadedInferenceSession- Returns:
- model metadata including input/output schemas
-
-