From 2be49246fda8488af0bdbb7864f2a64a82c86330 Mon Sep 17 00:00:00 2001 From: Jeremy Hummel Date: Sun, 23 May 2021 20:54:33 -0700 Subject: [PATCH] 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)