Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@unittest.skipIf(StrictVersion(ort_version) <= StrictVersion("0.4.0"),
reason="issues with shapes")
@unittest.skipIf(
not one_hot_encoder_supports_string(),
reason="OneHotEncoder did not have categories_ before 0.20",
)
def test_model_one_hot_encoder(self):
model = OneHotEncoder(categories='auto')
data = numpy.array([[1, 2, 3], [4, 3, 0], [0, 1, 4], [0, 5, 6]],
dtype=numpy.int64)
model.fit(data)
model_onnx = convert_sklearn(
model,
"scikit-learn one-hot encoder",
[("input", Int64TensorType([None, 3]))],
)
self.assertTrue(model_onnx is not None)
@unittest.skipIf(StrictVersion(ort_version) <= StrictVersion("0.4.0"),
reason="issues with shapes")
@unittest.skipIf(
not one_hot_encoder_supports_string(),
reason="OneHotEncoder does not support strings in 0.19",
)
def test_one_hot_encoder_twocats(self):
data = [["cat2"], ["cat1"]]
model = OneHotEncoder(categories="auto")
model.fit(data)
inputs = [("input1", StringTensorType([None, 1]))]
model_onnx = convert_sklearn(model, "one-hot encoder two string cats",
inputs)
self.assertTrue(model_onnx is not None)
dump_data_and_model(
data,
model,
StrictVersion(__version__) <= StrictVersion(THRESHOLD),
reason="Depends on PR #1015 onnxruntime.")
def test_gradient_boosting_classifier1Deviance(self):
model = GradientBoostingClassifier(n_estimators=1, max_depth=2)
X, y = make_classification(10, n_features=4, random_state=42)
X = X[:, :2]
model.fit(X, y)
for cl in [None, 0.231, 1e-6, 0.9]:
if cl is not None:
model.init_.class_prior_ = np.array([cl, cl])
initial_types = [('input', FloatTensorType((None, X.shape[1])))]
model_onnx = convert_sklearn(model, initial_types=initial_types)
if "Regressor" in str(model_onnx):
raise AssertionError(str(model_onnx))
sess = InferenceSession(model_onnx.SerializeToString())
res = sess.run(None, {'input': X.astype(np.float32)})
def test_kernel_rbf1(self):
ker = RBF(length_scale=1, length_scale_bounds=(1e-3, 1e3))
onx = convert_kernel(ker, 'X', output_names=['Y'], dtype=np.float32,
op_version=onnx_opset_version())
model_onnx = onx.to_onnx(
inputs=[('X', FloatTensorType([None, None]))])
sess = InferenceSession(model_onnx.SerializeToString())
res = sess.run(None, {'X': Xtest_.astype(np.float32)})[0]
m1 = res
m2 = ker(Xtest_)
assert_almost_equal(m1, m2, decimal=5)
model = Sequential()
model.add(Conv2D(2, kernel_size=(1, 2), strides=(1, 1), padding='valid', input_shape=(H, W, C),
data_format='channels_last')) # , activation='softmax')
model.add(MaxPooling2D((2, 2), strides=(2, 2), data_format='channels_last'))
model.compile(optimizer='sgd', loss='mse')
converted_model = onnxmltools.convert_keras(model, channel_first_inputs=[model.inputs[0].name])
expected = model.predict(x)
self.assertIsNotNone(expected)
self.assertIsNotNone(converted_model)
try:
import onnxruntime
sess = onnxruntime.InferenceSession(converted_model.SerializeToString())
actual = sess.run([], {sess.get_inputs()[0].name:
np.transpose(x.astype(np.float32), [0, 3, 1, 2])})
self.assertTrue(np.allclose(expected, actual, rtol=1.e-3))
except ImportError:
pass
assert nno[0].output_names == ['variable']
assert len(nva) == 1
assert isinstance(nva[0], tuple)
assert nva[0][1] == 0
def shape(operator):
N = operator.inputs[0].type.shape[0]
W = operator.raw_operator.W
operator.outputs[0].type.shape = [N, W.shape[0]]
model_onnx = convert_sklearn(
tr, 'a-sub', [('input', FloatTensorType([None, 2]))],
custom_shape_calculators={CustomOpTransformer: shape},
custom_conversion_functions={CustomOpTransformer: conv})
sess = InferenceSession(model_onnx.SerializeToString())
z2 = sess.run(None, {'input': mat.astype(np.float32)})[0]
assert_almost_equal(z, z2)
def _get_backend_version(self):
version = None
if self.backend == "onnxruntime":
import onnxruntime as ort
version = ort.__version__
elif self.backend == "caffe2":
# TODO: get caffe2 version
pass
if version:
version = LooseVersion(version)
return version
StrictVersion(ort_version) <= StrictVersion("0.4.0"),
reason="old onnxruntime does not support double")
def test_model_linear_regression64(self):
model, X = fit_regression_model(linear_model.LinearRegression())
model_onnx = convert_sklearn(model, "linear regression",
[("input", DoubleTensorType(X.shape))],
dtype=numpy.float64)
self.assertIsNotNone(model_onnx)
self.assertIn("elem_type: 11", str(model_onnx))
elif dataset == 'mnist':
input = np.array(test_input, dtype=np.float32).reshape([1, 28, 28, 1])
if is_onnx:
input = input.transpose(0, 3, 1, 2)
for name, shape in output_info:
out_node = helper.ValueInfoProto(type = helper.TypeProto())
out_node.name = name
out_node.type.tensor_type.elem_type = model.graph.output[0].type.tensor_type.elem_type
if len(shape)==4:
shape = [shape[0], shape[3], shape[1], shape[2]]
for dim_value in shape:
dim = out_node.type.tensor_type.shape.dim.add()
dim.dim_value = dim_value
model.graph.output.append(out_node)
runnable = rt.prepare(model, 'CPU')
pred = runnable.run(input)
#print(pred)
else:
if not (is_saved_tf_model or is_pb_file):
input = np.array(test_input, dtype=np.float32)
output_names = [e[0] for e in output_info]
pred = sess.run(get_out_tensors(output_names), {sess.graph.get_operations()[0].name + ':0': input})
#print(pred)
pred_eran = np.asarray([(i+j)/2 for i, j in zip(nlb[-1], nub[-1])])
pred_model = np.asarray(pred[-1]).reshape(-1)
if len(pred_eran) != len(pred_model):
tested_file.write(', '.join([dataset, network, domain, 'predictions have not the same number of labels. ERAN: ' + str(len(pred_eran)) + ' model: ' + str(len(pred_model))]) + '\n\n\n')
tested_file.flush()
continue
difference = pred_eran - pred_model
if np.all([abs(elem) < .001 for elem in difference]):
def get_onnxruntime_output(model, inputs, dtype='float32'):
import onnxruntime.backend
rep = onnxruntime.backend.prepare(model, 'CPU')
if isinstance(inputs, list) and len(inputs) > 1:
ort_out = rep.run(inputs)
else:
x = inputs.astype(dtype)
ort_out = rep.run(x)[0]
return ort_out