From 2302105dcbdb5f84b10a9593e4527c75caea6dc6 Mon Sep 17 00:00:00 2001 From: jh Date: Thu, 12 Sep 2019 19:03:00 -0700 Subject: [PATCH 1/6] Random color change using L*A*B* space --- imagelib/color_transfer.py | 15 +++++++++++++++ models/Model_SAE/Model.py | 7 +++++++ samplelib/SampleProcessor.py | 6 +++++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/imagelib/color_transfer.py b/imagelib/color_transfer.py index 20cc12c..2277932 100644 --- a/imagelib/color_transfer.py +++ b/imagelib/color_transfer.py @@ -6,6 +6,8 @@ import numpy as np import scipy as sp import scipy.sparse from scipy.sparse.linalg import spsolve +from scipy.stats import special_ortho_group + class ColorTransferMode(IntEnum): NONE = 0 @@ -145,6 +147,19 @@ def seamless_clone(source, target, mask): return np.clip( np.dstack(result), 0, 1 ) +def random_color_transform(image, seed=None): + image = cv2.cvtColor(image, cv2.COLOR_BGR2LAB) + M = special_ortho_group.rvs(3, 1, seed) + image = image.dot(M) + l, a, b = cv2.split(image) + l = np.clip(l, 0, 100) + a = np.clip(a, 0, 100) + b = np.clip(b, 0, 100) + image = cv2.merge([l, a, b]) + image = cv2.cvtColor(image, cv2.COLOR_LAB2BGR) + np.clip(image, 0, 1, out=image) + return image + def reinhard_color_transfer(source, target, clip=False, preserve_paper=False, source_mask=None, target_mask=None): """ Transfers the color distribution from the target to the source diff --git a/models/Model_SAE/Model.py b/models/Model_SAE/Model.py index 5db9c89..68daed9 100644 --- a/models/Model_SAE/Model.py +++ b/models/Model_SAE/Model.py @@ -142,6 +142,11 @@ class SAEModel(ModelBase): "but the training time may be longer, due to the src faceset is becoming more diverse."), ColorTransferMode.NONE, ColorTransferMode.MASKED_RCT_PAPER_CLIP) + default_random_color_change = False if is_first_run else self.options.get('random_color_change', False) + self.options['random_color_change'] = io.input_bool( + "Enable random color change? (y/n, ?:help skip:%s) : " % (yn_str[default_random_color_change]), default_random_color_change, + help_message="") + if nnlib.device.backend != 'plaidML': # todo https://github.com/plaidml/plaidml/issues/301 default_clipgrad = False if is_first_run else self.options.get('clipgrad', False) self.options['clipgrad'] = io.input_bool( @@ -464,6 +469,8 @@ class SAEModel(ModelBase): global t_mode_bgr t_mode_bgr = t.MODE_BGR if not self.pretrain else t.MODE_BGR_SHUFFLE + if self.options['random_color_change']: + t_mode_bgr = t.MODE_LAB_RAND_TRANSFORM global training_data_src_path training_data_src_path = self.training_data_src_path diff --git a/samplelib/SampleProcessor.py b/samplelib/SampleProcessor.py index 7d7c4b5..9232bd8 100644 --- a/samplelib/SampleProcessor.py +++ b/samplelib/SampleProcessor.py @@ -6,7 +6,7 @@ import numpy as np import imagelib from facelib import FaceType, LandmarksProcessor -from imagelib.color_transfer import ColorTransferMode +from imagelib.color_transfer import ColorTransferMode, random_color_transform """ output_sample_types = [ @@ -72,6 +72,7 @@ class SampleProcessor(object): MODE_GGG = 42 #3xGrayscale MODE_M = 43 #mask only MODE_BGR_SHUFFLE = 44 #BGR shuffle + MODE_LAB_RAND_TRANSFORM = 45 #Random transform in LAB space MODE_END = 50 class Options(object): @@ -281,6 +282,9 @@ class SampleProcessor(object): elif mode_type == SPTF.MODE_BGR_SHUFFLE: rnd_state = np.random.RandomState (sample_rnd_seed) img = np.take (img_bgr, rnd_state.permutation(img_bgr.shape[-1]), axis=-1) + elif mode_type == SPTF.MODE_LAB_RAND_TRANSFORM: + rnd_state = np.random.RandomState (sample_rnd_seed) + img = random_color_transform(img_bgr, rnd_state) elif mode_type == SPTF.MODE_G: img = np.concatenate ( (np.expand_dims(cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY),-1),img_mask) , -1 ) elif mode_type == SPTF.MODE_GGG: From 1ef27162931bc379d3ac6b55239d20825873bfe9 Mon Sep 17 00:00:00 2001 From: jh Date: Thu, 12 Sep 2019 19:08:02 -0700 Subject: [PATCH 2/6] needs float32 --- imagelib/color_transfer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/imagelib/color_transfer.py b/imagelib/color_transfer.py index 2277932..44f04c6 100644 --- a/imagelib/color_transfer.py +++ b/imagelib/color_transfer.py @@ -148,7 +148,7 @@ def seamless_clone(source, target, mask): def random_color_transform(image, seed=None): - image = cv2.cvtColor(image, cv2.COLOR_BGR2LAB) + image = cv2.cvtColor(image.astype(np.float32), cv2.COLOR_BGR2LAB) M = special_ortho_group.rvs(3, 1, seed) image = image.dot(M) l, a, b = cv2.split(image) From 0eaeb3e689d01165e7b4469200e13fabd1ad6092 Mon Sep 17 00:00:00 2001 From: jh Date: Thu, 12 Sep 2019 19:11:53 -0700 Subject: [PATCH 3/6] fix type? --- imagelib/color_transfer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/imagelib/color_transfer.py b/imagelib/color_transfer.py index 44f04c6..a4dd77d 100644 --- a/imagelib/color_transfer.py +++ b/imagelib/color_transfer.py @@ -148,7 +148,7 @@ def seamless_clone(source, target, mask): def random_color_transform(image, seed=None): - image = cv2.cvtColor(image.astype(np.float32), cv2.COLOR_BGR2LAB) + image = cv2.cvtColor(image, cv2.COLOR_BGR2LAB) M = special_ortho_group.rvs(3, 1, seed) image = image.dot(M) l, a, b = cv2.split(image) @@ -156,7 +156,7 @@ def random_color_transform(image, seed=None): a = np.clip(a, 0, 100) b = np.clip(b, 0, 100) image = cv2.merge([l, a, b]) - image = cv2.cvtColor(image, cv2.COLOR_LAB2BGR) + image = cv2.cvtColor(image.astype(np.float32), cv2.COLOR_LAB2BGR) np.clip(image, 0, 1, out=image) return image From d94d65c8ea577d81ad2fd58607d8a91316e27b80 Mon Sep 17 00:00:00 2001 From: jh Date: Thu, 12 Sep 2019 19:17:58 -0700 Subject: [PATCH 4/6] only rotate over A*B* --- imagelib/color_transfer.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/imagelib/color_transfer.py b/imagelib/color_transfer.py index a4dd77d..b1882a1 100644 --- a/imagelib/color_transfer.py +++ b/imagelib/color_transfer.py @@ -149,7 +149,8 @@ def seamless_clone(source, target, mask): def random_color_transform(image, seed=None): image = cv2.cvtColor(image, cv2.COLOR_BGR2LAB) - M = special_ortho_group.rvs(3, 1, seed) + M = np.eye(3) + M[1:, 1:] = special_ortho_group.rvs(2, 1, seed) image = image.dot(M) l, a, b = cv2.split(image) l = np.clip(l, 0, 100) From 80307cba62784a91348ad1eef5570422398cd217 Mon Sep 17 00:00:00 2001 From: jh Date: Thu, 12 Sep 2019 19:24:54 -0700 Subject: [PATCH 5/6] clipped A* B* wrong --- imagelib/color_transfer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/imagelib/color_transfer.py b/imagelib/color_transfer.py index b1882a1..4e2b99d 100644 --- a/imagelib/color_transfer.py +++ b/imagelib/color_transfer.py @@ -154,8 +154,8 @@ def random_color_transform(image, seed=None): image = image.dot(M) l, a, b = cv2.split(image) l = np.clip(l, 0, 100) - a = np.clip(a, 0, 100) - b = np.clip(b, 0, 100) + a = np.clip(a, -127, 127) + b = np.clip(b, -127, 127) image = cv2.merge([l, a, b]) image = cv2.cvtColor(image.astype(np.float32), cv2.COLOR_LAB2BGR) np.clip(image, 0, 1, out=image) From a74e7f44d3d3eaf441c9fa3ee21a7c0c90a67f7f Mon Sep 17 00:00:00 2001 From: Jeremy Hummel Date: Fri, 13 Sep 2019 01:39:05 -0700 Subject: [PATCH 6/6] pre-train use LAB randomization --- models/Model_SAE/Model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/Model_SAE/Model.py b/models/Model_SAE/Model.py index 812f319..4ac420a 100644 --- a/models/Model_SAE/Model.py +++ b/models/Model_SAE/Model.py @@ -469,7 +469,7 @@ class SAEModel(ModelBase): face_type = t.FACE_TYPE_FULL if self.options['face_type'] == 'f' else t.FACE_TYPE_HALF global t_mode_bgr - t_mode_bgr = t.MODE_BGR if not self.pretrain else t.MODE_BGR_SHUFFLE + t_mode_bgr = t.MODE_BGR if not self.pretrain else t.MODE_LAB_RAND_TRANSFORM if self.options['random_color_change']: t_mode_bgr = t.MODE_LAB_RAND_TRANSFORM