How to use the coremltools.converters.keras.convert function in coremltools

To help you get started, we’ve selected a few coremltools examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github junhwanjang / face_landmark_dnn / testing / convert.py View on Github external
def keras_to_coreml():
    with custom_object_scope({'smoothL1': smoothL1, 'relu6': relu6}):
        ml_model = load_model(MODEL_PATH)
    coreml_model = coremltools.converters.keras.convert(ml_model, 
                                                        input_names='image', image_input_names='image', 
                                                        is_bgr=False)
    coreml_model.save(ML_MODEL_PATH)
github lutzroeder / netron / tools / coreml-script.py View on Github external
def convert():
    file = sys.argv[2];
    base, extension = os.path.splitext(file)
    if extension == '.h5':
        import coremltools
        coreml_model = coremltools.converters.keras.convert(file)
        coreml_model.save(base + '.mlmodel')
    elif extension == '.pkl':
        import coremltools
        import sklearn
        sklearn_model = sklearn.externals.joblib.load(file)
        coreml_model = coremltools.converters.sklearn.convert(sklearn_model)
        coreml_model.save(base + '.mlmodel')
github apple / coremltools / coremltools / _scripts / converter.py View on Github external
return 1 # error
        return 0

    elif args.srcModelFormat == 'keras':
        try:
            if not args.inputNames:
                raise TypeError("Neural network 'inputNames' are required for converting Keras models.")
            if not args.outputNames:
                raise TypeError("Neural network 'outputNames' are required for converting Keras models.")

            if args.kerasJsonPath:
                model = (args.kerasJsonPath, args.srcModelPath)
            else:
                model = args.srcModelPath
            
            model = converters.keras.convert(model,
                                    args.inputNames,
                                    args.outputNames,
                                    image_input_names = set(args.imageInputNames) if args.imageInputNames else None,
                                    is_bgr = args.isBGR,
                                    red_bias = args.redBias,
                                    blue_bias = args.blueBias,
                                    green_bias = args.greenBias,
                                    gray_bias = args.grayBias,
                                    image_scale = args.scale,
                                    class_labels = args.classInputPath if args.classInputPath else None,
                                    predicted_feature_name = args.predictedFeatureName,
                                    respect_trainable = args.respectTrainable)
            model.save(args.dstModelPath)
        except Exception as e:
            print('error: coremlconverter: %s.' % str(e))
            return 1 # error
github junhwanjang / face_landmark_dnn / mobile_converter / keras_to_coreml.py View on Github external
def keras_to_coreml():
    with custom_object_scope({'smoothL1': smoothL1, 'relu6': relu6, 'DepthwiseConv2D': mobilenet.DepthwiseConv2D}):
        ml_model = load_model(MODEL_PATH)
    coreml_model = coremltools.converters.keras.convert(ml_model, 
                                                        input_names='image', image_input_names='image', 
                                                        is_bgr=False)
    coreml_model.save(ML_MODEL_PATH)
github ashislaha / CarDetection-Keras / model / mlmodel_converter.py View on Github external
import coremltools

DNN_ml_model = coremltools.converters.keras.convert('car_detection_keras_DNN_model.h5')
DNN_ml_model.author = 'Ashis Laha'
DNN_ml_model.description = 'Use for Car Detection'
DNN_ml_model.save('car_detection_keras_DNN.mlmodel')
print(DNN_ml_model)


DNN_ml_model = coremltools.converters.keras.convert('car_detection_keras_CNN_model.h5')
DNN_ml_model.author = 'Ashis Laha'
DNN_ml_model.description = 'Use for Car Detection'
DNN_ml_model.save('car_detection_keras_CNN.mlmodel')
print(DNN_ml_model)
github akirasosa / mobile-semantic-segmentation / coreml-converter.py View on Github external
def main(input_model_path):
    """
    Convert hdf5 file to CoreML model.
    :param input_model_path:
    :return:
    """
    out_path = re.sub(r"h5$", 'mlmodel', input_model_path)

    hack_coremltools()

    with CustomObjectScope(custom_objects()):
        model = load_model(input_model_path)
        # https://github.com/akirasosa/mobile-semantic-segmentation/issues/6#issuecomment-344508193
        coreml_model = coremltools.converters.keras.convert(model,
                                                            input_names='image',
                                                            image_input_names='image',
                                                            red_bias=29.24429131 / 64.881128947,
                                                            green_bias=29.24429131 / 64.881128947,
                                                            blue_bias=29.24429131 / 64.881128947,
                                                            image_scale=1. / 64.881128947)
    coreml_model.save(out_path)

    print('CoreML model is created at %s' % out_path)