Exemple #1
0
 def test_rename_output(self):
     rename_feature(self.spec, 'output', 'renamed_output', rename_inputs=False, rename_outputs=True)
     model = MLModel(self.spec)
     preds = model.predict({'feature_1': 1.0, 'feature_2': 1.0})
     self.assertIsNotNone(preds)
     self.assertEquals(preds['renamed_output'], 3.1)
     rename_feature(self.spec, 'renamed_output', 'output', rename_inputs=False, rename_outputs=True)
Exemple #2
0
    def test_rename_feature_mlprogram(self):
        @mb.program(input_specs=[mb.TensorSpec(shape=(3, ))])
        def linear_prog(input):
            W = np.ones((10, 3), dtype=np.float)
            out = mb.linear(x=input, weight=W, name="output")
            return out

        model = coremltools.convert(linear_prog, convert_to='mlprogram')

        spec = model.get_spec()
        input_name = spec.description.input[0].name
        output_name = spec.description.output[0].name

        # rename input
        rename_feature(spec, input_name, "new_input_name")
        self.assertEqual(spec.description.input[0].name, "new_input_name")
        model = coremltools.models.MLModel(spec, weights_dir=model.weights_dir)
        out = model.predict({"new_input_name": np.array([1.0, 2.0,
                                                         3.0])})[output_name]
        self.assertEqual(out.shape, (10, ))
        self.assertEqual(out[0], 6.0)

        # rename output
        rename_feature(spec, output_name, "new_output_name")
        self.assertEqual(spec.description.output[0].name, "new_output_name")
        model = coremltools.models.MLModel(spec, weights_dir=model.weights_dir)
        out = model.predict({"new_input_name":
                             np.array([1.0, 2.0, 3.0])})["new_output_name"]
        self.assertEqual(out.shape, (10, ))
        self.assertEqual(out[1], 6.0)
    def test_rename_input(self):
        rename_feature(self.spec,
                       "feature_1",
                       "renamed_feature",
                       rename_inputs=True)
        model = MLModel(self.spec)

        package = tempfile.TemporaryDirectory(suffix=".mlpackage")
        package.cleanup()

        model.save(package.name)
        loaded_model = MLModel(package.name)

        if utils._macos_version() >= (12, 0):
            preds = loaded_model.predict({
                "renamed_feature": 1.0,
                "feature_2": 1.0
            })
            self.assertIsNotNone(preds)
            self.assertEqual(preds["output"], 3.1)

        # reset the spec for next run
        rename_feature(self.spec,
                       "renamed_feature",
                       "feature_1",
                       rename_inputs=True)

        # cleanup
        MLModelTest._remove_path(package.name)
Exemple #4
0
 def test_rename_output_bad(self):
     rename_feature(
         self.spec, "blah", "bad_name", rename_inputs=False, rename_outputs=True
     )
     model = MLModel(self.spec)
     preds = model.predict({"feature_1": 1.0, "feature_2": 1.0})
     self.assertIsNotNone(preds)
     self.assertEqual(preds["output"], 3.1)
Exemple #5
0
 def test_rename_input(self):
     rename_feature(self.spec, "feature_1", "renamed_feature", rename_inputs=True)
     model = MLModel(self.spec)
     preds = model.predict({"renamed_feature": 1.0, "feature_2": 1.0})
     self.assertIsNotNone(preds)
     self.assertEqual(preds["output"], 3.1)
     # reset the spec for next run
     rename_feature(self.spec, "renamed_feature", "feature_1", rename_inputs=True)
Exemple #6
0
 def test_rename_input(self):
     rename_feature(self.spec, 'feature_1', 'renamed_feature', rename_inputs=True)
     model = MLModel(self.spec)
     preds = model.predict({'renamed_feature': 1.0, 'feature_2': 1.0})
     self.assertIsNotNone(preds)
     self.assertEquals(preds['output'], 3.1)
     # reset the spec for next run
     rename_feature(self.spec, 'renamed_feature', 'feature_1', rename_inputs=True)
Exemple #7
0
    def test_rename_feature_classifier_mlprogram(self):
        torch_model = _torch.nn.ReLU().eval()
        model = coremltools.convert(
            _torch.jit.trace(torch_model, _torch.rand(3, )),
            inputs=[coremltools.TensorType(shape=(3,))],
            classifier_config=coremltools.ClassifierConfig(['a', 'b', 'c']),
            convert_to='mlprogram'
        )
        spec = model.get_spec()
        input_name = spec.description.input[0].name

        rename_feature(spec, 'classLabel', 'highestProbClass')
        model = coremltools.models.MLModel(spec)
        output_class = model.predict({input_name: np.array([1.0, 2.0, 3.0])})['highestProbClass']
        self.assertEqual(output_class, 'c')
