Import ONNX format custom googlenet model into MATLAB and Python. And test the same image, but get the different result.
Show older comments
Hello,
I train a custom googlenet model by MATLAB, but I need to use it in python. So I export the model into ONNX format and import it into python.
Then I test an image in python and get a result. I also import ONNX model into MATLAB to test the same image, but the result is different.
The result in MATLAB is correct!
I have no idea why I use the same model and same image, but get a different result between MATLAB and Python.
Can someone help me?
Thanks!
My ONNX format model on google drive (about 23MB)
MATLAB code
img=imread('24.png');
net = importONNXNetwork('cookie_bag_20190505.onnx', 'OutputLayerType', 'classification');
[YPred,scores]=classify(net,img)
MATLAB result
scores = [0.0000 0.0000 0.0000 1.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000]
YPred = 4
Python code
import onnx
import numpy as np
import cv2
from onnx_tf.backend import prepare
#import onnx format model
model = onnx.load('cookie_bag_20190505.onnx')
tf_rep = prepare(model)
#read image
img = cv2.imread('24.png')
img = np.moveaxis(img, -1,0)
#run predict
output=tf_rep.run(img[np.newaxis,:,:,:])
print("outpu mat: \n",output)
print("The digit is classified as ", np.argmax(output)+1)
Python result
outpu mat:
Outputs(prob=array([[6.5663549e-08, 4.3900876e-04, 8.8683555e-06, 9.8281691e-04,
3.4135218e-07, 1.5289265e-03, 1.3246261e-09, 2.7378071e-06,
2.1950313e-01, 7.7753413e-01]], dtype=float32))
The digit is classified as 10
1 Comment
Zhang Jen-You
on 29 May 2019
Edited: Zhang Jen-You
on 30 May 2019
Answers (2)
Sivylla Paraskevopoulou
on 9 May 2022
1 vote
The Inference Comparison Between ONNX and Imported Networks for Image Classification example shows how to compare image classification results between an ONNX model and a MATLAB network.
xingxingcui
on 30 May 2019
0 votes
Can you please use the opencv dnn library to make the same result, without calling the onnx_tf.backend library?
Categories
Find more on Deep Learning with Simulink in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!