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

조회 수: 4 (최근 30일)
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
Zhang Jen-You
Zhang Jen-You 2019년 5월 29일
편집: Zhang Jen-You 2019년 5월 30일
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]

댓글을 달려면 로그인하십시오.

답변 (2개)

Sivylla Paraskevopoulou
Sivylla Paraskevopoulou 2022년 5월 9일
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.

cui,xingxing
cui,xingxing 2019년 5월 30일
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.

카테고리

Help CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by