Skip to content

Commit 59b9ad1

Browse files
glenn-jocherClay Januhowski
authored andcommitted
DetectMultiBackend improvements (ultralytics#9269)
* Update common.py Signed-off-by: Glenn Jocher <[email protected]> * Update common.py Signed-off-by: Glenn Jocher <[email protected]> * Update common.py Signed-off-by: Glenn Jocher <[email protected]> * Update common.py Signed-off-by: Glenn Jocher <[email protected]> Signed-off-by: Glenn Jocher <[email protected]>
1 parent 65bc4ba commit 59b9ad1

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

models/common.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ def __init__(self, weights='yolov5s.pt', device=torch.device('cpu'), dnn=False,
354354
import onnxruntime
355355
providers = ['CUDAExecutionProvider', 'CPUExecutionProvider'] if cuda else ['CPUExecutionProvider']
356356
session = onnxruntime.InferenceSession(w, providers=providers)
357+
output_names = [x.name for x in session.get_outputs()]
357358
meta = session.get_modelmeta().custom_metadata_map # metadata
358359
if 'stride' in meta:
359360
stride, names = int(meta['stride']), eval(meta['names'])
@@ -372,9 +373,7 @@ def __init__(self, weights='yolov5s.pt', device=torch.device('cpu'), dnn=False,
372373
batch_size = batch_dim.get_length()
373374
executable_network = ie.compile_model(network, device_name="CPU") # device_name="MYRIAD" for Intel NCS2
374375
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
378377
elif engine: # TensorRT
379378
LOGGER.info(f'Loading {w} for TensorRT inference...')
380379
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):
476475
y = self.net.forward()
477476
elif self.onnx: # ONNX Runtime
478477
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]
480479
elif self.xml: # OpenVINO
481480
im = im.cpu().numpy() # FP32
482481
y = self.executable_network([im])[self.output_layer]
@@ -524,7 +523,7 @@ def forward(self, im, augment=False, visualize=False, val=False):
524523
y[..., :4] *= [w, h, w, h] # xywh normalized to pixels
525524

526525
if isinstance(y, np.ndarray):
527-
y = torch.tensor(y, device=self.device)
526+
y = torch.from_numpy(y).to(self.device)
528527
return (y, []) if val else y
529528

530529
def warmup(self, imgsz=(1, 3, 640, 640)):
@@ -548,10 +547,12 @@ def _model_type(p='path/to/model.pt'):
548547
return pt, jit, onnx, xml, engine, coreml, saved_model, pb, tflite, edgetpu, tfjs
549548

550549
@staticmethod
551-
def _load_metadata(f='path/to/meta.yaml'):
550+
def _load_metadata(f=Path('path/to/meta.yaml')):
552551
# 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
555556

556557

557558
class AutoShape(nn.Module):

0 commit comments

Comments
 (0)