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