MATLAB Answers

0

Import ONNX format custom googlenet model into MATLAB and Python. And test the same image, but get the different result.

Asked by Zhang Jen-You on 29 May 2019
Latest activity Answered by cui
on 30 May 2019
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

I already know the answer...
The problem is very simple.
Because MATLAB read image as RGB, but opencv read image as BGR.
So the model which I trained by MATLAB, if want to use it on python, we need to convert image from BGR to RGB.
Then use image to test the model which import from onnx, can get the same result as MATLAB.
The key point is that we need to know model trained by which image color format.
image BGR to RGB Python code like this
img = cv2.imread('24.png')
img= img[:,:,::-1]

Sign in to comment.

Tags

Products


Release

R2018b

1 Answer

Answer by cui
on 30 May 2019

Can you please use the opencv dnn library to make the same result, without calling the onnx_tf.backend library?
My similar question link is here.

  0 Comments

Sign in to comment.