This repository has been archived by the owner on Jan 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
|
||
.. blogpost:: | ||
:title: ONNX from C# | ||
:keywords: ONNX, C# | ||
:date: 2021-07-09 | ||
:categories: runtime | ||
|
||
This example shows how to compute the predictions of a model | ||
using C#. | ||
|
||
:: | ||
|
||
using System.Collections.Generic; | ||
using Microsoft.ML.OnnxRuntime; | ||
using Microsoft.ML.OnnxRuntime.Tensors; | ||
|
||
namespace ConsoleAppOnnx | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
// Loads the model. | ||
var opts = new SessionOptions(); | ||
string model_path = "model.onnx"; | ||
var session = new InferenceSession(model_path, opts); | ||
|
||
// Creating an input tensor (assuming there is only one). | ||
// Get the name of the input and the number of features. | ||
string name = string.Empty; | ||
int n_features = -1; | ||
foreach (var inp in session.InputMetadata) | ||
{ | ||
name = inp.Key; | ||
n_features = inp.Value.Dimensions[1]; | ||
break; | ||
} | ||
|
||
// Creates an empty input. | ||
var dims = new int[] { 1, n_features }; | ||
var t = new DenseTensor<float>(dims); | ||
for (int i = 0; i < dims[1]; ++i) | ||
t.SetValue(i, 1.0f / (dims[1] + 1)); | ||
var tensor = NamedOnnxValue.CreateFromTensor(name, t); | ||
|
||
// Runs the inference. | ||
var inputs = new List<NamedOnnxValue>() { tensor }; | ||
using (var outputs = session.Run(inputs)) | ||
{ | ||
foreach (var o in outputs) | ||
{ | ||
DenseTensor<float> to = o.AsTensor<float>().ToDenseTensor(); | ||
var values = new float[to.Length]; | ||
to.Buffer.CopyTo(values); | ||
// values contains the results. | ||
foreach (var i in values) | ||
System.Console.Write(string.Format("{0}, ", i)); | ||
System.Console.WriteLine(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
.. blogpost:: | ||
:title: Convert a Lightgbm dump | ||
:keywords: ONNX, lightgbm, onnxmltools | ||
:date: 2021-07-09 | ||
:categories: converters | ||
|
||
This example shows how to convert a :epkg:`lightgbm` model | ||
dumped as a text file. It uses :epkg:`lightgbm` to restore | ||
the model, converts it and checks the discrepencies. | ||
|
||
:: | ||
|
||
import numpy | ||
from numpy.testing import assert_almost_equal | ||
import lightgbm | ||
from onnxruntime import InferenceSession | ||
from onnxmltools import convert_lightgbm | ||
from skl2onnx.common.data_types import FloatTensorType | ||
|
||
booster = lightgbm.Booster(model_file="model.txt") | ||
n = booster.num_feature() | ||
|
||
onx = convert_lightgbm(booster, initial_types=[('input', FloatTensorType([None, n]))]) | ||
|
||
sess = InferenceSession(onx.SerializeToString()) | ||
rnd = numpy.random.random((1, n)).astype(numpy.float32) | ||
|
||
expected = booster.predict(rnd) | ||
got = sess.run(None, {'input': rnd})[0] | ||
|
||
assert_almost_equal(expected, got.ravel(), decimal=4) |