From 4e83a1e9af2f70ffde81829a932f39bcef5f8dff Mon Sep 17 00:00:00 2001 From: Jeremy Hummel Date: Sat, 22 May 2021 21:46:09 -0700 Subject: [PATCH 01/22] Random downsampling --- models/Model_SAEHD/Model.py | 11 +++++++++-- samplelib/SampleProcessor.py | 6 ++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index 9b9cd2d..030057a 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -159,6 +159,7 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... self.options['random_warp'] = io.input_bool ("Enable random warp of samples", default_random_warp, help_message="Random warp is required to generalize facial expressions of both faces. When the face is trained enough, you can disable it to get extra sharpness and reduce subpixel shake for less amount of iterations.") + self.options['random_downsample'] = io.input_bool("Enable random downsample of samples", False, help_message="") self.options['gan_version'] = np.clip (io.input_int("GAN version", default_gan_version, add_info="2 or 3", help_message="Choose GAN version (v2: 7/16/2020, v3: 1/3/2021):"), 2, 3) if self.options['gan_version'] == 2: @@ -745,7 +746,7 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... self.set_training_data_generators ([ SampleGeneratorFace(training_data_src_path, random_ct_samples_path=random_ct_samples_path, debug=self.is_debug(), batch_size=self.get_batch_size(), sample_process_options=SampleProcessor.Options(random_flip=self.random_flip), - output_sample_types = [ {'sample_type': SampleProcessor.SampleType.FACE_IMAGE,'warp':random_warp, 'transform':True, 'channel_type' : channel_type, 'ct_mode': ct_mode, 'face_type':self.face_type, 'data_format':nn.data_format, 'resolution': resolution}, + output_sample_types = [ {'sample_type': SampleProcessor.SampleType.FACE_IMAGE,'warp':random_warp, 'random_downsample': self.options['random_downsample'], 'transform':True, 'channel_type' : channel_type, 'ct_mode': ct_mode, 'face_type':self.face_type, 'data_format':nn.data_format, 'resolution': resolution}, {'sample_type': SampleProcessor.SampleType.FACE_IMAGE,'warp':False , 'transform':True, 'channel_type' : channel_type, 'ct_mode': ct_mode, 'face_type':self.face_type, 'data_format':nn.data_format, 'resolution': resolution}, {'sample_type': SampleProcessor.SampleType.FACE_MASK, 'warp':False , 'transform':True, 'channel_type' : SampleProcessor.ChannelType.G, 'face_mask_type' : SampleProcessor.FaceMaskType.FULL_FACE, 'face_type':self.face_type, 'data_format':nn.data_format, 'resolution': resolution}, {'sample_type': SampleProcessor.SampleType.FACE_MASK, 'warp':False , 'transform':True, 'channel_type' : SampleProcessor.ChannelType.G, 'face_mask_type' : SampleProcessor.FaceMaskType.FULL_FACE_EYES, 'face_type':self.face_type, 'data_format':nn.data_format, 'resolution': resolution}, @@ -755,7 +756,7 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... SampleGeneratorFace(training_data_dst_path, debug=self.is_debug(), batch_size=self.get_batch_size(), sample_process_options=SampleProcessor.Options(random_flip=self.random_flip), - output_sample_types = [ {'sample_type': SampleProcessor.SampleType.FACE_IMAGE,'warp':random_warp, 'transform':True, 'channel_type' : channel_type, 'ct_mode': fs_aug, 'face_type':self.face_type, 'data_format':nn.data_format, 'resolution': resolution}, + output_sample_types = [ {'sample_type': SampleProcessor.SampleType.FACE_IMAGE,'warp':random_warp, 'random_downsample': self.options['random_downsample'], 'transform':True, 'channel_type' : channel_type, 'ct_mode': fs_aug, 'face_type':self.face_type, 'data_format':nn.data_format, 'resolution': resolution}, {'sample_type': SampleProcessor.SampleType.FACE_IMAGE,'warp':False , 'transform':True, 'channel_type' : channel_type, 'ct_mode': fs_aug, 'face_type':self.face_type, 'data_format':nn.data_format, 'resolution': resolution}, {'sample_type': SampleProcessor.SampleType.FACE_MASK, 'warp':False , 'transform':True, 'channel_type' : SampleProcessor.ChannelType.G, 'face_mask_type' : SampleProcessor.FaceMaskType.FULL_FACE, 'face_type':self.face_type, 'data_format':nn.data_format, 'resolution': resolution}, {'sample_type': SampleProcessor.SampleType.FACE_MASK, 'warp':False , 'transform':True, 'channel_type' : SampleProcessor.ChannelType.G, 'face_mask_type' : SampleProcessor.FaceMaskType.FULL_FACE_EYES, 'face_type':self.face_type, 'data_format':nn.data_format, 'resolution': resolution}, @@ -830,6 +831,7 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... (warped_dst, target_dst, target_dstm, target_dstm_em) ) = samples S, D, SS, SSM, DD, DDM, SD, SDM = [ np.clip( nn.to_data_format(x,"NHWC", self.model_data_format), 0.0, 1.0) for x in ([target_src,target_dst] + self.AE_view (target_src, target_dst) ) ] + SW, DW = [ np.clip( nn.to_data_format(x,"NHWC", self.model_data_format), 0.0, 1.0) for x in ([warped_src,warped_dst]) ] SSM, DDM, SDM, = [ np.repeat (x, (3,), -1) for x in [SSM, DDM, SDM] ] target_srcm, target_dstm = [ nn.to_data_format(x,"NHWC", self.model_data_format) for x in ([target_srcm, target_dstm] )] @@ -845,6 +847,11 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... st.append ( np.concatenate ( ar, axis=1) ) result += [ ('SAEHD', np.concatenate (st, axis=0 )), ] + wt = [] + for i in range(n_samples): + ar = SW[i], SS[i], DW[i], DD[i], SD[i] + st.append ( np.concatenate ( ar, axis=1) ) + result += [ ('SAEHD warped', np.concatenate (wt, axis=0 )), ] st_m = [] for i in range(n_samples): diff --git a/samplelib/SampleProcessor.py b/samplelib/SampleProcessor.py index 4df44a3..1f1b96f 100644 --- a/samplelib/SampleProcessor.py +++ b/samplelib/SampleProcessor.py @@ -112,6 +112,7 @@ class SampleProcessor(object): nearest_resize_to = opts.get('nearest_resize_to', None) warp = opts.get('warp', False) transform = opts.get('transform', False) + random_downsample = opts.get('random_downsample', False) motion_blur = opts.get('motion_blur', None) gaussian_blur = opts.get('gaussian_blur', None) random_bilinear_resize = opts.get('random_bilinear_resize', None) @@ -213,6 +214,11 @@ class SampleProcessor(object): ct_sample_bgr = ct_sample.load_bgr() img = imagelib.color_transfer (ct_mode, img, cv2.resize( ct_sample_bgr, (resolution,resolution), interpolation=cv2.INTER_LINEAR ) ) + # Apply random downsampling + if random_downsample: + down_res = np.random.randint(int(0.125*resolution), int(0.25*resolution)) + img = cv2.resize(img, (down_res, down_res), interpolation=cv2.INTER_CUBIC) + img = cv2.resize(img, (resolution, resolution), interpolation=cv2.INTER_CUBIC) img = imagelib.warp_by_params (params_per_resolution[resolution], img, warp, transform, can_flip=True, border_replicate=border_replicate) img = np.clip(img.astype(np.float32), 0, 1) From bf0bd92492836ae0af3fc54d39049e8eafc45805 Mon Sep 17 00:00:00 2001 From: Jeremy Hummel Date: Sat, 22 May 2021 22:15:24 -0700 Subject: [PATCH 02/22] Fix preview --- models/Model_SAEHD/Model.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index 030057a..4a0d5fd 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -160,6 +160,9 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... self.options['random_warp'] = io.input_bool ("Enable random warp of samples", default_random_warp, help_message="Random warp is required to generalize facial expressions of both faces. When the face is trained enough, you can disable it to get extra sharpness and reduce subpixel shake for less amount of iterations.") self.options['random_downsample'] = io.input_bool("Enable random downsample of samples", False, help_message="") + self.options['random_noise'] = io.input_bool("Enable random noise added to samples", False, help_message="") + self.options['random_blur'] = io.input_bool("Enable random blur of samples", False, help_message="") + self.options['random_jpeg'] = io.input_bool("Enable random jpeg compression of samples", False, help_message="") self.options['gan_version'] = np.clip (io.input_int("GAN version", default_gan_version, add_info="2 or 3", help_message="Choose GAN version (v2: 7/16/2020, v3: 1/3/2021):"), 2, 3) if self.options['gan_version'] == 2: From d9560361d8becd2bc037ba60533dfd251a465234 Mon Sep 17 00:00:00 2001 From: Jeremy Hummel Date: Sat, 22 May 2021 22:33:43 -0700 Subject: [PATCH 03/22] fix previews --- models/Model_SAEHD/Model.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index 4a0d5fd..439972d 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -163,6 +163,7 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... self.options['random_noise'] = io.input_bool("Enable random noise added to samples", False, help_message="") self.options['random_blur'] = io.input_bool("Enable random blur of samples", False, help_message="") self.options['random_jpeg'] = io.input_bool("Enable random jpeg compression of samples", False, help_message="") + self.options['gan_version'] = np.clip (io.input_int("GAN version", default_gan_version, add_info="2 or 3", help_message="Choose GAN version (v2: 7/16/2020, v3: 1/3/2021):"), 2, 3) if self.options['gan_version'] == 2: @@ -853,7 +854,7 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... wt = [] for i in range(n_samples): ar = SW[i], SS[i], DW[i], DD[i], SD[i] - st.append ( np.concatenate ( ar, axis=1) ) + wt.append ( np.concatenate ( ar, axis=1) ) result += [ ('SAEHD warped', np.concatenate (wt, axis=0 )), ] st_m = [] From ba34a65a87a5b82c31e20d0c7a0cc73a2caf4f09 Mon Sep 17 00:00:00 2001 From: Jeremy Hummel Date: Sat, 22 May 2021 23:23:46 -0700 Subject: [PATCH 04/22] Set default value for random downsampling --- models/Model_SAEHD/Model.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index 439972d..8e91f0d 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -56,6 +56,7 @@ class SAEHDModel(ModelBase): default_loss_function = self.options['loss_function'] = self.load_or_def_option('loss_function', 'SSIM') default_random_warp = self.options['random_warp'] = self.load_or_def_option('random_warp', True) + default_random_downsample = self.options['random_downsample'] = self.load_or_def_option('random_downsample', False) default_background_power = self.options['background_power'] = self.load_or_def_option('background_power', 0.0) default_true_face_power = self.options['true_face_power'] = self.load_or_def_option('true_face_power', 0.0) default_face_style_power = self.options['face_style_power'] = self.load_or_def_option('face_style_power', 0.0) @@ -159,10 +160,10 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... self.options['random_warp'] = io.input_bool ("Enable random warp of samples", default_random_warp, help_message="Random warp is required to generalize facial expressions of both faces. When the face is trained enough, you can disable it to get extra sharpness and reduce subpixel shake for less amount of iterations.") - self.options['random_downsample'] = io.input_bool("Enable random downsample of samples", False, help_message="") - self.options['random_noise'] = io.input_bool("Enable random noise added to samples", False, help_message="") - self.options['random_blur'] = io.input_bool("Enable random blur of samples", False, help_message="") - self.options['random_jpeg'] = io.input_bool("Enable random jpeg compression of samples", False, help_message="") + self.options['random_downsample'] = io.input_bool("Enable random downsample of samples", default_random_downsample, help_message="") + # self.options['random_noise'] = io.input_bool("Enable random noise added to samples", False, help_message="") + # self.options['random_blur'] = io.input_bool("Enable random blur of samples", False, help_message="") + # self.options['random_jpeg'] = io.input_bool("Enable random jpeg compression of samples", False, help_message="") self.options['gan_version'] = np.clip (io.input_int("GAN version", default_gan_version, add_info="2 or 3", help_message="Choose GAN version (v2: 7/16/2020, v3: 1/3/2021):"), 2, 3) From b9af0d283c78cd79582c01f87074061c6be29247 Mon Sep 17 00:00:00 2001 From: Jeremy Hummel Date: Sat, 22 May 2021 23:33:57 -0700 Subject: [PATCH 05/22] Update >256px previews --- models/Model_SAEHD/Model.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index 8e91f0d..79ffa30 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -887,6 +887,23 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... st.append ( np.concatenate ( ar, axis=1) ) result += [ ('SAEHD pred', np.concatenate (st, axis=0 )), ] + wt = [] + for i in range(n_samples): + ar = SW[i], SS[i] + wt.append ( np.concatenate ( ar, axis=1) ) + result += [ ('SAEHD warped src-src', np.concatenate (wt, axis=0 )), ] + + wt = [] + for i in range(n_samples): + ar = DW[i], DD[i] + wt.append ( np.concatenate ( ar, axis=1) ) + result += [ ('SAEHD warped dst-dst', np.concatenate (wt, axis=0 )), ] + + wt = [] + for i in range(n_samples): + ar = DW[i], SD[i] + wt.append ( np.concatenate ( ar, axis=1) ) + result += [ ('SAEHD warped pred', np.concatenate (wt, axis=0 )), ] st_m = [] for i in range(n_samples): From de11c2ed2e44678629dd2e105fc2b5749ca3b289 Mon Sep 17 00:00:00 2001 From: Jeremy Hummel Date: Sun, 23 May 2021 01:10:40 -0700 Subject: [PATCH 06/22] random noise --- models/Model_SAEHD/Model.py | 15 ++++++++++++--- samplelib/SampleProcessor.py | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index 79ffa30..8618d6f 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -57,6 +57,7 @@ class SAEHDModel(ModelBase): default_random_warp = self.options['random_warp'] = self.load_or_def_option('random_warp', True) default_random_downsample = self.options['random_downsample'] = self.load_or_def_option('random_downsample', False) + default_random_noise = self.options['random_noise'] = self.load_or_def_option('random_noise', False) default_background_power = self.options['background_power'] = self.load_or_def_option('background_power', 0.0) default_true_face_power = self.options['true_face_power'] = self.load_or_def_option('true_face_power', 0.0) default_face_style_power = self.options['face_style_power'] = self.load_or_def_option('face_style_power', 0.0) @@ -161,7 +162,7 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... self.options['random_warp'] = io.input_bool ("Enable random warp of samples", default_random_warp, help_message="Random warp is required to generalize facial expressions of both faces. When the face is trained enough, you can disable it to get extra sharpness and reduce subpixel shake for less amount of iterations.") self.options['random_downsample'] = io.input_bool("Enable random downsample of samples", default_random_downsample, help_message="") - # self.options['random_noise'] = io.input_bool("Enable random noise added to samples", False, help_message="") + self.options['random_noise'] = io.input_bool("Enable random noise added to samples", False, help_message="") # self.options['random_blur'] = io.input_bool("Enable random blur of samples", False, help_message="") # self.options['random_jpeg'] = io.input_bool("Enable random jpeg compression of samples", False, help_message="") @@ -751,7 +752,11 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... self.set_training_data_generators ([ SampleGeneratorFace(training_data_src_path, random_ct_samples_path=random_ct_samples_path, debug=self.is_debug(), batch_size=self.get_batch_size(), sample_process_options=SampleProcessor.Options(random_flip=self.random_flip), - output_sample_types = [ {'sample_type': SampleProcessor.SampleType.FACE_IMAGE,'warp':random_warp, 'random_downsample': self.options['random_downsample'], 'transform':True, 'channel_type' : channel_type, 'ct_mode': ct_mode, 'face_type':self.face_type, 'data_format':nn.data_format, 'resolution': resolution}, + output_sample_types = [ {'sample_type': SampleProcessor.SampleType.FACE_IMAGE,'warp':random_warp, + 'random_downsample': self.options['random_downsample'], + 'random_noise': self.options['random_noise'], + 'transform':True, 'channel_type' : channel_type, 'ct_mode': ct_mode, + 'face_type':self.face_type, 'data_format':nn.data_format, 'resolution': resolution}, {'sample_type': SampleProcessor.SampleType.FACE_IMAGE,'warp':False , 'transform':True, 'channel_type' : channel_type, 'ct_mode': ct_mode, 'face_type':self.face_type, 'data_format':nn.data_format, 'resolution': resolution}, {'sample_type': SampleProcessor.SampleType.FACE_MASK, 'warp':False , 'transform':True, 'channel_type' : SampleProcessor.ChannelType.G, 'face_mask_type' : SampleProcessor.FaceMaskType.FULL_FACE, 'face_type':self.face_type, 'data_format':nn.data_format, 'resolution': resolution}, {'sample_type': SampleProcessor.SampleType.FACE_MASK, 'warp':False , 'transform':True, 'channel_type' : SampleProcessor.ChannelType.G, 'face_mask_type' : SampleProcessor.FaceMaskType.FULL_FACE_EYES, 'face_type':self.face_type, 'data_format':nn.data_format, 'resolution': resolution}, @@ -761,7 +766,11 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... SampleGeneratorFace(training_data_dst_path, debug=self.is_debug(), batch_size=self.get_batch_size(), sample_process_options=SampleProcessor.Options(random_flip=self.random_flip), - output_sample_types = [ {'sample_type': SampleProcessor.SampleType.FACE_IMAGE,'warp':random_warp, 'random_downsample': self.options['random_downsample'], 'transform':True, 'channel_type' : channel_type, 'ct_mode': fs_aug, 'face_type':self.face_type, 'data_format':nn.data_format, 'resolution': resolution}, + output_sample_types = [ {'sample_type': SampleProcessor.SampleType.FACE_IMAGE,'warp':random_warp, + 'random_downsample': self.options['random_downsample'], + 'random_noise': self.options['random_noise'], + 'transform':True, 'channel_type' : channel_type, 'ct_mode': fs_aug, + 'face_type':self.face_type, 'data_format':nn.data_format, 'resolution': resolution}, {'sample_type': SampleProcessor.SampleType.FACE_IMAGE,'warp':False , 'transform':True, 'channel_type' : channel_type, 'ct_mode': fs_aug, 'face_type':self.face_type, 'data_format':nn.data_format, 'resolution': resolution}, {'sample_type': SampleProcessor.SampleType.FACE_MASK, 'warp':False , 'transform':True, 'channel_type' : SampleProcessor.ChannelType.G, 'face_mask_type' : SampleProcessor.FaceMaskType.FULL_FACE, 'face_type':self.face_type, 'data_format':nn.data_format, 'resolution': resolution}, {'sample_type': SampleProcessor.SampleType.FACE_MASK, 'warp':False , 'transform':True, 'channel_type' : SampleProcessor.ChannelType.G, 'face_mask_type' : SampleProcessor.FaceMaskType.FULL_FACE_EYES, 'face_type':self.face_type, 'data_format':nn.data_format, 'resolution': resolution}, diff --git a/samplelib/SampleProcessor.py b/samplelib/SampleProcessor.py index 1f1b96f..c20f628 100644 --- a/samplelib/SampleProcessor.py +++ b/samplelib/SampleProcessor.py @@ -113,6 +113,7 @@ class SampleProcessor(object): warp = opts.get('warp', False) transform = opts.get('transform', False) random_downsample = opts.get('random_downsample', False) + random_noise = opts.get('random_noise', False) motion_blur = opts.get('motion_blur', None) gaussian_blur = opts.get('gaussian_blur', None) random_bilinear_resize = opts.get('random_bilinear_resize', None) @@ -220,6 +221,24 @@ class SampleProcessor(object): img = cv2.resize(img, (down_res, down_res), interpolation=cv2.INTER_CUBIC) img = cv2.resize(img, (resolution, resolution), interpolation=cv2.INTER_CUBIC) + # Apply random noise + if random_noise: + noise_type = np.random.choice(['gaussian', 'laplace', 'poisson']) + noise_scale = (20 * np.random.random() + 20) / 255.0 + + if noise_type == 'gaussian': + noise = np.random.normal(scale=noise_scale, size=img.shape) + img += noise + elif noise_type == 'laplace': + # noise = np.random.laplace(scale=noise_scale, size=img.shape) + # img += noise + pass + elif noise_type == 'poisson': + # noise_lam = (15 * np.random.random() + 15) + # noise = np.random.poisson(lam=noise_lam, size=img.shape) + # img += noise + pass + img = imagelib.warp_by_params (params_per_resolution[resolution], img, warp, transform, can_flip=True, border_replicate=border_replicate) img = np.clip(img.astype(np.float32), 0, 1) From 08c4967afb779ba95cd68dfc7fc763276a4d29c8 Mon Sep 17 00:00:00 2001 From: Jeremy Hummel Date: Sun, 23 May 2021 01:29:06 -0700 Subject: [PATCH 07/22] laplace noise --- samplelib/SampleProcessor.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/samplelib/SampleProcessor.py b/samplelib/SampleProcessor.py index c20f628..421526d 100644 --- a/samplelib/SampleProcessor.py +++ b/samplelib/SampleProcessor.py @@ -227,12 +227,13 @@ class SampleProcessor(object): noise_scale = (20 * np.random.random() + 20) / 255.0 if noise_type == 'gaussian': - noise = np.random.normal(scale=noise_scale, size=img.shape) - img += noise - elif noise_type == 'laplace': - # noise = np.random.laplace(scale=noise_scale, size=img.shape) + # noise = np.random.normal(scale=noise_scale, size=img.shape) # img += noise pass + elif noise_type == 'laplace': + noise = np.random.laplace(scale=noise_scale, size=img.shape) + img += noise + # pass elif noise_type == 'poisson': # noise_lam = (15 * np.random.random() + 15) # noise = np.random.poisson(lam=noise_lam, size=img.shape) From 6856b5ad22736529c64e1787be03247121b755af Mon Sep 17 00:00:00 2001 From: Jeremy Hummel Date: Sun, 23 May 2021 01:40:42 -0700 Subject: [PATCH 08/22] poisson noise --- samplelib/SampleProcessor.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/samplelib/SampleProcessor.py b/samplelib/SampleProcessor.py index 421526d..f36c559 100644 --- a/samplelib/SampleProcessor.py +++ b/samplelib/SampleProcessor.py @@ -224,21 +224,21 @@ class SampleProcessor(object): # Apply random noise if random_noise: noise_type = np.random.choice(['gaussian', 'laplace', 'poisson']) - noise_scale = (20 * np.random.random() + 20) / 255.0 + noise_scale = (20 * np.random.random() + 20) if noise_type == 'gaussian': # noise = np.random.normal(scale=noise_scale, size=img.shape) # img += noise pass elif noise_type == 'laplace': - noise = np.random.laplace(scale=noise_scale, size=img.shape) - img += noise - # pass - elif noise_type == 'poisson': - # noise_lam = (15 * np.random.random() + 15) - # noise = np.random.poisson(lam=noise_lam, size=img.shape) + # noise = np.random.laplace(scale=noise_scale, size=img.shape) # img += noise pass + elif noise_type == 'poisson': + noise_lam = (15 * np.random.random() + 15) + noise = np.random.poisson(lam=noise_lam, size=img.shape) + img += noise / 255.0 + # pass img = imagelib.warp_by_params (params_per_resolution[resolution], img, warp, transform, can_flip=True, border_replicate=border_replicate) img = np.clip(img.astype(np.float32), 0, 1) From 411346b2bd0baa3758d3f9f90cd289daf6ed5025 Mon Sep 17 00:00:00 2001 From: Jeremy Hummel Date: Sun, 23 May 2021 01:45:46 -0700 Subject: [PATCH 09/22] all noise enabled --- samplelib/SampleProcessor.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/samplelib/SampleProcessor.py b/samplelib/SampleProcessor.py index f36c559..f7d7f17 100644 --- a/samplelib/SampleProcessor.py +++ b/samplelib/SampleProcessor.py @@ -227,13 +227,13 @@ class SampleProcessor(object): noise_scale = (20 * np.random.random() + 20) if noise_type == 'gaussian': - # noise = np.random.normal(scale=noise_scale, size=img.shape) - # img += noise - pass + noise = np.random.normal(scale=noise_scale, size=img.shape) + img += noise + # pass elif noise_type == 'laplace': - # noise = np.random.laplace(scale=noise_scale, size=img.shape) - # img += noise - pass + noise = np.random.laplace(scale=noise_scale, size=img.shape) + img += noise + # pass elif noise_type == 'poisson': noise_lam = (15 * np.random.random() + 15) noise = np.random.poisson(lam=noise_lam, size=img.shape) From 84c22511c00725a9cf2887b44fd54d2306c49537 Mon Sep 17 00:00:00 2001 From: Jeremy Hummel Date: Sun, 23 May 2021 01:47:18 -0700 Subject: [PATCH 10/22] default random noise option --- models/Model_SAEHD/Model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index 8618d6f..b925cb1 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -162,7 +162,7 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... self.options['random_warp'] = io.input_bool ("Enable random warp of samples", default_random_warp, help_message="Random warp is required to generalize facial expressions of both faces. When the face is trained enough, you can disable it to get extra sharpness and reduce subpixel shake for less amount of iterations.") self.options['random_downsample'] = io.input_bool("Enable random downsample of samples", default_random_downsample, help_message="") - self.options['random_noise'] = io.input_bool("Enable random noise added to samples", False, help_message="") + self.options['random_noise'] = io.input_bool("Enable random noise added to samples", default_random_noise, help_message="") # self.options['random_blur'] = io.input_bool("Enable random blur of samples", False, help_message="") # self.options['random_jpeg'] = io.input_bool("Enable random jpeg compression of samples", False, help_message="") From 4a3d1d971e9a3ab3317bf71ff8f3dbd50787fd32 Mon Sep 17 00:00:00 2001 From: Jeremy Hummel Date: Sun, 23 May 2021 01:51:19 -0700 Subject: [PATCH 11/22] Fix scaling --- samplelib/SampleProcessor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samplelib/SampleProcessor.py b/samplelib/SampleProcessor.py index f7d7f17..2e47fc9 100644 --- a/samplelib/SampleProcessor.py +++ b/samplelib/SampleProcessor.py @@ -228,11 +228,11 @@ class SampleProcessor(object): if noise_type == 'gaussian': noise = np.random.normal(scale=noise_scale, size=img.shape) - img += noise + img += noise / 255.0 # pass elif noise_type == 'laplace': noise = np.random.laplace(scale=noise_scale, size=img.shape) - img += noise + img += noise / 255.0 # pass elif noise_type == 'poisson': noise_lam = (15 * np.random.random() + 15) From 3855adb1bdc46ea776f8b27b37ee1257401b4c8e Mon Sep 17 00:00:00 2001 From: Jeremy Hummel Date: Sun, 23 May 2021 14:04:01 -0700 Subject: [PATCH 12/22] remove some commented-out code --- samplelib/SampleProcessor.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/samplelib/SampleProcessor.py b/samplelib/SampleProcessor.py index 2e47fc9..3f9a74f 100644 --- a/samplelib/SampleProcessor.py +++ b/samplelib/SampleProcessor.py @@ -229,16 +229,13 @@ class SampleProcessor(object): if noise_type == 'gaussian': noise = np.random.normal(scale=noise_scale, size=img.shape) img += noise / 255.0 - # pass elif noise_type == 'laplace': noise = np.random.laplace(scale=noise_scale, size=img.shape) img += noise / 255.0 - # pass elif noise_type == 'poisson': noise_lam = (15 * np.random.random() + 15) noise = np.random.poisson(lam=noise_lam, size=img.shape) img += noise / 255.0 - # pass img = imagelib.warp_by_params (params_per_resolution[resolution], img, warp, transform, can_flip=True, border_replicate=border_replicate) img = np.clip(img.astype(np.float32), 0, 1) From b2cf0dec59067b729c898d8c7619dfc09f728487 Mon Sep 17 00:00:00 2001 From: Jeremy Hummel Date: Sun, 23 May 2021 18:18:35 -0700 Subject: [PATCH 13/22] Motion blur --- models/Model_SAEHD/Model.py | 6 +++++- samplelib/SampleProcessor.py | 14 +++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index b925cb1..0f4d2d7 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -58,6 +58,8 @@ class SAEHDModel(ModelBase): default_random_warp = self.options['random_warp'] = self.load_or_def_option('random_warp', True) default_random_downsample = self.options['random_downsample'] = self.load_or_def_option('random_downsample', False) default_random_noise = self.options['random_noise'] = self.load_or_def_option('random_noise', False) + default_random_blur = self.options['random_blur'] = self.load_or_def_option('random_blur', False) + default_background_power = self.options['background_power'] = self.load_or_def_option('background_power', 0.0) default_true_face_power = self.options['true_face_power'] = self.load_or_def_option('true_face_power', 0.0) default_face_style_power = self.options['face_style_power'] = self.load_or_def_option('face_style_power', 0.0) @@ -163,7 +165,7 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... self.options['random_downsample'] = io.input_bool("Enable random downsample of samples", default_random_downsample, help_message="") self.options['random_noise'] = io.input_bool("Enable random noise added to samples", default_random_noise, help_message="") - # self.options['random_blur'] = io.input_bool("Enable random blur of samples", False, help_message="") + self.options['random_blur'] = io.input_bool("Enable random blur of samples", False, help_message="") # self.options['random_jpeg'] = io.input_bool("Enable random jpeg compression of samples", False, help_message="") self.options['gan_version'] = np.clip (io.input_int("GAN version", default_gan_version, add_info="2 or 3", help_message="Choose GAN version (v2: 7/16/2020, v3: 1/3/2021):"), 2, 3) @@ -755,6 +757,7 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... output_sample_types = [ {'sample_type': SampleProcessor.SampleType.FACE_IMAGE,'warp':random_warp, 'random_downsample': self.options['random_downsample'], 'random_noise': self.options['random_noise'], + 'random_blur': self.options['random_blur'], 'transform':True, 'channel_type' : channel_type, 'ct_mode': ct_mode, 'face_type':self.face_type, 'data_format':nn.data_format, 'resolution': resolution}, {'sample_type': SampleProcessor.SampleType.FACE_IMAGE,'warp':False , 'transform':True, 'channel_type' : channel_type, 'ct_mode': ct_mode, 'face_type':self.face_type, 'data_format':nn.data_format, 'resolution': resolution}, @@ -769,6 +772,7 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... output_sample_types = [ {'sample_type': SampleProcessor.SampleType.FACE_IMAGE,'warp':random_warp, 'random_downsample': self.options['random_downsample'], 'random_noise': self.options['random_noise'], + 'random_blur': self.options['random_blur'], 'transform':True, 'channel_type' : channel_type, 'ct_mode': fs_aug, 'face_type':self.face_type, 'data_format':nn.data_format, 'resolution': resolution}, {'sample_type': SampleProcessor.SampleType.FACE_IMAGE,'warp':False , 'transform':True, 'channel_type' : channel_type, 'ct_mode': fs_aug, 'face_type':self.face_type, 'data_format':nn.data_format, 'resolution': resolution}, diff --git a/samplelib/SampleProcessor.py b/samplelib/SampleProcessor.py index 3f9a74f..43250f2 100644 --- a/samplelib/SampleProcessor.py +++ b/samplelib/SampleProcessor.py @@ -7,7 +7,7 @@ import numpy as np from core import imagelib from core.cv2ex import * -from core.imagelib import sd +from core.imagelib import sd, blursharpen from core.imagelib.color_transfer import random_lab_rotation from facelib import FaceType, LandmarksProcessor @@ -114,6 +114,7 @@ class SampleProcessor(object): transform = opts.get('transform', False) random_downsample = opts.get('random_downsample', False) random_noise = opts.get('random_noise', False) + random_blur = opts.get('random_blur', False) motion_blur = opts.get('motion_blur', None) gaussian_blur = opts.get('gaussian_blur', None) random_bilinear_resize = opts.get('random_bilinear_resize', None) @@ -237,6 +238,17 @@ class SampleProcessor(object): noise = np.random.poisson(lam=noise_lam, size=img.shape) img += noise / 255.0 + # Apply random blur + if random_blur: + blur_type = np.random.choice(['motion', 'gaussian']) + + if blur_type == 'motion': + blur_k = np.random.randint(10, 20) + blur_angle = 360 * np.random.random() + img = blursharpen.LinearMotionBlur(img, blur_k, blur_angle) + elif blur_type == 'gaussian': + pass + img = imagelib.warp_by_params (params_per_resolution[resolution], img, warp, transform, can_flip=True, border_replicate=border_replicate) img = np.clip(img.astype(np.float32), 0, 1) From ba61e2ffeb14d8b599c5d5412117bb22cb4502fb Mon Sep 17 00:00:00 2001 From: Jeremy Hummel Date: Sun, 23 May 2021 18:25:25 -0700 Subject: [PATCH 14/22] fix import --- samplelib/SampleProcessor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samplelib/SampleProcessor.py b/samplelib/SampleProcessor.py index 43250f2..e80951f 100644 --- a/samplelib/SampleProcessor.py +++ b/samplelib/SampleProcessor.py @@ -7,7 +7,7 @@ import numpy as np from core import imagelib from core.cv2ex import * -from core.imagelib import sd, blursharpen +from core.imagelib import sd, LinearMotionBlur from core.imagelib.color_transfer import random_lab_rotation from facelib import FaceType, LandmarksProcessor @@ -245,7 +245,7 @@ class SampleProcessor(object): if blur_type == 'motion': blur_k = np.random.randint(10, 20) blur_angle = 360 * np.random.random() - img = blursharpen.LinearMotionBlur(img, blur_k, blur_angle) + img = LinearMotionBlur(img, blur_k, blur_angle) elif blur_type == 'gaussian': pass From f6f4d9e44671495ed391aa051270120d1b187b3d Mon Sep 17 00:00:00 2001 From: Jeremy Hummel Date: Sun, 23 May 2021 18:44:10 -0700 Subject: [PATCH 15/22] gaussian blur --- samplelib/SampleProcessor.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/samplelib/SampleProcessor.py b/samplelib/SampleProcessor.py index e80951f..34024d7 100644 --- a/samplelib/SampleProcessor.py +++ b/samplelib/SampleProcessor.py @@ -243,11 +243,19 @@ class SampleProcessor(object): blur_type = np.random.choice(['motion', 'gaussian']) if blur_type == 'motion': - blur_k = np.random.randint(10, 20) - blur_angle = 360 * np.random.random() - img = LinearMotionBlur(img, blur_k, blur_angle) - elif blur_type == 'gaussian': + # blur_k = np.random.randint(10, 20) + # blur_angle = 360 * np.random.random() + # img = LinearMotionBlur(img, blur_k, blur_angle) pass + elif blur_type == 'gaussian': + blur_sigma = 5 * np.random.random() + 3 + + if blur_sigma < 5.0: + kernel_size = 2.9 * blur_sigma # 97% of weight + else: + kernel_size = 2.6 * blur_sigma # 95% of weight + + img = cv2.GaussianBlur(img, (kernel_size, kernel_size), blur_sigma) img = imagelib.warp_by_params (params_per_resolution[resolution], img, warp, transform, can_flip=True, border_replicate=border_replicate) img = np.clip(img.astype(np.float32), 0, 1) From 6d8abe34c2e892df5dc049e96d22182ff6fa7dba Mon Sep 17 00:00:00 2001 From: Jeremy Hummel Date: Sun, 23 May 2021 19:37:46 -0700 Subject: [PATCH 16/22] fix kernel size --- samplelib/SampleProcessor.py | 1 + 1 file changed, 1 insertion(+) diff --git a/samplelib/SampleProcessor.py b/samplelib/SampleProcessor.py index 34024d7..db78ccf 100644 --- a/samplelib/SampleProcessor.py +++ b/samplelib/SampleProcessor.py @@ -254,6 +254,7 @@ class SampleProcessor(object): kernel_size = 2.9 * blur_sigma # 97% of weight else: kernel_size = 2.6 * blur_sigma # 95% of weight + kernel_size = int(kernel_size) img = cv2.GaussianBlur(img, (kernel_size, kernel_size), blur_sigma) From f8263d2914c3392254f043caf3fd458167f6172c Mon Sep 17 00:00:00 2001 From: Jeremy Hummel Date: Sun, 23 May 2021 19:44:46 -0700 Subject: [PATCH 17/22] fix kernel_size --- samplelib/SampleProcessor.py | 1 + 1 file changed, 1 insertion(+) diff --git a/samplelib/SampleProcessor.py b/samplelib/SampleProcessor.py index db78ccf..299dcc0 100644 --- a/samplelib/SampleProcessor.py +++ b/samplelib/SampleProcessor.py @@ -255,6 +255,7 @@ class SampleProcessor(object): else: kernel_size = 2.6 * blur_sigma # 95% of weight kernel_size = int(kernel_size) + kernel_size = kernel_size + 1 if kernel_size % 2 == 0 else kernel_size img = cv2.GaussianBlur(img, (kernel_size, kernel_size), blur_sigma) From 22a5e68f56bf9f0f17a6fa0b105534073dc62bbc Mon Sep 17 00:00:00 2001 From: Jeremy Hummel Date: Sun, 23 May 2021 19:47:56 -0700 Subject: [PATCH 18/22] enable motion blur --- samplelib/SampleProcessor.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/samplelib/SampleProcessor.py b/samplelib/SampleProcessor.py index 299dcc0..8278108 100644 --- a/samplelib/SampleProcessor.py +++ b/samplelib/SampleProcessor.py @@ -243,10 +243,9 @@ class SampleProcessor(object): blur_type = np.random.choice(['motion', 'gaussian']) if blur_type == 'motion': - # blur_k = np.random.randint(10, 20) - # blur_angle = 360 * np.random.random() - # img = LinearMotionBlur(img, blur_k, blur_angle) - pass + blur_k = np.random.randint(10, 20) + blur_angle = 360 * np.random.random() + img = LinearMotionBlur(img, blur_k, blur_angle) elif blur_type == 'gaussian': blur_sigma = 5 * np.random.random() + 3 From 2be49246fda8488af0bdbb7864f2a64a82c86330 Mon Sep 17 00:00:00 2001 From: Jeremy Hummel Date: Sun, 23 May 2021 20:54:33 -0700 Subject: [PATCH 19/22] random jpeg --- models/Model_SAEHD/Model.py | 7 +++++-- samplelib/SampleProcessor.py | 8 ++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index 0f4d2d7..46e1000 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -59,6 +59,7 @@ class SAEHDModel(ModelBase): default_random_downsample = self.options['random_downsample'] = self.load_or_def_option('random_downsample', False) default_random_noise = self.options['random_noise'] = self.load_or_def_option('random_noise', False) default_random_blur = self.options['random_blur'] = self.load_or_def_option('random_blur', False) + default_random_jpeg = self.options['random_jpeg'] = self.load_or_def_option('random_jpeg', False) default_background_power = self.options['background_power'] = self.load_or_def_option('background_power', 0.0) default_true_face_power = self.options['true_face_power'] = self.load_or_def_option('true_face_power', 0.0) @@ -165,8 +166,8 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... self.options['random_downsample'] = io.input_bool("Enable random downsample of samples", default_random_downsample, help_message="") self.options['random_noise'] = io.input_bool("Enable random noise added to samples", default_random_noise, help_message="") - self.options['random_blur'] = io.input_bool("Enable random blur of samples", False, help_message="") - # self.options['random_jpeg'] = io.input_bool("Enable random jpeg compression of samples", False, help_message="") + self.options['random_blur'] = io.input_bool("Enable random blur of samples", default_random_blur, help_message="") + self.options['random_jpeg'] = io.input_bool("Enable random jpeg compression of samples", default_random_jpeg, help_message="") self.options['gan_version'] = np.clip (io.input_int("GAN version", default_gan_version, add_info="2 or 3", help_message="Choose GAN version (v2: 7/16/2020, v3: 1/3/2021):"), 2, 3) @@ -758,6 +759,7 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... 'random_downsample': self.options['random_downsample'], 'random_noise': self.options['random_noise'], 'random_blur': self.options['random_blur'], + 'random_jpeg': self.options['random_jpeg'], 'transform':True, 'channel_type' : channel_type, 'ct_mode': ct_mode, 'face_type':self.face_type, 'data_format':nn.data_format, 'resolution': resolution}, {'sample_type': SampleProcessor.SampleType.FACE_IMAGE,'warp':False , 'transform':True, 'channel_type' : channel_type, 'ct_mode': ct_mode, 'face_type':self.face_type, 'data_format':nn.data_format, 'resolution': resolution}, @@ -773,6 +775,7 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... 'random_downsample': self.options['random_downsample'], 'random_noise': self.options['random_noise'], 'random_blur': self.options['random_blur'], + 'random_jpeg': self.options['random_jpeg'], 'transform':True, 'channel_type' : channel_type, 'ct_mode': fs_aug, 'face_type':self.face_type, 'data_format':nn.data_format, 'resolution': resolution}, {'sample_type': SampleProcessor.SampleType.FACE_IMAGE,'warp':False , 'transform':True, 'channel_type' : channel_type, 'ct_mode': fs_aug, 'face_type':self.face_type, 'data_format':nn.data_format, 'resolution': resolution}, diff --git a/samplelib/SampleProcessor.py b/samplelib/SampleProcessor.py index 8278108..620b973 100644 --- a/samplelib/SampleProcessor.py +++ b/samplelib/SampleProcessor.py @@ -115,6 +115,7 @@ class SampleProcessor(object): random_downsample = opts.get('random_downsample', False) random_noise = opts.get('random_noise', False) random_blur = opts.get('random_blur', False) + random_jpeg = opts.get('random_jpeg', False) motion_blur = opts.get('motion_blur', None) gaussian_blur = opts.get('gaussian_blur', None) random_bilinear_resize = opts.get('random_bilinear_resize', None) @@ -258,6 +259,13 @@ class SampleProcessor(object): img = cv2.GaussianBlur(img, (kernel_size, kernel_size), blur_sigma) + # Apply random jpeg compression + if random_jpeg: + jpeg_compression_level = np.random.randint(50, 85) + encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), jpeg_compression_level] + _, encimg = cv2.imencode('.jpg', img, encode_param) + img = cv2.imdecode(encimg, 1) + img = imagelib.warp_by_params (params_per_resolution[resolution], img, warp, transform, can_flip=True, border_replicate=border_replicate) img = np.clip(img.astype(np.float32), 0, 1) From 0c8ab9b510ddf5f84485da324f8b6f30c18cd803 Mon Sep 17 00:00:00 2001 From: Jeremy Hummel Date: Sun, 23 May 2021 22:15:22 -0700 Subject: [PATCH 20/22] fix jpeg range --- samplelib/SampleProcessor.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/samplelib/SampleProcessor.py b/samplelib/SampleProcessor.py index 620b973..f8d1106 100644 --- a/samplelib/SampleProcessor.py +++ b/samplelib/SampleProcessor.py @@ -261,10 +261,11 @@ class SampleProcessor(object): # Apply random jpeg compression if random_jpeg: + img = np.clip(img*255, 0, 255).astype(np.uint8) jpeg_compression_level = np.random.randint(50, 85) encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), jpeg_compression_level] - _, encimg = cv2.imencode('.jpg', img, encode_param) - img = cv2.imdecode(encimg, 1) + _, enc_img = cv2.imencode('.jpg', img, encode_param) + img = cv2.imdecode(enc_img, cv2.IMREAD_UNCHANGED).astype(np.float32) / 255.0 img = imagelib.warp_by_params (params_per_resolution[resolution], img, warp, transform, can_flip=True, border_replicate=border_replicate) img = np.clip(img.astype(np.float32), 0, 1) From 5f89a12e01302758d5ff946b33f5ad137c651410 Mon Sep 17 00:00:00 2001 From: Jeremy Hummel Date: Sun, 23 May 2021 22:27:11 -0700 Subject: [PATCH 21/22] randomize order of distortions --- samplelib/SampleProcessor.py | 86 ++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 42 deletions(-) diff --git a/samplelib/SampleProcessor.py b/samplelib/SampleProcessor.py index f8d1106..5fa854c 100644 --- a/samplelib/SampleProcessor.py +++ b/samplelib/SampleProcessor.py @@ -217,55 +217,57 @@ class SampleProcessor(object): ct_sample_bgr = ct_sample.load_bgr() img = imagelib.color_transfer (ct_mode, img, cv2.resize( ct_sample_bgr, (resolution,resolution), interpolation=cv2.INTER_LINEAR ) ) - # Apply random downsampling - if random_downsample: - down_res = np.random.randint(int(0.125*resolution), int(0.25*resolution)) - img = cv2.resize(img, (down_res, down_res), interpolation=cv2.INTER_CUBIC) - img = cv2.resize(img, (resolution, resolution), interpolation=cv2.INTER_CUBIC) + randomization_order = np.random.shuffle(['blur', 'noise', 'jpeg', 'down']) + for random_distortion in randomization_order: + # Apply random blur + if random_distortion == 'blur' and random_blur: + blur_type = np.random.choice(['motion', 'gaussian']) - # Apply random noise - if random_noise: - noise_type = np.random.choice(['gaussian', 'laplace', 'poisson']) - noise_scale = (20 * np.random.random() + 20) + if blur_type == 'motion': + blur_k = np.random.randint(10, 20) + blur_angle = 360 * np.random.random() + img = LinearMotionBlur(img, blur_k, blur_angle) + elif blur_type == 'gaussian': + blur_sigma = 5 * np.random.random() + 3 - if noise_type == 'gaussian': - noise = np.random.normal(scale=noise_scale, size=img.shape) - img += noise / 255.0 - elif noise_type == 'laplace': - noise = np.random.laplace(scale=noise_scale, size=img.shape) - img += noise / 255.0 - elif noise_type == 'poisson': - noise_lam = (15 * np.random.random() + 15) - noise = np.random.poisson(lam=noise_lam, size=img.shape) - img += noise / 255.0 + if blur_sigma < 5.0: + kernel_size = 2.9 * blur_sigma # 97% of weight + else: + kernel_size = 2.6 * blur_sigma # 95% of weight + kernel_size = int(kernel_size) + kernel_size = kernel_size + 1 if kernel_size % 2 == 0 else kernel_size - # Apply random blur - if random_blur: - blur_type = np.random.choice(['motion', 'gaussian']) + img = cv2.GaussianBlur(img, (kernel_size, kernel_size), blur_sigma) - if blur_type == 'motion': - blur_k = np.random.randint(10, 20) - blur_angle = 360 * np.random.random() - img = LinearMotionBlur(img, blur_k, blur_angle) - elif blur_type == 'gaussian': - blur_sigma = 5 * np.random.random() + 3 + # Apply random noise + if random_distortion == 'noise' and random_noise: + noise_type = np.random.choice(['gaussian', 'laplace', 'poisson']) + noise_scale = (20 * np.random.random() + 20) - if blur_sigma < 5.0: - kernel_size = 2.9 * blur_sigma # 97% of weight - else: - kernel_size = 2.6 * blur_sigma # 95% of weight - kernel_size = int(kernel_size) - kernel_size = kernel_size + 1 if kernel_size % 2 == 0 else kernel_size + if noise_type == 'gaussian': + noise = np.random.normal(scale=noise_scale, size=img.shape) + img += noise / 255.0 + elif noise_type == 'laplace': + noise = np.random.laplace(scale=noise_scale, size=img.shape) + img += noise / 255.0 + elif noise_type == 'poisson': + noise_lam = (15 * np.random.random() + 15) + noise = np.random.poisson(lam=noise_lam, size=img.shape) + img += noise / 255.0 - img = cv2.GaussianBlur(img, (kernel_size, kernel_size), blur_sigma) + # Apply random jpeg compression + if random_distortion == 'jpeg' and random_jpeg: + img = np.clip(img*255, 0, 255).astype(np.uint8) + jpeg_compression_level = np.random.randint(50, 85) + encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), jpeg_compression_level] + _, enc_img = cv2.imencode('.jpg', img, encode_param) + img = cv2.imdecode(enc_img, cv2.IMREAD_UNCHANGED).astype(np.float32) / 255.0 - # Apply random jpeg compression - if random_jpeg: - img = np.clip(img*255, 0, 255).astype(np.uint8) - jpeg_compression_level = np.random.randint(50, 85) - encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), jpeg_compression_level] - _, enc_img = cv2.imencode('.jpg', img, encode_param) - img = cv2.imdecode(enc_img, cv2.IMREAD_UNCHANGED).astype(np.float32) / 255.0 + # Apply random downsampling + if random_distortion == 'down' and random_downsample: + down_res = np.random.randint(int(0.125*resolution), int(0.25*resolution)) + img = cv2.resize(img, (down_res, down_res), interpolation=cv2.INTER_CUBIC) + img = cv2.resize(img, (resolution, resolution), interpolation=cv2.INTER_CUBIC) img = imagelib.warp_by_params (params_per_resolution[resolution], img, warp, transform, can_flip=True, border_replicate=border_replicate) img = np.clip(img.astype(np.float32), 0, 1) From 924c3fc6fd8a127a41e840ffb50396acbf7f21f3 Mon Sep 17 00:00:00 2001 From: Jeremy Hummel Date: Sun, 23 May 2021 22:32:43 -0700 Subject: [PATCH 22/22] fix shuffle --- samplelib/SampleProcessor.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/samplelib/SampleProcessor.py b/samplelib/SampleProcessor.py index 5fa854c..3b657d7 100644 --- a/samplelib/SampleProcessor.py +++ b/samplelib/SampleProcessor.py @@ -217,7 +217,9 @@ class SampleProcessor(object): ct_sample_bgr = ct_sample.load_bgr() img = imagelib.color_transfer (ct_mode, img, cv2.resize( ct_sample_bgr, (resolution,resolution), interpolation=cv2.INTER_LINEAR ) ) - randomization_order = np.random.shuffle(['blur', 'noise', 'jpeg', 'down']) + + randomization_order = ['blur', 'noise', 'jpeg', 'down'] + np.random.shuffle(randomization_order) for random_distortion in randomization_order: # Apply random blur if random_distortion == 'blur' and random_blur: