From 7f4348bc4fa6f4a8b98421403aebacc42f97b849 Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 28 Nov 2021 16:16:26 +0100 Subject: [PATCH 01/16] added missing random_hsv_power to sample generator --- 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 ce74d1b..feb2a49 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -806,7 +806,7 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... '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, + 'transform':True, 'channel_type' : channel_type, 'ct_mode': ct_mode, 'random_hsv_shift_amount' : random_hsv_power, '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}, From 44dcdbea7eba8b57553b381eb900c4c4dbfd5ef1 Mon Sep 17 00:00:00 2001 From: Cioscos Date: Mon, 29 Nov 2021 12:02:20 +0100 Subject: [PATCH 02/16] Added reading options from yaml file --- main.py | 2 + mainscripts/Trainer.py | 2 + models/ModelBase.py | 53 +++++++++- models/Model_SAEHD/Model.py | 187 ++++++++++++++++++------------------ 4 files changed, 152 insertions(+), 92 deletions(-) diff --git a/main.py b/main.py index 52e0216..f96157b 100644 --- a/main.py +++ b/main.py @@ -131,6 +131,7 @@ if __name__ == "__main__": 'start_tensorboard' : arguments.start_tensorboard, 'dump_ckpt' : arguments.dump_ckpt, 'flask_preview' : arguments.flask_preview, + 'config_training_file' : arguments.config_training_file } from mainscripts import Trainer Trainer.main(**kwargs) @@ -150,6 +151,7 @@ if __name__ == "__main__": p.add_argument('--silent-start', action="store_true", dest="silent_start", default=False, help="Silent start. Automatically chooses Best GPU and last used model.") p.add_argument('--tensorboard-logdir', action=fixPathAction, dest="tensorboard_dir", help="Directory of the tensorboard output files") p.add_argument('--start-tensorboard', action="store_true", dest="start_tensorboard", default=False, help="Automatically start the tensorboard server preconfigured to the tensorboard-logdir") + p.add_argument('--config-training-file', action=fixPathAction, dest="config_training_file", help="Path to custom yaml configuration file") p.add_argument('--dump-ckpt', action="store_true", dest="dump_ckpt", default=False, help="Dump the model to ckpt format.") diff --git a/mainscripts/Trainer.py b/mainscripts/Trainer.py index 732d6f9..0c751fc 100644 --- a/mainscripts/Trainer.py +++ b/mainscripts/Trainer.py @@ -67,6 +67,7 @@ def trainerThread (s2c, c2s, e, debug=False, tensorboard_dir=None, start_tensorboard=False, + config_training_file=None, dump_ckpt=False, **kwargs): while True: @@ -97,6 +98,7 @@ def trainerThread (s2c, c2s, e, force_gpu_idxs=force_gpu_idxs, cpu_only=cpu_only, silent_start=silent_start, + config_training_file=config_training_file, debug=debug) is_reached_goal = model.is_reached_iter_goal() diff --git a/models/ModelBase.py b/models/ModelBase.py index 634e797..0bad6d1 100644 --- a/models/ModelBase.py +++ b/models/ModelBase.py @@ -10,6 +10,7 @@ import tempfile import time import datetime from pathlib import Path +import yaml import cv2 import numpy as np @@ -35,6 +36,7 @@ class ModelBase(object): cpu_only=False, debug=False, force_model_class_name=None, + config_training_file=None, silent_start=False, **kwargs): self.is_training = is_training @@ -140,14 +142,35 @@ class ModelBase(object): self.sample_for_preview = None self.choosed_gpu_indexes = None + # MODIFY HERE!!! --------------------------------------------------------------------------------------- model_data = {} + self.config_file_exists = False + self.read_from_conf = False + #check if config_training_file mode is enabled + if config_training_file is not None: + self.config_file_path = Path(config_training_file) + if self.config_file_path.exists(): + self.read_from_conf = io.input_bool( + f'Do you want to read training options from {self.config_file_path.stem} file?', + False, + 'Read options from configuration file instead of asking one by one each option' + ) + + if self.read_from_conf: + self.options = self.read_from_config_file() + self.config_file_exists = True + else: + io.log_info(f"Configuration file doesn't exist. A standard configuration file will be created.") + + self.model_data_path = Path( self.get_strpath_storage_for_file('data.dat') ) if self.model_data_path.exists(): io.log_info (f"Loading {self.model_name} model...") model_data = pickle.loads ( self.model_data_path.read_bytes() ) self.iter = model_data.get('iter',0) if self.iter != 0: - self.options = model_data['options'] + if not self.config_file_exists: + self.options = model_data['options'] self.loss_history = model_data.get('loss_history', []) self.sample_for_preview = model_data.get('sample_for_preview', None) self.choosed_gpu_indexes = model_data.get('choosed_gpu_indexes', None) @@ -183,6 +206,9 @@ class ModelBase(object): if self.is_first_run(): # save as default options only for first run model initialize self.default_options_path.write_bytes( pickle.dumps (self.options) ) + # save config file + self.save_config_file() + self.session_name = self.options.get('session_name', "") self.autobackup_hour = self.options.get('autobackup_hour', 0) self.maximum_n_backups = self.options.get('maximum_n_backups', 24) @@ -426,6 +452,29 @@ class ModelBase(object): self.autobackup_start_time += self.autobackup_hour*3600 self.create_backup() + def read_from_config_file(self): + with open(self.config_file_path, 'r') as file: + data = yaml.safe_load(file) + + for key, value in data.items(): + if isinstance(value, int): + data[key] = np.int32(value) + elif isinstance(value, float): + data[key] = np.float64(value) + + return data + + def save_config_file(self): + saving_dict = {} + for key, value in self.options.items(): + if isinstance(value, np.int32) or isinstance(value, np.float64): + saving_dict[key] = value.item() + else: + saving_dict[key] = value + + with open(self.config_file_path, 'w') as file: + yaml.dump(saving_dict, file) + def create_backup(self): io.log_info ("Creating backup...", end='\r') @@ -558,6 +607,8 @@ class ModelBase(object): def get_strpath_storage_for_file(self, filename): return str( self.saved_models_path / ( self.get_model_name() + '_' + filename) ) + + def get_summary_path(self): return self.get_strpath_storage_for_file('summary.txt') diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index e922705..29df794 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -28,7 +28,7 @@ class SAEHDModel(ModelBase): min_res = 64 max_res = 640 - #default_usefp16 = self.options['use_fp16'] = self.load_or_def_option('use_fp16', False) + default_usefp16 = self.options['use_fp16'] = self.load_or_def_option('use_fp16', False) default_resolution = self.options['resolution'] = self.load_or_def_option('resolution', 128) default_face_type = self.options['face_type'] = self.load_or_def_option('face_type', 'f') default_models_opt_on_gpu = self.options['models_opt_on_gpu'] = self.load_or_def_option('models_opt_on_gpu', True) @@ -71,84 +71,88 @@ class SAEHDModel(ModelBase): ask_override = self.ask_override() if self.is_first_run() or ask_override: - self.ask_session_name() - self.ask_autobackup_hour() - self.ask_maximum_n_backups() - self.ask_write_preview_history() - self.ask_target_iter() - self.ask_retraining_samples() - self.ask_random_src_flip() - self.ask_random_dst_flip() - self.ask_batch_size(suggest_batch_size) - #self.options['use_fp16'] = io.input_bool ("Use fp16", default_usefp16, help_message='Increases training/inference speed, reduces model size. Model may crash. Enable it after 1-5k iters.') + if (self.read_from_conf and not self.config_file_exists) or not self.read_from_conf: + self.ask_session_name() + self.ask_autobackup_hour() + self.ask_maximum_n_backups() + self.ask_write_preview_history() + self.ask_target_iter() + self.ask_retraining_samples() + self.ask_random_src_flip() + self.ask_random_dst_flip() + self.ask_batch_size(suggest_batch_size) + self.options['use_fp16'] = io.input_bool ("Use fp16", default_usefp16, help_message='Increases training/inference speed, reduces model size. Model may crash. Enable it after 1-5k iters.') if self.is_first_run(): - resolution = io.input_int("Resolution", default_resolution, add_info="64-640", help_message="More resolution requires more VRAM and time to train. Value will be adjusted to multiple of 16 and 32 for -d archi.") - resolution = np.clip ( (resolution // 16) * 16, min_res, max_res) - self.options['resolution'] = resolution - self.options['face_type'] = io.input_str ("Face type", default_face_type, ['h','mf','f','wf','head', 'custom'], help_message="Half / mid face / full face / whole face / head / custom. Half face has better resolution, but covers less area of cheeks. Mid face is 30% wider than half face. 'Whole face' covers full area of face include forehead. 'head' covers full head, but requires XSeg for src and dst faceset.").lower() + if (self.read_from_conf and not self.config_file_exists) or not self.read_from_conf: + resolution = io.input_int("Resolution", default_resolution, add_info="64-640", help_message="More resolution requires more VRAM and time to train. Value will be adjusted to multiple of 16 and 32 for -d archi.") + resolution = np.clip ( (resolution // 16) * 16, min_res, max_res) + self.options['resolution'] = resolution + self.options['face_type'] = io.input_str ("Face type", default_face_type, ['h','mf','f','wf','head', 'custom'], help_message="Half / mid face / full face / whole face / head / custom. Half face has better resolution, but covers less area of cheeks. Mid face is 30% wider than half face. 'Whole face' covers full area of face include forehead. 'head' covers full head, but requires XSeg for src and dst faceset.").lower() - while True: - archi = io.input_str ("AE architecture", default_archi, help_message=\ -""" -'df' keeps more identity-preserved face. -'liae' can fix overly different face shapes. -'-u' increased likeness of the face. -'-d' (experimental) doubling the resolution using the same computation cost. -Examples: df, liae, df-d, df-ud, liae-ud, ... -""").lower() + while True: + archi = io.input_str ("AE architecture", default_archi, help_message=\ + """ + 'df' keeps more identity-preserved face. + 'liae' can fix overly different face shapes. + '-u' increased likeness of the face. + '-d' (experimental) doubling the resolution using the same computation cost. + Examples: df, liae, df-d, df-ud, liae-ud, ... + """).lower() - archi_split = archi.split('-') + archi_split = archi.split('-') - if len(archi_split) == 2: - archi_type, archi_opts = archi_split - elif len(archi_split) == 1: - archi_type, archi_opts = archi_split[0], None - else: - continue - - if archi_type not in ['df', 'liae']: - continue - - if archi_opts is not None: - if len(archi_opts) == 0: - continue - if len([ 1 for opt in archi_opts if opt not in ['u','d','t','c'] ]) != 0: + if len(archi_split) == 2: + archi_type, archi_opts = archi_split + elif len(archi_split) == 1: + archi_type, archi_opts = archi_split[0], None + else: continue - if 'd' in archi_opts: - self.options['resolution'] = np.clip ( (self.options['resolution'] // 32) * 32, min_res, max_res) + if archi_type not in ['df', 'liae']: + continue - break - self.options['archi'] = archi + if archi_opts is not None: + if len(archi_opts) == 0: + continue + if len([ 1 for opt in archi_opts if opt not in ['u','d','t','c'] ]) != 0: + continue - default_d_dims = self.options['d_dims'] = self.load_or_def_option('d_dims', 64) + if 'd' in archi_opts: + self.options['resolution'] = np.clip ( (self.options['resolution'] // 32) * 32, min_res, max_res) - default_d_mask_dims = default_d_dims // 3 - default_d_mask_dims += default_d_mask_dims % 2 - default_d_mask_dims = self.options['d_mask_dims'] = self.load_or_def_option('d_mask_dims', default_d_mask_dims) + break + self.options['archi'] = archi + + default_d_dims = self.options['d_dims'] = self.load_or_def_option('d_dims', 64) + + default_d_mask_dims = default_d_dims // 3 + default_d_mask_dims += default_d_mask_dims % 2 + default_d_mask_dims = self.options['d_mask_dims'] = self.load_or_def_option('d_mask_dims', default_d_mask_dims) if self.is_first_run(): - self.options['ae_dims'] = np.clip ( io.input_int("AutoEncoder dimensions", default_ae_dims, add_info="32-1024", help_message="All face information will packed to AE dims. If amount of AE dims are not enough, then for example closed eyes will not be recognized. More dims are better, but require more VRAM. You can fine-tune model size to fit your GPU." ), 32, 1024 ) + if (self.read_from_conf and not self.config_file_exists) or not self.read_from_conf: + self.options['ae_dims'] = np.clip ( io.input_int("AutoEncoder dimensions", default_ae_dims, add_info="32-1024", help_message="All face information will packed to AE dims. If amount of AE dims are not enough, then for example closed eyes will not be recognized. More dims are better, but require more VRAM. You can fine-tune model size to fit your GPU." ), 32, 1024 ) - e_dims = np.clip ( io.input_int("Encoder dimensions", default_e_dims, add_info="16-256", help_message="More dims help to recognize more facial features and achieve sharper result, but require more VRAM. You can fine-tune model size to fit your GPU." ), 16, 256 ) - self.options['e_dims'] = e_dims + e_dims % 2 + e_dims = np.clip ( io.input_int("Encoder dimensions", default_e_dims, add_info="16-256", help_message="More dims help to recognize more facial features and achieve sharper result, but require more VRAM. You can fine-tune model size to fit your GPU." ), 16, 256 ) + self.options['e_dims'] = e_dims + e_dims % 2 - d_dims = np.clip ( io.input_int("Decoder dimensions", default_d_dims, add_info="16-256", help_message="More dims help to recognize more facial features and achieve sharper result, but require more VRAM. You can fine-tune model size to fit your GPU." ), 16, 256 ) - self.options['d_dims'] = d_dims + d_dims % 2 + d_dims = np.clip ( io.input_int("Decoder dimensions", default_d_dims, add_info="16-256", help_message="More dims help to recognize more facial features and achieve sharper result, but require more VRAM. You can fine-tune model size to fit your GPU." ), 16, 256 ) + self.options['d_dims'] = d_dims + d_dims % 2 - d_mask_dims = np.clip ( io.input_int("Decoder mask dimensions", default_d_mask_dims, add_info="16-256", help_message="Typical mask dimensions = decoder dimensions / 3. If you manually cut out obstacles from the dst mask, you can increase this parameter to achieve better quality." ), 16, 256 ) - self.options['d_mask_dims'] = d_mask_dims + d_mask_dims % 2 + d_mask_dims = np.clip ( io.input_int("Decoder mask dimensions", default_d_mask_dims, add_info="16-256", help_message="Typical mask dimensions = decoder dimensions / 3. If you manually cut out obstacles from the dst mask, you can increase this parameter to achieve better quality." ), 16, 256 ) + self.options['d_mask_dims'] = d_mask_dims + d_mask_dims % 2 if self.is_first_run() or ask_override: - if self.options['face_type'] == 'wf' or self.options['face_type'] == 'head' or self.options['face_type'] == 'custom': - self.options['masked_training'] = io.input_bool ("Masked training", default_masked_training, help_message="This option is available only for 'whole_face' or 'head' type. Masked training clips training area to full_face mask or XSeg mask, thus network will train the faces properly.") + if (self.read_from_conf and not self.config_file_exists) or not self.read_from_conf: + if self.options['face_type'] == 'wf' or self.options['face_type'] == 'head' or self.options['face_type'] == 'custom': + self.options['masked_training'] = io.input_bool ("Masked training", default_masked_training, help_message="This option is available only for 'whole_face' or 'head' type. Masked training clips training area to full_face mask or XSeg mask, thus network will train the faces properly.") - self.options['eyes_prio'] = io.input_bool ("Eyes priority", default_eyes_prio, help_message='Helps to fix eye problems during training like "alien eyes" and wrong eyes direction ( especially on HD architectures ) by forcing the neural network to train eyes with higher priority. before/after https://i.imgur.com/YQHOuSR.jpg ') - self.options['mouth_prio'] = io.input_bool ("Mouth priority", default_mouth_prio, help_message='Helps to fix mouth problems during training by forcing the neural network to train mouth with higher priority similar to eyes ') + self.options['eyes_prio'] = io.input_bool ("Eyes priority", default_eyes_prio, help_message='Helps to fix eye problems during training like "alien eyes" and wrong eyes direction ( especially on HD architectures ) by forcing the neural network to train eyes with higher priority. before/after https://i.imgur.com/YQHOuSR.jpg ') + self.options['mouth_prio'] = io.input_bool ("Mouth priority", default_mouth_prio, help_message='Helps to fix mouth problems during training by forcing the neural network to train mouth with higher priority similar to eyes ') - self.options['uniform_yaw'] = io.input_bool ("Uniform yaw distribution of samples", default_uniform_yaw, help_message='Helps to fix blurry side faces due to small amount of them in the faceset.') - self.options['blur_out_mask'] = io.input_bool ("Blur out mask", default_blur_out_mask, help_message='Blurs nearby area outside of applied face mask of training samples. The result is the background near the face is smoothed and less noticeable on swapped face. The exact xseg mask in src and dst faceset is required.') + self.options['uniform_yaw'] = io.input_bool ("Uniform yaw distribution of samples", default_uniform_yaw, help_message='Helps to fix blurry side faces due to small amount of them in the faceset.') + self.options['blur_out_mask'] = io.input_bool ("Blur out mask", default_blur_out_mask, help_message='Blurs nearby area outside of applied face mask of training samples. The result is the background near the face is smoothed and less noticeable on swapped face. The exact xseg mask in src and dst faceset is required.') default_gan_power = self.options['gan_power'] = self.load_or_def_option('gan_power', 0.0) default_gan_patch_size = self.options['gan_patch_size'] = self.load_or_def_option('gan_patch_size', self.options['resolution'] // 8) @@ -157,52 +161,53 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... default_gan_noise = self.options['gan_noise'] = self.load_or_def_option('gan_noise', 0.0) if self.is_first_run() or ask_override: - self.options['models_opt_on_gpu'] = io.input_bool ("Place models and optimizer on GPU", default_models_opt_on_gpu, help_message="When you train on one GPU, by default model and optimizer weights are placed on GPU to accelerate the process. You can place they on CPU to free up extra VRAM, thus set bigger dimensions.") + if (self.read_from_conf and not self.config_file_exists) or not self.read_from_conf: + self.options['models_opt_on_gpu'] = io.input_bool ("Place models and optimizer on GPU", default_models_opt_on_gpu, help_message="When you train on one GPU, by default model and optimizer weights are placed on GPU to accelerate the process. You can place they on CPU to free up extra VRAM, thus set bigger dimensions.") - self.options['adabelief'] = io.input_bool ("Use AdaBelief optimizer?", default_adabelief, help_message="Use AdaBelief optimizer. It requires more VRAM, but the accuracy and the generalization of the model is higher.") + self.options['adabelief'] = io.input_bool ("Use AdaBelief optimizer?", default_adabelief, help_message="Use AdaBelief optimizer. It requires more VRAM, but the accuracy and the generalization of the model is higher.") - self.options['lr_dropout'] = io.input_str (f"Use learning rate dropout", default_lr_dropout, ['n','y','cpu'], help_message="When the face is trained enough, you can enable this option to get extra sharpness and reduce subpixel shake for less amount of iterations. Enabled it before `disable random warp` and before GAN. \nn - disabled.\ny - enabled\ncpu - enabled on CPU. This allows not to use extra VRAM, sacrificing 20% time of iteration.") + self.options['lr_dropout'] = io.input_str (f"Use learning rate dropout", default_lr_dropout, ['n','y','cpu'], help_message="When the face is trained enough, you can enable this option to get extra sharpness and reduce subpixel shake for less amount of iterations. Enabled it before `disable random warp` and before GAN. \nn - disabled.\ny - enabled\ncpu - enabled on CPU. This allows not to use extra VRAM, sacrificing 20% time of iteration.") - self.options['loss_function'] = io.input_str(f"Loss function", default_loss_function, ['SSIM', 'MS-SSIM', 'MS-SSIM+L1'], - help_message="Change loss function used for image quality assessment.") + self.options['loss_function'] = io.input_str(f"Loss function", default_loss_function, ['SSIM', 'MS-SSIM', 'MS-SSIM+L1'], + help_message="Change loss function used for image quality assessment.") - 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_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_hsv_power'] = np.clip ( io.input_number ("Random hue/saturation/light intensity", default_random_hsv_power, add_info="0.0 .. 0.3", help_message="Random hue/saturation/light intensity applied to the src face set only at the input of the neural network. Stabilizes color perturbations during face swapping. Reduces the quality of the color transfer by selecting the closest one in the src faceset. Thus the src faceset must be diverse enough. Typical fine value is 0.05"), 0.0, 0.3 ) + self.options['random_hsv_power'] = np.clip ( io.input_number ("Random hue/saturation/light intensity", default_random_hsv_power, add_info="0.0 .. 0.3", help_message="Random hue/saturation/light intensity applied to the src face set only at the input of the neural network. Stabilizes color perturbations during face swapping. Reduces the quality of the color transfer by selecting the closest one in the src faceset. Thus the src faceset must be diverse enough. Typical fine value is 0.05"), 0.0, 0.3 ) - 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", 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['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", 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_power'] = np.clip ( io.input_number ("GAN power", default_gan_power, add_info="0.0 .. 10.0", help_message="Train the network in Generative Adversarial manner. Forces the neural network to learn small details of the face. Enable it only when the face is trained enough and don't disable. Typical value is 0.1"), 0.0, 10.0 ) + self.options['gan_power'] = np.clip ( io.input_number ("GAN power", default_gan_power, add_info="0.0 .. 10.0", help_message="Train the network in Generative Adversarial manner. Forces the neural network to learn small details of the face. Enable it only when the face is trained enough and don't disable. Typical value is 0.1"), 0.0, 10.0 ) - if self.options['gan_power'] != 0.0: - if self.options['gan_version'] == 3: - gan_patch_size = np.clip ( io.input_int("GAN patch size", default_gan_patch_size, add_info="3-640", help_message="The higher patch size, the higher the quality, the more VRAM is required. You can get sharper edges even at the lowest setting. Typical fine value is resolution / 8." ), 3, 640 ) - self.options['gan_patch_size'] = gan_patch_size + if self.options['gan_power'] != 0.0: + if self.options['gan_version'] == 3: + gan_patch_size = np.clip ( io.input_int("GAN patch size", default_gan_patch_size, add_info="3-640", help_message="The higher patch size, the higher the quality, the more VRAM is required. You can get sharper edges even at the lowest setting. Typical fine value is resolution / 8." ), 3, 640 ) + self.options['gan_patch_size'] = gan_patch_size - gan_dims = np.clip ( io.input_int("GAN dimensions", default_gan_dims, add_info="4-64", help_message="The dimensions of the GAN network. The higher dimensions, the more VRAM is required. You can get sharper edges even at the lowest setting. Typical fine value is 16." ), 4, 64 ) - self.options['gan_dims'] = gan_dims + gan_dims = np.clip ( io.input_int("GAN dimensions", default_gan_dims, add_info="4-64", help_message="The dimensions of the GAN network. The higher dimensions, the more VRAM is required. You can get sharper edges even at the lowest setting. Typical fine value is 16." ), 4, 64 ) + self.options['gan_dims'] = gan_dims - self.options['gan_smoothing'] = np.clip ( io.input_number("GAN label smoothing", default_gan_smoothing, add_info="0 - 0.5", help_message="Uses soft labels with values slightly off from 0/1 for GAN, has a regularizing effect"), 0, 0.5) - self.options['gan_noise'] = np.clip ( io.input_number("GAN noisy labels", default_gan_noise, add_info="0 - 0.5", help_message="Marks some images with the wrong label, helps prevent collapse"), 0, 0.5) + self.options['gan_smoothing'] = np.clip ( io.input_number("GAN label smoothing", default_gan_smoothing, add_info="0 - 0.5", help_message="Uses soft labels with values slightly off from 0/1 for GAN, has a regularizing effect"), 0, 0.5) + self.options['gan_noise'] = np.clip ( io.input_number("GAN noisy labels", default_gan_noise, add_info="0 - 0.5", help_message="Marks some images with the wrong label, helps prevent collapse"), 0, 0.5) - if 'df' in self.options['archi']: - self.options['true_face_power'] = np.clip ( io.input_number ("'True face' power.", default_true_face_power, add_info="0.0000 .. 1.0", help_message="Experimental option. Discriminates result face to be more like src face. Higher value - stronger discrimination. Typical value is 0.01 . Comparison - https://i.imgur.com/czScS9q.png"), 0.0, 1.0 ) - else: - self.options['true_face_power'] = 0.0 + if 'df' in self.options['archi']: + self.options['true_face_power'] = np.clip ( io.input_number ("'True face' power.", default_true_face_power, add_info="0.0000 .. 1.0", help_message="Experimental option. Discriminates result face to be more like src face. Higher value - stronger discrimination. Typical value is 0.01 . Comparison - https://i.imgur.com/czScS9q.png"), 0.0, 1.0 ) + else: + self.options['true_face_power'] = 0.0 - self.options['background_power'] = np.clip ( io.input_number("Background power", default_background_power, add_info="0.0..1.0", help_message="Learn the area outside of the mask. Helps smooth out area near the mask boundaries. Can be used at any time"), 0.0, 1.0 ) + self.options['background_power'] = np.clip ( io.input_number("Background power", default_background_power, add_info="0.0..1.0", help_message="Learn the area outside of the mask. Helps smooth out area near the mask boundaries. Can be used at any time"), 0.0, 1.0 ) - self.options['face_style_power'] = np.clip ( io.input_number("Face style power", default_face_style_power, add_info="0.0..100.0", help_message="Learn the color of the predicted face to be the same as dst inside mask. If you want to use this option with 'whole_face' you have to use XSeg trained mask. Warning: Enable it only after 10k iters, when predicted face is clear enough to start learn style. Start from 0.001 value and check history changes. Enabling this option increases the chance of model collapse."), 0.0, 100.0 ) - self.options['bg_style_power'] = np.clip ( io.input_number("Background style power", default_bg_style_power, add_info="0.0..100.0", help_message="Learn the area outside mask of the predicted face to be the same as dst. If you want to use this option with 'whole_face' you have to use XSeg trained mask. For whole_face you have to use XSeg trained mask. This can make face more like dst. Enabling this option increases the chance of model collapse. Typical value is 2.0"), 0.0, 100.0 ) + self.options['face_style_power'] = np.clip ( io.input_number("Face style power", default_face_style_power, add_info="0.0..100.0", help_message="Learn the color of the predicted face to be the same as dst inside mask. If you want to use this option with 'whole_face' you have to use XSeg trained mask. Warning: Enable it only after 10k iters, when predicted face is clear enough to start learn style. Start from 0.001 value and check history changes. Enabling this option increases the chance of model collapse."), 0.0, 100.0 ) + self.options['bg_style_power'] = np.clip ( io.input_number("Background style power", default_bg_style_power, add_info="0.0..100.0", help_message="Learn the area outside mask of the predicted face to be the same as dst. If you want to use this option with 'whole_face' you have to use XSeg trained mask. For whole_face you have to use XSeg trained mask. This can make face more like dst. Enabling this option increases the chance of model collapse. Typical value is 2.0"), 0.0, 100.0 ) - self.options['ct_mode'] = io.input_str (f"Color transfer for src faceset", default_ct_mode, ['none','rct','lct','mkl','idt','sot', 'fs-aug'], help_message="Change color distribution of src samples close to dst samples. Try all modes to find the best. FS aug adds random color to dst and src") - self.options['random_color'] = io.input_bool ("Random color", default_random_color, help_message="Samples are randomly rotated around the L axis in LAB colorspace, helps generalize training") - self.options['clipgrad'] = io.input_bool ("Enable gradient clipping", default_clipgrad, help_message="Gradient clipping reduces chance of model collapse, sacrificing speed of training.") + self.options['ct_mode'] = io.input_str (f"Color transfer for src faceset", default_ct_mode, ['none','rct','lct','mkl','idt','sot', 'fs-aug'], help_message="Change color distribution of src samples close to dst samples. Try all modes to find the best. FS aug adds random color to dst and src") + self.options['random_color'] = io.input_bool ("Random color", default_random_color, help_message="Samples are randomly rotated around the L axis in LAB colorspace, helps generalize training") + self.options['clipgrad'] = io.input_bool ("Enable gradient clipping", default_clipgrad, help_message="Gradient clipping reduces chance of model collapse, sacrificing speed of training.") - self.options['pretrain'] = io.input_bool ("Enable pretraining mode", default_pretrain, help_message="Pretrain the model with large amount of various faces. After that, model can be used to train the fakes more quickly. Forces random_warp=N, random_flips=Y, gan_power=0.0, lr_dropout=N, styles=0.0, uniform_yaw=Y") + self.options['pretrain'] = io.input_bool ("Enable pretraining mode", default_pretrain, help_message="Pretrain the model with large amount of various faces. After that, model can be used to train the fakes more quickly. Forces random_warp=N, random_flips=Y, gan_power=0.0, lr_dropout=N, styles=0.0, uniform_yaw=Y") if self.options['pretrain'] and self.get_pretraining_data_path() is None: raise Exception("pretraining_data_path is not defined") From f1ddbb05ded3aff341cf365170d15918061718bc Mon Sep 17 00:00:00 2001 From: Cioscos Date: Tue, 30 Nov 2021 22:18:35 +0100 Subject: [PATCH 03/16] Bug fixes --- models/ModelBase.py | 11 ++++++++--- models/Model_SAEHD/Model.py | 32 ++++++++++++++++---------------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/models/ModelBase.py b/models/ModelBase.py index 9f54e43..fa0f232 100644 --- a/models/ModelBase.py +++ b/models/ModelBase.py @@ -150,7 +150,9 @@ class ModelBase(object): #check if config_training_file mode is enabled if config_training_file is not None: self.config_file_path = Path(config_training_file) - if self.config_file_path.exists(): + if not self.config_file_path.exists(): + os.mkdir(self.config_file_path) + if Path(self.get_strpath_configuration_path()).exists(): self.read_from_conf = io.input_bool( f'Do you want to read training options from {self.config_file_path.stem} file?', False, @@ -463,7 +465,7 @@ class ModelBase(object): Returns: [type]: [description] """ - with open(self.config_file_path, 'r') as file: + with open(self.get_strpath_configuration_path(), 'r') as file: data = yaml.safe_load(file) for key, value in data.items(): @@ -487,7 +489,7 @@ class ModelBase(object): else: saving_dict[key] = value - with open(self.config_file_path, 'w') as file: + with open(self.get_strpath_configuration_path(), 'w') as file: yaml.dump(saving_dict, file, sort_keys=False) def create_backup(self): @@ -622,6 +624,9 @@ class ModelBase(object): def get_strpath_storage_for_file(self, filename): return str( self.saved_models_path / ( self.get_model_name() + '_' + filename) ) + def get_strpath_configuration_path(self): + return str(self.config_file_path / 'configuration_file.yaml') + def get_summary_path(self): return self.get_strpath_storage_for_file('summary.txt') diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index a2f6e04..69fe681 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -28,7 +28,7 @@ class SAEHDModel(ModelBase): min_res = 64 max_res = 640 - default_usefp16 = self.options['use_fp16'] = self.load_or_def_option('use_fp16', False) + #default_usefp16 = self.options['use_fp16'] = self.load_or_def_option('use_fp16', False) default_resolution = self.options['resolution'] = self.load_or_def_option('resolution', 128) default_face_type = self.options['face_type'] = self.load_or_def_option('face_type', 'f') default_models_opt_on_gpu = self.options['models_opt_on_gpu'] = self.load_or_def_option('models_opt_on_gpu', True) @@ -68,7 +68,7 @@ class SAEHDModel(ModelBase): default_random_color = self.options['random_color'] = self.load_or_def_option('random_color', False) default_clipgrad = self.options['clipgrad'] = self.load_or_def_option('clipgrad', False) default_pretrain = self.options['pretrain'] = self.load_or_def_option('pretrain', False) - default_use_fp16 = self.options['use_fp16'] = self.load_or_def_option('use_fp16', False) + #default_use_fp16 = self.options['use_fp16'] = self.load_or_def_option('use_fp16', False) ask_override = False if self.read_from_conf else self.ask_override() if self.is_first_run() or ask_override: @@ -82,7 +82,7 @@ class SAEHDModel(ModelBase): self.ask_random_src_flip() self.ask_random_dst_flip() self.ask_batch_size(suggest_batch_size) - self.options['use_fp16'] = io.input_bool ("Use fp16", default_usefp16, help_message='Increases training/inference speed, reduces model size. Model may crash. Enable it after 1-5k iters.') + #self.options['use_fp16'] = io.input_bool ("Use fp16", default_usefp16, help_message='Increases training/inference speed, reduces model size. Model may crash. Enable it after 1-5k iters.') if self.is_first_run(): if (self.read_from_conf and not self.config_file_exists) or not self.read_from_conf: @@ -184,23 +184,23 @@ class SAEHDModel(ModelBase): self.options['gan_power'] = np.clip ( io.input_number ("GAN power", default_gan_power, add_info="0.0 .. 10.0", help_message="Train the network in Generative Adversarial manner. Forces the neural network to learn small details of the face. Enable it only when the face is trained enough and don't disable. Typical value is 0.1"), 0.0, 10.0 ) - if self.options['gan_power'] != 0.0: - 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_power'] != 0.0: + 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'] == 3: - gan_patch_size = np.clip ( io.input_int("GAN patch size", default_gan_patch_size, add_info="3-640", help_message="The higher patch size, the higher the quality, the more VRAM is required. You can get sharper edges even at the lowest setting. Typical fine value is resolution / 8." ), 3, 640 ) - self.options['gan_patch_size'] = gan_patch_size + if self.options['gan_version'] == 3: + gan_patch_size = np.clip ( io.input_int("GAN patch size", default_gan_patch_size, add_info="3-640", help_message="The higher patch size, the higher the quality, the more VRAM is required. You can get sharper edges even at the lowest setting. Typical fine value is resolution / 8." ), 3, 640 ) + self.options['gan_patch_size'] = gan_patch_size - gan_dims = np.clip ( io.input_int("GAN dimensions", default_gan_dims, add_info="4-64", help_message="The dimensions of the GAN network. The higher dimensions, the more VRAM is required. You can get sharper edges even at the lowest setting. Typical fine value is 16." ), 4, 64 ) - self.options['gan_dims'] = gan_dims + gan_dims = np.clip ( io.input_int("GAN dimensions", default_gan_dims, add_info="4-64", help_message="The dimensions of the GAN network. The higher dimensions, the more VRAM is required. You can get sharper edges even at the lowest setting. Typical fine value is 16." ), 4, 64 ) + self.options['gan_dims'] = gan_dims - self.options['gan_smoothing'] = np.clip ( io.input_number("GAN label smoothing", default_gan_smoothing, add_info="0 - 0.5", help_message="Uses soft labels with values slightly off from 0/1 for GAN, has a regularizing effect"), 0, 0.5) - self.options['gan_noise'] = np.clip ( io.input_number("GAN noisy labels", default_gan_noise, add_info="0 - 0.5", help_message="Marks some images with the wrong label, helps prevent collapse"), 0, 0.5) + self.options['gan_smoothing'] = np.clip ( io.input_number("GAN label smoothing", default_gan_smoothing, add_info="0 - 0.5", help_message="Uses soft labels with values slightly off from 0/1 for GAN, has a regularizing effect"), 0, 0.5) + self.options['gan_noise'] = np.clip ( io.input_number("GAN noisy labels", default_gan_noise, add_info="0 - 0.5", help_message="Marks some images with the wrong label, helps prevent collapse"), 0, 0.5) - if 'df' in self.options['archi']: - self.options['true_face_power'] = np.clip ( io.input_number ("'True face' power.", default_true_face_power, add_info="0.0000 .. 1.0", help_message="Experimental option. Discriminates result face to be more like src face. Higher value - stronger discrimination. Typical value is 0.01 . Comparison - https://i.imgur.com/czScS9q.png"), 0.0, 1.0 ) - else: - self.options['true_face_power'] = 0.0 + if 'df' in self.options['archi']: + self.options['true_face_power'] = np.clip ( io.input_number ("'True face' power.", default_true_face_power, add_info="0.0000 .. 1.0", help_message="Experimental option. Discriminates result face to be more like src face. Higher value - stronger discrimination. Typical value is 0.01 . Comparison - https://i.imgur.com/czScS9q.png"), 0.0, 1.0 ) + else: + self.options['true_face_power'] = 0.0 self.options['background_power'] = np.clip ( io.input_number("Background power", default_background_power, add_info="0.0..1.0", help_message="Learn the area outside of the mask. Helps smooth out area near the mask boundaries. Can be used at any time"), 0.0, 1.0 ) From 6959f690843e53494be35db375ef8619ffaba850 Mon Sep 17 00:00:00 2001 From: Cioscos Date: Tue, 30 Nov 2021 23:07:41 +0100 Subject: [PATCH 04/16] Change behaviour --- models/ModelBase.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/ModelBase.py b/models/ModelBase.py index fa0f232..0dd8d56 100644 --- a/models/ModelBase.py +++ b/models/ModelBase.py @@ -155,7 +155,7 @@ class ModelBase(object): if Path(self.get_strpath_configuration_path()).exists(): self.read_from_conf = io.input_bool( f'Do you want to read training options from {self.config_file_path.stem} file?', - False, + True, 'Read options from configuration file instead of asking one by one each option' ) From 57cdf0e7947cdd2cfc20993f559455fee248d45f Mon Sep 17 00:00:00 2001 From: Cioscos Date: Thu, 2 Dec 2021 19:07:22 +0100 Subject: [PATCH 05/16] Bug fix --- models/ModelBase.py | 3 ++- models/Model_SAEHD/Model.py | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/models/ModelBase.py b/models/ModelBase.py index 0dd8d56..527b23b 100644 --- a/models/ModelBase.py +++ b/models/ModelBase.py @@ -211,7 +211,8 @@ class ModelBase(object): # save as default options only for first run model initialize self.default_options_path.write_bytes( pickle.dumps (self.options) ) # save config file - self.save_config_file() + if config_training_file is not None: + self.save_config_file() self.session_name = self.options.get('session_name', "") self.autobackup_hour = self.options.get('autobackup_hour', 0) diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index 69fe681..08420ba 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -28,7 +28,7 @@ class SAEHDModel(ModelBase): min_res = 64 max_res = 640 - #default_usefp16 = self.options['use_fp16'] = self.load_or_def_option('use_fp16', False) + default_usefp16 = self.options['use_fp16'] = self.load_or_def_option('use_fp16', False) default_resolution = self.options['resolution'] = self.load_or_def_option('resolution', 128) default_face_type = self.options['face_type'] = self.load_or_def_option('face_type', 'f') default_models_opt_on_gpu = self.options['models_opt_on_gpu'] = self.load_or_def_option('models_opt_on_gpu', True) @@ -82,7 +82,7 @@ class SAEHDModel(ModelBase): self.ask_random_src_flip() self.ask_random_dst_flip() self.ask_batch_size(suggest_batch_size) - #self.options['use_fp16'] = io.input_bool ("Use fp16", default_usefp16, help_message='Increases training/inference speed, reduces model size. Model may crash. Enable it after 1-5k iters.') + self.options['use_fp16'] = io.input_bool ("Use fp16", default_usefp16, help_message='Increases training/inference speed, reduces model size. Model may crash. Enable it after 1-5k iters.') if self.is_first_run(): if (self.read_from_conf and not self.config_file_exists) or not self.read_from_conf: From 8976ae3863efaf1c4cd4aa7e08da53e2ab8896b0 Mon Sep 17 00:00:00 2001 From: Cioscos Date: Sat, 4 Dec 2021 15:31:15 +0100 Subject: [PATCH 06/16] Added new features - New cli argument --auto-gen-config It allows to create and/or write a yaml conf file inside model folder. The conf file will be called with the name of the model. - Bug fixes - Code refactoring --- main.py | 4 ++- mainscripts/Trainer.py | 1 + models/ModelBase.py | 70 +++++++++++++++++++++++++++++++----------- 3 files changed, 56 insertions(+), 19 deletions(-) diff --git a/main.py b/main.py index f96157b..054dff5 100644 --- a/main.py +++ b/main.py @@ -131,7 +131,8 @@ if __name__ == "__main__": 'start_tensorboard' : arguments.start_tensorboard, 'dump_ckpt' : arguments.dump_ckpt, 'flask_preview' : arguments.flask_preview, - 'config_training_file' : arguments.config_training_file + 'config_training_file' : arguments.config_training_file, + 'auto_gen_config' : arguments.auto_gen_config } from mainscripts import Trainer Trainer.main(**kwargs) @@ -152,6 +153,7 @@ if __name__ == "__main__": p.add_argument('--tensorboard-logdir', action=fixPathAction, dest="tensorboard_dir", help="Directory of the tensorboard output files") p.add_argument('--start-tensorboard', action="store_true", dest="start_tensorboard", default=False, help="Automatically start the tensorboard server preconfigured to the tensorboard-logdir") p.add_argument('--config-training-file', action=fixPathAction, dest="config_training_file", help="Path to custom yaml configuration file") + p.add_argument('--auto-gen-config', action="store_true", dest="auto_gen_config", default=False, help="Saves a configuration file for each model used in the trainer. It'll have the same model name") p.add_argument('--dump-ckpt', action="store_true", dest="dump_ckpt", default=False, help="Dump the model to ckpt format.") diff --git a/mainscripts/Trainer.py b/mainscripts/Trainer.py index 9814d30..b894cc3 100644 --- a/mainscripts/Trainer.py +++ b/mainscripts/Trainer.py @@ -103,6 +103,7 @@ def trainerThread (s2c, c2s, e, cpu_only=cpu_only, silent_start=silent_start, config_training_file=config_training_file, + auto_gen_config=kwargs.get("auto_gen_config", False), debug=debug) is_reached_goal = model.is_reached_iter_goal() diff --git a/models/ModelBase.py b/models/ModelBase.py index 527b23b..0da7246 100644 --- a/models/ModelBase.py +++ b/models/ModelBase.py @@ -1,5 +1,6 @@ import colorsys import inspect +from io import FileIO import json import multiprocessing import operator @@ -37,6 +38,7 @@ class ModelBase(object): debug=False, force_model_class_name=None, config_training_file=None, + auto_gen_config=False, silent_start=False, **kwargs): self.is_training = is_training @@ -46,6 +48,8 @@ class ModelBase(object): self.training_data_dst_path = training_data_dst_path self.pretraining_data_path = pretraining_data_path self.pretrained_model_path = pretrained_model_path + self.config_training_file = config_training_file + self.auto_gen_config = auto_gen_config self.no_preview = no_preview self.debug = debug @@ -145,27 +149,36 @@ class ModelBase(object): model_data = {} # True if yaml conf file exists self.config_file_exists = False - # True if user chooses to read options from conf file + # True if user chooses to read options external or internal conf file self.read_from_conf = False #check if config_training_file mode is enabled if config_training_file is not None: self.config_file_path = Path(config_training_file) + # Creates folder if folder doesn't exist if not self.config_file_path.exists(): - os.mkdir(self.config_file_path) - if Path(self.get_strpath_configuration_path()).exists(): + os.makedirs(self.config_file_path, exist_ok=True) + # Ask if user wants to read options from external or internal conf file only if external conf file exists + # or auto_gen_config is true + if Path(self.get_strpath_configuration_path()).exists() or self.auto_gen_config: self.read_from_conf = io.input_bool( - f'Do you want to read training options from {self.config_file_path.stem} file?', + f'Do you want to read training options from {"external" if self.auto_gen_config else "internal"} file?', True, 'Read options from configuration file instead of asking one by one each option' ) + # If user decides to read from external or internal conf file if self.read_from_conf: - self.options = self.read_from_config_file() - self.config_file_exists = True + # Try to read dictionary from external of internal yaml file according + # to the value of auto_gen_config + self.options = self.read_from_config_file(auto_gen=self.auto_gen_config) + # If options dict is empty options will be loaded from dat file + if not self.options.keys(): + io.log_info(f"Configuration file doesn't exist. A standard configuration file will be created.") + else: + self.config_file_exists = True else: io.log_info(f"Configuration file doesn't exist. A standard configuration file will be created.") - self.model_data_path = Path( self.get_strpath_storage_for_file('data.dat') ) if self.model_data_path.exists(): io.log_info (f"Loading {self.model_name} model...") @@ -210,9 +223,6 @@ class ModelBase(object): if self.is_first_run(): # save as default options only for first run model initialize self.default_options_path.write_bytes( pickle.dumps (self.options) ) - # save config file - if config_training_file is not None: - self.save_config_file() self.session_name = self.options.get('session_name', "") self.autobackup_hour = self.options.get('autobackup_hour', 0) @@ -453,6 +463,10 @@ class ModelBase(object): } pathex.write_bytes_safe (self.model_data_path, pickle.dumps(model_data) ) + # save config file + if self.config_training_file is not None: + self.save_config_file(self.auto_gen_config) + if self.autobackup_hour != 0: diff_hour = int ( (time.time() - self.autobackup_start_time) // 3600 ) @@ -460,14 +474,23 @@ class ModelBase(object): self.autobackup_start_time += self.autobackup_hour*3600 self.create_backup() - def read_from_config_file(self): + def read_from_config_file(self, auto_gen=False): """ Read yaml config file and saves it into a dictionary + + Args: + auto_gen (bool, optional): True if you want that a yaml file is readed from model folder. Defaults to False. + Returns: - [type]: [description] + [dict]: Returns the options dictionary if everything is alright otherwise an empty dictionary. """ - with open(self.get_strpath_configuration_path(), 'r') as file: - data = yaml.safe_load(file) + fun = self.get_strpath_configuration_path if not auto_gen else self.get_model_conf_path + + try: + with open(fun(), 'r') as file: + data = yaml.safe_load(file) + except FileNotFoundError: + return {} for key, value in data.items(): if isinstance(value, bool): @@ -479,9 +502,12 @@ class ModelBase(object): return data - def save_config_file(self): + def save_config_file(self, auto_gen=False): """ - Saves options dictionary in a yaml file + Saves options dictionary in a yaml file. + + Args: + auto_gen ([bool], optional): True if you want that a yaml file is generated inside model folder for each model. Defaults to None. """ saving_dict = {} for key, value in self.options.items(): @@ -490,8 +516,13 @@ class ModelBase(object): else: saving_dict[key] = value - with open(self.get_strpath_configuration_path(), 'w') as file: - yaml.dump(saving_dict, file, sort_keys=False) + fun = self.get_strpath_configuration_path if not auto_gen else self.get_model_conf_path + + try: + with open(fun(), 'w') as file: + yaml.dump(saving_dict, file, sort_keys=False) + except OSError as exception: + print('Impossible to write YAML configuration file -> ', exception) def create_backup(self): io.log_info ("Creating backup...", end='\r') @@ -631,6 +662,9 @@ class ModelBase(object): def get_summary_path(self): return self.get_strpath_storage_for_file('summary.txt') + def get_model_conf_path(self): + return self.get_strpath_storage_for_file('configuration_file.yaml') + def get_summary_text(self): visible_options = self.options.copy() visible_options.update(self.options_show_override) From e754bf5bd66008cf0dd4241287dc3f1cf15ee496 Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 5 Dec 2021 11:09:47 +0100 Subject: [PATCH 07/16] io.log_info instead of print --- models/ModelBase.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/ModelBase.py b/models/ModelBase.py index 0da7246..ad57527 100644 --- a/models/ModelBase.py +++ b/models/ModelBase.py @@ -522,7 +522,7 @@ class ModelBase(object): with open(fun(), 'w') as file: yaml.dump(saving_dict, file, sort_keys=False) except OSError as exception: - print('Impossible to write YAML configuration file -> ', exception) + io.log_info('Impossible to write YAML configuration file -> ', exception) def create_backup(self): io.log_info ("Creating backup...", end='\r') From 13fb700403483dd5b02379c1d8c47aec5759f9ad Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 5 Dec 2021 11:15:11 +0100 Subject: [PATCH 08/16] added validation --- models/ModelBase.py | 24 ++++++++++++++++++------ models/__init__.py | 6 ++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/models/ModelBase.py b/models/ModelBase.py index ad57527..eed768a 100644 --- a/models/ModelBase.py +++ b/models/ModelBase.py @@ -12,6 +12,8 @@ import time import datetime from pathlib import Path import yaml +from jsonschema import validate, ValidationError +import models import cv2 import numpy as np @@ -151,6 +153,7 @@ class ModelBase(object): self.config_file_exists = False # True if user chooses to read options external or internal conf file self.read_from_conf = False + config_error = False #check if config_training_file mode is enabled if config_training_file is not None: self.config_file_path = Path(config_training_file) @@ -172,7 +175,10 @@ class ModelBase(object): # to the value of auto_gen_config self.options = self.read_from_config_file(auto_gen=self.auto_gen_config) # If options dict is empty options will be loaded from dat file - if not self.options.keys(): + if self.options is None: + io.log_info(f"Config file validation error, check your config") + config_error = True + elif not self.options.keys(): io.log_info(f"Configuration file doesn't exist. A standard configuration file will be created.") else: self.config_file_exists = True @@ -224,6 +230,10 @@ class ModelBase(object): # save as default options only for first run model initialize self.default_options_path.write_bytes( pickle.dumps (self.options) ) + # save config file + if self.config_training_file is not None and not self.config_file_exists and not config_error: + self.save_config_file(self.auto_gen_config) + self.session_name = self.options.get('session_name', "") self.autobackup_hour = self.options.get('autobackup_hour', 0) self.maximum_n_backups = self.options.get('maximum_n_backups', 24) @@ -463,10 +473,6 @@ class ModelBase(object): } pathex.write_bytes_safe (self.model_data_path, pickle.dumps(model_data) ) - # save config file - if self.config_training_file is not None: - self.save_config_file(self.auto_gen_config) - if self.autobackup_hour != 0: diff_hour = int ( (time.time() - self.autobackup_start_time) // 3600 ) @@ -487,10 +493,16 @@ class ModelBase(object): fun = self.get_strpath_configuration_path if not auto_gen else self.get_model_conf_path try: - with open(fun(), 'r') as file: + with open(fun(), 'r') as file, open(models.get_config_schema_path(), 'r') as schema: + + data = yaml.safe_load(file) + validate(data, yaml.safe_load(schema)) except FileNotFoundError: return {} + except ValidationError as ve: + io.log_err("%s"%ve) + return None for key, value in data.items(): if isinstance(value, bool): diff --git a/models/__init__.py b/models/__init__.py index 490e9c8..905e2cf 100644 --- a/models/__init__.py +++ b/models/__init__.py @@ -1,5 +1,11 @@ from .ModelBase import ModelBase +from pathlib import Path def import_model(model_class_name): module = __import__('Model_'+model_class_name, globals(), locals(), [], 1) return getattr(module, 'Model') + + +def get_config_schema_path(): + config_path = Path(__file__).parent.absolute() / Path("config_schema.json") + return config_path \ No newline at end of file From 5c5d9af5883cdd71832f5a895731ad7a87e3c2ab Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 5 Dec 2021 11:16:26 +0100 Subject: [PATCH 09/16] added basic models schema --- models/config_schema.json | 265 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 265 insertions(+) create mode 100644 models/config_schema.json diff --git a/models/config_schema.json b/models/config_schema.json new file mode 100644 index 0000000..ae9b930 --- /dev/null +++ b/models/config_schema.json @@ -0,0 +1,265 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/dfl_config", + "definitions": { + "dfl_config": { + "type": "object", + "additionalProperties": false, + "properties": { + "use_fp16": { + "type": "boolean" + }, + "archi": { + "type": "string", + "pattern": "^(df|liae)-(\\b(?!\\w*(\\w)\\w*\\1)[udtc]+\\b)+|^(df|liae)$" + }, + "resolution": { + "type": "integer", + "minimum": 64, + "maximum": 640, + "multipleOf": 16 + }, + "face_type": { + "type": "string", + "enum": [ + "h", + "mf", + "f", + "wf", + "head", + "custom" + ] + }, + "models_opt_on_gpu": { + "type": "boolean" + }, + "ae_dims": { + "type": "integer", + "minimum": 32, + "maximum": 1024 + }, + "e_dims": { + "type": "integer", + "minimum": 16, + "maximum": 256, + "multipleOf": 2 + }, + "d_dims": { + "type": "integer", + "minimum": 16, + "maximum": 256, + "multipleOf": 2 + }, + "d_mask_dims": { + "type": "integer", + "minimum": 16, + "maximum": 256, + "multipleOf": 2 + }, + "masked_training": { + "type": "boolean" + }, + "eyes_prio": { + "type": "boolean" + }, + "mouth_prio": { + "type": "boolean" + }, + "uniform_yaw": { + "type": "boolean" + }, + "blur_out_mask": { + "type": "boolean" + }, + "adabelief": { + "type": "boolean" + }, + "lr_dropout": { + "type": "string", + "enum": [ + "y", + "n", + "cpu" + ] + }, + "loss_function": { + "type": "string", + "enum": [ + "SSIM", + "MS-SSIM", + "MS-SSIM+L1" + ] + }, + "random_warp": { + "type": "boolean" + }, + "random_hsv_power": { + "type": "number", + "minimum": 0.0, + "maximum": 0.3 + }, + "random_downsample": { + "type": "boolean" + }, + "random_noise": { + "type": "boolean" + }, + "random_blur": { + "type": "boolean" + }, + "random_jpeg": { + "type": "boolean" + }, + "background_power": { + "type": "number", + "minimum": 0.0, + "maximum": 1.0 + }, + "true_face_power": { + "type": "number", + "minimum": 0.0, + "maximum": 1.0 + }, + "face_style_power": { + "type": "number", + "minimum": 0.0, + "maximum": 100.0 + }, + "bg_style_power": { + "type": "number", + "minimum": 0.0, + "maximum": 100.0 + }, + "ct_mode": { + "type": "string", + "enum": [ + "none", + "rct", + "lct", + "mkl", + "idt", + "sot" + ] + }, + "random_color": { + "type": "boolean" + }, + "clipgrad": { + "type": "boolean" + }, + "pretrain": { + "type": "boolean" + }, + "session_name": { + "type": "string" + }, + "autobackup_hour": { + "type": "integer", + "minimum": 0, + "maximum": 24 + }, + "maximum_n_backups": { + "type": "integer" + }, + "write_preview_history": { + "type": "boolean" + }, + "target_iter": { + "type": "integer", + "minimum": 0 + }, + "retraining_samples": { + "type": "boolean" + }, + "random_src_flip": { + "type": "boolean" + }, + "random_dst_flip": { + "type": "boolean" + }, + "batch_size": { + "type": "integer" + }, + "gan_power": { + "type": "number", + "minimum": 0.0, + "maximum": 5.0 + }, + "gan_version": { + "type": "integer", + "minimum": 2, + "maximum": 3 + }, + "gan_patch_size": { + "type": "integer", + "minimum": 3, + "maximum": 640 + }, + "gan_dims": { + "type": "integer", + "minimum": 4, + "maximum": 512 + }, + "gan_smoothing": { + "type": "number", + "minimum": 0.0, + "maximum": 0.5 + }, + "gan_noise": { + "type": "number", + "minimum": 0.0, + "maximum": 0.5 + } + }, + "required": [ + "adabelief", + "ae_dims", + "archi", + "autobackup_hour", + "background_power", + "batch_size", + "bg_style_power", + "blur_out_mask", + "clipgrad", + "ct_mode", + "d_dims", + "d_mask_dims", + "e_dims", + "eyes_prio", + "face_style_power", + "face_type", + "gan_dims", + "gan_noise", + "gan_patch_size", + "gan_power", + "gan_smoothing", + "gan_version", + "loss_function", + "lr_dropout", + "masked_training", + "maximum_n_backups", + "models_opt_on_gpu", + "mouth_prio", + "pretrain", + "random_blur", + "random_color", + "random_downsample", + "random_dst_flip", + "random_hsv_power", + "random_jpeg", + "random_noise", + "random_src_flip", + "random_warp", + "resolution", + "retraining_samples", + "session_name", + "target_iter", + "true_face_power", + "uniform_yaw", + "use_fp16", + "write_preview_history" + ], + "title": "dfl_config" + } + } +} \ No newline at end of file From d7c5e7e9f13ecefa62f42e8dcaae90d7b28e78b8 Mon Sep 17 00:00:00 2001 From: Cioscos Date: Sun, 5 Dec 2021 12:21:34 +0100 Subject: [PATCH 10/16] Minor update - Updated requirements - Little refactoring in models/ModelBase.py --- models/ModelBase.py | 4 +--- requirements-colab.txt | 1 + requirements-cuda.txt | 1 + 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/models/ModelBase.py b/models/ModelBase.py index eed768a..75cd4fe 100644 --- a/models/ModelBase.py +++ b/models/ModelBase.py @@ -494,14 +494,12 @@ class ModelBase(object): try: with open(fun(), 'r') as file, open(models.get_config_schema_path(), 'r') as schema: - - data = yaml.safe_load(file) validate(data, yaml.safe_load(schema)) except FileNotFoundError: return {} except ValidationError as ve: - io.log_err("%s"%ve) + io.log_err(f"{ve}") return None for key, value in data.items(): diff --git a/requirements-colab.txt b/requirements-colab.txt index 9d638f5..a4b4399 100644 --- a/requirements-colab.txt +++ b/requirements-colab.txt @@ -11,3 +11,4 @@ tensorflow-gpu==2.4.0 tf2onnx==1.9.3 tensorboardX crc32c +jsonschema diff --git a/requirements-cuda.txt b/requirements-cuda.txt index 1cc4902..ce516c7 100644 --- a/requirements-cuda.txt +++ b/requirements-cuda.txt @@ -14,3 +14,4 @@ Flask==1.1.1 flask-socketio==4.2.1 tensorboardX crc32c +jsonschema From 46e7307ecad0e63ce5795ae389ec36f6e5958daf Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 5 Dec 2021 15:33:01 +0100 Subject: [PATCH 11/16] added different schemas for the model types (currently, amp and saehd) --- models/ModelBase.py | 6 +++++- models/Model_AMP/Model.py | 8 ++++++++ models/{ => Model_AMP}/config_schema.json | 18 +++++++++++++----- models/Model_SAEHD/Model.py | 7 +++++++ models/__init__.py | 4 ---- 5 files changed, 33 insertions(+), 10 deletions(-) rename models/{ => Model_AMP}/config_schema.json (94%) diff --git a/models/ModelBase.py b/models/ModelBase.py index 75cd4fe..33b7922 100644 --- a/models/ModelBase.py +++ b/models/ModelBase.py @@ -433,6 +433,10 @@ class ModelBase(object): #return predictor_func, predictor_input_shape, MergerConfig() for the model raise NotImplementedError + #overridable + def get_config_schema_path(self): + raise NotImplementedError + def get_pretraining_data_path(self): return self.pretraining_data_path @@ -493,7 +497,7 @@ class ModelBase(object): fun = self.get_strpath_configuration_path if not auto_gen else self.get_model_conf_path try: - with open(fun(), 'r') as file, open(models.get_config_schema_path(), 'r') as schema: + with open(fun(), 'r') as file, open(self.get_config_schema_path(), 'r') as schema: data = yaml.safe_load(file) validate(data, yaml.safe_load(schema)) except FileNotFoundError: diff --git a/models/Model_AMP/Model.py b/models/Model_AMP/Model.py index 4a026c2..6aa0093 100644 --- a/models/Model_AMP/Model.py +++ b/models/Model_AMP/Model.py @@ -11,6 +11,8 @@ from models import ModelBase from samplelib import * from core.cv2ex import * +from pathlib import Path + class AMPModel(ModelBase): #override @@ -812,4 +814,10 @@ class AMPModel(ModelBase): import merger return predictor_morph, (self.options['resolution'], self.options['resolution'], 3), merger.MergerConfigMasked(face_type=self.face_type, default_mode = 'overlay') + #override + def get_config_schema_path(self): + config_path = Path(__file__).parent.absolute() / Path("config_schema.json") + return config_path + + Model = AMPModel diff --git a/models/config_schema.json b/models/Model_AMP/config_schema.json similarity index 94% rename from models/config_schema.json rename to models/Model_AMP/config_schema.json index ae9b930..56c492e 100644 --- a/models/config_schema.json +++ b/models/Model_AMP/config_schema.json @@ -4,14 +4,15 @@ "definitions": { "dfl_config": { "type": "object", - "additionalProperties": false, + "additionalProperties": true, "properties": { "use_fp16": { "type": "boolean" }, - "archi": { - "type": "string", - "pattern": "^(df|liae)-(\\b(?!\\w*(\\w)\\w*\\1)[udtc]+\\b)+|^(df|liae)$" + "morph_factor": { + "type": "number", + "minimum":0.0, + "maximum":1.0 }, "resolution": { "type": "integer", @@ -44,6 +45,12 @@ "maximum": 256, "multipleOf": 2 }, + "inter_dims": { + "type": "integer", + "minimum": 32, + "maximum": 2048, + "multipleOf": 2 + }, "d_dims": { "type": "integer", "minimum": 16, @@ -214,7 +221,6 @@ "required": [ "adabelief", "ae_dims", - "archi", "autobackup_hour", "background_power", "batch_size", @@ -225,6 +231,8 @@ "d_dims", "d_mask_dims", "e_dims", + "inter_dims", + "morph_factor", "eyes_prio", "face_style_power", "face_type", diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index 08420ba..5b87442 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -10,6 +10,8 @@ from facelib import FaceType from models import ModelBase from samplelib import * +from pathlib import Path + class SAEHDModel(ModelBase): #override @@ -1058,4 +1060,9 @@ class SAEHDModel(ModelBase): import merger return self.predictor_func, (self.options['resolution'], self.options['resolution'], 3), merger.MergerConfigMasked(face_type=self.face_type, default_mode = 'overlay') + #override + def get_config_schema_path(self): + config_path = Path(__file__).parent.absolute() / Path("config_schema.json") + return config_path + Model = SAEHDModel diff --git a/models/__init__.py b/models/__init__.py index 905e2cf..7c0782d 100644 --- a/models/__init__.py +++ b/models/__init__.py @@ -1,11 +1,7 @@ from .ModelBase import ModelBase -from pathlib import Path def import_model(model_class_name): module = __import__('Model_'+model_class_name, globals(), locals(), [], 1) return getattr(module, 'Model') -def get_config_schema_path(): - config_path = Path(__file__).parent.absolute() / Path("config_schema.json") - return config_path \ No newline at end of file From 8cb37f3f2861fe3c8bb8158eb4c2dd07782d31e1 Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 5 Dec 2021 15:54:57 +0100 Subject: [PATCH 12/16] config updates and pathes to xseg / q96 --- models/Model_AMP/config_schema.json | 3 +- models/Model_Quick96/Model.py | 6 ++++ models/Model_Quick96/config_schema.json | 20 +++++++++++++ models/Model_XSeg/Model.py | 7 +++++ models/Model_XSeg/config_schema.json | 39 +++++++++++++++++++++++++ 5 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 models/Model_Quick96/config_schema.json create mode 100644 models/Model_XSeg/config_schema.json diff --git a/models/Model_AMP/config_schema.json b/models/Model_AMP/config_schema.json index 56c492e..cc2b25d 100644 --- a/models/Model_AMP/config_schema.json +++ b/models/Model_AMP/config_schema.json @@ -185,7 +185,8 @@ "type": "boolean" }, "batch_size": { - "type": "integer" + "type": "integer", + "minimum": 1 }, "gan_power": { "type": "number", diff --git a/models/Model_Quick96/Model.py b/models/Model_Quick96/Model.py index fa9e215..7ac3f0d 100644 --- a/models/Model_Quick96/Model.py +++ b/models/Model_Quick96/Model.py @@ -10,6 +10,8 @@ from facelib import FaceType from models import ModelBase from samplelib import * +from pathlib import Path + class QModel(ModelBase): #override def on_initialize(self): @@ -317,5 +319,9 @@ class QModel(ModelBase): return self.predictor_func, (self.resolution, self.resolution, 3), merger.MergerConfigMasked(face_type=self.face_type, default_mode = 'overlay', ) + #override + def get_config_schema_path(self): + config_path = Path(__file__).parent.absolute() / Path("config_schema.json") + return config_path Model = QModel diff --git a/models/Model_Quick96/config_schema.json b/models/Model_Quick96/config_schema.json new file mode 100644 index 0000000..b1d2239 --- /dev/null +++ b/models/Model_Quick96/config_schema.json @@ -0,0 +1,20 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/dfl_config", + "definitions": { + "dfl_config": { + "type": "object", + "additionalProperties": false, + "properties": { + "batch_size": { + "type": "integer", + "minimum": 1 + } + }, + "required": [ + "batch_size", + ], + "title": "dfl_config" + } + } +} \ No newline at end of file diff --git a/models/Model_XSeg/Model.py b/models/Model_XSeg/Model.py index b0addfd..b6e875f 100644 --- a/models/Model_XSeg/Model.py +++ b/models/Model_XSeg/Model.py @@ -11,6 +11,8 @@ from facelib import FaceType, XSegNet from models import ModelBase from samplelib import * +from pathlib import Path + class XSegModel(ModelBase): def __init__(self, *args, **kwargs): @@ -279,5 +281,10 @@ class XSegModel(ModelBase): output_names=['out_mask:0'], opset=13, output_path=output_path) + + #override + def get_config_schema_path(self): + config_path = Path(__file__).parent.absolute() / Path("config_schema.json") + return config_path Model = XSegModel \ No newline at end of file diff --git a/models/Model_XSeg/config_schema.json b/models/Model_XSeg/config_schema.json new file mode 100644 index 0000000..e53fc8b --- /dev/null +++ b/models/Model_XSeg/config_schema.json @@ -0,0 +1,39 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$ref": "#/definitions/dfl_config", + "definitions": { + "dfl_config": { + "type": "object", + "additionalProperties": false, + "properties": { + "use_fp16": { + "type": "boolean" + }, + "face_type": { + "type": "string", + "enum": [ + "h", + "mf", + "f", + "wf", + "head", + "custom" + ] + }, + "pretrain": { + "type": "boolean" + }, + "batch_size": { + "type": "integer", + "minimum": 1 + } + }, + "required": [ + "batch_size", + "face_type", + "pretrain", + ], + "title": "dfl_config" + } + } +} \ No newline at end of file From 7fd4089b3014e1a03f915ee23ca220f78c60c8ce Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 5 Dec 2021 16:42:50 +0100 Subject: [PATCH 13/16] config schema updates --- models/Model_AMP/config_schema.json | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/models/Model_AMP/config_schema.json b/models/Model_AMP/config_schema.json index cc2b25d..6c9533c 100644 --- a/models/Model_AMP/config_schema.json +++ b/models/Model_AMP/config_schema.json @@ -12,7 +12,7 @@ "morph_factor": { "type": "number", "minimum":0.0, - "maximum":1.0 + "maximum":0.5 }, "resolution": { "type": "integer", @@ -132,11 +132,6 @@ "minimum": 0.0, "maximum": 100.0 }, - "bg_style_power": { - "type": "number", - "minimum": 0.0, - "maximum": 100.0 - }, "ct_mode": { "type": "string", "enum": [ @@ -186,7 +181,7 @@ }, "batch_size": { "type": "integer", - "minimum": 1 + "minimum": 1 }, "gan_power": { "type": "number", @@ -225,7 +220,6 @@ "autobackup_hour", "background_power", "batch_size", - "bg_style_power", "blur_out_mask", "clipgrad", "ct_mode", From 5bcb313ed6966e582b3af0bd3c7cb6748e696e45 Mon Sep 17 00:00:00 2001 From: Cioscos Date: Sun, 5 Dec 2021 16:48:56 +0100 Subject: [PATCH 14/16] Added reading from conf for Q96/XSeg model --- models/Model_Quick96/Model.py | 9 ++++++++- models/Model_XSeg/Model.py | 14 +++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/models/Model_Quick96/Model.py b/models/Model_Quick96/Model.py index 7ac3f0d..dba5738 100644 --- a/models/Model_Quick96/Model.py +++ b/models/Model_Quick96/Model.py @@ -13,6 +13,13 @@ from samplelib import * from pathlib import Path class QModel(ModelBase): + #override + def on_initialize_options(self): + ask_override = False if self.read_from_conf else self.ask_override() + if self.is_first_run() or ask_override: + if (self.read_from_conf and not self.config_file_exists) or not self.read_from_conf: + self.ask_batch_size() + #override def on_initialize(self): device_config = nn.getCurrentDeviceConfig() @@ -82,7 +89,7 @@ class QModel(ModelBase): if self.is_training: # Adjust batch size for multiple GPU gpu_count = max(1, len(devices) ) - bs_per_gpu = max(1, 4 // gpu_count) + bs_per_gpu = max(1, self.get_batch_size() // gpu_count) self.set_batch_size( gpu_count*bs_per_gpu) # Compute losses per GPU diff --git a/models/Model_XSeg/Model.py b/models/Model_XSeg/Model.py index b6e875f..4f536cf 100644 --- a/models/Model_XSeg/Model.py +++ b/models/Model_XSeg/Model.py @@ -20,7 +20,7 @@ class XSegModel(ModelBase): #override def on_initialize_options(self): - ask_override = self.ask_override() + ask_override = False if self.read_from_conf else self.ask_override() if not self.is_first_run() and ask_override: if io.input_bool(f"Restart training?", False, help_message="Reset model weights and start training from scratch."): @@ -30,11 +30,13 @@ class XSegModel(ModelBase): default_pretrain = self.options['pretrain'] = self.load_or_def_option('pretrain', False) if self.is_first_run(): - self.options['face_type'] = io.input_str ("Face type", default_face_type, ['h','mf','f','wf','head'], help_message="Half / mid face / full face / whole face / head. Choose the same as your deepfake model.").lower() + if (self.read_from_conf and not self.config_file_exists) or not self.read_from_conf: + self.options['face_type'] = io.input_str ("Face type", default_face_type, ['h','mf','f','wf','head'], help_message="Half / mid face / full face / whole face / head. Choose the same as your deepfake model.").lower() if self.is_first_run() or ask_override: - self.ask_batch_size(4, range=[2,16]) - self.options['pretrain'] = io.input_bool ("Enable pretraining mode", default_pretrain) + if (self.read_from_conf and not self.config_file_exists) or not self.read_from_conf: + self.ask_batch_size(4, range=[2,16]) + self.options['pretrain'] = io.input_bool ("Enable pretraining mode", default_pretrain) if not self.is_exporting and (self.options['pretrain'] and self.get_pretraining_data_path() is None): raise Exception("pretraining_data_path is not defined") @@ -53,13 +55,11 @@ class XSegModel(ModelBase): self.resolution = resolution = 256 - self.face_type = {'h' : FaceType.HALF, 'mf' : FaceType.MID_FULL, 'f' : FaceType.FULL, 'wf' : FaceType.WHOLE_FACE, 'head' : FaceType.HEAD}[ self.options['face_type'] ] - place_model_on_cpu = len(devices) == 0 models_opt_device = '/CPU:0' if place_model_on_cpu else nn.tf_default_device_name @@ -287,4 +287,4 @@ class XSegModel(ModelBase): config_path = Path(__file__).parent.absolute() / Path("config_schema.json") return config_path -Model = XSegModel \ No newline at end of file +Model = XSegModel From b7af0110db4e75b512919336ed09b9d3a56c9821 Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 5 Dec 2021 18:04:57 +0100 Subject: [PATCH 15/16] updated amp schema --- models/Model_AMP/config_schema.json | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/models/Model_AMP/config_schema.json b/models/Model_AMP/config_schema.json index 6c9533c..0facb41 100644 --- a/models/Model_AMP/config_schema.json +++ b/models/Model_AMP/config_schema.json @@ -4,7 +4,7 @@ "definitions": { "dfl_config": { "type": "object", - "additionalProperties": true, + "additionalProperties": false, "properties": { "use_fp16": { "type": "boolean" @@ -127,11 +127,6 @@ "minimum": 0.0, "maximum": 1.0 }, - "face_style_power": { - "type": "number", - "minimum": 0.0, - "maximum": 100.0 - }, "ct_mode": { "type": "string", "enum": [ @@ -229,7 +224,6 @@ "inter_dims", "morph_factor", "eyes_prio", - "face_style_power", "face_type", "gan_dims", "gan_noise", From 5308e5d2eaceef12e96963ad98173264d4f811e2 Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 5 Dec 2021 18:17:12 +0100 Subject: [PATCH 16/16] config updates --- models/Model_AMP/config_schema.json | 24 +++--------------------- 1 file changed, 3 insertions(+), 21 deletions(-) diff --git a/models/Model_AMP/config_schema.json b/models/Model_AMP/config_schema.json index cc2b25d..9630537 100644 --- a/models/Model_AMP/config_schema.json +++ b/models/Model_AMP/config_schema.json @@ -4,7 +4,7 @@ "definitions": { "dfl_config": { "type": "object", - "additionalProperties": true, + "additionalProperties": false, "properties": { "use_fp16": { "type": "boolean" @@ -12,7 +12,7 @@ "morph_factor": { "type": "number", "minimum":0.0, - "maximum":1.0 + "maximum":0.5 }, "resolution": { "type": "integer", @@ -122,21 +122,6 @@ "minimum": 0.0, "maximum": 1.0 }, - "true_face_power": { - "type": "number", - "minimum": 0.0, - "maximum": 1.0 - }, - "face_style_power": { - "type": "number", - "minimum": 0.0, - "maximum": 100.0 - }, - "bg_style_power": { - "type": "number", - "minimum": 0.0, - "maximum": 100.0 - }, "ct_mode": { "type": "string", "enum": [ @@ -186,7 +171,7 @@ }, "batch_size": { "type": "integer", - "minimum": 1 + "minimum": 1 }, "gan_power": { "type": "number", @@ -225,7 +210,6 @@ "autobackup_hour", "background_power", "batch_size", - "bg_style_power", "blur_out_mask", "clipgrad", "ct_mode", @@ -235,7 +219,6 @@ "inter_dims", "morph_factor", "eyes_prio", - "face_style_power", "face_type", "gan_dims", "gan_noise", @@ -263,7 +246,6 @@ "retraining_samples", "session_name", "target_iter", - "true_face_power", "uniform_yaw", "use_fp16", "write_preview_history"