developer0hye glenn-jocher commited on
Commit
3749573
·
unverified ·
1 Parent(s): 03281f8

Add `xyxy2xywhn()` (#3765)

Browse files

* Edit Comments for numpy2torch tensor process

Edit Comments for numpy2torch tensor process

* add xyxy2xywhn

add xyxy2xywhn

* add xyxy2xywhn

* formatting

* pass arguments

pass arguments

* edit comment for xyxy2xywhn()

edit comment for xyxy2xywhn()

* cleanup datasets.py

Co-authored-by: Glenn Jocher <[email protected]>

Files changed (2) hide show
  1. utils/datasets.py +6 -8
  2. utils/general.py +10 -0
utils/datasets.py CHANGED
@@ -23,8 +23,8 @@ from PIL import Image, ExifTags
23
  from torch.utils.data import Dataset
24
  from tqdm import tqdm
25
 
26
- from utils.general import check_requirements, check_file, check_dataset, xyxy2xywh, xywh2xyxy, xywhn2xyxy, xyn2xy, \
27
- segment2box, segments2boxes, resample_segments, clean_str
28
  from utils.torch_utils import torch_distributed_zero_first
29
 
30
  # Parameters
@@ -192,7 +192,7 @@ class LoadImages: # for inference
192
  img = letterbox(img0, self.img_size, stride=self.stride)[0]
193
 
194
  # Convert
195
- img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416
196
  img = np.ascontiguousarray(img)
197
 
198
  return path, img, img0, self.cap
@@ -255,7 +255,7 @@ class LoadWebcam: # for inference
255
  img = letterbox(img0, self.img_size, stride=self.stride)[0]
256
 
257
  # Convert
258
- img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416
259
  img = np.ascontiguousarray(img)
260
 
261
  return img_path, img, img0, None
@@ -336,7 +336,7 @@ class LoadStreams: # multiple IP or RTSP cameras
336
  img = np.stack(img, 0)
337
 
338
  # Convert
339
- img = img[:, :, :, ::-1].transpose(0, 3, 1, 2) # BGR to RGB, to bsx3x416x416
340
  img = np.ascontiguousarray(img)
341
 
342
  return self.sources, img, img0, None
@@ -552,9 +552,7 @@ class LoadImagesAndLabels(Dataset): # for training/testing
552
 
553
  nL = len(labels) # number of labels
554
  if nL:
555
- labels[:, 1:5] = xyxy2xywh(labels[:, 1:5]) # convert xyxy to xywh
556
- labels[:, [2, 4]] /= img.shape[0] # normalized height 0-1
557
- labels[:, [1, 3]] /= img.shape[1] # normalized width 0-1
558
 
559
  if self.augment:
560
  # flip up-down
 
23
  from torch.utils.data import Dataset
24
  from tqdm import tqdm
25
 
26
+ from utils.general import check_requirements, check_file, check_dataset, xywh2xyxy, xywhn2xyxy, xyxy2xywhn, \
27
+ xyn2xy, segment2box, segments2boxes, resample_segments, clean_str
28
  from utils.torch_utils import torch_distributed_zero_first
29
 
30
  # Parameters
 
192
  img = letterbox(img0, self.img_size, stride=self.stride)[0]
193
 
194
  # Convert
195
+ img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB and HWC to CHW
196
  img = np.ascontiguousarray(img)
197
 
198
  return path, img, img0, self.cap
 
255
  img = letterbox(img0, self.img_size, stride=self.stride)[0]
256
 
257
  # Convert
258
+ img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB and HWC to CHW
259
  img = np.ascontiguousarray(img)
260
 
261
  return img_path, img, img0, None
 
336
  img = np.stack(img, 0)
337
 
338
  # Convert
339
+ img = img[:, :, :, ::-1].transpose(0, 3, 1, 2) # BGR to RGB and BHWC to BCHW
340
  img = np.ascontiguousarray(img)
341
 
342
  return self.sources, img, img0, None
 
552
 
553
  nL = len(labels) # number of labels
554
  if nL:
555
+ labels[:, 1:5] = xyxy2xywhn(labels[:, 1:5], w=img.shape[1], h=img.shape[0]) # xyxy to xywh normalized
 
 
556
 
557
  if self.augment:
558
  # flip up-down
utils/general.py CHANGED
@@ -393,6 +393,16 @@ def xywhn2xyxy(x, w=640, h=640, padw=0, padh=0):
393
  return y
394
 
395
 
 
 
 
 
 
 
 
 
 
 
396
  def xyn2xy(x, w=640, h=640, padw=0, padh=0):
397
  # Convert normalized segments into pixel segments, shape (n,2)
398
  y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
 
393
  return y
394
 
395
 
396
+ def xyxy2xywhn(x, w=640, h=640):
397
+ # Convert nx4 boxes from [x1, y1, x2, y2] to [x, y, w, h] normalized where xy1=top-left, xy2=bottom-right
398
+ y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
399
+ y[:, 0] = ((x[:, 0] + x[:, 2]) / 2) / w # x center
400
+ y[:, 1] = ((x[:, 1] + x[:, 3]) / 2) / h # y center
401
+ y[:, 2] = (x[:, 2] - x[:, 0]) / w # width
402
+ y[:, 3] = (x[:, 3] - x[:, 1]) / h # height
403
+ return y
404
+
405
+
406
  def xyn2xy(x, w=640, h=640, padw=0, padh=0):
407
  # Convert normalized segments into pixel segments, shape (n,2)
408
  y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)