def export_coreml(self, filename):
        """
        Save the model in Core ML format.

        See Also
        --------
        save

        Examples
        --------
        >>> model.export_coreml('./myModel.mlmodel')
        """
        import coremltools
        from coremltools.proto.FeatureTypes_pb2 import ArrayFeatureType
        from .._mxnet import _mxnet_utils

        prob_name = self.target + 'Probability'

        def get_custom_model_spec():
            from coremltools.models.neural_network import NeuralNetworkBuilder
            from coremltools.models.datatypes import Array, Dictionary, String

            input_name = 'output1'
            input_length = self._feature_extractor.output_length
            builder = NeuralNetworkBuilder([(input_name, Array(input_length,))],
                                           [(prob_name, Dictionary(String))],
                                           'classifier')

            ctx = _mxnet_utils.get_mxnet_context()[0]
            input_name, output_name = input_name, 0
            for i, cur_layer in enumerate(self._custom_classifier):
                W = cur_layer.weight.data(ctx).asnumpy()
                nC, nB = W.shape
                Wb = cur_layer.bias.data(ctx).asnumpy()

                builder.add_inner_product(name="inner_product_"+str(i),
                                          W=W,
                                          b=Wb,
                                          input_channels=nB,
                                          output_channels=nC,
                                          has_bias=True,
                                          input_name=str(input_name),
                                          output_name='inner_product_'+str(output_name))

                if cur_layer.act:
                    builder.add_activation("activation"+str(i), 'RELU', 'inner_product_'+str(output_name), str(output_name))

                input_name = i
                output_name = i + 1

            last_output = builder.spec.neuralNetworkClassifier.layers[-1].output[0]
            builder.add_softmax('softmax', last_output, self.target)

            builder.set_class_labels(self.classes)
            builder.set_input([input_name], [(input_length,)])
            builder.set_output([self.target], [(self.num_classes,)])

            return builder.spec


        top_level_spec = coremltools.proto.Model_pb2.Model()
        top_level_spec.specificationVersion = 3

        # Set input
        desc = top_level_spec.description
        input = desc.input.add()
        input.name = self.feature
        input.type.multiArrayType.dataType = ArrayFeatureType.ArrayDataType.Value('FLOAT32')
        input.type.multiArrayType.shape.append(15600)

        # Set outputs
        prob_output = desc.output.add()
        prob_output.name = prob_name
        label_output = desc.output.add()
        label_output.name = 'classLabel'
        desc.predictedFeatureName = 'classLabel'
        desc.predictedProbabilitiesName = prob_name
        if type(self.classes[0]) == int:
            # Class labels are ints
            prob_output.type.dictionaryType.int64KeyType.MergeFromString(b'')
            label_output.type.int64Type.MergeFromString(b'')
        else:     # Class are strings
            prob_output.type.dictionaryType.stringKeyType.MergeFromString(b'')
            label_output.type.stringType.MergeFromString(b'')


        pipeline = top_level_spec.pipelineClassifier.pipeline

        # Add the preprocessing model
        preprocessing_model = pipeline.models.add()
        preprocessing_model.customModel.className = 'TCSoundClassifierPreprocessing'
        preprocessing_model.specificationVersion = 3
        preprocessing_input = preprocessing_model.description.input.add()
        preprocessing_input.CopyFrom(input)

        preprocessed_output = preprocessing_model.description.output.add()
        preprocessed_output.name = 'preprocessed_data'
        preprocessed_output.type.multiArrayType.dataType = ArrayFeatureType.ArrayDataType.Value('DOUBLE')
        preprocessed_output.type.multiArrayType.shape.append(1)
        preprocessed_output.type.multiArrayType.shape.append(96)
        preprocessed_output.type.multiArrayType.shape.append(64)

        # Add the feature extractor, updating its input name
        feature_extractor_spec = self._feature_extractor.get_spec()
        pipeline.models.add().CopyFrom(feature_extractor_spec)
        pipeline.models[-1].description.input[0].name = preprocessed_output.name
        pipeline.models[-1].neuralNetwork.layers[0].input[0] = preprocessed_output.name

        # Add the custom neural network
        pipeline.models.add().CopyFrom(get_custom_model_spec())

        # Set key type for the probability dictionary
        prob_output_type = pipeline.models[-1].description.output[0].type.dictionaryType
        if type(self.classes[0]) == int:
            prob_output_type.int64KeyType.MergeFromString(b'')
        else:    # String labels
            prob_output_type.stringKeyType.MergeFromString(b'')

        mlmodel = coremltools.models.MLModel(top_level_spec)
        mlmodel.save(filename)
