Skip HSV augmentation when hyperparameters are [0, 0, 0] (#3686)
Browse files* Create shortcircuit in augment_hsv when hyperparameter are zero
* implement faster opt-in
Co-authored-by: Glenn Jocher <[email protected]>
- utils/datasets.py +12 -11
utils/datasets.py
CHANGED
@@ -632,17 +632,18 @@ def load_image(self, index):
|
|
632 |
|
633 |
|
634 |
def augment_hsv(img, hgain=0.5, sgain=0.5, vgain=0.5):
|
635 |
-
|
636 |
-
|
637 |
-
|
638 |
-
|
639 |
-
|
640 |
-
|
641 |
-
|
642 |
-
|
643 |
-
|
644 |
-
|
645 |
-
|
|
|
646 |
|
647 |
|
648 |
def hist_equalize(img, clahe=True, bgr=False):
|
|
|
632 |
|
633 |
|
634 |
def augment_hsv(img, hgain=0.5, sgain=0.5, vgain=0.5):
|
635 |
+
if hgain or sgain or vgain:
|
636 |
+
r = np.random.uniform(-1, 1, 3) * [hgain, sgain, vgain] + 1 # random gains
|
637 |
+
hue, sat, val = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HSV))
|
638 |
+
dtype = img.dtype # uint8
|
639 |
+
|
640 |
+
x = np.arange(0, 256, dtype=r.dtype)
|
641 |
+
lut_hue = ((x * r[0]) % 180).astype(dtype)
|
642 |
+
lut_sat = np.clip(x * r[1], 0, 255).astype(dtype)
|
643 |
+
lut_val = np.clip(x * r[2], 0, 255).astype(dtype)
|
644 |
+
|
645 |
+
img_hsv = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val)))
|
646 |
+
cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR, dst=img) # no return needed
|
647 |
|
648 |
|
649 |
def hist_equalize(img, clahe=True, bgr=False):
|