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

Fix TensorRT --dynamic excess outputs bug #8869

Merged
merged 5 commits into from
Aug 4, 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
16 changes: 9 additions & 7 deletions models/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,13 @@ def __init__(self, weights='yolov5s.pt', device=torch.device('cpu'), dnn=False,
context = model.create_execution_context()
bindings = OrderedDict()
fp16 = False # default updated below
dynamic_input = False
dynamic = False
for index in range(model.num_bindings):
name = model.get_binding_name(index)
dtype = trt.nptype(model.get_binding_dtype(index))
if model.binding_is_input(index):
if -1 in tuple(model.get_binding_shape(index)): # dynamic
dynamic_input = True
dynamic = True
context.set_binding_shape(index, tuple(model.get_profile_shape(0, index)[2]))
if dtype == np.float16:
fp16 = True
Expand Down Expand Up @@ -471,12 +471,14 @@ def forward(self, im, augment=False, visualize=False, val=False):
im = im.cpu().numpy() # FP32
y = self.executable_network([im])[self.output_layer]
elif self.engine: # TensorRT
if im.shape != self.bindings['images'].shape and self.dynamic_input:
self.context.set_binding_shape(self.model.get_binding_index('images'), im.shape) # reshape if dynamic
if self.dynamic and im.shape != self.bindings['images'].shape:
i_in, i_out = (self.model.get_binding_index(x) for x in ('images', 'output'))
self.context.set_binding_shape(i_in, im.shape) # reshape if dynamic
self.bindings['images'] = self.bindings['images']._replace(shape=im.shape)
assert im.shape == self.bindings['images'].shape, (
f"image shape {im.shape} exceeds model max shape {self.bindings['images'].shape}" if self.dynamic_input
else f"image shape {im.shape} does not match model shape {self.bindings['images'].shape}")
self.bindings['output'].data.resize_(tuple(self.context.get_binding_shape(i_out)))
s = self.bindings['images'].shape
assert im.shape == s, f"image shape {im.shape} " + \
f"exceeds model max shape {s}" if self.dynamic else f"does not match model shape {s}"
self.binding_addrs['images'] = int(im.data_ptr())
self.context.execute_v2(list(self.binding_addrs.values()))
y = self.bindings['output'].data
Expand Down