2018-12-26 14:46:39 +00:00
|
|
|
import os
|
2019-01-03 22:44:51 +00:00
|
|
|
import onnx
|
2018-12-26 14:46:39 +00:00
|
|
|
from onnx import onnx_pb
|
|
|
|
from onnx_coreml import convert
|
|
|
|
import glob
|
|
|
|
|
|
|
|
|
|
|
|
# https://github.com/onnx/onnx-coreml
|
|
|
|
# http://machinethink.net/blog/mobilenet-ssdlite-coreml/
|
2018-12-26 14:57:18 +00:00
|
|
|
# https://github.com/hollance/YOLO-CoreML-MPSNNGraph
|
|
|
|
|
2018-12-26 14:46:39 +00:00
|
|
|
def main():
|
|
|
|
os.system('rm -rf saved_models && mkdir saved_models')
|
|
|
|
files = glob.glob('saved_models/*.onnx') + glob.glob('../yolov3/weights/*.onnx')
|
|
|
|
|
|
|
|
for f in files:
|
|
|
|
# 1. ONNX to CoreML
|
|
|
|
name = 'saved_models/' + f.split('/')[-1].replace('.onnx', '')
|
|
|
|
|
2019-01-03 22:44:51 +00:00
|
|
|
# # Load the ONNX model
|
|
|
|
model = onnx.load(f)
|
|
|
|
|
|
|
|
# Check that the IR is well formed
|
|
|
|
print(onnx.checker.check_model(model))
|
|
|
|
|
|
|
|
# Print a human readable representation of the graph
|
|
|
|
print(onnx.helper.printable_graph(model.graph))
|
|
|
|
|
2018-12-26 14:46:39 +00:00
|
|
|
model_file = open(f, 'rb')
|
|
|
|
model_proto = onnx_pb.ModelProto()
|
|
|
|
model_proto.ParseFromString(model_file.read())
|
2019-01-03 22:44:51 +00:00
|
|
|
yolov3_model = convert(model_proto, image_input_names=['0'], preprocessing_args={'image_scale': 1. / 255})
|
2018-12-26 14:46:39 +00:00
|
|
|
|
|
|
|
# 2. Reduce model to FP16, change outputs to DOUBLE and save
|
|
|
|
import coremltools
|
|
|
|
|
2019-01-03 22:44:51 +00:00
|
|
|
spec = yolov3_model.get_spec()
|
2018-12-26 14:46:39 +00:00
|
|
|
for i in range(2):
|
|
|
|
spec.description.output[i].type.multiArrayType.dataType = \
|
|
|
|
coremltools.proto.FeatureTypes_pb2.ArrayFeatureType.ArrayDataType.Value('DOUBLE')
|
|
|
|
|
|
|
|
spec = coremltools.utils.convert_neural_network_spec_weights_to_fp16(spec)
|
2019-01-03 22:44:51 +00:00
|
|
|
yolov3_model = coremltools.models.MLModel(spec)
|
2018-12-26 14:46:39 +00:00
|
|
|
|
2019-01-06 13:58:41 +00:00
|
|
|
name_out0 = spec.description.output[0].name
|
|
|
|
name_out1 = spec.description.output[1].name
|
|
|
|
|
2018-12-26 14:46:39 +00:00
|
|
|
num_classes = 80
|
2019-01-06 13:58:41 +00:00
|
|
|
num_anchors = 507 # 507 for yolov3-tiny,
|
2018-12-26 14:46:39 +00:00
|
|
|
spec.description.output[0].type.multiArrayType.shape.append(num_anchors)
|
2019-01-03 22:44:51 +00:00
|
|
|
spec.description.output[0].type.multiArrayType.shape.append(num_classes)
|
|
|
|
# spec.description.output[0].type.multiArrayType.shape.append(1)
|
2018-12-26 14:46:39 +00:00
|
|
|
|
|
|
|
spec.description.output[1].type.multiArrayType.shape.append(num_anchors)
|
2019-01-03 22:44:51 +00:00
|
|
|
spec.description.output[1].type.multiArrayType.shape.append(4)
|
|
|
|
# spec.description.output[1].type.multiArrayType.shape.append(1)
|
|
|
|
|
|
|
|
# rename
|
|
|
|
# input_mlmodel = input_tensor.replace(":", "__").replace("/", "__")
|
|
|
|
# class_output_mlmodel = class_output_tensor.replace(":", "__").replace("/", "__")
|
|
|
|
# bbox_output_mlmodel = bbox_output_tensor.replace(":", "__").replace("/", "__")
|
|
|
|
#
|
|
|
|
# for i in range(len(spec.neuralNetwork.layers)):
|
|
|
|
# if spec.neuralNetwork.layers[i].input[0] == input_mlmodel:
|
|
|
|
# spec.neuralNetwork.layers[i].input[0] = 'image'
|
|
|
|
# if spec.neuralNetwork.layers[i].output[0] == class_output_mlmodel:
|
|
|
|
# spec.neuralNetwork.layers[i].output[0] = 'scores'
|
|
|
|
# if spec.neuralNetwork.layers[i].output[0] == bbox_output_mlmodel:
|
|
|
|
# spec.neuralNetwork.layers[i].output[0] = 'boxes'
|
|
|
|
|
|
|
|
spec.neuralNetwork.preprocessing[0].featureName = '0'
|
|
|
|
|
|
|
|
yolov3_model.save(name + '.mlmodel')
|
2019-01-06 12:23:04 +00:00
|
|
|
# yolov3_model.visualize_spec()
|
2018-12-26 14:46:39 +00:00
|
|
|
print(spec.description)
|
|
|
|
|
2019-01-03 22:44:51 +00:00
|
|
|
# 2.5. Try to Predict:
|
|
|
|
from PIL import Image
|
|
|
|
img = Image.open('../yolov3/data/samples/zidane_416.jpg')
|
2019-01-06 12:23:04 +00:00
|
|
|
out = yolov3_model.predict({'0': img}, useCPUOnly=True)
|
2019-01-06 13:58:41 +00:00
|
|
|
print(out[name_out0].shape, out[name_out1].shape)
|
2019-01-03 22:44:51 +00:00
|
|
|
|
2018-12-26 14:46:39 +00:00
|
|
|
# 3. Create NMS protobuf
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
nms_spec = coremltools.proto.Model_pb2.Model()
|
|
|
|
nms_spec.specificationVersion = 3
|
|
|
|
|
|
|
|
for i in range(2):
|
2019-01-03 22:44:51 +00:00
|
|
|
decoder_output = yolov3_model._spec.description.output[i].SerializeToString()
|
2018-12-26 14:46:39 +00:00
|
|
|
|
|
|
|
nms_spec.description.input.add()
|
|
|
|
nms_spec.description.input[i].ParseFromString(decoder_output)
|
|
|
|
|
|
|
|
nms_spec.description.output.add()
|
|
|
|
nms_spec.description.output[i].ParseFromString(decoder_output)
|
|
|
|
|
|
|
|
nms_spec.description.output[0].name = 'confidence'
|
|
|
|
nms_spec.description.output[1].name = 'coordinates'
|
|
|
|
|
|
|
|
output_sizes = [num_classes, 4]
|
|
|
|
for i in range(2):
|
|
|
|
ma_type = nms_spec.description.output[i].type.multiArrayType
|
|
|
|
ma_type.shapeRange.sizeRanges.add()
|
|
|
|
ma_type.shapeRange.sizeRanges[0].lowerBound = 0
|
|
|
|
ma_type.shapeRange.sizeRanges[0].upperBound = -1
|
|
|
|
ma_type.shapeRange.sizeRanges.add()
|
|
|
|
ma_type.shapeRange.sizeRanges[1].lowerBound = output_sizes[i]
|
|
|
|
ma_type.shapeRange.sizeRanges[1].upperBound = output_sizes[i]
|
|
|
|
del ma_type.shape[:]
|
|
|
|
|
|
|
|
nms = nms_spec.nonMaximumSuppression
|
2019-01-06 13:58:41 +00:00
|
|
|
nms.confidenceInputFeatureName = name_out0 # 1x507x80
|
|
|
|
nms.coordinatesInputFeatureName = name_out1 # 1x507x4
|
2018-12-26 14:46:39 +00:00
|
|
|
nms.confidenceOutputFeatureName = 'confidence'
|
|
|
|
nms.coordinatesOutputFeatureName = 'coordinates'
|
|
|
|
nms.iouThresholdInputFeatureName = 'iouThreshold'
|
|
|
|
nms.confidenceThresholdInputFeatureName = 'confidenceThreshold'
|
|
|
|
|
2019-01-06 13:58:41 +00:00
|
|
|
nms.iouThreshold = 0.4
|
|
|
|
nms.confidenceThreshold = 0.5
|
2018-12-26 14:46:39 +00:00
|
|
|
nms.pickTop.perClass = True
|
|
|
|
|
|
|
|
labels = np.loadtxt('../yolov3/data/coco.names', dtype=str, delimiter='\n')
|
|
|
|
nms.stringClassLabels.vector.extend(labels)
|
|
|
|
|
|
|
|
nms_model = coremltools.models.MLModel(nms_spec)
|
|
|
|
nms_model.save(name + '_nms.mlmodel')
|
|
|
|
|
2019-01-03 22:44:51 +00:00
|
|
|
# out_nms = nms_model.predict({
|
|
|
|
# '143': out['143'].squeeze().reshape((80, 507)),
|
|
|
|
# '144': out['144'].squeeze().reshape((4, 507))
|
|
|
|
# })
|
|
|
|
# print(out_nms['confidence'].shape, out_nms['coordinates'].shape)
|
|
|
|
|
|
|
|
# # # 3.5 Add Softmax model
|
|
|
|
# from coremltools.models import datatypes
|
|
|
|
# from coremltools.models import neural_network
|
|
|
|
#
|
|
|
|
# input_features = [
|
|
|
|
# ("141", datatypes.Array(num_anchors, num_classes, 1)),
|
|
|
|
# ("143", datatypes.Array(num_anchors, 4, 1))
|
|
|
|
# ]
|
|
|
|
#
|
|
|
|
# output_features = [
|
|
|
|
# ("141", datatypes.Array(num_anchors, num_classes, 1)),
|
|
|
|
# ("143", datatypes.Array(num_anchors, 4, 1))
|
|
|
|
# ]
|
|
|
|
#
|
|
|
|
# builder = neural_network.NeuralNetworkBuilder(input_features, output_features)
|
|
|
|
# builder.add_softmax(name="softmax_pcls",
|
|
|
|
# dim=(0, 3, 2, 1),
|
|
|
|
# input_name="scores",
|
|
|
|
# output_name="permute_scores_output")
|
|
|
|
# softmax_model = coremltools.models.MLModel(builder.spec)
|
|
|
|
# softmax_model.save("softmax.mlmodel")
|
|
|
|
|
2018-12-26 14:46:39 +00:00
|
|
|
# 4. Pipeline models togethor
|
|
|
|
from coremltools.models import datatypes
|
|
|
|
# from coremltools.models import neural_network
|
|
|
|
from coremltools.models.pipeline import Pipeline
|
|
|
|
|
2019-01-03 22:44:51 +00:00
|
|
|
input_features = [('0', datatypes.Array(3, 416, 416)),
|
2018-12-26 14:46:39 +00:00
|
|
|
('iouThreshold', datatypes.Double()),
|
|
|
|
('confidenceThreshold', datatypes.Double())]
|
|
|
|
|
|
|
|
output_features = ['confidence', 'coordinates']
|
|
|
|
|
|
|
|
pipeline = Pipeline(input_features, output_features)
|
|
|
|
|
|
|
|
# Add 3rd dimension of size 1 (apparently not needed, produces error on compile)
|
2019-01-03 22:44:51 +00:00
|
|
|
yolov3_output = yolov3_model._spec.description.output
|
|
|
|
yolov3_output[0].type.multiArrayType.shape[:] = [num_anchors, num_classes, 1]
|
|
|
|
yolov3_output[1].type.multiArrayType.shape[:] = [num_anchors, 4, 1]
|
|
|
|
|
|
|
|
nms_input = nms_model._spec.description.input
|
|
|
|
for i in range(2):
|
|
|
|
nms_input[i].type.multiArrayType.shape[:] = yolov3_output[i].type.multiArrayType.shape[:]
|
2018-12-26 14:46:39 +00:00
|
|
|
|
|
|
|
# And now we can add the three models, in order:
|
2019-01-03 22:44:51 +00:00
|
|
|
pipeline.add_model(yolov3_model)
|
|
|
|
|
2018-12-26 14:46:39 +00:00
|
|
|
pipeline.add_model(nms_model)
|
|
|
|
|
|
|
|
# Correct datatypes
|
2019-01-03 22:44:51 +00:00
|
|
|
pipeline.spec.description.input[0].ParseFromString(yolov3_model._spec.description.input[0].SerializeToString())
|
2018-12-26 14:46:39 +00:00
|
|
|
pipeline.spec.description.output[0].ParseFromString(nms_model._spec.description.output[0].SerializeToString())
|
|
|
|
pipeline.spec.description.output[1].ParseFromString(nms_model._spec.description.output[1].SerializeToString())
|
|
|
|
|
|
|
|
# Update metadata
|
|
|
|
pipeline.spec.description.metadata.versionString = 'yolov3-tiny.pt imported from PyTorch'
|
|
|
|
pipeline.spec.description.metadata.shortDescription = 'https://github.com/ultralytics/yolov3'
|
|
|
|
pipeline.spec.description.metadata.author = 'glenn.jocher@ultralytics.com'
|
|
|
|
pipeline.spec.description.metadata.license = 'https://github.com/ultralytics/yolov3'
|
|
|
|
|
|
|
|
user_defined_metadata = {'classes': ','.join(labels),
|
|
|
|
'iou_threshold': str(nms.iouThreshold),
|
|
|
|
'confidence_threshold': str(nms.confidenceThreshold)}
|
|
|
|
pipeline.spec.description.metadata.userDefined.update(user_defined_metadata)
|
|
|
|
|
|
|
|
# Save the model
|
|
|
|
pipeline.spec.specificationVersion = 3
|
|
|
|
final_model = coremltools.models.MLModel(pipeline.spec)
|
|
|
|
final_model.save((name + '_pipelined.mlmodel'))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|