コード例 #1
0
    def test_model_api(self):
        model = MLModel(self.spec)
        self.assertIsNotNone(model)

        model.author = 'Test author'
        self.assertEquals(model.author, 'Test author')
        self.assertEquals(model.get_spec().description.metadata.author, 'Test author')

        model.license = 'Test license'
        self.assertEquals(model.license, 'Test license')
        self.assertEquals(model.get_spec().description.metadata.license, 'Test license')

        model.short_description = 'Test model'
        self.assertEquals(model.short_description, 'Test model')
        self.assertEquals(model.get_spec().description.metadata.shortDescription, 'Test model')

        model.input_description['feature_1'] = 'This is feature 1'
        self.assertEquals(model.input_description['feature_1'], 'This is feature 1')

        model.output_description['output'] = 'This is output'
        self.assertEquals(model.output_description['output'], 'This is output')

        filename = tempfile.mktemp(suffix = '.mlmodel')
        model.save(filename)
        loaded_model = MLModel(filename)

        self.assertEquals(model.author, 'Test author')
        self.assertEquals(model.license, 'Test license')
        # self.assertEquals(model.short_description, 'Test model')
        self.assertEquals(model.input_description['feature_1'], 'This is feature 1')
        self.assertEquals(model.output_description['output'], 'This is output')
コード例 #2
0
    def test_model_api(self):
        model = MLModel(self.spec)
        self.assertIsNotNone(model)

        model.author = "Test author"
        self.assertEqual(model.author, "Test author")
        self.assertEqual(model.get_spec().description.metadata.author,
                         "Test author")

        model.license = "Test license"
        self.assertEqual(model.license, "Test license")
        self.assertEqual(model.get_spec().description.metadata.license,
                         "Test license")

        model.short_description = "Test model"
        self.assertEqual(model.short_description, "Test model")
        self.assertEqual(
            model.get_spec().description.metadata.shortDescription,
            "Test model")

        model.version = "1.3"
        self.assertEqual(model.version, "1.3")
        self.assertEqual(model.get_spec().description.metadata.versionString,
                         "1.3")

        model.input_description["feature_1"] = "This is feature 1"
        self.assertEqual(model.input_description["feature_1"],
                         "This is feature 1")

        model.output_description["output"] = "This is output"
        self.assertEqual(model.output_description["output"], "This is output")

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

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

        self.assertEqual(model.author, "Test author")
        self.assertEqual(model.license, "Test license")
        self.assertEqual(model.short_description, "Test model")
        self.assertEqual(model.input_description["feature_1"],
                         "This is feature 1")
        self.assertEqual(model.output_description["output"], "This is output")

        # cleanup
        MLModelTest._remove_path(package.name)
コード例 #3
0
ファイル: test_model.py プロジェクト: aseemw/coremltools
    def test_model_api(self):
        model = MLModel(self.spec)
        self.assertIsNotNone(model)

        model.author = "Test author"
        self.assertEqual(model.author, "Test author")
        self.assertEqual(model.get_spec().description.metadata.author,
                         "Test author")

        model.license = "Test license"
        self.assertEqual(model.license, "Test license")
        self.assertEqual(model.get_spec().description.metadata.license,
                         "Test license")

        model.short_description = "Test model"
        self.assertEqual(model.short_description, "Test model")
        self.assertEqual(
            model.get_spec().description.metadata.shortDescription,
            "Test model")

        model.version = "1.3"
        self.assertEqual(model.version, "1.3")
        self.assertEqual(model.get_spec().description.metadata.versionString,
                         "1.3")

        model.input_description["feature_1"] = "This is feature 1"
        self.assertEqual(model.input_description["feature_1"],
                         "This is feature 1")

        model.output_description["output"] = "This is output"
        self.assertEqual(model.output_description["output"], "This is output")

        filename = tempfile.mktemp(suffix=".mlmodel")
        model.save(filename)
        loaded_model = MLModel(filename)

        self.assertEqual(model.author, "Test author")
        self.assertEqual(model.license, "Test license")
        # self.assertEqual(model.short_description, 'Test model')
        self.assertEqual(model.input_description["feature_1"],
                         "This is feature 1")
        self.assertEqual(model.output_description["output"], "This is output")
