pull/1378/head
bmaltais 2023-08-11 11:05:50 -04:00
parent 4104599942
commit fc5b0b1798
5 changed files with 341 additions and 249 deletions

View File

@ -511,6 +511,7 @@ If you come across a `FileNotFoundError`, it is likely due to an installation is
* 2023/08/05 (v21.8.8)
- Fix issue with aiofiles: https://github.com/bmaltais/kohya_ss/issues/1359
- Merge sd-scripts updates as of Aug 11 2023
* 2023/08/05 (v21.8.7)
- Add manual captioning option. Thanks to https://github.com/channelcat for this great contribution. (https://github.com/bmaltais/kohya_ss/pull/1352)
- Added support for `v_pred_like_loss` to the advanced training tab

View File

@ -1,5 +1,5 @@
import gradio as gr
from library.merge_lora_gui import gradio_merge_lora_tab
from library.merge_lora_gui import GradioMergeLoRaTab
from library.svd_merge_lora_gui import gradio_svd_merge_lora_tab
from library.verify_lora_gui import gradio_verify_lora_tab
from library.resize_lora_gui import gradio_resize_lora_tab
@ -25,7 +25,7 @@ class LoRATools:
gradio_extract_dylora_tab(headless=headless)
gradio_extract_lora_tab(headless=headless)
gradio_extract_lycoris_locon_tab(headless=headless)
gradio_merge_lora_tab(headless=headless)
gradio_merge_lora_tab = GradioMergeLoRaTab()
gradio_merge_lycoris_tab(headless=headless)
gradio_svd_merge_lora_tab(headless=headless)
gradio_resize_lora_tab(headless=headless)

View File

