def test_initializers(self): context = ConvertContext() # create nodes with initializers mb = ModelBuilder() nb = NodeBuilder(context, 'bar') nb.add_input('Input') nb.add_output('Output') test_array = [1, 2, 3] tensor = model_util.make_tensor('classes', onnx_proto.TensorProto.FLOAT, [1, len(test_array)], test_array) nb.add_initializer(tensor) node = nb.make_node() mb.add_nodes([node.onnx_node]) mb.add_initializers(node.initializers) mb.add_inputs([ model_util.make_tensor_value_info('Input', onnx_proto.TensorProto.FLOAT, [1]) ]) mb.add_outputs([ model_util.make_tensor_value_info('Output', onnx_proto.TensorProto.FLOAT, [1]) ]) model = mb.make_model() self.assertEqual(len(model.graph.initializer), 1) self.assertEqual(model.graph.initializer[0].name, 'bar.classes')
def test_combine_inputs_with_string(self): context = ConvertContext() input1 = model_util.make_tensor_value_info( "foo", onnx_proto.TensorProto.FLOAT, [1, 3]) input2 = model_util.make_tensor_value_info( "foo2", onnx_proto.TensorProto.STRING, [1, 2]) inputs = [input1, input2] self.assertRaises(RuntimeError, _combine_inputs, context, inputs)
def test_add_inputs(self): context = ConvertContext() nb = NodeBuilder(context, "foo") nb.add_input('test') nb.add_empty_input() nb.add_input( model_util.make_tensor_value_info('value_test', onnx_proto.TensorProto.FLOAT, [1, 3])) test_array = [1, 2, 3] init = model_util.make_tensor('init', onnx_proto.TensorProto.FLOAT, [1, len(test_array)], test_array) nb.add_initializer(init) value = model_util.make_tensor('value', onnx_proto.TensorProto.FLOAT, [1, len(test_array)], test_array) nb.add_value(value) node = nb.make_node() input_names = node.input_names self.assertEqual(len(input_names), 5) # Confirm the order of the names based upon when added expected_names = ['test', '', 'value_test', 'foo.init', 'foo.value'] self.assertEqual(input_names, expected_names)
def test_combine_inputs(self): context = ConvertContext() input1 = model_util.make_tensor_value_info( "foo", onnx_proto.TensorProto.FLOAT, [1, 3]) input2 = model_util.make_tensor_value_info( "foo2", onnx_proto.TensorProto.FLOAT, [1, 2]) inputs = (input1, input2) nodes = _combine_inputs(context, inputs) self.assertEqual(len(nodes), 1) self.assertEqual(len(nodes[0].outputs), 1) self.assertEqual( nodes[0].outputs[0].type.tensor_type.shape.dim[0].dim_value, 1) self.assertEqual( nodes[0].outputs[0].type.tensor_type.shape.dim[1].dim_value, 5)
def test_binarizer_converter(self): model = Binarizer(threshold=0.5) context = ConvertContext() node = BinarizerConverter.convert(context, model, [ make_tensor_value_info('input', onnx_proto.TensorProto.FLOAT, [4]) ]) self.assertTrue(node is not None)
def test_combine_inputs_floats_ints(self): context = ConvertContext() input1 = model_util.make_tensor_value_info( "foo", onnx_proto.TensorProto.FLOAT, [1, 3]) input2 = model_util.make_tensor_value_info( "foo2", onnx_proto.TensorProto.INT64, [1, 2]) input3 = model_util.make_tensor_value_info( "foo3", onnx_proto.TensorProto.UINT8, [1, 4]) inputs = [input1, input2, input3] nodes = _combine_inputs(context, inputs) self.assertEqual(len(nodes), 3) self.assertEqual( nodes[-1].outputs[0].type.tensor_type.shape.dim[0].dim_value, 1) self.assertEqual( nodes[-1].outputs[0].type.tensor_type.shape.dim[1].dim_value, 9)
def test_glm_regressor_converter(self): model = self._fit_model(linear_model.LinearRegression()) context = ConvertContext() node = GLMRegressorConverter.convert(context, model, [ make_tensor_value_info('feature', onnx_proto.TensorProto.FLOAT, [1, 4]) ]) self.assertIsNotNone(node)
def test_normalizer_converter(self): model = Normalizer(norm='l2') context = ConvertContext() node = NormalizerConverter.convert(context, model, [ make_tensor_value_info('feature1', onnx_proto.TensorProto.INT64, [1]) ]) self.assertTrue(node is not None)
def test_label_encoder_converter(self): model = LabelEncoder() model.fit(['str3', 'str2', 'str0', 'str1', 'str3']) context = ConvertContext() node = LabelEncoderConverter.convert(context, model, [ make_tensor_value_info('Input', onnx_proto.TensorProto.STRING, [1]) ]) self.assertTrue(node is not None)
def test_glm_classifier_converter(self): model = self._fit_model_binary_classification( linear_model.LogisticRegression()) context = ConvertContext() node = GLMClassifierConverter.convert(context, model, [ make_tensor_value_info('feature', onnx_proto.TensorProto.FLOAT, [1, 3]) ]) self.assertIsNotNone(node)
def test_scaler_converter(self): model = StandardScaler() model.fit([[0, 0, 3], [1, 1, 0], [0, 2, 1], [1, 0, 2]]) context = ConvertContext() model_onnx = ScalerConverter.convert(context, model, [ make_tensor_value_info('features', onnx_proto.TensorProto.INT64, [3]) ]) self.assertTrue(model_onnx is not None)
def test_imputer_int_inputs(self): model = Imputer(missing_values='NaN', strategy='mean', axis=0) model.fit([[1, 2], [np.nan, 3], [7, 6]]) context = ConvertContext() node = ImputerConverter.convert(context, model, [ make_tensor_value_info('features', onnx_proto.TensorProto.INT32, [2]) ]) self.assertTrue(node is not None) # should contain two nodes self.assertEqual(len(node), 2) # last node should contain the Imputer outputs = node[-1].outputs self.assertEqual(len(outputs), 1) self.assertEqual(outputs[0].type.tensor_type.shape.dim[-1].dim_value, 2)
class TestSklearnSVM(unittest.TestCase): defaultInput = [model_util.make_tensor_value_info('input', onnx_proto.TensorProto.FLOAT, [1])] def _fit_binary_classification(self, model): iris = load_iris() X = iris.data[:, :3] y = iris.target y[y == 2] = 1 model.fit(X, y) return model def _fit_multi_classification(self, model): iris = load_iris() X = iris.data[:, :3] y = iris.target model.fit(X, y) return model def _check_attributes(self, node, attribute_test): attributes = node.attributes attribute_map = {} for attribute in attributes: attribute_map[attribute.name] = attribute for k, v in attribute_test.items(): self.assertTrue(k in attribute_map) if v is not None: attrib = attribute_map[k] if isinstance(v, str): self.assertEqual(attrib.s, v.encode(encoding='UTF-8')) elif isinstance(v, int): self.assertEqual(attrib.i, v) elif isinstance(v, float): self.assertEqual(attrib.f, v) elif isinstance(v, list): self.assertEqual(attrib.ints, v) else: self.fail('Unknown type') def _check_outputs(self, node, output_names): outputs = node.outputs output_map = {} for output in outputs: output_map[output.name] = output for name in output_names: self.assertTrue(name in output_map) def test_convert_svmc_linear_binary(self): model = self._fit_binary_classification(SVC(kernel='linear', probability=False)) context = ConvertContext() nodes = SVCConverter.convert(context, model, self.defaultInput) self.assertIsNotNone(nodes) self.assertEqual(len(nodes), 2) svc_node = nodes[0] self._check_attributes(svc_node, {'coefficients': None, 'kernel_params': None, 'kernel_type': 'LINEAR', 'post_transform': None, 'rho': None, 'support_vectors': None, 'vectors_per_class': None}) self._check_outputs(svc_node, [svc_node.name]) def test_convert_svmc_linear_multi(self): model = self._fit_multi_classification(SVC(kernel='linear', probability=False)) context = ConvertContext() nodes = SVCConverter.convert(context, model, self.defaultInput) self.assertIsNotNone(nodes) self.assertEqual(len(nodes), 2) svc_node = nodes[0] self._check_attributes(svc_node, {'coefficients': None, 'kernel_params': None, 'kernel_type': 'LINEAR', 'post_transform': None, 'rho': None, 'support_vectors': None, 'vectors_per_class': None}) self._check_outputs(svc_node, [svc_node.name]) def test_convert_svmr_linear_binary(self): model = self._fit_binary_classification(SVR(kernel='linear')) context = ConvertContext() node = SVRConverter.convert(context, model, self.defaultInput) self.assertIsNotNone(node) self._check_attributes(node, {'coefficients': None, 'kernel_params': None, 'kernel_type': 'LINEAR', 'post_transform': None, 'rho': None, 'support_vectors': None}) self._check_outputs(node, [node.name]) def test_convert_svmr_linear_multi(self): model = self._fit_multi_classification(SVR(kernel='linear')) context = ConvertContext() node = SVRConverter.convert(context, model, self.defaultInput) self.assertIsNotNone(node) self._check_attributes(node, {'coefficients': None, 'kernel_params': None, 'kernel_type': 'LINEAR', 'post_transform': None, 'rho': None, 'support_vectors': None}) self._check_outputs(node, [node.name]) def test_convert_nusvmc_binary(self): model = self._fit_binary_classification(NuSVC(probability=False)) context = ConvertContext() nodes = SVCConverter.convert(context, model, self.defaultInput) self.assertIsNotNone(nodes) self.assertEqual(len(nodes), 2) svc_node = nodes[0] self._check_attributes(svc_node, {'coefficients': None, 'kernel_params': None, 'kernel_type': 'RBF', 'post_transform': None, 'rho': None, 'support_vectors': None, 'vectors_per_class': None}) self._check_outputs(svc_node, [svc_node.name]) def test_convert_nusvmc_multi(self): model = self._fit_multi_classification(NuSVC(probability=False)) context = ConvertContext() nodes = SVCConverter.convert(context, model, self.defaultInput) self.assertIsNotNone(nodes) self.assertEqual(len(nodes), 2) svc_node = nodes[0] self._check_attributes(svc_node, {'coefficients': None, 'kernel_params': None, 'kernel_type': 'RBF', 'post_transform': None, 'rho': None, 'support_vectors': None, 'vectors_per_class': None}) self._check_outputs(svc_node, [svc_node.name]) def test_convert_nusvmr_binary(self): model = self._fit_binary_classification(NuSVR()) context = ConvertContext() node = SVRConverter.convert(context, model, self.defaultInput) self.assertIsNotNone(node) self._check_attributes(node, {'coefficients': None, 'kernel_params': None, 'kernel_type': 'RBF', 'post_transform': None, 'rho': None, 'support_vectors': None}) def test_convert_nusvmr_multi(self): model = self._fit_multi_classification(NuSVR()) context = ConvertContext() node = SVRConverter.convert(context, model, self.defaultInput) self.assertIsNotNone(node) self._check_attributes(node, {'coefficients': None, 'kernel_params': None, 'kernel_type': 'RBF', 'post_transform': None, 'rho': None, 'support_vectors': None}) def test_registration_convert_nusvr_model(self): model = self._fit_binary_classification(NuSVR()) model_onnx = onnxmltools.convert_sklearn(model) self.assertIsNotNone(model_onnx) def test_registration_convert_nusvc_model(self): model = self._fit_multi_classification(NuSVC(probability=False)) model_onnx = onnxmltools.convert_sklearn(model) self.assertIsNotNone(model_onnx) def test_registration_convert_svr_model(self): model = self._fit_multi_classification(SVR(kernel='linear')) model_onnx = onnxmltools.convert_sklearn(model) self.assertIsNotNone(model_onnx) def test_registration_convert_svc_model(self): model = self._fit_binary_classification(SVC(kernel='linear', probability=False)) model_onnx = onnxmltools.convert_sklearn(model) self.assertIsNotNone(model_onnx)