From 0472062a129d5f0442d103d9ebb0574835e16d7c Mon Sep 17 00:00:00 2001 From: Mai Thanh Minh Date: Fri, 18 Jun 2021 22:32:39 +0700 Subject: [PATCH 1/2] Create shortcircuit in augment_hsv when hyperparameter are zero --- utils/datasets.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/utils/datasets.py b/utils/datasets.py index f927abb20f5a..eeb66f4803ca 100755 --- a/utils/datasets.py +++ b/utils/datasets.py @@ -632,6 +632,9 @@ def load_image(self, index): def augment_hsv(img, hgain=0.5, sgain=0.5, vgain=0.5): + if hgain == 0.0 and sgain == 0.0 and vgain == 0.0: + return + r = np.random.uniform(-1, 1, 3) * [hgain, sgain, vgain] + 1 # random gains hue, sat, val = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HSV)) dtype = img.dtype # uint8 From f98583e33b7e285ec68169bba3c2e3a96417c6e1 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Sat, 19 Jun 2021 11:47:25 +0200 Subject: [PATCH 2/2] implement faster opt-in --- utils/datasets.py | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/utils/datasets.py b/utils/datasets.py index eeb66f4803ca..25ec79ddd48f 100755 --- a/utils/datasets.py +++ b/utils/datasets.py @@ -632,20 +632,18 @@ def load_image(self, index): def augment_hsv(img, hgain=0.5, sgain=0.5, vgain=0.5): - if hgain == 0.0 and sgain == 0.0 and vgain == 0.0: - return - - r = np.random.uniform(-1, 1, 3) * [hgain, sgain, vgain] + 1 # random gains - hue, sat, val = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HSV)) - dtype = img.dtype # uint8 - - x = np.arange(0, 256, dtype=r.dtype) - lut_hue = ((x * r[0]) % 180).astype(dtype) - lut_sat = np.clip(x * r[1], 0, 255).astype(dtype) - lut_val = np.clip(x * r[2], 0, 255).astype(dtype) - - img_hsv = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val))) - cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR, dst=img) # no return needed + if hgain or sgain or vgain: + r = np.random.uniform(-1, 1, 3) * [hgain, sgain, vgain] + 1 # random gains + hue, sat, val = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HSV)) + dtype = img.dtype # uint8 + + x = np.arange(0, 256, dtype=r.dtype) + lut_hue = ((x * r[0]) % 180).astype(dtype) + lut_sat = np.clip(x * r[1], 0, 255).astype(dtype) + lut_val = np.clip(x * r[2], 0, 255).astype(dtype) + + img_hsv = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val))) + cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR, dst=img) # no return needed def hist_equalize(img, clahe=True, bgr=False):