@ -37,261 +37,257 @@ def verify_conditions(sd_model, lora_models):
return True
return False
class GradioMergeLoRaTab:
def __init__(self, headless=False):
self.headless = headless
self.build_tab()
def save_inputs_to_json(self, file_path, inputs):
with open(file_path, 'w') as file:
json.dump(inputs, file)
log.info(f'Saved inputs to {file_path}')
def merge_lora(
sd_model,
sdxl_model,
lora_a_model,
lora_b_model,
lora_c_model,
lora_d_model,
ratio_a,
ratio_b,
ratio_c,
ratio_d,
save_to,
precision,
save_precision,
):
log.info('Merge model...')
models = [sd_model, lora_a_model, lora_b_model, lora_c_model, lora_d_model]
lora_models = models[1:]
ratios = [ratio_a, ratio_b, ratio_c, ratio_d]
def load_inputs_from_json(self, file_path):
with open(file_path, 'r') as file:
inputs = json.load(file)
log.info(f'Loaded inputs from {file_path}')
return inputs
if not verify_conditions(sd_model, lora_models):
log.info(
'Warning: Either provide at least one LoRa model along with the sd_model or at least two LoRa models if no sd_model is provided.'
)
return
def build_tab(self):
with gr.Tab('Merge LoRA'):
gr.Markdown(
'This utility can merge up to 4 LoRA together or alternatively merge up to 4 LoRA into a SD checkpoint.'
)
for model in models:
if not check_model(model):
lora_ext = gr.Textbox(value='*.safetensors *.pt', visible=False)
lora_ext_name = gr.Textbox(value='LoRA model types', visible=False)
ckpt_ext = gr.Textbox(value='*.safetensors *.ckpt', visible=False)
ckpt_ext_name = gr.Textbox(value='SD model types', visible=False)
with gr.Row():
sd_model = gr.Textbox(
label='SD Model',
placeholder='(Optional) Stable Diffusion model',
interactive=True,
info='Provide a SD file path IF you want to merge it with LoRA files',
)
sd_model_file = gr.Button(
folder_symbol,
elem_id='open_folder_small',
visible=(not self.headless),
)
sd_model_file.click(
get_file_path,
inputs=[sd_model, ckpt_ext, ckpt_ext_name],
outputs=sd_model,
show_progress=False,
)
sdxl_model = gr.Checkbox(label='SDXL model', value=False)
with gr.Row():
lora_a_model = gr.Textbox(
label='LoRA model "A"',
placeholder='Path to the LoRA A model',
interactive=True,
)
button_lora_a_model_file = gr.Button(
folder_symbol,
elem_id='open_folder_small',
visible=(not self.headless),
)
button_lora_a_model_file.click(
get_file_path,
inputs=[lora_a_model, lora_ext, lora_ext_name],
outputs=lora_a_model,
show_progress=False,
)
lora_b_model = gr.Textbox(
label='LoRA model "B"',
placeholder='Path to the LoRA B model',
interactive=True,
)
button_lora_b_model_file = gr.Button(
folder_symbol,
elem_id='open_folder_small',
visible=(not self.headless),
)
button_lora_b_model_file.click(
get_file_path,
inputs=[lora_b_model, lora_ext, lora_ext_name],
outputs=lora_b_model,
show_progress=False,
)
with gr.Row():
ratio_a = gr.Slider(
label='Model A merge ratio (eg: 0.5 mean 50%)',
minimum=0,
maximum=1,
step=0.01,
value=0.0,
interactive=True,
)
ratio_b = gr.Slider(
label='Model B merge ratio (eg: 0.5 mean 50%)',
minimum=0,
maximum=1,
step=0.01,
value=0.0,
interactive=True,
)
with gr.Row():
lora_c_model = gr.Textbox(
label='LoRA model "C"',
placeholder='Path to the LoRA C model',
interactive=True,
)
button_lora_c_model_file = gr.Button(
folder_symbol,
elem_id='open_folder_small',
visible=(not self.headless),
)
button_lora_c_model_file.click(
get_file_path,
inputs=[lora_c_model, lora_ext, lora_ext_name],
outputs=lora_c_model,
show_progress=False,
)
lora_d_model = gr.Textbox(
label='LoRA model "D"',
placeholder='Path to the LoRA D model',
interactive=True,
)
button_lora_d_model_file = gr.Button(
folder_symbol,
elem_id='open_folder_small',
visible=(not self.headless),
)
button_lora_d_model_file.click(
get_file_path,
inputs=[lora_d_model, lora_ext, lora_ext_name],
outputs=lora_d_model,
show_progress=False,
)
with gr.Row():
ratio_c = gr.Slider(
label='Model C merge ratio (eg: 0.5 mean 50%)',
minimum=0,
maximum=1,
step=0.01,
value=0.0,
interactive=True,
)
ratio_d = gr.Slider(
label='Model D merge ratio (eg: 0.5 mean 50%)',
minimum=0,
maximum=1,
step=0.01,
value=0.0,
interactive=True,
)
with gr.Row():
save_to = gr.Textbox(
label='Save to',
placeholder='path for the file to save...',
interactive=True,
)
button_save_to = gr.Button(
folder_symbol,
elem_id='open_folder_small',
visible=(not self.headless),
)
button_save_to.click(
get_saveasfilename_path,
inputs=[save_to, lora_ext, lora_ext_name],
outputs=save_to,
show_progress=False,
)
precision = gr.Dropdown(
label='Merge precision',
choices=['fp16', 'bf16', 'float'],
value='float',
interactive=True,
)
save_precision = gr.Dropdown(
label='Save precision',
choices=['fp16', 'bf16', 'float'],
value='fp16',
interactive=True,
)
merge_button = gr.Button('Merge model')
merge_button.click(
self.merge_lora,
inputs=[
sd_model,
sdxl_model,
lora_a_model,
lora_b_model,
lora_c_model,
lora_d_model,
ratio_a,
ratio_b,
ratio_c,
ratio_d,
save_to,
precision,
save_precision,
],
show_progress=False,
)
def merge_lora(self, sd_model, sdxl_model, lora_a_model, lora_b_model, lora_c_model, lora_d_model, ratio_a, ratio_b, ratio_c, ratio_d, save_to, precision, save_precision):
log.info('Merge model...')
models = [sd_model, lora_a_model, lora_b_model, lora_c_model, lora_d_model]
lora_models = models[1:]
ratios = [ratio_a, ratio_b, ratio_c, ratio_d]
if not verify_conditions(sd_model, lora_models):
log.info(
'Warning: Either provide at least one LoRa model along with the sd_model or at least two LoRa models if no sd_model is provided.'
)
return
if not sdxl_model:
run_cmd = f'{PYTHON} "{os.path.join("networks","merge_lora.py")}"'
else:
run_cmd = f'{PYTHON} "{os.path.join("networks","sdxl_merge_lora.py")}"'
if sd_model:
run_cmd += f' --sd_model "{sd_model}"'
run_cmd += f' --save_precision {save_precision}'
run_cmd += f' --precision {precision}'
run_cmd += f' --save_to "{save_to}"'
for model in models:
if not check_model(model):
return
# Create a space-separated string of non-empty models (from the second element onwards), enclosed in double quotes
models_cmd = ' '.join([f'"{model}"' for model in lora_models if model])
if not sdxl_model:
run_cmd = f'{PYTHON} "{os.path.join("networks","merge_lora.py")}"'
else:
run_cmd = f'{PYTHON} "{os.path.join("networks","sdxl_merge_lora.py")}"'
if sd_model:
run_cmd += f' --sd_model "{sd_model}"'
run_cmd += f' --save_precision {save_precision}'
run_cmd += f' --precision {precision}'
run_cmd += f' --save_to "{save_to}"'
# Create a space-separated string of non-zero ratios corresponding to non-empty LoRa models
valid_ratios = [ratios[i] for i, model in enumerate(lora_models) if model]
ratios_cmd = ' '.join([str(ratio) for ratio in valid_ratios])
# Create a space-separated string of non-empty models (from the second element onwards), enclosed in double quotes
models_cmd = ' '.join([f'"{model}"' for model in lora_models if model])
if models_cmd:
run_cmd += f' --models {models_cmd}'
run_cmd += f' --ratios {ratios_cmd}'
# Create a space-separated string of non-zero ratios corresponding to non-empty LoRa models
valid_ratios = [ratios[i] for i, model in enumerate(lora_models) if model]
ratios_cmd = ' '.join([str(ratio) for ratio in valid_ratios])
log.info(run_cmd)
if models_cmd:
run_cmd += f' --models {models_cmd}'
run_cmd += f' --ratios {ratios_cmd}'
# Run the command
if os.name == 'posix':
os.system(run_cmd)
else:
subprocess.run(run_cmd)
log.info(run_cmd)
log.info('Done merging...')
# Run the command
if os.name == 'posix':
os.system(run_cmd)
else:
subprocess.run(run_cmd)
###
# Gradio UI
###
def gradio_merge_lora_tab(headless=False):
with gr.Tab('Merge LoRA'):
gr.Markdown(
'This utility can merge up to 4 LoRA together or alternatively merge up to 4 LoRA into a SD checkpoint.'
)
lora_ext = gr.Textbox(value='*.safetensors *.pt', visible=False)
lora_ext_name = gr.Textbox(value='LoRA model types', visible=False)
ckpt_ext = gr.Textbox(value='*.safetensors *.ckpt', visible=False)
ckpt_ext_name = gr.Textbox(value='SD model types', visible=False)
with gr.Row():
sd_model = gr.Textbox(
label='SD Model',
placeholder='(Optional) Stable Diffusion model',
interactive=True,
info='Provide a SD file path IF you want to merge it with LoRA files',
)
sd_model_file = gr.Button(
folder_symbol,
elem_id='open_folder_small',
visible=(not headless),
)
sd_model_file.click(
get_file_path,
inputs=[sd_model, ckpt_ext, ckpt_ext_name],
outputs=sd_model,
show_progress=False,
)
sdxl_model = gr.Checkbox(label='SDXL model', value=False)
with gr.Row():
lora_a_model = gr.Textbox(
label='LoRA model "A"',
placeholder='Path to the LoRA A model',
interactive=True,
)
button_lora_a_model_file = gr.Button(
folder_symbol,
elem_id='open_folder_small',
visible=(not headless),
)
button_lora_a_model_file.click(
get_file_path,
inputs=[lora_a_model, lora_ext, lora_ext_name],
outputs=lora_a_model,
show_progress=False,
)
lora_b_model = gr.Textbox(
label='LoRA model "B"',
placeholder='Path to the LoRA B model',
interactive=True,
)
button_lora_b_model_file = gr.Button(
folder_symbol,
elem_id='open_folder_small',
visible=(not headless),
)
button_lora_b_model_file.click(
get_file_path,
inputs=[lora_b_model, lora_ext, lora_ext_name],
outputs=lora_b_model,
show_progress=False,
)
with gr.Row():
ratio_a = gr.Slider(
label='Model A merge ratio (eg: 0.5 mean 50%)',
minimum=0,
maximum=1,
step=0.01,
value=0.0,
interactive=True,
)
ratio_b = gr.Slider(
label='Model B merge ratio (eg: 0.5 mean 50%)',
minimum=0,
maximum=1,
step=0.01,
value=0.0,
interactive=True,
)
with gr.Row():
lora_c_model = gr.Textbox(
label='LoRA model "C"',
placeholder='Path to the LoRA C model',
interactive=True,
)
button_lora_c_model_file = gr.Button(
folder_symbol,
elem_id='open_folder_small',
visible=(not headless),
)
button_lora_c_model_file.click(
get_file_path,
inputs=[lora_c_model, lora_ext, lora_ext_name],
outputs=lora_c_model,
show_progress=False,
)
lora_d_model = gr.Textbox(
label='LoRA model "D"',
placeholder='Path to the LoRA D model',
interactive=True,
)
button_lora_d_model_file = gr.Button(
folder_symbol,
elem_id='open_folder_small',
visible=(not headless),
)
button_lora_d_model_file.click(
get_file_path,
inputs=[lora_d_model, lora_ext, lora_ext_name],
outputs=lora_d_model,
show_progress=False,
)
with gr.Row():
ratio_c = gr.Slider(
label='Model C merge ratio (eg: 0.5 mean 50%)',
minimum=0,
maximum=1,
step=0.01,
value=0.0,
interactive=True,
)
ratio_d = gr.Slider(
label='Model D merge ratio (eg: 0.5 mean 50%)',
minimum=0,
maximum=1,
step=0.01,
value=0.0,
interactive=True,
)
with gr.Row():
save_to = gr.Textbox(
label='Save to',
placeholder='path for the file to save...',
interactive=True,
)
button_save_to = gr.Button(
folder_symbol,
elem_id='open_folder_small',
visible=(not headless),
)
button_save_to.click(
get_saveasfilename_path,
inputs=[save_to, lora_ext, lora_ext_name],
outputs=save_to,
show_progress=False,
)
precision = gr.Dropdown(
label='Merge precision',
choices=['fp16', 'bf16', 'float'],
value='float',
interactive=True,
)
save_precision = gr.Dropdown(
label='Save precision',
choices=['fp16', 'bf16', 'float'],
value='fp16',
interactive=True,
)
merge_button = gr.Button('Merge model')
merge_button.click(
merge_lora,
inputs=[
sd_model,
sdxl_model,
lora_a_model,
lora_b_model,
lora_c_model,
lora_d_model,
ratio_a,
ratio_b,
ratio_c,
ratio_d,
save_to,
precision,
save_precision,
],
show_progress=False,
)
log.info('Done merging...')

