Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def supports_device(cls, device):
"""
Check whether the backend is compiled with particular device support.
In particular it's used in the testing suite.
"""
return device in get_device()
name = datasets.get_example("logreg_iris.onnx")
model = load(name)
rep = backend.prepare(model, 'CPU')
x = np.array([[-1.0, -2.0, 5.0, 6.0],
[-1.0, -2.0, -3.0, -4.0],
[-1.0, -2.0, 7.0, 8.0]],
dtype=np.float32)
label, proba = rep.run(x)
print("label={}".format(label))
print("probabilities={}".format(proba))
########################################
# The device depends on how the package was compiled,
# GPU or CPU.
print(get_device())
########################################
# The backend can also directly load the model
# without using *onnx*.
rep = backend.prepare(name, 'CPU')
x = np.array([[-1.0, -2.0, -3.0, -4.0],
[-1.0, -2.0, -3.0, -4.0],
[-1.0, -2.0, -3.0, -4.0]],
dtype=np.float32)
label, proba = rep.run(x)
print("label={}".format(label))
print("probabilities={}".format(proba))
#######################################
# The backend API is implemented by other frameworks
model = load(name)
rep = backend.prepare(model, 'CPU')
x = np.array([[-1.0, -2.0]], dtype=np.float32)
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,
# GPU or CPU.
from onnxruntime import get_device
print(get_device())
########################################
# The backend can also directly load the model
# without using *onnx*.
rep = backend.prepare(name, 'CPU')
x = np.array([[-1.0, -2.0]], dtype=np.float32)
try:
label, proba = rep.run(x)
print("label={}".format(label))
print("probabilities={}".format(proba))
except (RuntimeError, InvalidArgument) as e:
print(e)
model = load(name)
rep = backend.prepare(model, 'CPU')
x = np.array([[-1.0, -2.0]], dtype=np.float32)
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,
# GPU or CPU.
from onnxruntime import get_device
print(get_device())
########################################
# The backend can also directly load the model
# without using *onnx*.
rep = backend.prepare(name, 'CPU')
x = np.array([[-1.0, -2.0]], dtype=np.float32)
try:
label, proba = rep.run(x)
print("label={}".format(label))
print("probabilities={}".format(proba))
except (RuntimeError, InvalidArgument) as e:
print(e)
def is_compatible(cls, model, device=None, **kwargs):
"""
Return whether the model is compatible with the backend.
:param model: unused
:param device: None to use the default device or a string (ex: `'CPU'`)
:return: boolean
"""
if device is None:
device = get_device()
return cls.supports_device(device)
"""
if isinstance(model, OnnxRuntimeBackendRep):
return model
elif isinstance(model, InferenceSession):
return OnnxRuntimeBackendRep(model)
elif isinstance(model, (str, bytes)):
options = SessionOptions()
for k, v in kwargs.items():
if hasattr(options, k):
setattr(options, k, v)
inf = InferenceSession(model, options)
# backend API is primarily used for ONNX test/validation. As such, we should disable session.run() fallback
# which may hide test failures.
inf.disable_fallback()
if device is not None and not cls.supports_device(device):
raise RuntimeError("Incompatible device expected '{0}', got '{1}'".format(device, get_device()))
return cls.prepare(inf, device, **kwargs)
else:
# type: ModelProto
check_model(model)
bin = model.SerializeToString()
return cls.prepare(bin, device, **kwargs)