Example of using ONNX model to recognize handwritten digits - MetaTrader 5 Expert - MT4/MT5 Resources


This MNIST database contains 60,000 training images and 10,000 test images. The images were created by "remixing" a set of original NIST 20x20 pixel black and white samples, which in turn were obtained from the U.S. Census Bureau, supplemented by test samples collected from U.S. high school students. The samples are normalized to 28x28 pixel size and anti-aliased, thereby introducing grayscale levels.
The trained handwritten digit recognition model mnist.onnx downloads the model zoo (opset 8) from Github. Interested parties can download and try other models, excluding the model with opset 1, which is no longer supported by the latest ONNX runtime. Surprisingly, the output vector is not processed with a soft max activation function, which is common in classification models. Well, that’s not a problem because we can easily implement it ourselves.
Integer prediction number ( blank ) { Still matrix f image ( 28 , 28 ); Stationary vector f results ( 10 ); prepareMatrix(image); if (! Onnx run (external model, ONNX_DEFAULT , image, result)) { print (" Onnx running error", get last error ()); return (- 1 ); } result.activation(result, AF_SOFTMAX ); integer prediction = integer (result.ArgMax()); if (result[prediction]< 0.8 ) print (result); print ("value",prediction,"prediction with probability",result[prediction]); return (prediction); }
Use your mouse to draw numbers in a special grid, holding down the left mouse button. To identify the drawn numbers, press the sort button.

If the obtained probability of the recognized digit is less than 0.8, print the resulting vector containing the probabilities for each category to the log. For example, try to classify an empty unfilled input field.
[ 0.095331445 , 0.10048489 , 0.10673151 , 0.10274081 , 0.087865397 , 0.11471312 , 0.094342403 , 0.094900772 , 0.10847695 , 0.09441267 ] Value 5 is predicted with probability 0.11471312493085861For some reason, the recognition accuracy for the number nine (9) is significantly lower. Numbers tilted to the left are recognized more accurately.
Attachment download
📎testmnistonnx.mq5 (8.36 KB)
📎 mnist.onnx (25.83 KB)
Source: MQL5 #47225
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •