-
Notifications
You must be signed in to change notification settings - Fork 1
/
coremlconverter.py
52 lines (40 loc) · 1.95 KB
/
coremlconverter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# This script is obtained from:
# https://www.pyimagesearch.com/2018/04/23/running-keras-models-on-ios-with-coreml/
# Author: Adrian Rosebrock
# This script converts from Keras .model file to Core ML .mlmodel file
from keras.models import load_model
import coremltools
# import argparse
import pickle
LABEL_PATH = r"C:\Users\PC-user\Dropbox\workspace-python\Landmarks-of-Melbourne\output\lb-sml"
MODEL_PATH = r"C:\Users\PC-user\Dropbox\workspace-python\Landmarks-of-Melbourne\output\model-sml"
OUTPUT_PATH = r"C:\Users\PC-user\Dropbox\workspace-python\Landmarks-of-Melbourne\output\ml-model-sml"
# USAGE
# python coremlconverter.py --model pokedex.model --labelbin lb.pickle
# construct the argument parser and parse the arguments
# ap = argparse.ArgumentParser()
# ap.add_argument("-m", "--model", required=True,
# help="path to trained model model")
# ap.add_argument("-l", "--labelbin", required=True,
# help="path to label binarizer")
# args = vars(ap.parse_args())
# load the class labels
print("[INFO] loading class labels from label binarizer")
MobileNet_label = pickle.loads(open(LABEL_PATH, "rb").read())
class_labels = MobileNet_label.classes_.tolist()
print("[INFO] class labels: {}".format(class_labels))
# load the trained convolutional neural network
print("[INFO] loading model...")
MobileNet_model = load_model(MODEL_PATH)
# convert the model to coreml format
print("[INFO] converting model")
coreml_model = coremltools.converters.keras.convert(MobileNet_model,
input_names="image",
image_input_names="image",
image_scale=1/255.0,
class_labels=class_labels,
is_bgr=True)
# save the model to disk
output = OUTPUT_PATH + ".mlmodel"
print("[INFO] saving model as {}".format(output))
coreml_model.save(output)