-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconvert_to_coreml.py
More file actions
50 lines (40 loc) · 1.22 KB
/
Copy pathconvert_to_coreml.py
File metadata and controls
50 lines (40 loc) · 1.22 KB
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
import argparse
import sys
import coremltools
import keras
from image_segmentation.icnet import ICNetModelFactory
def convert(argv):
parser = argparse.ArgumentParser(
description='Convert a Keras ICNet model to Core ML'
)
parser.add_argument(
'keras_checkpoint', nargs='?', type=str,
help='a Keras model checkpoint to load and convert.'
)
parser.add_argument(
'mlmodel_output', nargs='?', type=str,
help='a .mlmodel output file.'
)
args = parser.parse_args(argv)
original_keras_model = keras.models.load_model(args.keras_checkpoint)
img_size = original_keras_model.input_shape[1]
num_classes = original_keras_model.output_shape[0][-1]
keras_model = ICNetModelFactory.build(
img_size,
num_classes,
weights_path=args.keras_checkpoint,
train=False
)
mlmodel = coremltools.converters.keras.convert(
keras_model,
input_names='image',
image_input_names='image',
image_scale=2.0 / 255.0,
red_bias=-1.0,
green_bias=-1.0,
blue_bias=-1.0,
output_names='output'
)
mlmodel.save(args.mlmodel_output)
if __name__ == '__main__':
convert(sys.argv[1:])