Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DetectMultiBackend improvements #9269

Merged
merged 4 commits into from
Sep 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions models/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ def __init__(self, weights='yolov5s.pt', device=torch.device('cpu'), dnn=False,
import onnxruntime
providers = ['CUDAExecutionProvider', 'CPUExecutionProvider'] if cuda else ['CPUExecutionProvider']
session = onnxruntime.InferenceSession(w, providers=providers)
output_names = [x.name for x in session.get_outputs()]
meta = session.get_modelmeta().custom_metadata_map # metadata
if 'stride' in meta:
stride, names = int(meta['stride']), eval(meta['names'])
Expand All @@ -372,9 +373,7 @@ def __init__(self, weights='yolov5s.pt', device=torch.device('cpu'), dnn=False,
batch_size = batch_dim.get_length()
executable_network = ie.compile_model(network, device_name="CPU") # device_name="MYRIAD" for Intel NCS2
output_layer = next(iter(executable_network.outputs))
meta = Path(w).with_suffix('.yaml')
if meta.exists():
stride, names = self._load_metadata(meta) # load metadata
stride, names = self._load_metadata(Path(w).with_suffix('.yaml')) # load metadata
elif engine: # TensorRT
LOGGER.info(f'Loading {w} for TensorRT inference...')
import tensorrt as trt # https://developer.nvidia.com/nvidia-tensorrt-download
Expand Down Expand Up @@ -476,7 +475,7 @@ def forward(self, im, augment=False, visualize=False, val=False):
y = self.net.forward()
elif self.onnx: # ONNX Runtime
im = im.cpu().numpy() # torch to numpy
y = self.session.run([self.session.get_outputs()[0].name], {self.session.get_inputs()[0].name: im})[0]
y = self.session.run(self.output_names, {self.session.get_inputs()[0].name: im})[0]
elif self.xml: # OpenVINO
im = im.cpu().numpy() # FP32
y = self.executable_network([im])[self.output_layer]
Expand Down Expand Up @@ -524,7 +523,7 @@ def forward(self, im, augment=False, visualize=False, val=False):
y[..., :4] *= [w, h, w, h] # xywh normalized to pixels

if isinstance(y, np.ndarray):
y = torch.tensor(y, device=self.device)
y = torch.from_numpy(y).to(self.device)
return (y, []) if val else y

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

@staticmethod
def _load_metadata(f='path/to/meta.yaml'):
def _load_metadata(f=Path('path/to/meta.yaml')):
# Load metadata from meta.yaml if it exists
d = yaml_load(f)
return d['stride'], d['names'] # assign stride, names
if f.exists():
d = yaml_load(f)
return d['stride'], d['names'] # assign stride, names
return None, None


class AutoShape(nn.Module):
Expand Down