microsoft / microsoft/onnxruntime

sklearn SVM model converted into ONNX model but gives different output results

Open
#11,284 5 comments 0 reactions 1 assignee View on GitHub

@xadupre is already working on this.

Since Apr 20, 2022.

Dominant language
C++
Stars
21.9k
Forks
4.2k
Avg merge
4d 8h
Merged PRs (30d)
179

Description

I am using sklearn to train a SVM model and I converted it into ONNX model. However, when testing the two models with the same input, the outputs are different.

System information

  • OS Platform and Distribution: Windows 10 Enterprise 64 bit
  • ONNX Runtime installed from (source or binary): install using PIP from Python 3.9.7
  • ONNX Runtime version: 1.11.0
  • sklearn version: 1.0.2
  • Python version: 3.9.7
  • Visual Studio version (if applicable): N/A
  • GCC/Compiler version (if compiling from source): N/A
  • CUDA/cuDNN version: N/A
  • GPU model and memory: Not using GPU

To Reproduce

import pickle
from sklearn import svm
import numpy as np
from skl2onnx import convert_sklearn
from skl2onnx.common.data_types import FloatTensorType
import onnxruntime as rt
svm1_file = 'svm.model'
classifier_1 = pickle.load(open(svm1_file, 'rb'))
testingexample = [62, 62, 99, 99, 99, 99, 297, 297, 343, 343, 512, 0.0925, 262, 612, 322.6, 6.7963, 0.01993]
features = np.array([testingexample]).astype(np.float32)
result1_1 = classifier_1.predict(features)[0] #SKLEARN Result
result1_2 = classifier_1.predict_proba(features)[0]
print('svm1_file, SKLEARN result is %s, %s\n'%(str(result1_1), str(result1_2)))
 
#Convert into ONNX
initial_types = [('float_input',FloatTensorType([None, 17]))]
onx1 = convert_sklearn(classifier_1, initial_types = initial_types)
with open("Convert_svm.onnx", "wb") as f:
        f.write(onx1.SerializeToString())
sess1 = rt.InferenceSession(onx1.SerializeToString())
res1 = sess1.run(None, {'float_input': features.astype(np.float32)})
print(res1) #ONNX result
 

Sklearn result is 0.0, [0.99006934 0.00993066]
Onnx result is [array([0], dtype=int64), [{0: 0.9864753484725952, 1: 0.01352467481046915}]]

Expected behavior
The Sklearn model result should match the ONNX model result.
Onnx_and_sklearn_Models.zip

Is there anything wrong in my code when converting the SVM learned by using SKLEARN into ONNX format? Or, is this level of discrepancies expected?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.