Example #1
0
def caffe_model(caffe_model_file_path, caffe_prototxt_file_path):
    # Sometimes, critical information in the Caffe converter is missing from the .caffemodel file. 
    #This information is present in the deploy.prototxt file. 
    #You can provide us with both files in the conversion process.
 
    if caffe_model_file_path == True and caffe_prototxt_file_path == True:
        #caffe to coreml
        caffe_to_coreml = caffe.convert(caffe_input, caffe_prototxt)
        caffe_to_coreml = caffe_to_coreml.save('./path/to/save/caffe_to_coreml.mlmodel')
    elif caffe_model == True and caffe_prototxt == False:
        caffe_to_coreml = caffe.convert(caffe_input)
        caffe_to_coreml = caffe_to_coreml.save('./path/to/save/caffe_to_coreml.mlmodel')     
    return caffe_to_coreml
Example #2
0
def conversion_to_mlmodel(net_name, proto_name, layer_type, input_layer):
    filename = '{}nets/{}/mlkitmodels/{}.mlmodel'.format(
        nets_path, layer_type, net_name)
    caffe_model_path = '{}nets/{}/caffemodels/{}.caffemodel'.format(
        nets_path, layer_type, net_name),
    proto_path = '{}nets/{}/prototxt/{}.prototxt'.format(
        nets_path, layer_type, proto_name)
    model_path = caffe_model_path[0]
    if isinstance(input_layer, str):
        input_layer = [input_layer]
    try:
        model = caffe_converter.convert((model_path, proto_path), )
        model.save(filename)
    except RuntimeError as e:
        print(e)
        return False
    return True
Example #3
0
    def _evaluate_and_test_model_meanpreprocessing(self, n):

        failed_tests_load = []
        failed_tests_conversion = []
        failed_tests_evaluation = []

        extract_tarfile('{}nets/{}.gz'.format(nets_path, FOLDER_NAME),
                        '{}nets/'.format(nets_path))

        path_prototxt = '{}nets/{}/{}/image{}.prototxt'.format(
            nets_path, FOLDER_NAME, str(n), str(n))
        path_caffemodel = '{}nets/{}/{}/image{}.caffemodel'.format(
            nets_path, FOLDER_NAME, str(n), str(n))
        path_mlmodel = '{}nets/{}/{}/image{}.mlmodel'.format(
            nets_path, FOLDER_NAME, str(n), str(n))
        if n == 1:
            path_binaryproto = '{}nets/{}/1/mean_binary_proto1.binaryproto'.format(
                nets_path, FOLDER_NAME)
        else:
            path_binaryproto = dict()
            for i in range(n):
                path_binaryproto["data{}".format(
                    str(i + 1)
                )] = '{}nets/{}/{}/mean_binary_proto{}.binaryproto'.format(
                    nets_path, FOLDER_NAME, str(n), str(i + 1))

        image_input_names = []
        for i in range(n):
            image_input_names.append("data{}".format(str(i + 1)))

        #convert it
        try:
            model = caffe_converter.convert(
                (path_caffemodel, path_prototxt, path_binaryproto),
                image_input_names=image_input_names)
            model.save(path_mlmodel)
        except RuntimeError as e:
            print(e)
            failed_tests_conversion.append(
                'image mean preprocessing: conversion failure')

        #load it (compile it)
        load_result = load_mlmodel(path_mlmodel)
        if load_result is False:
            failed_tests_load.append('image mean preprocessing: load failure')

        #load Caffe's input and output
        with open('{}nets/{}/{}/input.json'.format(nets_path, FOLDER_NAME,
                                                   str(n))) as data_file:
            input_data_dict = json.load(data_file)
        with open('{}nets/{}/{}/output.json'.format(nets_path, FOLDER_NAME,
                                                    str(n))) as data_file:
            output_data_dict = json.load(data_file)

        output_data = np.array(output_data_dict["output_data"])

        coreml_input_dict = dict()

        for i in range(n):
            input_data = np.array(input_data_dict["input_data{}".format(
                str(i + 1))]).astype(np.uint8)
            img = PIL.Image.fromarray(
                np.transpose(input_data[0, :, :, :], [1, 2, 0]))
            coreml_input_dict["data{}".format(str(i + 1))] = img

        #load and evaluate mlmodel
        mlmodel = coremltools.models.MLModel(path_mlmodel)
        coreml_out = mlmodel.predict(coreml_input_dict)['output']

        caffe_preds = output_data.flatten()
        coreml_preds = coreml_out.flatten()
        if len(caffe_preds) != len(coreml_preds):
            failed_tests_evaluation.append(
                'single image mean preprocessing: evaluation failure')

        max_relative_error = compare_models(output_data.flatten(),
                                            coreml_out.flatten())
        if max_relative_error > 0.001:
            failed_tests_evaluation.append(
                'single image mean preprocessing: evaluation failure')

        self.assertEqual(failed_tests_conversion, [])
        self.assertEqual(failed_tests_load, [])
        self.assertEqual(failed_tests_evaluation, [])
        shutil.rmtree('{}nets/{}'.format(nets_path, FOLDER_NAME))