def export_coreml ( self , filename ) : import coremltools from coremltools . proto . FeatureTypes_pb2 import ArrayFeatureType from . . _mxnet import _mxnet_utils prob_name = self . target + 'Probability' def get_custom_model_spec ( ) : from coremltools . models . neural_network import NeuralNetworkBuilder from coremltools . models . datatypes import Array , Dictionary , String input_name = 'output1' input_length = self . _feature_extractor . output_length builder = NeuralNetworkBuilder ( [ ( input_name , Array ( input_length , ) ) ] , [ ( prob_name , Dictionary ( String ) ) ] , 'classifier' ) ctx = _mxnet_utils . get_mxnet_context ( ) [ 0 ] input_name , output_name = input_name , 0 for i , cur_layer in enumerate ( self . _custom_classifier ) : W = cur_layer . weight . data ( ctx ) . asnumpy ( ) nC , nB = W . shape Wb = cur_layer . bias . data ( ctx ) . asnumpy ( ) builder . add_inner_product ( name = "inner_product_" + str ( i ) , W = W , b = Wb , input_channels = nB , output_channels = nC , has_bias = True , input_name = str ( input_name ) , output_name = 'inner_product_' + str ( output_name ) ) if cur_layer . act : builder . add_activation ( "activation" + str ( i ) , 'RELU' , 'inner_product_' + str ( output_name ) , str ( output_name ) ) input_name = i output_name = i + 1 last_output = builder . spec . neuralNetworkClassifier . layers [ - 1 ] . output [ 0 ] builder . add_softmax ( 'softmax' , last_output , self . target ) builder . set_class_labels ( self . classes ) builder . set_input ( [ input_name ] , [ ( input_length , ) ] ) builder . set_output ( [ self . target ] , [ ( self . num_classes , ) ] ) return builder . spec top_level_spec = coremltools . proto . Model_pb2 . Model ( ) top_level_spec . specificationVersion = 3 desc = top_level_spec . description input = desc . input . add ( ) input . name = self . feature input . type . multiArrayType . dataType = ArrayFeatureType . ArrayDataType . Value ( 'FLOAT32' ) input . type . multiArrayType . shape . append ( 15600 ) prob_output = desc . output . add ( ) prob_output . name = prob_name label_output = desc . output . add ( ) label_output . name = 'classLabel' desc . predictedFeatureName = 'classLabel' desc . predictedProbabilitiesName = prob_name if type ( self . classes [ 0 ] ) == int : prob_output . type . dictionaryType . int64KeyType . MergeFromString ( b'' ) label_output . type . int64Type . MergeFromString ( b'' ) else : prob_output . type . dictionaryType . stringKeyType . MergeFromString ( b'' ) label_output . type . stringType . MergeFromString ( b'' ) pipeline = top_level_spec . pipelineClassifier . pipeline preprocessing_model = pipeline . models . add ( ) preprocessing_model . customModel . className = 'TCSoundClassifierPreprocessing' preprocessing_model . specificationVersion = 3 preprocessing_input = preprocessing_model . description . input . add ( ) preprocessing_input . CopyFrom ( input ) preprocessed_output = preprocessing_model . description . output . add ( ) preprocessed_output . name = 'preprocessed_data' preprocessed_output . type . multiArrayType . dataType = ArrayFeatureType . ArrayDataType . Value ( 'DOUBLE' ) preprocessed_output . type . multiArrayType . shape . append ( 1 ) preprocessed_output . type . multiArrayType . shape . append ( 96 ) preprocessed_output . type . multiArrayType . shape . append ( 64 ) feature_extractor_spec = self . _feature_extractor . get_spec ( ) pipeline . models . add ( ) . CopyFrom ( feature_extractor_spec ) pipeline . models [ - 1 ] . description . input [ 0 ] . name = preprocessed_output . name pipeline . models [ - 1 ] . neuralNetwork . layers [ 0 ] . input [ 0 ] = preprocessed_output . name pipeline . models . add ( ) . CopyFrom ( get_custom_model_spec ( ) ) prob_output_type = pipeline . models [ - 1 ] . description . output [ 0 ] . type . dictionaryType if type ( self . classes [ 0 ] ) == int : prob_output_type . int64KeyType . MergeFromString ( b'' ) else : prob_output_type . stringKeyType . MergeFromString ( b'' ) mlmodel = coremltools . models . MLModel ( top_level_spec ) mlmodel . save ( filename )
Save the model in Core ML format.

        See Also
        --------
        save

        Examples
        --------
        >>> model.export_coreml('./myModel.mlmodel')