mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-05-31 23:27:43 +00:00
update python examples (#1935)
This commit is contained in:
parent
a5a57a49c4
commit
2ecac41614
5 changed files with 73 additions and 37 deletions
|
|
@ -16,6 +16,7 @@ of a simple logistic regression model.
|
|||
"""
|
||||
import numpy as np
|
||||
from onnxruntime import datasets
|
||||
from onnxruntime.capi.onnxruntime_pybind11_state import InvalidArgument
|
||||
import onnxruntime.backend as backend
|
||||
from onnx import load
|
||||
|
||||
|
|
@ -24,9 +25,12 @@ model = load(name)
|
|||
|
||||
rep = backend.prepare(model, 'CPU')
|
||||
x = np.array([[-1.0, -2.0]], dtype=np.float32)
|
||||
label, proba = rep.run(x)
|
||||
print("label={}".format(label))
|
||||
print("probabilities={}".format(proba))
|
||||
try:
|
||||
label, proba = rep.run(x)
|
||||
print("label={}".format(label))
|
||||
print("probabilities={}".format(proba))
|
||||
except (RuntimeError, InvalidArgument) as e:
|
||||
print(e)
|
||||
|
||||
########################################
|
||||
# The device depends on how the package was compiled,
|
||||
|
|
@ -40,9 +44,12 @@ print(get_device())
|
|||
|
||||
rep = backend.prepare(name, 'CPU')
|
||||
x = np.array([[-1.0, -2.0]], dtype=np.float32)
|
||||
label, proba = rep.run(x)
|
||||
print("label={}".format(label))
|
||||
print("probabilities={}".format(proba))
|
||||
try:
|
||||
label, proba = rep.run(x)
|
||||
print("label={}".format(label))
|
||||
print("probabilities={}".format(proba))
|
||||
except (RuntimeError, InvalidArgument) as e:
|
||||
print(e)
|
||||
|
||||
#######################################
|
||||
# The backend API is implemented by other frameworks
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ trained on *Iris* datasets. The model takes
|
|||
a vector of dimension 2 and returns a class among three.
|
||||
"""
|
||||
import onnxruntime as rt
|
||||
from onnxruntime.capi.onnxruntime_pybind11_state import InvalidArgument
|
||||
import numpy
|
||||
from onnxruntime.datasets import get_example
|
||||
|
||||
|
|
@ -53,9 +54,12 @@ except Exception as e:
|
|||
# and *onnxruntime* will then return all the outputs.
|
||||
|
||||
x = numpy.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], dtype=numpy.float32)
|
||||
res = sess.run(None, {input_name: x})
|
||||
print("All outputs")
|
||||
print(res)
|
||||
try:
|
||||
res = sess.run(None, {input_name: x})
|
||||
print("All outputs")
|
||||
print(res)
|
||||
except (RuntimeError, InvalidArgument) as e:
|
||||
print(e)
|
||||
|
||||
#########################
|
||||
# The same goes if the input name is misspelled.
|
||||
|
|
@ -78,8 +82,11 @@ for x in [
|
|||
numpy.array([1.0, 2.0, 3.0], dtype=numpy.float32),
|
||||
numpy.array([[1.0, 2.0, 3.0]], dtype=numpy.float32),
|
||||
]:
|
||||
r = sess.run([output_name], {input_name: x})
|
||||
print("Shape={0} and predicted labels={1}".format(x.shape, r))
|
||||
try:
|
||||
r = sess.run([output_name], {input_name: x})
|
||||
print("Shape={0} and predicted labels={1}".format(x.shape, r))
|
||||
except (RuntimeError, InvalidArgument) as e:
|
||||
print("ERROR with Shape={0} - {1}".format(x.shape, e))
|
||||
|
||||
for x in [
|
||||
numpy.array([1.0, 2.0, 3.0, 4.0], dtype=numpy.float32),
|
||||
|
|
@ -88,8 +95,11 @@ for x in [
|
|||
numpy.array([1.0, 2.0, 3.0], dtype=numpy.float32),
|
||||
numpy.array([[1.0, 2.0, 3.0]], dtype=numpy.float32),
|
||||
]:
|
||||
r = sess.run(None, {input_name: x})
|
||||
print("Shape={0} and predicted probabilities={1}".format(x.shape, r[1]))
|
||||
try:
|
||||
r = sess.run(None, {input_name: x})
|
||||
print("Shape={0} and predicted probabilities={1}".format(x.shape, r[1]))
|
||||
except (RuntimeError, InvalidArgument) as e:
|
||||
print("ERROR with Shape={0} - {1}".format(x.shape, e))
|
||||
|
||||
#########################
|
||||
# It does not fail either if the number of dimension
|
||||
|
|
@ -100,5 +110,8 @@ for x in [
|
|||
numpy.array([[[1.0, 2.0, 3.0]]], dtype=numpy.float32),
|
||||
numpy.array([[[1.0, 2.0]], [[3.0, 4.0]]], dtype=numpy.float32),
|
||||
]:
|
||||
r = sess.run([output_name], {input_name: x})
|
||||
print("Shape={0} and predicted labels={1}".format(x.shape, r))
|
||||
try:
|
||||
r = sess.run([output_name], {input_name: x})
|
||||
print("Shape={0} and predicted labels={1}".format(x.shape, r))
|
||||
except (RuntimeError, InvalidArgument) as e:
|
||||
print("ERROR with Shape={0} - {1}".format(x.shape, e))
|
||||
|
|
|
|||
|
|
@ -70,6 +70,8 @@ with open("pipeline_vectorize.onnx", "wb") as f:
|
|||
# We load the model with ONNX Runtime and look at
|
||||
# its input and output.
|
||||
import onnxruntime as rt
|
||||
from onnxruntime.capi.onnxruntime_pybind11_state import InvalidArgument
|
||||
|
||||
sess = rt.InferenceSession("pipeline_vectorize.onnx")
|
||||
|
||||
import numpy
|
||||
|
|
@ -83,7 +85,7 @@ print("output name='{}' and shape={} and type={}".format(out.name, out.shape, ou
|
|||
|
||||
try:
|
||||
pred_onx = sess.run([out.name], {inp.name: X_test_dict})[0]
|
||||
except RuntimeError as e:
|
||||
except (RuntimeError, InvalidArgument) as e:
|
||||
print(e)
|
||||
|
||||
#############################
|
||||
|
|
|
|||
|
|
@ -43,40 +43,52 @@ plt.axis('off')
|
|||
#############################################
|
||||
# Let's load the model with onnxruntime.
|
||||
import onnxruntime as rt
|
||||
from onnxruntime.capi.onnxruntime_pybind11_state import InvalidGraph
|
||||
|
||||
sess = rt.InferenceSession('dense121.onnx')
|
||||
try:
|
||||
sess = rt.InferenceSession('dense121.onnx')
|
||||
ok = True
|
||||
except (InvalidGraph, TypeError, RuntimeError) as e:
|
||||
# Probably a mismatch between onnxruntime and onnx version.
|
||||
print(e)
|
||||
ok = False
|
||||
|
||||
print("The model expects input shape:", sess.get_inputs()[0].shape)
|
||||
print("image shape:", ximg.shape)
|
||||
if ok:
|
||||
print("The model expects input shape:", sess.get_inputs()[0].shape)
|
||||
print("image shape:", ximg.shape)
|
||||
|
||||
#######################################
|
||||
# Let's resize the image.
|
||||
from skimage.transform import resize
|
||||
import numpy
|
||||
|
||||
ximg224 = resize(ximg / 255, (224, 224, 3), anti_aliasing=True)
|
||||
ximg = ximg224[numpy.newaxis, :, :, :]
|
||||
ximg = ximg.astype(numpy.float32)
|
||||
if ok:
|
||||
from skimage.transform import resize
|
||||
import numpy
|
||||
|
||||
print("new shape:", ximg.shape)
|
||||
ximg224 = resize(ximg / 255, (224, 224, 3), anti_aliasing=True)
|
||||
ximg = ximg224[numpy.newaxis, :, :, :]
|
||||
ximg = ximg.astype(numpy.float32)
|
||||
|
||||
print("new shape:", ximg.shape)
|
||||
|
||||
##################################
|
||||
# Let's compute the output.
|
||||
|
||||
input_name = sess.get_inputs()[0].name
|
||||
res = sess.run(None, {input_name: ximg})
|
||||
prob = res[0]
|
||||
print(prob.ravel()[:10]) # Too big to be displayed.
|
||||
if ok:
|
||||
input_name = sess.get_inputs()[0].name
|
||||
res = sess.run(None, {input_name: ximg})
|
||||
prob = res[0]
|
||||
print(prob.ravel()[:10]) # Too big to be displayed.
|
||||
|
||||
|
||||
##################################
|
||||
# Let's get more comprehensive results.
|
||||
|
||||
from keras.applications.densenet import decode_predictions
|
||||
decoded = decode_predictions(prob)
|
||||
if ok:
|
||||
from keras.applications.densenet import decode_predictions
|
||||
decoded = decode_predictions(prob)
|
||||
|
||||
import pandas
|
||||
df = pandas.DataFrame(decoded[0], columns=["class_id", "name", "P"])
|
||||
print(df)
|
||||
import pandas
|
||||
df = pandas.DataFrame(decoded[0], columns=["class_id", "name", "P"])
|
||||
print(df)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ print(confusion_matrix(y_test, pred))
|
|||
from skl2onnx import convert_sklearn
|
||||
from skl2onnx.common.data_types import FloatTensorType
|
||||
|
||||
initial_type = [('float_input', FloatTensorType([1, 4]))]
|
||||
initial_type = [('float_input', FloatTensorType([None, 4]))]
|
||||
onx = convert_sklearn(clr, initial_types=initial_type)
|
||||
with open("logreg_iris.onnx", "wb") as f:
|
||||
f.write(onx.SerializeToString())
|
||||
|
|
@ -66,8 +66,10 @@ with open("logreg_iris.onnx", "wb") as f:
|
|||
import onnxruntime as rt
|
||||
sess = rt.InferenceSession("logreg_iris.onnx")
|
||||
|
||||
print("input name='{}' and shape={}".format(sess.get_inputs()[0].name, sess.get_inputs()[0].shape))
|
||||
print("output name='{}' and shape={}".format(sess.get_outputs()[0].name, sess.get_outputs()[0].shape))
|
||||
print("input name='{}' and shape={}".format(
|
||||
sess.get_inputs()[0].name, sess.get_inputs()[0].shape))
|
||||
print("output name='{}' and shape={}".format(
|
||||
sess.get_outputs()[0].name, sess.get_outputs()[0].shape))
|
||||
|
||||
##################################
|
||||
# We compute the predictions.
|
||||
|
|
|
|||
Loading…
Reference in a new issue