コード例 #4
0
    def test_model_api(self):
        model = MLModel(self.spec)
        assert model is not None

        model.author = "Test author"
        assert model.author == "Test author"
        assert model.get_spec().description.metadata.author == "Test author"

        model.license = "Test license"
        assert model.license == "Test license"
        assert model.get_spec().description.metadata.license == "Test license"

        model.short_description = "Test model"
        assert model.short_description == "Test model"
        assert model.get_spec(
        ).description.metadata.shortDescription == "Test model"

        model.version = "1.3"
        assert model.version == "1.3"
        assert model.get_spec().description.metadata.versionString == "1.3"

        model.input_description["feature_1"] = "This is feature 1"
        assert model.input_description["feature_1"] == "This is feature 1"

        model.output_description["output"] = "This is output"
        assert model.output_description["output"] == "This is output"

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

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

        assert model.author == "Test author"
        assert model.license == "Test license"
        assert model.short_description == "Test model"
        assert model.input_description["feature_1"] == "This is feature 1"
        assert model.output_description["output"] == "This is output"

        # cleanup
        _remove_path(package.name)
コード例 #5
0
def convert_keras_to_mlmodel(keras_model_path, coreml_model_path):

    import importlib.machinery as imm
    from coremltools.converters.keras._keras_converter import convertToSpec
    from coremltools.models import MLModel, _MLMODEL_FULL_PRECISION, _MLMODEL_HALF_PRECISION
    #    from coremltools.models.utils import convert_double_to_float_multiarray_type

    from keras.models import load_model
    from kerassurgeon.operations import delete_layer

    sys.path.append(os.path.dirname(sys.argv[4]))

    # Import neural network code
    NN_file_name = os.path.splitext(os.path.basename(sys.argv[3]))[0]
    NN = imm.SourceFileLoader(NN_file_name, sys.argv[3]).load_module()

    try:
        NN_model_name = NN.Model_Name()
    except:
        NN_model_name = NN_file_name

    try:
        NN_model_description = NN.Model_Description()
    except:
        NN_model_description = None

    # Load custom layers if implemented in each Keras model
    # Take care the imported NN may not have Custom_Layers() def, so try and catch except.
    # The type is a dictionary. The keys are supposed to be same as the corresponding values (=defs).
    try:
        NN_custom_layers = NN.Custom_Layers()
    except:
        NN_custom_layers = {}

    # Import Train.py to get custom loss and metrics
    Train_name = os.path.splitext(os.path.basename(sys.argv[4]))[0]
    Train_py = imm.SourceFileLoader(Train_name, sys.argv[4]).load_module()

    custom_loss = Train_py.get_loss()
    custom_metrics = Train_py.get_metrics()

    kpt, kex = os.path.splitext(keras_model_path)
    keras_model_path_temp = kpt + '_temp' + kex

    print('----------------------------------------------------------')
    print('NN model file path: {}'.format(sys.argv[3]))
    print('NN model name: {}'.format(NN_model_name))
    print('NN model description: {}'.format(NN_model_description))
    print('NN custom layers:')
    print(NN_custom_layers)
    print('Training file path and loss/metrics used:')
    print(sys.argv[4])
    print(custom_loss)
    print(custom_metrics)

    print('----------------------------------------------------------')
    print('Keras model file: {}'.format(keras_model_path))
    print('Keras model file temp: {}'.format(keras_model_path_temp))
    print('CoreML model file: {}'.format(coreml_model_path))

    print('----------------------------------------------------------')
    print('Keras custom layers implemented in AIAS for this code:')
    for k in conversion_func_in_AIAS:
        print(k)

    # Deleting Dropout layers from the Keras model to be converted
    # Because the layers will cause unknown conversion failures in coremltools
    keras_model = load_model(keras_model_path,
                             custom_objects=dict(**custom_loss,
                                                 **custom_metrics,
                                                 **NN_custom_layers),
                             compile=False)

    print('----------------------------------------------------------')
    keras_model.summary()

    del_prefixs = [
        'gaussian_dropout', 'gaussian_noise', 'dropout', 'spatial_dropout2d'
    ]  # Add here to define the layer to be deleted

    for del_prefix in del_prefixs:
        idp = 1
        while True:
            try:
                layer = keras_model.get_layer('{}_{}'.format(del_prefix, idp))
            except:
                break
            print('Deleting layer: {}_{}'.format(del_prefix, idp))
            keras_model = delete_layer(model=keras_model,
                                       layer=layer,
                                       copy=False)
            idp += 1

    keras_model.summary()
    print('Saving temporary Keras model: {}'.format(keras_model_path_temp))
    keras_model.save(keras_model_path_temp)

    # Construct custom layers and conversion functions
    custom_layers = {}
    custom_conversion_func = {}
    print('----------------------------------------------------------')

    if NN_custom_layers is not None:
        print('Custom layers in this Keras model:')
        for keras_layer_key in NN_custom_layers:
            if keras_layer_key in conversion_func_in_AIAS:
                print(keras_layer_key + ' - available')
                custom_layers[keras_layer_key] = NN_custom_layers[
                    keras_layer_key]
                custom_conversion_func[
                    keras_layer_key] = conversion_func_in_AIAS[keras_layer_key]
            else:
                print(keras_layer_key + ' - unavailable')

        print('Matched layers and conversion functions for coremltools:')
        print(custom_layers)
        print(custom_conversion_func)

    else:
        print('Custom layers not found in this Keras model.')

    custom_objects = dict(**custom_loss, **custom_metrics, **custom_layers)

    print('----------------------------------------------------------')
    print('Custom objects passed into coremltools converter:')
    print(custom_objects)
    print('----------------------------------------------------------')

    # Convert
    # Do not change the input_names/output_names because they are used to identify input/output layers in Keras code
    spec = convertToSpec(keras_model_path_temp,
                         input_names='input',
                         output_names='output',
                         add_custom_layers=True,
                         custom_conversion_functions=custom_conversion_func,
                         custom_objects=custom_objects,
                         respect_trainable=False)  # should be True???
    model = MLModel(spec)

    # Set descriptions
    model.author = 'Takashi Shirakawa'
    model.license = '(C) 2019-2020, Takashi Shirakawa. All right reserved.'
    model.short_description = NN_model_name + ' for A.I.Segmentation'
    model.input_description[
        'input'] = 'Input is a square image with 8-bit grayscale per pixel.'
    model.output_description[
        'output'] = 'Output (segmentation) is supposed to be an image with the same dimension and format.'

    # Save mlmodel
    model.save(coreml_model_path)

    #    spec_f = model.get_spec()
    #    convert_double_to_float_multiarray_type(spec_f)
    #    model_f = MLModel(spec_f)
    #    model_f.save(os.path.splitext(coreml_model_path)[0] + ', float_multiarray.mlmodel')

    # Show results
    spec = model.get_spec()
    print('----------------------------------------------------------')
    print('Model descriptions:')
    print(spec.description)
    #    print('Model descriptions (float multiarray type):')
    #    print(spec_f.description)

    print('Custom layers:')
    for i, layer in enumerate(spec.neuralNetwork.layers):
        if layer.HasField('custom'):
            print('Layer %d = %s : class name = %s' %
                  (i + 1, layer.name, layer.custom.className))
#        else:
#            print('Layer %d = %s' % (i, layer.name))

    print('Done.')