@@ -354,6 +354,7 @@ def __init__(self, weights='yolov5s.pt', device=torch.device('cpu'), dnn=False,
354
354
import onnxruntime
355
355
providers = ['CUDAExecutionProvider' , 'CPUExecutionProvider' ] if cuda else ['CPUExecutionProvider' ]
356
356
session = onnxruntime .InferenceSession (w , providers = providers )
357
+ output_names = [x .name for x in session .get_outputs ()]
357
358
meta = session .get_modelmeta ().custom_metadata_map # metadata
358
359
if 'stride' in meta :
359
360
stride , names = int (meta ['stride' ]), eval (meta ['names' ])
@@ -372,9 +373,7 @@ def __init__(self, weights='yolov5s.pt', device=torch.device('cpu'), dnn=False,
372
373
batch_size = batch_dim .get_length ()
373
374
executable_network = ie .compile_model (network , device_name = "CPU" ) # device_name="MYRIAD" for Intel NCS2
374
375
output_layer = next (iter (executable_network .outputs ))
375
- meta = Path (w ).with_suffix ('.yaml' )
376
- if meta .exists ():
377
- stride , names = self ._load_metadata (meta ) # load metadata
376
+ stride , names = self ._load_metadata (Path (w ).with_suffix ('.yaml' )) # load metadata
378
377
elif engine : # TensorRT
379
378
LOGGER .info (f'Loading { w } for TensorRT inference...' )
380
379
import tensorrt as trt # https://developer.nvidia.com/nvidia-tensorrt-download
@@ -476,7 +475,7 @@ def forward(self, im, augment=False, visualize=False, val=False):
476
475
y = self .net .forward ()
477
476
elif self .onnx : # ONNX Runtime
478
477
im = im .cpu ().numpy () # torch to numpy
479
- y = self .session .run ([ self .session . get_outputs ()[ 0 ]. name ] , {self .session .get_inputs ()[0 ].name : im })[0 ]
478
+ y = self .session .run (self .output_names , {self .session .get_inputs ()[0 ].name : im })[0 ]
480
479
elif self .xml : # OpenVINO
481
480
im = im .cpu ().numpy () # FP32
482
481
y = self .executable_network ([im ])[self .output_layer ]
@@ -524,7 +523,7 @@ def forward(self, im, augment=False, visualize=False, val=False):
524
523
y [..., :4 ] *= [w , h , w , h ] # xywh normalized to pixels
525
524
526
525
if isinstance (y , np .ndarray ):
527
- y = torch .tensor ( y , device = self .device )
526
+ y = torch .from_numpy ( y ). to ( self .device )
528
527
return (y , []) if val else y
529
528
530
529
def warmup (self , imgsz = (1 , 3 , 640 , 640 )):
@@ -548,10 +547,12 @@ def _model_type(p='path/to/model.pt'):
548
547
return pt , jit , onnx , xml , engine , coreml , saved_model , pb , tflite , edgetpu , tfjs
549
548
550
549
@staticmethod
551
- def _load_metadata (f = 'path/to/meta.yaml' ):
550
+ def _load_metadata (f = Path ( 'path/to/meta.yaml' ) ):
552
551
# Load metadata from meta.yaml if it exists
553
- d = yaml_load (f )
554
- return d ['stride' ], d ['names' ] # assign stride, names
552
+ if f .exists ():
553
+ d = yaml_load (f )
554
+ return d ['stride' ], d ['names' ] # assign stride, names
555
+ return None , None
555
556
556
557
557
558
class AutoShape (nn .Module ):
0 commit comments