Update DetectMultiBackend for tuple outputs 2 (#9275)
* Update DetectMultiBackend for tuple outputs 2 Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com> * Update * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update * Update * Update Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
96c3c7f71d
commit
7aa263c5f2
3 files changed
+12
-7
No files matched your search
+7
-5
@@ -457,7 +457,7 @@ class DetectMultiBackend(nn.Module):
|
|||||||
|
|
||||||
self.__dict__.update(locals()) # assign all variables to self
|
self.__dict__.update(locals()) # assign all variables to self
|
||||||
|
|
||||||
def forward(self, im, augment=False, visualize=False, val=False):
|
def forward(self, im, augment=False, visualize=False):
|
||||||
# YOLOv5 MultiBackend inference
|
# YOLOv5 MultiBackend inference
|
||||||
b, ch, h, w = im.shape # batch, channel, height, width
|
b, ch, h, w = im.shape # batch, channel, height, width
|
||||||
if self.fp16 and im.dtype != torch.float16:
|
if self.fp16 and im.dtype != torch.float16:
|
||||||
@@ -521,10 +521,12 @@ class DetectMultiBackend(nn.Module):
|
|||||||
y[..., :4] *= [w, h, w, h] # xywh normalized to pixels
|
y[..., :4] *= [w, h, w, h] # xywh normalized to pixels
|
||||||
|
|
||||||
if isinstance(y, (list, tuple)):
|
if isinstance(y, (list, tuple)):
|
||||||
y = y[0]
|
return self.from_numpy(y[0]) if len(y) == 1 else [self.from_numpy(x) for x in y]
|
||||||
if isinstance(y, np.ndarray):
|
else:
|
||||||
y = torch.from_numpy(y).to(self.device)
|
return self.from_numpy(y)
|
||||||
return (y, []) if val else y
|
|
||||||
|
def from_numpy(self, x):
|
||||||
|
return torch.from_numpy(x).to(self.device) if isinstance(x, np.ndarray) else x
|
||||||
|
|
||||||
def warmup(self, imgsz=(1, 3, 640, 640)):
|
def warmup(self, imgsz=(1, 3, 640, 640)):
|
||||||
# Warmup model by running inference once
|
# Warmup model by running inference once
|
||||||
|
|||||||
@@ -813,6 +813,9 @@ def non_max_suppression(prediction,
|
|||||||
list of detections, on (n,6) tensor per image [xyxy, conf, cls]
|
list of detections, on (n,6) tensor per image [xyxy, conf, cls]
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if isinstance(prediction, (list, tuple)): # YOLOv5 model in validation model, output = (inference_out, loss_out)
|
||||||
|
prediction = prediction[0] # select only inference output
|
||||||
|
|
||||||
bs = prediction.shape[0] # batch size
|
bs = prediction.shape[0] # batch size
|
||||||
nc = prediction.shape[2] - 5 # number of classes
|
nc = prediction.shape[2] - 5 # number of classes
|
||||||
xc = prediction[..., 4] > conf_thres # candidates
|
xc = prediction[..., 4] > conf_thres # candidates
|
||||||
|
|||||||
@@ -204,11 +204,11 @@ def run(
|
|||||||
|
|
||||||
# Inference
|
# Inference
|
||||||
with dt[1]:
|
with dt[1]:
|
||||||
out, train_out = model(im) if training else model(im, augment=augment, val=True) # inference, loss outputs
|
out, train_out = model(im) if compute_loss else (model(im, augment=augment), None)
|
||||||
|
|
||||||
# Loss
|
# Loss
|
||||||
if compute_loss:
|
if compute_loss:
|
||||||
loss += compute_loss([x.float() for x in train_out], targets)[1] # box, obj, cls
|
loss += compute_loss(train_out, targets)[1] # box, obj, cls
|
||||||
|
|
||||||
# NMS
|
# NMS
|
||||||
targets[:, 2:] *= torch.tensor((width, height, width, height), device=device) # to pixels
|
targets[:, 2:] *= torch.tensor((width, height, width, height), device=device) # to pixels
|
||||||
|
|||||||
Reference in new issue
Block a user