From 21a74365824ead2b886f992262880678fb99d540 Mon Sep 17 00:00:00 2001 From: Yonghye Kwon Date: Wed, 2 Mar 2022 21:13:54 +0900 Subject: [PATCH 1/8] add random_interpolation option to make model robust to interpolation methods --- utils/augmentations.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/utils/augmentations.py b/utils/augmentations.py index 0311b97b63db..d54dcdae7b01 100644 --- a/utils/augmentations.py +++ b/utils/augmentations.py @@ -88,7 +88,7 @@ def replicate(im, labels): return im, labels -def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, stride=32): +def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, stride=32, random_interpolation=False): # Resize and pad image while meeting stride-multiple constraints shape = im.shape[:2] # current shape [height, width] if isinstance(new_shape, int): @@ -114,7 +114,9 @@ def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleF dh /= 2 if shape[::-1] != new_unpad: # resize - im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR) + interpolation = cv2.INTER_LINEAR if random_interpolation == False else random.choice([cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4]) + im = cv2.resize(im, new_unpad, interpolation=interpolation) + top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1)) left, right = int(round(dw - 0.1)), int(round(dw + 0.1)) im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border From 2b601d2d61a3692a70fb0bc9f02932b2a1a206de Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 2 Mar 2022 12:21:50 +0000 Subject: [PATCH 2/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- utils/augmentations.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/augmentations.py b/utils/augmentations.py index d54dcdae7b01..bb262a43ca96 100644 --- a/utils/augmentations.py +++ b/utils/augmentations.py @@ -114,9 +114,9 @@ def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleF dh /= 2 if shape[::-1] != new_unpad: # resize - interpolation = cv2.INTER_LINEAR if random_interpolation == False else random.choice([cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4]) + interpolation = cv2.INTER_LINEAR if random_interpolation == False else random.choice([cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4]) im = cv2.resize(im, new_unpad, interpolation=interpolation) - + top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1)) left, right = int(round(dw - 0.1)), int(round(dw + 0.1)) im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border From cc576359982d6215c3e798690cf2f9007820a7b2 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Fri, 4 Mar 2022 16:55:09 +0100 Subject: [PATCH 3/8] Fix precommit error --- utils/augmentations.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/utils/augmentations.py b/utils/augmentations.py index bb262a43ca96..9f7fdaac421a 100644 --- a/utils/augmentations.py +++ b/utils/augmentations.py @@ -114,7 +114,9 @@ def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleF dh /= 2 if shape[::-1] != new_unpad: # resize - interpolation = cv2.INTER_LINEAR if random_interpolation == False else random.choice([cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4]) + interpolation = random.choice( + [cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4]) if + random_interpolation else cv2.INTER_LINEAR im = cv2.resize(im, new_unpad, interpolation=interpolation) top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1)) From 261c58cc75e704d7a3916400521fdf3210ee31b0 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Fri, 4 Mar 2022 16:58:00 +0100 Subject: [PATCH 4/8] Update augmentations.py --- utils/augmentations.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/utils/augmentations.py b/utils/augmentations.py index 9f7fdaac421a..3d2c5e55391d 100644 --- a/utils/augmentations.py +++ b/utils/augmentations.py @@ -88,7 +88,8 @@ def replicate(im, labels): return im, labels -def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, stride=32, random_interpolation=False): +def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, stride=32, + random_interpolation=False): # Resize and pad image while meeting stride-multiple constraints shape = im.shape[:2] # current shape [height, width] if isinstance(new_shape, int): @@ -114,9 +115,8 @@ def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleF dh /= 2 if shape[::-1] != new_unpad: # resize - interpolation = random.choice( - [cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4]) if - random_interpolation else cv2.INTER_LINEAR + interpolation = random.choice([cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, + cv2.INTER_LANCZOS4]) if random_interpolation else cv2.INTER_LINEAR im = cv2.resize(im, new_unpad, interpolation=interpolation) top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1)) From 82a54416e040037d902c40d065bafd26a83eb199 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Fri, 4 Mar 2022 16:58:27 +0100 Subject: [PATCH 5/8] Update augmentations.py --- utils/augmentations.py | 1 - 1 file changed, 1 deletion(-) diff --git a/utils/augmentations.py b/utils/augmentations.py index 3d2c5e55391d..beb783e61ce2 100644 --- a/utils/augmentations.py +++ b/utils/augmentations.py @@ -118,7 +118,6 @@ def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleF interpolation = random.choice([cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4]) if random_interpolation else cv2.INTER_LINEAR im = cv2.resize(im, new_unpad, interpolation=interpolation) - top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1)) left, right = int(round(dw - 0.1)), int(round(dw + 0.1)) im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border From 090545f56facaf93e13d864c6b1a674f809ef025 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Thu, 17 Mar 2022 18:07:02 +0100 Subject: [PATCH 6/8] Update augmentations.py --- utils/augmentations.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/utils/augmentations.py b/utils/augmentations.py index beb783e61ce2..0311b97b63db 100644 --- a/utils/augmentations.py +++ b/utils/augmentations.py @@ -88,8 +88,7 @@ def replicate(im, labels): return im, labels -def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, stride=32, - random_interpolation=False): +def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, stride=32): # Resize and pad image while meeting stride-multiple constraints shape = im.shape[:2] # current shape [height, width] if isinstance(new_shape, int): @@ -115,9 +114,7 @@ def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleF dh /= 2 if shape[::-1] != new_unpad: # resize - interpolation = random.choice([cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, - cv2.INTER_LANCZOS4]) if random_interpolation else cv2.INTER_LINEAR - im = cv2.resize(im, new_unpad, interpolation=interpolation) + im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR) top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1)) left, right = int(round(dw - 0.1)), int(round(dw + 0.1)) im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border From 0413bbdc67285cc139fd8a165136514adb15b14a Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Thu, 17 Mar 2022 18:07:42 +0100 Subject: [PATCH 7/8] Update datasets.py --- utils/datasets.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/utils/datasets.py b/utils/datasets.py index 5ce6d607fb7a..359bd3cfea8a 100755 --- a/utils/datasets.py +++ b/utils/datasets.py @@ -379,6 +379,7 @@ def img2label_paths(img_paths): class LoadImagesAndLabels(Dataset): # YOLOv5 train_loader/val_loader, loads images and labels for training and validation cache_version = 0.6 # dataset labels *.cache version + rand_interp_methods = [cv2.INTER_NEAREST, cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4] def __init__(self, path, img_size=640, batch_size=16, augment=False, hyp=None, rect=False, image_weights=False, cache_images=False, single_cls=False, stride=32, pad=0.0, prefix=''): @@ -633,9 +634,8 @@ def load_image(self, i): h0, w0 = im.shape[:2] # orig hw r = self.img_size / max(h0, w0) # ratio if r != 1: # if sizes are not equal - im = cv2.resize(im, - (int(w0 * r), int(h0 * r)), - interpolation=cv2.INTER_LINEAR if (self.augment or r > 1) else cv2.INTER_AREA) + interpolation = random.choice(self.rand_interp_methods) if self.augment else cv2.INTER_AREA + im = cv2.resize(im, (int(w0 * r), int(h0 * r)), interpolation=interpolation) return im, (h0, w0), im.shape[:2] # im, hw_original, hw_resized else: return self.ims[i], self.im_hw0[i], self.im_hw[i] # im, hw_original, hw_resized From 066563d6bad0f02d7549af1c2a5d98b2e72bdd3c Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Fri, 13 May 2022 14:21:42 +0200 Subject: [PATCH 8/8] Update datasets.py --- utils/datasets.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/datasets.py b/utils/datasets.py index 778adf4ceb80..cc68f98f5125 100755 --- a/utils/datasets.py +++ b/utils/datasets.py @@ -663,8 +663,8 @@ def load_image(self, i): h0, w0 = im.shape[:2] # orig hw r = self.img_size / max(h0, w0) # ratio if r != 1: # if sizes are not equal - interpolation = random.choice(self.rand_interp_methods) if self.augment else cv2.INTER_AREA - im = cv2.resize(im, (int(w0 * r), int(h0 * r)), interpolation=interpolation) + interp = cv2.INTER_LINEAR if self.augment else cv2.INTER_AREA # random.choice(self.rand_interp_methods) + im = cv2.resize(im, (int(w0 * r), int(h0 * r)), interpolation=interp) return im, (h0, w0), im.shape[:2] # im, hw_original, hw_resized else: return self.ims[i], self.im_hw0[i], self.im_hw[i] # im, hw_original, hw_resized