glenn-jocher commited on
Commit
d921214
1 Parent(s): 1ca2d26

Add xywhn2xyxy() (#1983)

Browse files
Files changed (2) hide show
  1. utils/datasets.py +10 -25
  2. utils/general.py +10 -0
utils/datasets.py CHANGED
@@ -20,7 +20,7 @@ from PIL import Image, ExifTags
20
  from torch.utils.data import Dataset
21
  from tqdm import tqdm
22
 
23
- from utils.general import xyxy2xywh, xywh2xyxy, clean_str
24
  from utils.torch_utils import torch_distributed_zero_first
25
 
26
  # Parameters
@@ -515,16 +515,9 @@ class LoadImagesAndLabels(Dataset): # for training/testing
515
  img, ratio, pad = letterbox(img, shape, auto=False, scaleup=self.augment)
516
  shapes = (h0, w0), ((h / h0, w / w0), pad) # for COCO mAP rescaling
517
 
518
- # Load labels
519
- labels = []
520
- x = self.labels[index]
521
- if x.size > 0:
522
- # Normalized xywh to pixel xyxy format
523
- labels = x.copy()
524
- labels[:, 1] = ratio[0] * w * (x[:, 1] - x[:, 3] / 2) + pad[0] # pad width
525
- labels[:, 2] = ratio[1] * h * (x[:, 2] - x[:, 4] / 2) + pad[1] # pad height
526
- labels[:, 3] = ratio[0] * w * (x[:, 1] + x[:, 3] / 2) + pad[0]
527
- labels[:, 4] = ratio[1] * h * (x[:, 2] + x[:, 4] / 2) + pad[1]
528
 
529
  if self.augment:
530
  # Augment imagespace
@@ -674,13 +667,9 @@ def load_mosaic(self, index):
674
  padh = y1a - y1b
675
 
676
  # Labels
677
- x = self.labels[index]
678
- labels = x.copy()
679
- if x.size > 0: # Normalized xywh to pixel xyxy format
680
- labels[:, 1] = w * (x[:, 1] - x[:, 3] / 2) + padw
681
- labels[:, 2] = h * (x[:, 2] - x[:, 4] / 2) + padh
682
- labels[:, 3] = w * (x[:, 1] + x[:, 3] / 2) + padw
683
- labels[:, 4] = h * (x[:, 2] + x[:, 4] / 2) + padh
684
  labels4.append(labels)
685
 
686
  # Concat/clip labels
@@ -737,13 +726,9 @@ def load_mosaic9(self, index):
737
  x1, y1, x2, y2 = [max(x, 0) for x in c] # allocate coords
738
 
739
  # Labels
740
- x = self.labels[index]
741
- labels = x.copy()
742
- if x.size > 0: # Normalized xywh to pixel xyxy format
743
- labels[:, 1] = w * (x[:, 1] - x[:, 3] / 2) + padx
744
- labels[:, 2] = h * (x[:, 2] - x[:, 4] / 2) + pady
745
- labels[:, 3] = w * (x[:, 1] + x[:, 3] / 2) + padx
746
- labels[:, 4] = h * (x[:, 2] + x[:, 4] / 2) + pady
747
  labels9.append(labels)
748
 
749
  # Image
 
20
  from torch.utils.data import Dataset
21
  from tqdm import tqdm
22
 
23
+ from utils.general import xyxy2xywh, xywh2xyxy, xywhn2xyxy, clean_str
24
  from utils.torch_utils import torch_distributed_zero_first
25
 
26
  # Parameters
 
515
  img, ratio, pad = letterbox(img, shape, auto=False, scaleup=self.augment)
516
  shapes = (h0, w0), ((h / h0, w / w0), pad) # for COCO mAP rescaling
517
 
518
+ labels = self.labels[index].copy()
519
+ if labels.size: # normalized xywh to pixel xyxy format
520
+ labels[:, 1:] = xywhn2xyxy(labels[:, 1:], ratio[0] * w, ratio[1] * h, padw=pad[0], padh=pad[1])
 
 
 
 
 
 
 
521
 
522
  if self.augment:
523
  # Augment imagespace
 
667
  padh = y1a - y1b
668
 
669
  # Labels
670
+ labels = self.labels[index].copy()
671
+ if labels.size:
672
+ labels[:, 1:] = xywhn2xyxy(labels[:, 1:], w, h, padw, padh) # normalized xywh to pixel xyxy format
 
 
 
 
673
  labels4.append(labels)
674
 
675
  # Concat/clip labels
 
726
  x1, y1, x2, y2 = [max(x, 0) for x in c] # allocate coords
727
 
728
  # Labels
729
+ labels = self.labels[index].copy()
730
+ if labels.size:
731
+ labels[:, 1:] = xywhn2xyxy(labels[:, 1:], w, h, padx, pady) # normalized xywh to pixel xyxy format
 
 
 
 
732
  labels9.append(labels)
733
 
734
  # Image
utils/general.py CHANGED
@@ -223,6 +223,16 @@ def xywh2xyxy(x):
223
  return y
224
 
225
 
 
 
 
 
 
 
 
 
 
 
226
  def scale_coords(img1_shape, coords, img0_shape, ratio_pad=None):
227
  # Rescale coords (xyxy) from img1_shape to img0_shape
228
  if ratio_pad is None: # calculate from img0_shape
 
223
  return y
224
 
225
 
226
+ def xywhn2xyxy(x, w=640, h=640, padw=32, padh=32):
227
+ # Convert nx4 boxes from [x, y, w, h] normalized to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right
228
+ y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
229
+ y[:, 0] = w * (x[:, 0] - x[:, 2] / 2) + padw # top left x
230
+ y[:, 1] = h * (x[:, 1] - x[:, 3] / 2) + padh # top left y
231
+ y[:, 2] = w * (x[:, 0] + x[:, 2] / 2) + padw # bottom right x
232
+ y[:, 3] = h * (x[:, 1] + x[:, 3] / 2) + padh # bottom right y
233
+ return y
234
+
235
+
236
  def scale_coords(img1_shape, coords, img0_shape, ratio_pad=None):
237
  # Rescale coords (xyxy) from img1_shape to img0_shape
238
  if ratio_pad is None: # calculate from img0_shape