-
Notifications
You must be signed in to change notification settings - Fork 0
/
coreml_conv_body25.py
30 lines (23 loc) · 979 Bytes
/
coreml_conv_body25.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
import coremltools
from coremltools.models.neural_network import quantization_utils
import coremltools.proto.FeatureTypes_pb2 as ft
proto_file = 'pose/body_25/pose_deploy.prototxt'
caffe_model = 'pose/body_25/pose_iter_584000.caffemodel'
coreml_model = coremltools.converters.caffe.convert((caffe_model, proto_file)
, image_input_names='image'
, image_scale=1/255.
, is_bgr=True
)
# Image (Color 656 × 368)
# MultiArray (Double)
coreml_model.save('body_25.mlmodel')
model_fp16 = quantization_utils.quantize_weights(coreml_model, nbits=16)
model_fp16.save("body_25_fp16.mlmodel")
# spec = coremltools.utils.load_spec("body_25_fp16.mlmodel")
spec = model_fp16.get_spec()
def update_multiarray_to_float(feature):
if feature.type.HasField("multiArrayType"):
feature.type.multiArrayType.dataType = ft.ArrayFeatureType.FLOAT32
for feature in spec.description.output:
update_multiarray_to_float(feature)
coremltools.utils.save_spec(spec, "body_25_fp16.mlmodel")