Exemple #8
0
    def test_pipeline_rename(self):

        # Convert
        scikit_spec = converter.convert(self.scikit_model).get_spec()
        model = MLModel(scikit_spec)
        sample_data = self.scikit_data.data[0]

        # Rename
        rename_feature(scikit_spec, 'input', 'renamed_input')
        renamed_model = MLModel(scikit_spec)

        # Check the predictions
        self.assertEquals(
            model.predict({'input': sample_data}),
            renamed_model.predict({'renamed_input': sample_data}))
Exemple #9
0
    def test_pipeline_rename(self):
        # Convert
        scikit_spec = converter.convert(self.scikit_model).get_spec()
        model = MLModel(scikit_spec)
        sample_data = self.scikit_data.data[0]

        # Rename
        rename_feature(scikit_spec, "input", "renamed_input")
        renamed_model = MLModel(scikit_spec)

        # Check the predictions
        if _is_macos() and _macos_version() >= (10, 13):
            out_dict = model.predict({"input": sample_data})
            out_dict_renamed = renamed_model.predict({"renamed_input": sample_data})
            self.assertAlmostEqual(list(out_dict.keys()), list(out_dict_renamed.keys()))
            self.assertAlmostEqual(
                list(out_dict.values()), list(out_dict_renamed.values())
            )
Exemple #10
0
 def test_rename_image_input(self):
     input_features = [("data", datatypes.Array(3, 1, 1))]
     output_features = [("out", datatypes.Array(3, 1, 1))]
     builder = NeuralNetworkBuilder(
         input_features, output_features, disable_rank5_shape_mapping=True
     )
     builder.add_activation("linear", "LINEAR", "data", "out")
     spec = builder.spec
     # make an image input
     mlmodel = make_image_input(MLModel(spec), "data", image_format="NCHW", scale=2.0)
     # rename the input
     spec = mlmodel.get_spec()
     rename_feature(spec, "data", "new_input_name")
     mlmodel = MLModel(spec)
     # test
     x = np.array([4, 5, 6], dtype=np.uint8).reshape(1, 1, 3)
     pil_img = PIL.Image.fromarray(x)
     out = mlmodel.predict({"new_input_name": pil_img}, useCPUOnly=True)['out']
     np.testing.assert_equal(out, np.array([8.0, 10.0, 12.0]).reshape(3, 1, 1))
Exemple #11
0
    def test_rename_output_nn_classifier(self):
        input_features = [("data", datatypes.Array(3))]
        output_features = [("out", datatypes.Array(3))]
        builder = NeuralNetworkBuilder(
            input_features, output_features, disable_rank5_shape_mapping=True
        )
        builder.add_activation("linear", "LINEAR", "data", "out")
        spec = builder.spec
        mlmodel = MLModel(spec)

        class_labels = ["a", "b", "c"]
        mlmodel = make_nn_classifier(mlmodel, class_labels=["a", "b", "c"])

        # rename output
        spec = mlmodel.get_spec()
        rename_feature(spec, "out", "new_out_name")
        mlmodel = MLModel(spec)

        out_dict = mlmodel.predict({"data": np.array([4.0, 5.5, 6.0])}, useCPUOnly=True)
        self.assertEqual(out_dict["classLabel"], "c")
        self.assertTrue("new_out_name" in out_dict)
        self.assertTrue(isinstance(out_dict["new_out_name"], dict))
Exemple #12
0
    def test_rename_feature_mlprogram(self):
        torch_model = _torch.nn.ReLU().eval()
        model = coremltools.convert(
            _torch.jit.trace(torch_model, _torch.rand(3, )),
            inputs=[coremltools.TensorType(shape=(3,))],
            convert_to='mlprogram'
        )
        spec = model.get_spec()
        input_name = spec.description.input[0].name
        output_name = spec.description.output[0].name

        # rename input
        rename_feature(spec, input_name, "new_input_name")
        self.assertEqual(spec.description.input[0].name, "new_input_name")
        model = coremltools.models.MLModel(spec)
        out = model.predict({"new_input_name": np.array([1.0, 2.0, 3.0])})[output_name]
        self.assertEqual(out[0], 1.0)

        # rename output
        rename_feature(spec, output_name, "new_output_name")
        self.assertEqual(spec.description.output[0].name, "new_output_name")
        model = coremltools.models.MLModel(spec)
        out = model.predict({"new_input_name": np.array([1.0, 2.0, 3.0])})["new_output_name"]
        self.assertEqual(out[1], 2.0)
Exemple #13
0
 def test_rename_input_bad(self):
     rename_feature(self.spec, 'blah', 'bad_name', rename_inputs=True)
     model = MLModel(self.spec)
     preds = model.predict({'feature_1': 1.0, 'feature_2': 1.0})
     self.assertIsNotNone(preds)
     self.assertEqual(preds['output'], 3.1)