Mai Thanh Minh glenn-jocher commited on
Commit
bf209f6
·
unverified ·
1 Parent(s): 814806c

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]>

Files changed (1) hide show
  1. 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
- r = np.random.uniform(-1, 1, 3) * [hgain, sgain, vgain] + 1 # random gains
636
- hue, sat, val = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HSV))
637
- dtype = img.dtype # uint8
638
-
639
- x = np.arange(0, 256, dtype=r.dtype)
640
- lut_hue = ((x * r[0]) % 180).astype(dtype)
641
- lut_sat = np.clip(x * r[1], 0, 255).astype(dtype)
642
- lut_val = np.clip(x * r[2], 0, 255).astype(dtype)
643
-
644
- img_hsv = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val)))
645
- cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR, dst=img) # no return needed
 
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):