Skip to content

Commit b5b56a3

Browse files
authored
Add CoreML inference (ultralytics#6195)
* Add Apple CoreML inference * Cleanup
1 parent 7b31a53 commit b5b56a3

File tree

4 files changed

+9
-7
lines changed

4 files changed

+9
-7
lines changed

detect.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
yolov5s.onnx # ONNX Runtime or OpenCV DNN with --dnn
1818
yolov5s.xml # OpenVINO
1919
yolov5s.engine # TensorRT
20-
yolov5s.mlmodel # CoreML (under development)
20+
yolov5s.mlmodel # CoreML (MacOS-only)
2121
yolov5s_saved_model # TensorFlow SavedModel
2222
yolov5s.pb # TensorFlow GraphDef
2323
yolov5s.tflite # TensorFlow Lite

export.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
yolov5s.onnx # ONNX Runtime or OpenCV DNN with --dnn
2626
yolov5s.xml # OpenVINO
2727
yolov5s.engine # TensorRT
28-
yolov5s.mlmodel # CoreML (under development)
28+
yolov5s.mlmodel # CoreML (MacOS-only)
2929
yolov5s_saved_model # TensorFlow SavedModel
3030
yolov5s.pb # TensorFlow GraphDef
3131
yolov5s.tflite # TensorFlow Lite
@@ -156,7 +156,6 @@ def export_coreml(model, im, file, prefix=colorstr('CoreML:')):
156156
LOGGER.info(f'\n{prefix} starting export with coremltools {ct.__version__}...')
157157
f = file.with_suffix('.mlmodel')
158158

159-
model.train() # CoreML exports should be placed in model.train() mode
160159
ts = torch.jit.trace(model, im, strict=False) # TorchScript model
161160
ct_model = ct.convert(ts, inputs=[ct.ImageType('image', shape=im.shape, scale=1 / 255, bias=[0, 0, 0])])
162161
ct_model.save(f)

models/common.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -420,9 +420,12 @@ def forward(self, im, augment=False, visualize=False, val=False):
420420
im = Image.fromarray((im[0] * 255).astype('uint8'))
421421
# im = im.resize((192, 320), Image.ANTIALIAS)
422422
y = self.model.predict({'image': im}) # coordinates are xywh normalized
423-
box = xywh2xyxy(y['coordinates'] * [[w, h, w, h]]) # xyxy pixels
424-
conf, cls = y['confidence'].max(1), y['confidence'].argmax(1).astype(np.float)
425-
y = np.concatenate((box, conf.reshape(-1, 1), cls.reshape(-1, 1)), 1)
423+
if 'confidence' in y:
424+
box = xywh2xyxy(y['coordinates'] * [[w, h, w, h]]) # xyxy pixels
425+
conf, cls = y['confidence'].max(1), y['confidence'].argmax(1).astype(np.float)
426+
y = np.concatenate((box, conf.reshape(-1, 1), cls.reshape(-1, 1)), 1)
427+
else:
428+
y = y[list(y)[-1]] # last output
426429
else: # TensorFlow (SavedModel, GraphDef, Lite, Edge TPU)
427430
im = im.permute(0, 2, 3, 1).cpu().numpy() # torch BCHW to numpy BHWC shape(1,320,192,3)
428431
if self.saved_model: # SavedModel

val.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
yolov5s.onnx # ONNX Runtime or OpenCV DNN with --dnn
1212
yolov5s.xml # OpenVINO
1313
yolov5s.engine # TensorRT
14-
yolov5s.mlmodel # CoreML (under development)
14+
yolov5s.mlmodel # CoreML (MacOS-only)
1515
yolov5s_saved_model # TensorFlow SavedModel
1616
yolov5s.pb # TensorFlow GraphDef
1717
yolov5s.tflite # TensorFlow Lite

0 commit comments

Comments
 (0)