8000 Add `--half` support for OpenVINO exports (#7615) · brinkqiang2python/yolov5@177da7f · GitHub
[go: up one dir, main page]

Skip to content

Commit 177da7f

Browse files
Add --half support for OpenVINO exports (ultralytics#7615)
* feature(export): add half support for openvino models * Update export.py * Update export.py Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
1 parent b53917d commit 177da7f

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

export.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def export_onnx(model, im, file, opset, train, dynamic, simplify, prefix=colorst
168168
LOGGER.info(f'{prefix} export failure: {e}')
169169

170170

171-
def export_openvino(model, im, file, prefix=colorstr('OpenVINO:')):
171+
def export_openvino(model, im, file, half, prefix=colorstr('OpenVINO:')):
172172
# YOLOv5 OpenVINO export
173173
try:
174174
check_requirements(('openvino-dev',)) # requires openvino-dev: https://pypi.org/project/openvino-dev/
@@ -177,7 +177,7 @@ def export_openvino(model, im, file, prefix=colorstr('OpenVINO:')):
177177
LOGGER.info(f'\n{prefix} starting export with openvino {ie.__version__}...')
178178
f = str(file).replace('.pt', '_openvino_model' + os.sep)
179179

180-
cmd = f"mo --input_model {file.with_suffix('.onnx')} --output_dir {f}"
180+
cmd = f"mo --input_model {file.with_suffix('.onnx')} --output_dir {f} --data_type {'FP16' if half else 'FP32'}"
181181
subprocess.check_output(cmd, shell=True)
182182

183183
LOGGER.info(f'{prefix} export success, saved as {f} ({file_size(f):.1f} MB)')
@@ -477,7 +477,7 @@ def run(
477477
# Load PyTorch model
478478
device = select_device(device)
479479
if half:
480-
assert device.type != 'cpu' or coreml, '--half only compatible with GPU export, i.e. use --device 0'
480+
assert device.type != 'cpu' or coreml or xml, '--half only compatible with GPU export, i.e. use --device 0'
481481
model = attempt_load(weights, map_location=device, inplace=True, fuse=True) # load FP32 model
482482
nc, names = model.nc, model.names # number of classes, class names
483483

@@ -491,7 +491,7 @@ def run(
491491
im = torch.zeros(batch_size, 3, *imgsz).to(device) # image size(1,3,320,192) BCHW iDetection
492492

493493
# Update model
494-
if half and not coreml:
494+
if half and not (coreml or xml):
495495
im, model = im.half(), model.half() # to FP16
496496
model.train() if train else model.eval() # training mode = no Detect() layer grid construction
497497
for k, m in model.named_modules():
@@ -515,7 +515,7 @@ def run(
515515
if onnx or xml: # OpenVINO requires ONNX
516516
f[2] = export_onnx(model, im, file, opset, train, dynamic, simplify)
517517
if xml: # OpenVINO
518-
f[3] = export_openvino(model, im, file)
518+
f[3] = export_openvino(model, im, file, half)
519519
if coreml:
520520
_, f[4] = export_coreml(model, im, file, int8, half)
521521

0 commit comments

Comments
 (0)
0