Information about the ONNX model’s inputs and outputs – script MetaTrader 5

The ONNX model run function requires the model’s input and output sizes to be defined. The sizes are specified via the OnnxSetInputShape and OnnxSetOutputShape functions, respectively.

Not all models have fixed input and output data sizes. In these cases, the script will return -1.

   OnnxTypeInfo type_info;

   long input_count=OnnxGetInputCount(session_handle);
   Print("model has ",input_count," input(s)");
   for(long i=0; i<input_count; i++)
     {
      string input_name=OnnxGetInputName(session_handle,i);
      Print(i," input name is ",input_name);
      if(OnnxGetInputTypeInfo(session_handle,i,type_info))
         PrintTypeInfo(i,"input",type_info);
     }

   long output_count=OnnxGetOutputCount(session_handle);
   Print("model has ",output_count," output(s)");
   for(long i=0; i<output_count; i++)
     {
      string output_name=OnnxGetOutputName(session_handle,i);
      Print(i," output name is ",output_name);
      if(OnnxGetOutputTypeInfo(session_handle,i,type_info))
         PrintTypeInfo(i,"output",type_info);
     } 

Use the FileSelectDialog function to select a model from the MQL5Files folder

An example of how the script runs is shown below:

Create model from mnist.onnx with debug logs
ONNX: Creating and using per session threadpools since use_per_session_threads_ is true
ONNX: Dynamic block base set to 0
ONNX: Initializing session.
ONNX: Adding default CPU execution provider.
ONNX: Removing initializer 'Parameter6'. It is no longer used by any node.
ONNX: Removing initializer 'Parameter88'. It is no longer used by any node.
ONNX: Total shared scalar initializer count: 0
ONNX: Removing initializer 'Parameter193_reshape1_shape'. It is no longer used by any node.
ONNX: Removing initializer 'Parameter193'. It is no longer used by any node.
ONNX: Total fused reshape node count: 0
ONNX: Total shared scalar initializer count: 0
ONNX: Total fused reshape node count: 0
ONNX: Removing initializer 'Parameter5'. It is no longer used by any node.
ONNX: Removing initializer 'Parameter87'. It is no longer used by any node.
ONNX: Use DeviceBasedPartition as default
ONNX: Saving initialized tensors.
ONNX: Done saving initialized tensors
ONNX: Session successfully initialized.
model has 1 input(s)
0 input name is Input3
   type ONNX_TYPE_TENSOR
   data type ONNX_DATA_TYPE_FLOAT
   shape [1, 1, 28, 28]
   shape of input data can be reduced to [28, 28]
model has 1 output(s)
0 output name is Plus214_Output_0
   type ONNX_TYPE_TENSOR
   data type ONNX_DATA_TYPE_FLOAT
   shape [1, 10]
   shape of output data can be reduced to [10]

Update: Due to the fact that the OnnxTypeInfo structure was changed in build 3700, the source code of the script has been modified.

Alternative:  Speedometer - indicator MetaTrader 5


https://www.mql5.com/ru/code/43452

📈 ROBOTFX MetaTrader Expert Advisors and Indicators to maximize profits and minimize the risks