Skip to content

Commit 7d6dc40

Browse files
authored
RandomDistort: Resolve inconsistent expectations (#8613)
The original enhancement will overflow the uint range, making image standardization [0,1] ineffective. The original random brightness has a small effect.
1 parent efcb6ad commit 7d6dc40

File tree

1 file changed

+14
-31
lines changed

1 file changed

+14
-31
lines changed

ppdet/data/transform/operators.py

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import copy
3636
import logging
3737
import cv2
38-
from PIL import Image, ImageDraw
38+
from PIL import Image, ImageDraw, ImageEnhance
3939
import pickle
4040
import threading
4141
MUTEX = threading.Lock()
@@ -490,10 +490,10 @@ class RandomDistort(BaseOperator):
490490
saturation (list): saturation settings. in [lower, upper, probability] format.
491491
contrast (list): contrast settings. in [lower, upper, probability] format.
492492
brightness (list): brightness settings. in [lower, upper, probability] format.
493-
random_apply (bool): whether to apply in random (yolo) or fixed (SSD)
494-
order.
495-
count (int): the number of doing distrot
496-
random_channel (bool): whether to swap channels randomly
493+
random_apply (bool): whether to apply in random (yolo) or fixed (SSD) order.
494+
count (int): the number of doing distrot.
495+
random_channel (bool): whether to swap channels randomly.
496+
prob (float): the probability of enhancing the sample.
497497
"""
498498

499499
def __init__(self,
@@ -519,57 +519,41 @@ def apply_hue(self, img):
519519
low, high, prob = self.hue
520520
if np.random.uniform(0., 1.) < prob:
521521
return img
522-
523-
img = img.astype(np.float32)
524-
# it works, but result differ from HSV version
525522
delta = np.random.uniform(low, high)
526-
u = np.cos(delta * np.pi)
527-
w = np.sin(delta * np.pi)
528-
bt = np.array([[1.0, 0.0, 0.0], [0.0, u, -w], [0.0, w, u]])
529-
tyiq = np.array([[0.299, 0.587, 0.114], [0.596, -0.274, -0.321],
530-
[0.211, -0.523, 0.311]])
531-
ityiq = np.array([[1.0, 0.956, 0.621], [1.0, -0.272, -0.647],
532-
[1.0, -1.107, 1.705]])
533-
t = np.dot(np.dot(ityiq, bt), tyiq).T
534-
img = np.dot(img, t)
523+
img = np.array(img.convert('HSV'))
524+
img[:, :, 0] = img[:, :, 0] + delta
525+
img = Image.fromarray(img, mode='HSV').convert('RGB')
535526
return img
536527

537528
def apply_saturation(self, img):
538529
low, high, prob = self.saturation
539530
if np.random.uniform(0., 1.) < prob:
540531
return img
541532
delta = np.random.uniform(low, high)
542-
img = img.astype(np.float32)
543-
# it works, but result differ from HSV version
544-
gray = img * np.array([[[0.299, 0.587, 0.114]]], dtype=np.float32)
545-
gray = gray.sum(axis=2, keepdims=True)
546-
gray *= (1.0 - delta)
547-
img *= delta
548-
img += gray
533+
img = ImageEnhance.Color(img).enhance(delta)
549534
return img
550535

551536
def apply_contrast(self, img):
552537
low, high, prob = self.contrast
553538
if np.random.uniform(0., 1.) < prob:
554539
return img
555540
delta = np.random.uniform(low, high)
556-
img = img.astype(np.float32)
557-
img *= delta
541+
img = ImageEnhance.Contrast(img).enhance(delta)
558542
return img
559543

560544
def apply_brightness(self, img):
561545
low, high, prob = self.brightness
562546
if np.random.uniform(0., 1.) < prob:
563547
return img
564548
delta = np.random.uniform(low, high)
565-
img = img.astype(np.float32)
566-
img += delta
549+
img = ImageEnhance.Brightness(img).enhance(delta)
567550
return img
568551

569552
def apply(self, sample, context=None):
570553
if random.random() > self.prob:
571554
return sample
572555
img = sample['image']
556+
img = Image.fromarray(img.astype(np.uint8))
573557
if self.random_apply:
574558
functions = [
575559
self.apply_brightness, self.apply_contrast,
@@ -578,21 +562,20 @@ def apply(self, sample, context=None):
578562
distortions = np.random.permutation(functions)[:self.count]
579563
for func in distortions:
580564
img = func(img)
565+
img = np.asarray(img).astype(np.float32)
581566
sample['image'] = img
582567
return sample
583568

584569
img = self.apply_brightness(img)
585570
mode = np.random.randint(0, 2)
586-
587571
if mode:
588572
img = self.apply_contrast(img)
589-
590573
img = self.apply_saturation(img)
591574
img = self.apply_hue(img)
592-
593575
if not mode:
594576
img = self.apply_contrast(img)
595577

578+
img = np.asarray(img).astype(np.float32)
596579
if self.random_channel:
597580
if np.random.randint(0, 2):
598581
img = img[..., np.random.permutation(3)]

0 commit comments

Comments
 (0)