From 2ee1b24990a0fd175be96b42a9bdba5f850415b0 Mon Sep 17 00:00:00 2001 From: Yonghye Kwon Date: Fri, 25 Jun 2021 09:32:40 +0900 Subject: [PATCH 1/7] Edit Comments for numpy2torch tensor process Edit Comments for numpy2torch tensor process --- utils/datasets.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/utils/datasets.py b/utils/datasets.py index d3714d745b88..ca2bce8a19c5 100755 --- a/utils/datasets.py +++ b/utils/datasets.py @@ -192,7 +192,7 @@ def __next__(self): img = letterbox(img0, self.img_size, stride=self.stride)[0] # Convert - img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416 + img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3 x img_height x img_width img = np.ascontiguousarray(img) return path, img, img0, self.cap @@ -255,7 +255,7 @@ def __next__(self): img = letterbox(img0, self.img_size, stride=self.stride)[0] # Convert - img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416 + img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3 x img_height x img_width img = np.ascontiguousarray(img) return img_path, img, img0, None @@ -336,7 +336,7 @@ def __next__(self): img = np.stack(img, 0) # Convert - img = img[:, :, :, ::-1].transpose(0, 3, 1, 2) # BGR to RGB, to bsx3x416x416 + img = img[:, :, :, ::-1].transpose(0, 3, 1, 2) # BGR to RGB, to batch_size x 3 x img_height x img_width img = np.ascontiguousarray(img) return self.sources, img, img0, None From 76df38650ea57abd0135fa2ca1cbeabe4c091c69 Mon Sep 17 00:00:00 2001 From: Yonghye Kwon Date: Fri, 25 Jun 2021 09:37:24 +0900 Subject: [PATCH 2/7] add xyxy2xywhn add xyxy2xywhn --- utils/general.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/utils/general.py b/utils/general.py index 555975f07c5d..d7476c8d2fc4 100755 --- a/utils/general.py +++ b/utils/general.py @@ -393,6 +393,16 @@ def xywhn2xyxy(x, w=640, h=640, padw=0, padh=0): return y +def xyxy2xywhn(x, w=640, h=640): + # Convert nx4 boxes from [x, y, w, h] normalized to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right + y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) + y[:, 0] = ((x[:, 0] + x[:, 2]) / 2)/w # x center + y[:, 1] = ((x[:, 1] + x[:, 3]) / 2)/h # y center + y[:, 2] = (x[:, 2] - x[:, 0])/w # width + y[:, 3] = (x[:, 3] - x[:, 1])/h # height + return y + + def xyn2xy(x, w=640, h=640, padw=0, padh=0): # Convert normalized segments into pixel segments, shape (n,2) y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) From e4aa0a8afecb0894b1d0b9c0d4ad446bc1307e85 Mon Sep 17 00:00:00 2001 From: Yonghye Kwon Date: Fri, 25 Jun 2021 09:40:25 +0900 Subject: [PATCH 3/7] add xyxy2xywhn --- utils/datasets.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/utils/datasets.py b/utils/datasets.py index ca2bce8a19c5..b23492da13b3 100755 --- a/utils/datasets.py +++ b/utils/datasets.py @@ -23,7 +23,7 @@ from torch.utils.data import Dataset from tqdm import tqdm -from utils.general import check_requirements, check_file, check_dataset, xyxy2xywh, xywh2xyxy, xywhn2xyxy, xyn2xy, \ +from utils.general import check_requirements, check_file, check_dataset, xyxy2xywh, xywh2xyxy, xywhn2xyxy, xyxy2xywhn, xyn2xy, \ segment2box, segments2boxes, resample_segments, clean_str from utils.torch_utils import torch_distributed_zero_first @@ -552,9 +552,7 @@ def __getitem__(self, index): nL = len(labels) # number of labels if nL: - labels[:, 1:5] = xyxy2xywh(labels[:, 1:5]) # convert xyxy to xywh - labels[:, [2, 4]] /= img.shape[0] # normalized height 0-1 - labels[:, [1, 3]] /= img.shape[1] # normalized width 0-1 + labels[:, 1:5] = xyxy2xywhn(labels[:, 1:5]) # convert xyxy to normalized xywh if self.augment: # flip up-down From b77693f13b9775f7a7ee32598a886b0450cc6ce0 Mon Sep 17 00:00:00 2001 From: Yonghye Kwon Date: Fri, 25 Jun 2021 09:50:49 +0900 Subject: [PATCH 4/7] formatting --- utils/general.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/utils/general.py b/utils/general.py index d7476c8d2fc4..912961f3d60d 100755 --- a/utils/general.py +++ b/utils/general.py @@ -396,10 +396,10 @@ def xywhn2xyxy(x, w=640, h=640, padw=0, padh=0): def xyxy2xywhn(x, w=640, h=640): # Convert nx4 boxes from [x, y, w, h] normalized to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) - y[:, 0] = ((x[:, 0] + x[:, 2]) / 2)/w # x center - y[:, 1] = ((x[:, 1] + x[:, 3]) / 2)/h # y center - y[:, 2] = (x[:, 2] - x[:, 0])/w # width - y[:, 3] = (x[:, 3] - x[:, 1])/h # height + y[:, 0] = ((x[:, 0] + x[:, 2]) / 2) / w # x center + y[:, 1] = ((x[:, 1] + x[:, 3]) / 2) / h # y center + y[:, 2] = (x[:, 2] - x[:, 0]) / w # width + y[:, 3] = (x[:, 3] - x[:, 1]) / h # height return y From aabec2f6014ae70a8c8f7360a7955d95120eb178 Mon Sep 17 00:00:00 2001 From: Yonghye Kwon Date: Fri, 25 Jun 2021 12:18:25 +0900 Subject: [PATCH 5/7] pass arguments pass arguments --- utils/datasets.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/datasets.py b/utils/datasets.py index b23492da13b3..43759d4b992b 100755 --- a/utils/datasets.py +++ b/utils/datasets.py @@ -552,7 +552,7 @@ def __getitem__(self, index): nL = len(labels) # number of labels if nL: - labels[:, 1:5] = xyxy2xywhn(labels[:, 1:5]) # convert xyxy to normalized xywh + labels[:, 1:5] = xyxy2xywhn(labels[:, 1:5], w=img.shape[1], h=img.shape[0]) # convert xyxy to normalized xywh if self.augment: # flip up-down From d335485e84c4958e7330eda99f0eb518d3f18cc5 Mon Sep 17 00:00:00 2001 From: Yonghye Kwon Date: Fri, 25 Jun 2021 17:30:48 +0900 Subject: [PATCH 6/7] edit comment for xyxy2xywhn() edit comment for xyxy2xywhn() --- utils/general.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/general.py b/utils/general.py index 912961f3d60d..6a5b42f374e6 100755 --- a/utils/general.py +++ b/utils/general.py @@ -394,7 +394,7 @@ def xywhn2xyxy(x, w=640, h=640, padw=0, padh=0): def xyxy2xywhn(x, w=640, h=640): - # Convert nx4 boxes from [x, y, w, h] normalized to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right + # Convert nx4 boxes from [x1, y1, x2, y2] to [x, y, w, h] normalized where xy1=top-left, xy2=bottom-right y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x) y[:, 0] = ((x[:, 0] + x[:, 2]) / 2) / w # x center y[:, 1] = ((x[:, 1] + x[:, 3]) / 2) / h # y center From a212f6ef44a95c6a2bfade66c35e417563f06cbd Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Fri, 25 Jun 2021 11:44:53 +0200 Subject: [PATCH 7/7] cleanup datasets.py --- utils/datasets.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/utils/datasets.py b/utils/datasets.py index 43759d4b992b..eac0c7834308 100755 --- a/utils/datasets.py +++ b/utils/datasets.py @@ -23,8 +23,8 @@ from torch.utils.data import Dataset from tqdm import tqdm -from utils.general import check_requirements, check_file, check_dataset, xyxy2xywh, xywh2xyxy, xywhn2xyxy, xyxy2xywhn, xyn2xy, \ - segment2box, segments2boxes, resample_segments, clean_str +from utils.general import check_requirements, check_file, check_dataset, xywh2xyxy, xywhn2xyxy, xyxy2xywhn, \ + xyn2xy, segment2box, segments2boxes, resample_segments, clean_str from utils.torch_utils import torch_distributed_zero_first # Parameters @@ -192,7 +192,7 @@ def __next__(self): img = letterbox(img0, self.img_size, stride=self.stride)[0] # Convert - img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3 x img_height x img_width + img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB and HWC to CHW img = np.ascontiguousarray(img) return path, img, img0, self.cap @@ -255,7 +255,7 @@ def __next__(self): img = letterbox(img0, self.img_size, stride=self.stride)[0] # Convert - img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3 x img_height x img_width + img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB and HWC to CHW img = np.ascontiguousarray(img) return img_path, img, img0, None @@ -336,7 +336,7 @@ def __next__(self): img = np.stack(img, 0) # Convert - img = img[:, :, :, ::-1].transpose(0, 3, 1, 2) # BGR to RGB, to batch_size x 3 x img_height x img_width + img = img[:, :, :, ::-1].transpose(0, 3, 1, 2) # BGR to RGB and BHWC to BCHW img = np.ascontiguousarray(img) return self.sources, img, img0, None @@ -552,7 +552,7 @@ def __getitem__(self, index): nL = len(labels) # number of labels if nL: - labels[:, 1:5] = xyxy2xywhn(labels[:, 1:5], w=img.shape[1], h=img.shape[0]) # convert xyxy to normalized xywh + labels[:, 1:5] = xyxy2xywhn(labels[:, 1:5], w=img.shape[1], h=img.shape[0]) # xyxy to xywh normalized if self.augment: # flip up-down