Class OnnxModelLoader

  • All Implemented Interfaces:
    ModelLoader<InferenceSession>

    public class OnnxModelLoader
    extends Object
    implements ModelLoader<InferenceSession>
    ModelLoader implementation 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 ModelConfig model 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.ONNX format.

    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 ModelLoadException with 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