View File

@ -0,0 +1,95 @@
{
"LoRA_type": "Standard",
"adaptive_noise_scale": 0,
"additional_parameters": "--max_grad_norm=1",
"block_alphas": "",
"block_dims": "",
"block_lr_zero_threshold": "",
"bucket_no_upscale": true,
"bucket_reso_steps": 32,
"cache_latents": true,
"cache_latents_to_disk": true,
"caption_dropout_every_n_epochs": 0.0,
"caption_dropout_rate": 0,
"caption_extension": ".txt",
"clip_skip": "1",
"color_aug": false,
"conv_alpha": 4,
"conv_block_alphas": "",
"conv_block_dims": "",
"conv_dim": 8,
"decompose_both": false,
"dim_from_weights": false,
"down_lr_weight": "",
"enable_bucket": true,
"epoch": 160,
"factor": -1,
"flip_aug": false,
"full_bf16": false,
"full_fp16": false,
"gradient_accumulation_steps": 1.0,
"gradient_checkpointing": true,
"keep_tokens": "0",
"learning_rate": 1.0,
"lora_network_weights": "",
"lr_scheduler": "cosine",
"lr_scheduler_num_cycles": "",
"lr_scheduler_power": "",
"lr_warmup": 0,
"max_bucket_reso": 2048,
"max_data_loader_n_workers": "0",
"max_resolution": "1024,1024",
"max_timestep": 1000,
"max_token_length": "75",
"max_train_epochs": "",
"max_train_steps": "320",
"mem_eff_attn": false,
"mid_lr_weight": "",
"min_bucket_reso": 256,
"min_snr_gamma": 0,
"min_timestep": 0,
"mixed_precision": "bf16",
"module_dropout": 0,
"multires_noise_discount": 0.2,
"multires_noise_iterations": 8,
"network_alpha": 32,
"network_dim": 32,
"network_dropout": 0,
"no_token_padding": false,
"noise_offset": 0.0357,
"noise_offset_type": "Original",
"num_cpu_threads_per_process": 2,
"optimizer": "Prodigy",
"optimizer_args": "decouple=True weight_decay=0.5 betas=0.9,0.99 use_bias_correction=False",
"persistent_data_loader_workers": false,
"prior_loss_weight": 1.0,
"random_crop": false,
"rank_dropout": 0,
"save_every_n_epochs": 10,
"save_every_n_steps": 0,
"save_last_n_steps": 0,
"save_last_n_steps_state": 0,
"save_precision": "bf16",
"scale_v_pred_loss_like_noise_pred": false,
"scale_weight_norms": 1,
"sdxl": true,
"sdxl_cache_text_encoder_outputs": false,
"sdxl_no_half_vae": true,
"seed": "12345",
"shuffle_caption": false,
"stop_text_encoder_training": 0,
"text_encoder_lr": 1.0,
"train_batch_size": 4,
"train_on_input": false,
"training_comment": "",
"unet_lr": 1.0,
"unit": 1,
"up_lr_weight": "",
"use_cp": true,
"use_wandb": false,
"v2": false,
"v_parameterization": false,
"vae_batch_size": 0,
"weighted_captions": false,
"xformers": true
}

View File

@ -6,7 +6,7 @@
"block_dims": "",
"block_lr_zero_threshold": "",
"bucket_no_upscale": true,
"bucket_reso_steps": 32,
"bucket_reso_steps": 64,
"cache_latents": true,
"cache_latents_to_disk": true,
"caption_dropout_every_n_epochs": 0.0,