@@ -282,6 +282,7 @@ def __init__(self, weights='yolov5s.pt', device=None, dnn=False, data=None):
282
282
# PyTorch: weights = *.pt
283
283
# TorchScript: *.torchscript
284
284
# CoreML: *.mlmodel
285
+ # OpenVINO: *.xml
285
286
# TensorFlow: *_saved_model
286
287
# TensorFlow: *.pb
287
288
# TensorFlow Lite: *.tflite
@@ -294,31 +295,38 @@ def __init__(self, weights='yolov5s.pt', device=None, dnn=False, data=None):
294
295
super ().__init__ ()
295
296
w = str (weights [0 ] if isinstance (weights , list ) else weights )
296
297
suffix = Path (w ).suffix .lower ()
297
- suffixes = ['.pt' , '.torchscript' , '.onnx' , '.engine' , '.tflite' , '.pb' , '' , '.mlmodel' ]
298
+ suffixes = ['.pt' , '.torchscript' , '.onnx' , '.engine' , '.tflite' , '.pb' , '' , '.mlmodel' , '.xml' ]
298
299
check_suffix (w , suffixes ) # check weights have acceptable suffix
299
- pt , jit , onnx , engine , tflite , pb , saved_model , coreml = (suffix == x for x in suffixes ) # backend booleans
300
+ pt , jit , onnx , engine , tflite , pb , saved_model , coreml , xml = (suffix == x for x in suffixes ) # backends
300
301
stride , names = 64 , [f'class{ i } ' for i in range (1000 )] # assign defaults
301
302
w = attempt_download (w ) # download if not local
302
303
if data : # data.yaml path (optional)
303
304
with open (data , errors = 'ignore' ) as f :
304
305
names = yaml .safe_load (f )['names' ] # class names
305
306
306
- if jit : # TorchScript
307
+ if pt : # PyTorch
308
+ model = attempt_load (weights if isinstance (weights , list ) else w , map_location = device )
309
+ stride = int (model .stride .max ()) # model stride
310
+ names = model .module .names if hasattr (model , 'module' ) else model .names # get class names
311
+ self .model = model # explicitly assign for to(), cpu(), cuda(), half()
312
+ elif jit : # TorchScript
307
313
LOGGER .info (f'Loading { w } for TorchScript inference...' )
308
314
extra_files = {'config.txt' : '' } # model metadata
309
315
model = torch .jit .load (w , _extra_files = extra_files )
310
316
if extra_files ['config.txt' ]:
311
317
d = json .loads (extra_files ['config.txt' ]) # extra_files dict
312
318
stride , names = int (d ['stride' ]), d ['names' ]
313
- elif pt : # PyTorch
314
- model = attempt_load (weights if isinstance (weights , list ) else w , map_location = device )
315
- stride = int (model .stride .max ()) # model stride
316
- names = model .module .names if hasattr (model , 'module' ) else model .names # get class names
317
- self .model = model # explicitly assign for to(), cpu(), cuda(), half()
318
319
elif coreml : # CoreML
319
320
LOGGER .info (f'Loading { w } for CoreML inference...' )
320
321
import coremltools as ct
321
322
model = ct .models .MLModel (w )
323
+ elif xml : # OpenVINO
324
+ LOGGER .info (f'Loading { w } for OpenVINO inference...' )
325
+ check_requirements (('openvino-dev' ,)) # requires openvino-dev: https://pypi.org/project/openvino-dev/
326
+ import openvino .inference_engine as ie
327
+ core = ie .IECore ()
328
+ network = core .read_network (model = w , weights = Path (w ).with_suffix ('.bin' )) # *.xml, *.bin paths
329
+ executable_network = core .load_network (network , device_name = 'CPU' , num_requests = 1 )
322
330
elif dnn : # ONNX OpenCV DNN
323
331
LOGGER .info (f'Loading { w } for ONNX OpenCV DNN inference...' )
324
332
check_requirements (('opencv-python>=4.5.4' ,))
@@ -403,6 +411,13 @@ def forward(self, im, augment=False, visualize=False, val=False):
403
411
y = self .net .forward ()
404
412
else : # ONNX Runtime
405
413
y = self .session .run ([self .session .get_outputs ()[0 ].name ], {self .session .get_inputs ()[0 ].name : im })[0 ]
414
+ elif self .xml : # OpenVINO
415
+ im = im .cpu ().numpy () # FP32
416
+ desc = self .ie .TensorDesc (precision = 'FP32' , dims = im .shape , layout = 'NCHW' ) # Tensor Description
417
+ request = self .executable_network .requests [0 ] # inference request
418
+ request .set_blob (blob_name = 'images' , blob = self .ie .Blob (desc , im )) # name=next(iter(request.input_blobs))
419
+ request .infer ()
420
+ y = request .output_blobs ['output' ].buffer # name=next(iter(request.output_blobs))
406
421
elif self .engine : # TensorRT
407
422
assert im .shape == self .bindings ['images' ].shape , (im .shape , self .bindings ['images' ].shape )
408
423
self .binding_addrs ['images' ] = int (im .data_ptr ())
0 commit comments