Python coremltools.models() Examples

The following are 20 code examples of coremltools.models(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module coremltools , or try the search function .
Example #1
Source File: test_quantization.py    From coremltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _run_quantized_test(self, input_, full_precision_model, quantized_model, delta):
        # Output from both models should be the same
        full_output = full_precision_model.predict(input_)
        quantized_output = quantized_model.predict(input_)
        self.assertEqual(full_output.keys(), quantized_output.keys())

        for key in full_output.keys():
            full_output_flatten = full_output[key].flatten()
            quantized_output_flatten = quantized_output[key].flatten()

            self.assertTrue(len(full_output_flatten) == len(quantized_output_flatten))

            norm_factor = np.maximum(full_output_flatten, quantized_output_flatten)
            norm_factor = np.maximum(norm_factor, 1.0)
            f_out = full_output_flatten / norm_factor
            q_out = quantized_output_flatten / norm_factor

            for idx, full_value in enumerate(f_out):
                quantized_value = q_out[idx]
                self.assertAlmostEqual(full_value, quantized_value, delta=delta) 
Example #2
Source File: test_api_visibilities.py    From coremltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_top_level(self):
        expected = [
            "ClassifierConfig",
            "EnumeratedShapes",
            "ImageType",
            "RangeDim",
            "SPECIFICATION_VERSION",
            "Shape",
            "TensorType",
            "convert",
            "converters",
            "models",
            "proto",
            "target",
            "utils",
            "version",
        ]
        _check_visible_modules(_get_visible_items(ct), expected) 
Example #3
Source File: test_api_visibilities.py    From coremltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_models_neural_network(self):
        expected = [
            "AdamParams",
            "NeuralNetworkBuilder",
            "SgdParams",
            "builder",
            "datatypes",
            "flexible_shape_utils",
            "optimization_utils",
            "printer",
            "quantization_utils",
            "set_training_features",
            "set_transform_interface_params",
            "spec_inspection_utils",
            "update_optimizer_utils",
            "utils",
        ]
        _check_visible_modules(_get_visible_items(ct.models.neural_network), expected) 
Example #4
Source File: test_api_visibilities.py    From coremltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_models_neural_network_flexible_shape_utils(self):
        expected = [
            "NeuralNetworkImageSize",
            "NeuralNetworkImageSizeRange",
            "NeuralNetworkMultiArrayShape",
            "NeuralNetworkMultiArrayShapeRange",
            "Shape",
            "ShapeRange",
            "Size",
            "add_enumerated_image_sizes",
            "add_enumerated_multiarray_shapes",
            "add_multiarray_ndshape_enumeration",
            "set_multiarray_ndshape_range",
            "update_image_size_range",
            "update_multiarray_shape_range",
        ]
        _check_visible_modules(
            _get_visible_items(ct.models.neural_network.flexible_shape_utils), expected
        ) 
Example #5
Source File: test_api_visibilities.py    From coremltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_models_neural_network_quantization_utils(self):
        expected = [
            "AdvancedQuantizedLayerSelector",
            "MatrixMultiplyLayerSelector",
            "ModelMetrics",
            "NoiseMetrics",
            "OutputMetric",
            "QuantizedLayerSelector",
            "TopKMetrics",
            "activate_int8_int8_matrix_multiplications",
            "compare_models",
            "quantize_weights",
        ]
        _check_visible_modules(
            _get_visible_items(ct.models.neural_network.quantization_utils), expected
        ) 
Example #6
Source File: fritz_coreml_converter.py    From fritz-models with MIT License 5 votes vote down vote up
def _check_unsupported_layers(cls, model, supported_layers):
        """Check for any unsupported layers in the keras model.

        Args:
            model - a keras model
            supported_layers - a dictionary of supported layers. Keys are keras
                               layer classes and values are corresponding
                               coreml layer classes.
        """
        for i, layer in enumerate(model.layers):
            if (isinstance(layer, _keras.models.Sequential) or
                    isinstance(layer, _keras.models.Model)):
                cls._check_unsupported_layers(layer)
            else:
                if type(layer) not in supported_layers:
                    print(supported_layers)
                    raise ValueError(
                        "Keras layer '%s' not supported. " % str(type(layer))
                    )
                if isinstance(layer, _keras.layers.wrappers.TimeDistributed):
                    if type(layer.layer) not in supported_layers:
                        raise ValueError(
                            "Keras layer '%s' not supported. " %
                            str(type(layer.layer))
                        )
                if isinstance(layer, _keras.layers.wrappers.Bidirectional):
                    if not isinstance(layer.layer,
                                      _keras.layers.recurrent.LSTM):
                        raise ValueError(
                            'Keras bi-directional wrapper conversion supports '
                            'only LSTM layer at this time. ') 
Example #7
Source File: test_quantization.py    From coremltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def compare(self, specification_modified=True):
        x = np.random.rand(*self.input_shape)

        def _get_preds(spec):
            mlmodel = coremltools.models.MLModel(spec)
            return mlmodel.predict({"data": x}, useCPUOnly=True)["output"]

        preds = _get_preds(self.builder.spec)
        self.assertEqual(self.builder.spec.specificationVersion, 4)

        quantized_spec = activate_int8_int8_matrix_multiplications(
            self.builder.spec, self.selector
        )

        layer = self.builder.spec.neuralNetwork.layers[0]
        layer_type = layer.WhichOneof("layer")
        if layer_type == "innerProduct":
            matmul_layer = layer.innerProduct

        elif layer_type == "batchedMatmul":
            matmul_layer = layer.batchedMatmul
        wp = matmul_layer.weights

        if specification_modified:
            self.assertEqual(self.builder.spec.specificationVersion, 5)
            quant_preds = _get_preds(quantized_spec)
            self._test_predictions(preds, quant_preds, SNR=40)
            self.assertEqual(len(wp.floatValue), 0)
        else:
            self.assertEqual(self.builder.spec.specificationVersion, 4)
            quant_preds = _get_preds(quantized_spec)
            np.testing.assert_array_almost_equal(preds, quant_preds)
            self.assertGreater(len(wp.floatValue), 0) 
Example #8
Source File: test_nn_builder.py    From coremltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_simple_loop_fixed_iterations(self):
        input_features = [("data", datatypes.Array(1))]
        output_features = [("output", None)]

        builder_top = NeuralNetworkBuilder(
            input_features, output_features, disable_rank5_shape_mapping=True
        )
        builder_top.add_copy("copy_1", input_name="data", output_name="output")

        loop_layer = builder_top.add_loop("loop_layer")
        loop_layer.loop.maxLoopIterations = 5
        builder_body = NeuralNetworkBuilder(
            input_features=None,
            output_features=None,
            spec=None,
            nn_spec=loop_layer.loop.bodyNetwork,
        )
        builder_body.add_elementwise(
            "add", input_names=["output"], output_name="x", mode="ADD", alpha=2
        )

        builder_body.add_copy("copy_2", input_name="x", output_name="output")
        coremltools.models.utils.save_spec(
            builder_top.spec, "/tmp/simple_loop_fixed_iterations.mlmodel"
        )
        mlmodel = MLModel(builder_top.spec)

        # True branch case
        input_dict = {"data": np.array([0], dtype="float")}
        output_ref = {"output": np.array([10], dtype="float")}
        self._test_model(mlmodel, input_dict, output_ref) 
Example #9
Source File: test_api_visibilities.py    From coremltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_models_neural_network_optimization_utils(self):
        _check_visible_modules(
            _get_visible_items(ct.models.neural_network.optimization_utils), [],
        ) 
Example #10
Source File: test_api_visibilities.py    From coremltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_models_neural_network_update_optimizer_utils(self):
        expected = ["AdamParams", "Batch", "RangeParam", "SgdParams"]
        _check_visible_modules(
            _get_visible_items(ct.models.neural_network.update_optimizer_utils),
            expected,
        ) 
Example #11
Source File: test_api_visibilities.py    From coremltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_models_pipeline(self):
        expected = [
            "Pipeline",
            "PipelineClassifier",
            "PipelineRegressor",
            "set_classifier_interface_params",
            "set_regressor_interface_params",
            "set_training_features",
            "set_transform_interface_params",
        ]
        _check_visible_modules(_get_visible_items(ct.models.pipeline), expected) 
Example #12
Source File: test_api_visibilities.py    From coremltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_models_neural_network_utils(self):
        expected = ["NeuralNetworkBuilder", "make_image_input", "make_nn_classifier"]
        _check_visible_modules(
            _get_visible_items(ct.models.neural_network.utils), expected
        ) 
Example #13
Source File: test_api_visibilities.py    From coremltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_models_mlmodel(self):
        expected = [
            "author",
            "get_spec",
            "input_description",
            "license",
            "output_description",
            "predict",
            "save",
            "short_description",
            "user_defined_metadata",
            "version",
        ]
        _check_visible_modules(_get_visible_items(ct.models.MLModel), expected) 
Example #14
Source File: test_api_visibilities.py    From coremltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_models(self):
        expected = [
            "MLModel",
            "datatypes",
            "model",
            "neural_network",
            "pipeline",
            "tree_ensemble",
            "utils",
        ]
        _check_visible_modules(_get_visible_items(ct.models), expected) 
Example #15
Source File: extractor.py    From MMdnn with MIT License 4 votes vote down vote up
def inference(cls, architecture, model_path, image_path):
        # TODO
        from PIL import Image
        import numpy as np
        from coremltools.models._infer_shapes_nn_mlmodel import infer_shapes
        if cls.sanity_check(architecture):
            func = TestKit.preprocess_func['coreml'][architecture]


            import inspect
            funcstr = inspect.getsource(func)

            if len(funcstr.split(',')) == 3:
                size = int(funcstr.split('path,')[1].split(')')[0])
            else:
                size = int(funcstr.split('path,')[1].split(',')[0])



            img = Image.open(image_path)
            img = img.resize((size, size))

            # load model
            model = MLModel(model_path)
            spec = model.get_spec()

            # TODO: Multiple inputs
            input_name = spec.description.input[0].name

            # TODO: Multiple outputs
            output_name = spec.description.output[0].name

            # inference
            input_data = img
            coreml_input = {input_name: img}
            coreml_output = model.predict(coreml_input)


            prob = coreml_output[output_name]
            if isinstance(prob, dict):
                prob = list(coreml_output[output_name].values())
            prob = np.array(prob).squeeze()

            return prob

        else:
            return None 
Example #16
Source File: test_quantization.py    From coremltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def test_8bit_symmetric_and_skips(self):
        from keras.models import Sequential
        from keras.layers import Conv2D

        def stable_rel_error(x, ref):
            err = x - ref
            denom = np.maximum(np.abs(ref), np.ones_like(ref))
            return np.abs(err) / denom

        np.random.seed(1988)
        input_dim = 16
        num_kernels, kernel_height, kernel_width, input_channels = 64, 3, 3, 32

        # Define a model
        model = Sequential()
        model.add(
            Conv2D(
                input_shape=(input_dim, input_dim, input_channels),
                filters=num_kernels,
                kernel_size=(kernel_height, kernel_width),
            )
        )

        # Set some random weights
        weight, bias = model.layers[0].get_weights()
        num_filters = weight.shape[-1]
        filter_shape = weight.shape[:-1]

        new_weight = np.stack(
            [4.0 * np.random.rand(*filter_shape) - 2 for i in range(num_filters)],
            axis=-1,
        )
        model.layers[0].set_weights([new_weight, bias])

        mlmodel = keras_converter.convert(model, ["data"], ["output_0"])
        selector = quantization_utils.AdvancedQuantizedLayerSelector(
            skip_layer_types=["batchnorm", "bias", "depthwiseConv"],
            minimum_conv_kernel_channels=4,
            minimum_conv_weight_count=4096,
        )

        q_mlmodel = quantization_utils.quantize_weights(mlmodel, 8, selector=selector)

        input_shape = (1, 1, input_channels, input_dim, input_dim)
        input_val = 2 * np.random.rand(*input_shape) - 1

        coreml_input = {"data": input_val}
        coreml_output = mlmodel.predict(coreml_input)
        q_coreml_output = q_mlmodel.predict(coreml_input)

        val = coreml_output["output_0"]
        q_val = q_coreml_output["output_0"]
        rel_err = stable_rel_error(q_val, val)
        max_rel_err, mean_rel_err = np.max(rel_err), np.mean(rel_err)
        self.assertTrue(max_rel_err < 0.25)
        self.assertTrue(max_rel_err > 0.01)
        self.assertTrue(mean_rel_err < 0.02) 
Example #17
Source File: conversion_imagenet.py    From MMdnn with MIT License 4 votes vote down vote up
def test_nothing(self):
        pass

    # def test_caffe(self):
    #     try:
    #         import caffe
    #         self._test_function('caffe', self.caffe_parse)
    #     except ImportError:
    #         print('Please install caffe! Or caffe is not supported in your platform.', file=sys.stderr)


    # def test_cntk(self):
    #     try:
    #         import cntk
    #         self._test_function('cntk', self.cntk_parse)
    #     except ImportError:
    #         print('Please install cntk! Or cntk is not supported in your platform.', file=sys.stderr)


    # def test_coreml(self):
    #     from coremltools.models.utils import macos_version
    #     if macos_version() < (10, 13):
    #         print('Coreml is not supported in your platform.', file=sys.stderr)
    #     else:
    #         self._test_function('coreml', self.coreml_parse)


    # def test_keras(self):
    #     self._test_function('keras', self.keras_parse)


    # def test_mxnet(self):
    #     self._test_function('mxnet', self.mxnet_parse)


    # def test_darknet(self):
    #     self._test_function('darknet', self.darknet_parse)


    # def test_paddle(self):
    #     # omit tensorflow lead to crash
    #     import tensorflow as tf
    #     try:
    #         import paddle.v2 as paddle
    #         self._test_function('paddle', self.paddle_parse)
    #     except ImportError:
    #         print('Please install Paddlepaddle! Or Paddlepaddle is not supported in your platform.', file=sys.stderr)


    # def test_pytorch(self):
    #     self._test_function('pytorch', self.pytorch_parse)


    # def test_tensorflow(self):
    #     self._test_function('tensorflow', self.tensorflow_parse)


    # def test_tensorflow_frozen(self):
    #     self._test_function('tensorflow_frozen', self.tensorflow_frozen_parse) 
Example #18
Source File: test_quantization.py    From coremltools with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def test_embeddingND_quantize(self):
        input_features = [("data", datatypes.Array(10, 1))]
        output_features = [("output", None)]
        builder = neural_network.NeuralNetworkBuilder(
            input_features, output_features, disable_rank5_shape_mapping=True
        )

        builder.add_embedding_nd(
            name="embedding_nd",
            input_name="data",
            output_name="output",
            vocab_size=300,
            embedding_size=20,
            W=np.random.rand(20, 300),
        )

        spec = builder.spec
        model_fp32 = coremltools.models.MLModel(spec)
        self.assertEqual(
            len(spec.neuralNetwork.layers[0].embeddingND.weights.floatValue), 6000
        )

        # quantize to FP16
        model_fp16 = quantization_utils.quantize_weights(model_fp32, nbits=16)
        spec_fp16 = model_fp16.get_spec()
        self.assertEqual(
            len(spec_fp16.neuralNetwork.layers[0].embeddingND.weights.floatValue), 0
        )
        self.assertEqual(
            len(spec_fp16.neuralNetwork.layers[0].embeddingND.weights.float16Value),
            2 * 6000,
        )

        # quantize to uint8
        model_uint8 = quantization_utils.quantize_weights(model_fp32, nbits=8)
        spec_uint8 = model_uint8.get_spec()
        self.assertEqual(
            len(spec_uint8.neuralNetwork.layers[0].embeddingND.weights.floatValue), 0
        )
        self.assertEqual(
            len(spec_uint8.neuralNetwork.layers[0].embeddingND.weights.float16Value), 0
        )
        self.assertEqual(
            len(spec_uint8.neuralNetwork.layers[0].embeddingND.weights.rawValue), 6000
        )

        # quantize to uint5
        model_uint5 = quantization_utils.quantize_weights(model_fp32, nbits=5)
        spec_uint5 = model_uint5.get_spec()
        self.assertEqual(
            len(spec_uint5.neuralNetwork.layers[0].embeddingND.weights.floatValue), 0
        )
        self.assertEqual(
            len(spec_uint5.neuralNetwork.layers[0].embeddingND.weights.float16Value), 0
        )
        self.assertEqual(
            len(spec_uint5.neuralNetwork.layers[0].embeddingND.weights.rawValue), 3750
        )  # 3750 = 5*6000/8 
Example #19
Source File: test_mlmodel_optimizer.py    From tf-coreml with Apache License 2.0 4 votes vote down vote up
def test_pad_conv_fusion(self):

    Cin = 3
    Cout = 5
    K = 9
    Hin = 32
    Win = 18
    Xin = np.random.rand(Cin,Hin,Win)
    # Test for several combinations of (pad,stride)
    params = [(5,2),(4,3),(6,3),
              (5,1),(5,2),(6,2),
              (3,2),(1,1),(2,3)]
    for param in params:
      pad, stride = param
      input_features = [('data', datatypes.Array(*(Cin, Hin, Win)))]
      output_features = [('output', None)]
      builder = neural_network.NeuralNetworkBuilder(input_features, output_features)
      builder.add_padding(name='pad',
                          left=pad, right=pad, top=pad, bottom=pad,
                          input_name='data', output_name='pad_out'
                          )
      builder.add_convolution(name='conv',
                              kernel_channels=Cin, output_channels=Cout,
                              height=K, width=K,
                              stride_height=stride, stride_width=stride,
                              border_mode='valid', groups=1,
                              W=np.random.rand(K, K, Cin, Cout),
                              b=None, has_bias=False,
                              input_name='pad_out', output_name='output')

      #get unoptimized model
      original_spec = builder.spec
      model = coremltools.models.MLModel(original_spec)
      #get optimized model
      spec_copy = copy.deepcopy(original_spec)
      tfcoreml.optimize_nn_spec(spec_copy)
      model_opt = coremltools.models.MLModel(spec_copy)

      n_layers_original_model = len(model.get_spec().neuralNetwork.layers)
      n_layers_opt_model = len(model_opt.get_spec().neuralNetwork.layers)
      self.assertEqual(n_layers_original_model, 2)
      self.assertEqual(n_layers_opt_model, 1)

      original_model_out = model.predict({'data':Xin})['output']
      opt_model_out = model_opt.predict({'data':Xin})['output']
      self._compare_outputs(opt_model_out, original_model_out) 
Example #20
Source File: conversion_imagenet.py    From MMdnn with MIT License 4 votes vote down vote up
def _test_function(self, original_framework, parser):
        print("[{}] Testing {} models starts.".format(datetime.now(), original_framework), file=sys.stderr)
        
        ensure_dir(self.cachedir)
        ensure_dir(self.tmpdir)

        for network_name in self.test_table[original_framework].keys():
            print("[{}] Testing {} {} starts.".format(datetime.now(), original_framework, network_name), file=sys.stderr)

            # get test input path
            test_input = self._get_test_input(network_name)

            # get original model prediction result
            original_predict = parser(network_name, test_input)


            IR_file = TestModels.tmpdir + original_framework + '_' + network_name + "_converted"
            for emit in self.test_table[original_framework][network_name]:
                if isinstance(emit, staticmethod):
                    emit = emit.__func__
                target_framework = emit.__name__[:-5]

                if (target_framework == 'coreml'):
                    if not is_coreml_supported():
                        continue

                print('[{}] Converting {} from {} to {} starts.'.format(datetime.now(), network_name, original_framework, target_framework), file=sys.stderr)
                converted_predict = emit(
                    original_framework,
                    network_name,
                    IR_file + ".pb",
                    IR_file + ".npy",
                    test_input)


                self._compare_outputs(
                    original_framework,
                    target_framework,
                    network_name,
                    original_predict,
                    converted_predict,
                    self._need_assert(original_framework, target_framework, network_name, original_predict, converted_predict)
                )
                print('[{}] Converting {} from {} to {} passed.'.format(datetime.now(), network_name, original_framework, target_framework), file=sys.stderr)

            try:
                os.remove(IR_file + ".json")
            except OSError:
                pass

            os.remove(IR_file + ".pb")
            os.remove(IR_file + ".npy")
            print("[{}] Testing {} {} passed.".format(datetime.now(), original_framework, network_name), file=sys.stderr)

        print("[{}] Testing {} models passed.".format(datetime.now(), original_framework), file=sys.stderr)