Overriding ONNX options

Contents
[ ]

Aspose.OCR for .NET leverages ONNX Runtime as its machine learning engine. While its communication with ONNX Runtime is optimized for the majority of common environments, the library’s behavior can be fine-tuned for specific hardware.

ONNX runtime is configured through Aspose.OCR.OnnxRuntimeSessionOptions static class. This class exposes the following properties:

Property Type Description
GraphOptimizationLevel Aspose.OCR.GraphOptimizationLevelOnnx Graph optimization level for the session:
  • ORT_DISABLE_ALL - disable all optimizations.
  • ORT_ENABLE_BASIC - enable basic optimizations, such as node fusion and constant folding.
  • ORT_ENABLE_EXTENDED - enable extended optimizations, including memory layout improvements.
  • ORT_ENABLE_ALL - enable all available optimizations for maximum performance.
By default, all available optimizations are enabled.
ExecutionMode Aspose.OCR.ExecutionModeOnnx Execution mode for the session:
  • ORT_SEQUENTIAL - execute operators sequentially, ensuring that each operation is completed before the next one starts.
  • ORT_PARALLEL - execute operators in parallel (whenever possible), to improve performance.
By default, operators are executed concurrently, whenever possible.
IntraOpNumThreads int Number of threads for a single operations.
InterOpNumThreads int Number of threads for running multiple operations in parallel. If sequential execution (ExecutionModeOnnx.ORT_SEQUENTIAL) is enabled in ExecutionMode property, this value is ignored.

For technical details, refer to ONNX Runtime documentation.

Example

Changing the number of threads for ONNX runtime:

Aspose.OCR.OnnxRuntimeSessionOptions.IntraOpNumThreads = 8;
// Initialize recognition API
Aspose.OCR.AsposeOcr recognitionEngine = new Aspose.OCR.AsposeOcr();
// Add images to OcrInput object
Aspose.OCR.OcrInput input = new Aspose.OCR.OcrInput(Aspose.OCR.InputType.SingleImage);
input.Add("source1.png");
input.Add("source2.jpg");
// Recognize image
Aspose.OCR.OcrOutput results = recognitionEngine.Recognize(input);