kraiskil / onnx2c

Open Neural Network Exchange to C compiler.
Other
184 stars 30 forks source link

Error: [Fatal] (createNode) Unimplemented: node operation LinearClassifier #48

Open pd-vt opened 2 months ago

pd-vt commented 2 months ago

Trying to generate model.c for very Simple classification model which just says input value is positive or negative. Please find attached model.onnx file inside the zip file.

model.zip

kraiskil commented 2 months ago

I'm looking at the list of ONNX operators: https://github.com/onnx/onnx/blob/main/docs/Operators.md, and there is no LinearClassifier. What is happening? How did you create that model?

pd-vt commented 2 months ago

The above attached model is been created using following simple python script. Our goal is to just make sure the microcontroller we have is capable to run such basic model and then if it is ok, we go for more complex system.

`

Import

import numpy as np from sklearn.linear_model import LogisticRegression from skl2onnx import convert_sklearn from skl2onnx.common.data_types import FloatTensorType

Generate some sample data (numbers) and their corresponding labels

numbers = np.array([-2, -1, 0, 1, 2, 3, -3, -4, 5, -5]) labels = np.array(['negative', 'negative', 'positive', 'positive', 'positive', 'positive', 'negative', 'negative', 'positive', 'negative'])

Reshape the data for compatibility with Scikit-learn

numbers = numbers.reshape(-1, 1)

Initialize and train a logistic regression model

model = LogisticRegression() model.fit(numbers, labels)

Define the input type for the ONNX model

initial_type = [('input', FloatTensorType([None, 1]))]

Convert the Scikit-learn model to ONNX format

onnx_model = convert_sklearn(model, initial_types=initial_type)

Save the ONNX model to a file

with open("model.onnx", "wb") as f: f.write(onnx_model.SerializeToString())

`

kraiskil commented 2 months ago

Ah, seems there are two layers of ONNX, the base neural networks and on top of this classical ML operands. https://github.com/onnx/onnx/blob/main/docs/Overview.md

As of now, onnx2c has only considered the NN part of ONNX.

A quick look at ONNX-ML suggests the structure of the opearators are similar in ONNX-ML and ONNX-NN, so implementing ONNX-ML operands might not require (m)any changes to onnx2c internals. I can't find any detailed info on the ML internals, nor any backend tests for the ML operands, though. But seeing the error in the title of this issue is encouraging - at least onnx2c hadn't crashed on ONNX-ML input...

Adding a new operand to onnx2c isn't too difficult. Much of the work would be to make a few tests for it, as ONNX doesn't provide any. PRs welcome :) https://github.com/kraiskil/onnx2c/blob/master/development.md#adding-a-new-nodeop-implementation