KohyaSS/library/common_gui.py

979 lines
33 KiB
Python
Raw Permalink Normal View History

from tkinter import filedialog, Tk
2023-03-25 16:39:02 +00:00
from easygui import msgbox
import os
import gradio as gr
2023-03-20 12:47:00 +00:00
import easygui
import shutil
2023-01-16 00:59:40 +00:00
folder_symbol = '\U0001f4c2' # 📂
refresh_symbol = '\U0001f504' # 🔄
save_style_symbol = '\U0001f4be' # 💾
document_symbol = '\U0001F4C4' # 📄
2023-03-06 02:38:20 +00:00
# define a list of substrings to search for v2 base models
2023-03-06 02:42:28 +00:00
V2_BASE_MODELS = [
2023-03-06 02:38:20 +00:00
'stabilityai/stable-diffusion-2-1-base',
'stabilityai/stable-diffusion-2-base',
]
# define a list of substrings to search for v_parameterization models
2023-03-06 02:42:28 +00:00
V_PARAMETERIZATION_MODELS = [
2023-03-06 02:38:20 +00:00
'stabilityai/stable-diffusion-2-1',
'stabilityai/stable-diffusion-2',
]
# define a list of substrings to v1.x models
2023-03-06 02:42:28 +00:00
V1_MODELS = [
2023-03-06 02:38:20 +00:00
'CompVis/stable-diffusion-v1-4',
'runwayml/stable-diffusion-v1-5',
]
2023-03-06 02:42:28 +00:00
# define a list of substrings to search for
ALL_PRESET_MODELS = V2_BASE_MODELS + V_PARAMETERIZATION_MODELS + V1_MODELS
2023-03-29 23:43:23 +00:00
FILE_ENV_EXCLUSION = ['COLAB_GPU', 'RUNPOD_POD_ID']
2023-03-26 10:47:26 +00:00
2023-03-02 00:24:11 +00:00
2023-03-20 12:47:00 +00:00
def check_if_model_exist(output_name, output_dir, save_model_as):
if save_model_as in ['diffusers', 'diffusers_safetendors']:
ckpt_folder = os.path.join(output_dir, output_name)
if os.path.isdir(ckpt_folder):
msg = f'A diffuser model with the same name {ckpt_folder} already exists. Do you want to overwrite it?'
if not easygui.ynbox(msg, 'Overwrite Existing Model?'):
print(
'Aborting training due to existing model with same name...'
)
return True
elif save_model_as in ['ckpt', 'safetensors']:
ckpt_file = os.path.join(output_dir, output_name + '.' + save_model_as)
if os.path.isfile(ckpt_file):
msg = f'A model with the same file name {ckpt_file} already exists. Do you want to overwrite it?'
if not easygui.ynbox(msg, 'Overwrite Existing Model?'):
print(
'Aborting training due to existing model with same name...'
)
return True
else:
print(
'Can\'t verify if existing model exist when save model is set a "same as source model", continuing to train model...'
)
return False
return False
2023-03-04 23:56:22 +00:00
def update_my_data(my_data):
2023-03-25 16:39:02 +00:00
# Update the optimizer based on the use_8bit_adam flag
2023-03-15 23:31:52 +00:00
use_8bit_adam = my_data.get('use_8bit_adam', False)
2023-03-25 16:39:02 +00:00
my_data.setdefault('optimizer', 'AdamW8bit' if use_8bit_adam else 'AdamW')
2023-03-20 12:47:00 +00:00
2023-03-15 23:31:52 +00:00
# Update model_list to custom if empty or pretrained_model_name_or_path is not a preset model
model_list = my_data.get('model_list', [])
2023-03-25 16:39:02 +00:00
pretrained_model_name_or_path = my_data.get('pretrained_model_name_or_path', '')
if not model_list or pretrained_model_name_or_path not in ALL_PRESET_MODELS:
2023-03-04 22:46:32 +00:00
my_data['model_list'] = 'custom'
2023-03-06 02:10:24 +00:00
2023-03-15 23:31:52 +00:00
# Convert epoch and save_every_n_epochs values to int if they are strings
for key in ['epoch', 'save_every_n_epochs']:
value = my_data.get(key, -1)
2023-03-25 16:39:02 +00:00
if isinstance(value, str) and value.isdigit():
2023-03-15 23:31:52 +00:00
my_data[key] = int(value)
elif not value:
my_data[key] = -1
# Update LoRA_type if it is set to LoCon
2023-03-09 12:49:50 +00:00
if my_data.get('LoRA_type', 'Standard') == 'LoCon':
my_data['LoRA_type'] = 'LyCORIS/LoCon'
2023-03-25 16:39:02 +00:00
# Update model save choices due to changes for LoRA and TI training
2023-03-25 16:39:02 +00:00
if (
(my_data.get('LoRA_type') or my_data.get('num_vectors_per_token'))
and my_data.get('save_model_as') not in ['safetensors', 'ckpt']
):
message = (
'Updating save_model_as to safetensors because the current value in the config file is no longer applicable to {}'
)
if my_data.get('LoRA_type'):
2023-03-25 16:39:02 +00:00
print(message.format('LoRA'))
if my_data.get('num_vectors_per_token'):
2023-03-25 16:39:02 +00:00
print(message.format('TI'))
my_data['save_model_as'] = 'safetensors'
2023-03-15 23:31:52 +00:00
2023-03-02 00:02:04 +00:00
return my_data
2023-02-06 01:07:00 +00:00
def get_dir_and_file(file_path):
dir_path, file_name = os.path.split(file_path)
return (dir_path, file_name)
2023-03-15 23:31:52 +00:00
# def has_ext_files(directory, extension):
# # Iterate through all the files in the directory
# for file in os.listdir(directory):
# # If the file name ends with extension, return True
# if file.endswith(extension):
# return True
# # If no extension files were found, return False
# return False
def get_file_path(
2023-03-15 23:31:52 +00:00
file_path='', default_extension='.json', extension_name='Config files'
):
2023-03-26 10:47:26 +00:00
if not any(var in os.environ for var in FILE_ENV_EXCLUSION):
current_file_path = file_path
# print(f'current file path: {current_file_path}')
initial_dir, initial_file = get_dir_and_file(file_path)
# Create a hidden Tkinter root window
root = Tk()
root.wm_attributes('-topmost', 1)
root.withdraw()
# Show the open file dialog and get the selected file path
file_path = filedialog.askopenfilename(
filetypes=(
(extension_name, f'*{default_extension}'),
('All files', '*.*'),
),
defaultextension=default_extension,
initialfile=initial_file,
initialdir=initial_dir,
)
2022-12-16 18:16:23 +00:00
# Destroy the hidden root window
root.destroy()
2022-12-16 18:16:23 +00:00
# If no file is selected, use the current file path
if not file_path:
file_path = current_file_path
current_file_path = file_path
# print(f'current file path: {current_file_path}')
return file_path
def get_any_file_path(file_path=''):
2023-03-26 10:47:26 +00:00
if not any(var in os.environ for var in FILE_ENV_EXCLUSION):
current_file_path = file_path
# print(f'current file path: {current_file_path}')
initial_dir, initial_file = get_dir_and_file(file_path)
root = Tk()
root.wm_attributes('-topmost', 1)
root.withdraw()
file_path = filedialog.askopenfilename(
initialdir=initial_dir,
initialfile=initial_file,
)
root.destroy()
if file_path == '':
file_path = current_file_path
return file_path
2022-12-17 01:26:26 +00:00
def remove_doublequote(file_path):
if file_path != None:
file_path = file_path.replace('"', '')
2022-12-16 18:16:23 +00:00
2022-12-17 01:26:26 +00:00
return file_path
2023-03-02 00:24:11 +00:00
2023-03-05 15:34:09 +00:00
# def set_legacy_8bitadam(optimizer, use_8bit_adam):
# if optimizer == 'AdamW8bit':
# # use_8bit_adam = True
# return gr.Dropdown.update(value=optimizer), gr.Checkbox.update(
# value=True, interactive=False, visible=True
# )
# else:
# # use_8bit_adam = False
# return gr.Dropdown.update(value=optimizer), gr.Checkbox.update(
# value=False, interactive=False, visible=True
# )
2023-02-25 01:37:51 +00:00
def get_folder_path(folder_path=''):
2023-03-26 10:47:26 +00:00
if not any(var in os.environ for var in FILE_ENV_EXCLUSION):
current_folder_path = folder_path
initial_dir, initial_file = get_dir_and_file(folder_path)
root = Tk()
root.wm_attributes('-topmost', 1)
root.withdraw()
folder_path = filedialog.askdirectory(initialdir=initial_dir)
root.destroy()
if folder_path == '':
folder_path = current_folder_path
return folder_path
def get_saveasfile_path(
file_path='', defaultextension='.json', extension_name='Config files'
):
2023-03-26 10:47:26 +00:00
if not any(var in os.environ for var in FILE_ENV_EXCLUSION):
current_file_path = file_path
# print(f'current file path: {current_file_path}')
initial_dir, initial_file = get_dir_and_file(file_path)
root = Tk()
root.wm_attributes('-topmost', 1)
root.withdraw()
save_file_path = filedialog.asksaveasfile(
filetypes=(
(f'{extension_name}', f'{defaultextension}'),
('All files', '*'),
),
defaultextension=defaultextension,
initialdir=initial_dir,
initialfile=initial_file,
)
root.destroy()
# print(save_file_path)
2022-12-19 14:47:35 +00:00
if save_file_path == None:
file_path = current_file_path
else:
print(save_file_path.name)
file_path = save_file_path.name
# print(file_path)
return file_path
def get_saveasfilename_path(
file_path='', extensions='*', extension_name='Config files'
):
2023-03-26 10:47:26 +00:00
if not any(var in os.environ for var in FILE_ENV_EXCLUSION):
current_file_path = file_path
# print(f'current file path: {current_file_path}')
initial_dir, initial_file = get_dir_and_file(file_path)
root = Tk()
root.wm_attributes('-topmost', 1)
root.withdraw()
save_file_path = filedialog.asksaveasfilename(
filetypes=((f'{extension_name}', f'{extensions}'), ('All files', '*')),
defaultextension=extensions,
initialdir=initial_dir,
initialfile=initial_file,
)
root.destroy()
if save_file_path == '':
file_path = current_file_path
else:
# print(save_file_path)
file_path = save_file_path
return file_path
def add_pre_postfix(
2023-03-20 12:47:00 +00:00
folder: str = '',
prefix: str = '',
postfix: str = '',
caption_file_ext: str = '.caption',
) -> None:
2023-03-15 23:31:52 +00:00
"""
Add prefix and/or postfix to the content of caption files within a folder.
If no caption files are found, create one with the requested prefix and/or postfix.
Args:
folder (str): Path to the folder containing caption files.
prefix (str, optional): Prefix to add to the content of the caption files.
postfix (str, optional): Postfix to add to the content of the caption files.
caption_file_ext (str, optional): Extension of the caption files.
"""
2022-12-22 16:51:34 +00:00
if prefix == '' and postfix == '':
return
2023-03-15 23:31:52 +00:00
image_extensions = ('.jpg', '.jpeg', '.png', '.webp')
2023-03-20 12:47:00 +00:00
image_files = [
f for f in os.listdir(folder) if f.lower().endswith(image_extensions)
]
2023-03-15 23:31:52 +00:00
for image_file in image_files:
caption_file_name = os.path.splitext(image_file)[0] + caption_file_ext
caption_file_path = os.path.join(folder, caption_file_name)
if not os.path.exists(caption_file_path):
with open(caption_file_path, 'w') as f:
separator = ' ' if prefix and postfix else ''
f.write(f'{prefix}{separator}{postfix}')
else:
with open(caption_file_path, 'r+') as f:
content = f.read()
content = content.rstrip()
f.seek(0, 0)
prefix_separator = ' ' if prefix else ''
postfix_separator = ' ' if postfix else ''
2023-03-20 12:47:00 +00:00
f.write(
f'{prefix}{prefix_separator}{content}{postfix_separator}{postfix}'
)
2023-03-15 23:31:52 +00:00
def has_ext_files(folder_path: str, file_extension: str) -> bool:
"""
Check if there are any files with the specified extension in the given folder.
2023-03-20 12:47:00 +00:00
2023-03-15 23:31:52 +00:00
Args:
folder_path (str): Path to the folder containing files.
file_extension (str): Extension of the files to look for.
2023-03-20 12:47:00 +00:00
2023-03-15 23:31:52 +00:00
Returns:
bool: True if files with the specified extension are found, False otherwise.
"""
for file in os.listdir(folder_path):
if file.endswith(file_extension):
return True
return False
2023-03-20 12:47:00 +00:00
2023-03-15 23:31:52 +00:00
def find_replace(
2023-03-20 12:47:00 +00:00
folder_path: str = '',
caption_file_ext: str = '.caption',
search_text: str = '',
replace_text: str = '',
) -> None:
2023-03-15 23:31:52 +00:00
"""
Find and replace text in caption files within a folder.
2023-03-20 12:47:00 +00:00
2023-03-15 23:31:52 +00:00
Args:
folder_path (str, optional): Path to the folder containing caption files.
caption_file_ext (str, optional): Extension of the caption files.
search_text (str, optional): Text to search for in the caption files.
replace_text (str, optional): Text to replace the search text with.
"""
print('Running caption find/replace')
2023-03-20 12:47:00 +00:00
2023-03-15 23:31:52 +00:00
if not has_ext_files(folder_path, caption_file_ext):
msgbox(
2023-03-15 23:31:52 +00:00
f'No files with extension {caption_file_ext} were found in {folder_path}...'
)
return
2023-03-15 23:31:52 +00:00
if search_text == '':
return
2023-03-20 12:47:00 +00:00
caption_files = [
f for f in os.listdir(folder_path) if f.endswith(caption_file_ext)
]
2023-03-15 23:31:52 +00:00
for caption_file in caption_files:
2023-03-20 12:47:00 +00:00
with open(
os.path.join(folder_path, caption_file), 'r', errors='ignore'
) as f:
content = f.read()
2023-03-15 23:31:52 +00:00
content = content.replace(search_text, replace_text)
with open(os.path.join(folder_path, caption_file), 'w') as f:
f.write(content)
2023-03-15 23:31:52 +00:00
2023-03-20 12:47:00 +00:00
def color_aug_changed(color_aug):
if color_aug:
msgbox(
'Disabling "Cache latent" because "Color augmentation" has been selected...'
)
return gr.Checkbox.update(value=False, interactive=False)
else:
return gr.Checkbox.update(value=True, interactive=True)
def save_inference_file(output_dir, v2, v_parameterization, output_name):
# List all files in the directory
files = os.listdir(output_dir)
# Iterate over the list of files
for file in files:
# Check if the file starts with the value of output_name
if file.startswith(output_name):
# Check if it is a file or a directory
if os.path.isfile(os.path.join(output_dir, file)):
# Split the file name and extension
file_name, ext = os.path.splitext(file)
# Copy the v2-inference-v.yaml file to the current file, with a .yaml extension
if v2 and v_parameterization:
print(
f'Saving v2-inference-v.yaml as {output_dir}/{file_name}.yaml'
)
shutil.copy(
f'./v2_inference/v2-inference-v.yaml',
f'{output_dir}/{file_name}.yaml',
)
elif v2:
print(
f'Saving v2-inference.yaml as {output_dir}/{file_name}.yaml'
)
shutil.copy(
f'./v2_inference/v2-inference.yaml',
f'{output_dir}/{file_name}.yaml',
)
2023-03-04 23:56:22 +00:00
def set_pretrained_model_name_or_path_input(
model_list, pretrained_model_name_or_path, v2, v_parameterization
):
# check if $v2 and $v_parameterization are empty and if $pretrained_model_name_or_path contains any of the substrings in the v2 list
2023-03-06 02:42:28 +00:00
if str(model_list) in V2_BASE_MODELS:
print('SD v2 model detected. Setting --v2 parameter')
v2 = True
v_parameterization = False
2023-03-06 02:10:24 +00:00
pretrained_model_name_or_path = str(model_list)
# check if $v2 and $v_parameterization are empty and if $pretrained_model_name_or_path contains any of the substrings in the v_parameterization list
2023-03-06 02:42:28 +00:00
if str(model_list) in V_PARAMETERIZATION_MODELS:
print(
'SD v2 v_parameterization detected. Setting --v2 parameter and --v_parameterization'
)
v2 = True
v_parameterization = True
2023-03-06 02:10:24 +00:00
pretrained_model_name_or_path = str(model_list)
2023-03-06 02:42:28 +00:00
if str(model_list) in V1_MODELS:
v2 = False
v_parameterization = False
2023-03-06 02:10:24 +00:00
pretrained_model_name_or_path = str(model_list)
if model_list == 'custom':
2023-03-04 23:56:22 +00:00
if (
2023-03-06 02:42:28 +00:00
str(pretrained_model_name_or_path) in V1_MODELS
or str(pretrained_model_name_or_path) in V2_BASE_MODELS
2023-03-20 12:47:00 +00:00
or str(pretrained_model_name_or_path) in V_PARAMETERIZATION_MODELS
2023-03-04 23:56:22 +00:00
):
pretrained_model_name_or_path = ''
2023-03-03 12:11:15 +00:00
v2 = False
v_parameterization = False
2023-03-06 02:10:24 +00:00
return model_list, pretrained_model_name_or_path, v2, v_parameterization
2023-03-20 12:47:00 +00:00
def set_v2_checkbox(model_list, v2, v_parameterization):
2023-03-06 02:38:20 +00:00
# check if $v2 and $v_parameterization are empty and if $pretrained_model_name_or_path contains any of the substrings in the v2 list
2023-03-06 02:42:28 +00:00
if str(model_list) in V2_BASE_MODELS:
2023-03-06 02:38:20 +00:00
v2 = True
v_parameterization = False
# check if $v2 and $v_parameterization are empty and if $pretrained_model_name_or_path contains any of the substrings in the v_parameterization list
2023-03-06 02:42:28 +00:00
if str(model_list) in V_PARAMETERIZATION_MODELS:
2023-03-06 02:38:20 +00:00
v2 = True
v_parameterization = True
2023-03-06 02:42:28 +00:00
if str(model_list) in V1_MODELS:
2023-03-06 02:38:20 +00:00
v2 = False
v_parameterization = False
return v2, v_parameterization
2023-03-20 12:47:00 +00:00
2023-03-06 02:10:24 +00:00
def set_model_list(
model_list,
pretrained_model_name_or_path,
v2,
v_parameterization,
):
2023-03-06 02:42:28 +00:00
if not pretrained_model_name_or_path in ALL_PRESET_MODELS:
2023-03-06 02:10:24 +00:00
model_list = 'custom'
else:
model_list = pretrained_model_name_or_path
2023-03-20 12:47:00 +00:00
2023-03-06 02:10:24 +00:00
return model_list, v2, v_parameterization
2023-03-06 02:10:24 +00:00
###
### Gradio common GUI section
###
2023-02-06 01:07:00 +00:00
2023-01-16 00:59:40 +00:00
def gradio_config():
with gr.Accordion('Configuration file', open=False):
with gr.Row():
button_open_config = gr.Button('Open 📂', elem_id='open_folder')
button_save_config = gr.Button('Save 💾', elem_id='open_folder')
button_save_as_config = gr.Button(
'Save as... 💾', elem_id='open_folder'
)
config_file_name = gr.Textbox(
label='',
placeholder="type the configuration file path or use the 'Open' button above to select it...",
interactive=True,
)
button_load_config = gr.Button('Load 💾', elem_id='open_folder')
2023-03-20 12:47:00 +00:00
config_file_name.change(
remove_doublequote,
inputs=[config_file_name],
outputs=[config_file_name],
)
2023-02-06 01:07:00 +00:00
return (
button_open_config,
button_save_config,
button_save_as_config,
config_file_name,
button_load_config,
2023-02-06 01:07:00 +00:00
)
2023-01-16 00:59:40 +00:00
2023-03-06 02:10:24 +00:00
def get_pretrained_model_name_or_path_file(
model_list, pretrained_model_name_or_path
):
pretrained_model_name_or_path = get_any_file_path(
pretrained_model_name_or_path
)
set_model_list(model_list, pretrained_model_name_or_path)
def gradio_source_model(save_model_as_choices = [
'same as source model',
'ckpt',
'diffusers',
'diffusers_safetensors',
'safetensors',
]):
2023-01-16 00:59:40 +00:00
with gr.Tab('Source model'):
# Define the input elements
with gr.Row():
pretrained_model_name_or_path = gr.Textbox(
label='Pretrained model name or path',
placeholder='enter the path to custom model or name of pretrained model',
2023-03-04 23:56:22 +00:00
value='runwayml/stable-diffusion-v1-5',
2023-01-16 00:59:40 +00:00
)
pretrained_model_name_or_path_file = gr.Button(
document_symbol, elem_id='open_folder_small'
)
pretrained_model_name_or_path_file.click(
get_any_file_path,
inputs=pretrained_model_name_or_path,
outputs=pretrained_model_name_or_path,
2023-03-04 23:56:22 +00:00
show_progress=False,
2023-01-16 00:59:40 +00:00
)
pretrained_model_name_or_path_folder = gr.Button(
folder_symbol, elem_id='open_folder_small'
)
pretrained_model_name_or_path_folder.click(
get_folder_path,
inputs=pretrained_model_name_or_path,
outputs=pretrained_model_name_or_path,
2023-03-04 23:56:22 +00:00
show_progress=False,
2023-01-16 00:59:40 +00:00
)
model_list = gr.Dropdown(
label='Model Quick Pick',
2023-01-16 00:59:40 +00:00
choices=[
'custom',
'stabilityai/stable-diffusion-2-1-base',
'stabilityai/stable-diffusion-2-base',
'stabilityai/stable-diffusion-2-1',
'stabilityai/stable-diffusion-2',
'runwayml/stable-diffusion-v1-5',
'CompVis/stable-diffusion-v1-4',
],
2023-03-04 23:56:22 +00:00
value='runwayml/stable-diffusion-v1-5',
2023-01-16 00:59:40 +00:00
)
save_model_as = gr.Dropdown(
label='Save trained model as',
choices=save_model_as_choices,
value='safetensors',
2023-01-16 00:59:40 +00:00
)
with gr.Row():
v2 = gr.Checkbox(label='v2', value=False)
2023-01-16 00:59:40 +00:00
v_parameterization = gr.Checkbox(
label='v_parameterization', value=False
)
2023-03-20 12:47:00 +00:00
v2.change(
set_v2_checkbox,
inputs=[model_list, v2, v_parameterization],
outputs=[v2, v_parameterization],
show_progress=False,
)
v_parameterization.change(
set_v2_checkbox,
inputs=[model_list, v2, v_parameterization],
outputs=[v2, v_parameterization],
show_progress=False,
)
2023-01-16 00:59:40 +00:00
model_list.change(
set_pretrained_model_name_or_path_input,
2023-03-04 23:56:22 +00:00
inputs=[
model_list,
pretrained_model_name_or_path,
v2,
v_parameterization,
],
2023-01-16 00:59:40 +00:00
outputs=[
2023-03-06 02:10:24 +00:00
model_list,
pretrained_model_name_or_path,
v2,
v_parameterization,
],
2023-03-06 02:38:20 +00:00
show_progress=False,
2023-03-06 02:10:24 +00:00
)
# Update the model list and parameters when user click outside the button or field
pretrained_model_name_or_path.change(
set_model_list,
inputs=[
model_list,
2023-01-16 00:59:40 +00:00
pretrained_model_name_or_path,
v2,
v_parameterization,
],
2023-03-06 02:10:24 +00:00
outputs=[
model_list,
v2,
v_parameterization,
],
2023-03-06 02:38:20 +00:00
show_progress=False,
2023-01-16 00:59:40 +00:00
)
2023-02-06 01:07:00 +00:00
return (
pretrained_model_name_or_path,
v2,
v_parameterization,
save_model_as,
model_list,
)
2023-01-16 00:59:40 +00:00
2023-02-06 01:07:00 +00:00
def gradio_training(
learning_rate_value='1e-6',
lr_scheduler_value='constant',
lr_warmup_value='0',
):
2023-01-16 00:59:40 +00:00
with gr.Row():
train_batch_size = gr.Slider(
minimum=1,
2023-02-26 20:11:21 +00:00
maximum=64,
2023-01-16 00:59:40 +00:00
label='Train batch size',
value=1,
step=1,
)
epoch = gr.Number(label='Epoch', value=1, precision=0)
2023-03-20 12:47:00 +00:00
save_every_n_epochs = gr.Number(
label='Save every N epochs', value=1, precision=0
)
2023-01-16 00:59:40 +00:00
caption_extension = gr.Textbox(
label='Caption Extension',
placeholder='(Optional) Extension for caption files. default: .caption',
)
with gr.Row():
mixed_precision = gr.Dropdown(
label='Mixed precision',
choices=[
'no',
'fp16',
'bf16',
],
value='fp16',
)
save_precision = gr.Dropdown(
label='Save precision',
choices=[
'float',
'fp16',
'bf16',
],
value='fp16',
)
num_cpu_threads_per_process = gr.Slider(
minimum=1,
maximum=os.cpu_count(),
step=1,
2023-01-26 21:22:58 +00:00
label='Number of CPU threads per core',
value=2,
2023-01-16 00:59:40 +00:00
)
2023-02-25 01:37:51 +00:00
seed = gr.Textbox(label='Seed', placeholder='(Optional) eg:1234')
cache_latents = gr.Checkbox(label='Cache latent', value=True)
2023-01-16 00:59:40 +00:00
with gr.Row():
2023-02-06 01:07:00 +00:00
learning_rate = gr.Textbox(
label='Learning rate', value=learning_rate_value
)
2023-01-16 00:59:40 +00:00
lr_scheduler = gr.Dropdown(
label='LR Scheduler',
choices=[
2023-02-24 02:48:45 +00:00
'adafactor',
2023-01-16 00:59:40 +00:00
'constant',
'constant_with_warmup',
'cosine',
'cosine_with_restarts',
'linear',
'polynomial',
],
value=lr_scheduler_value,
)
2023-02-06 01:07:00 +00:00
lr_warmup = gr.Textbox(
label='LR warmup (% of steps)', value=lr_warmup_value
)
optimizer = gr.Dropdown(
label='Optimizer',
choices=[
'AdamW',
2023-02-23 01:32:57 +00:00
'AdamW8bit',
'Adafactor',
'DAdaptation',
'Lion',
2023-02-23 01:32:57 +00:00
'SGDNesterov',
2023-03-02 00:24:11 +00:00
'SGDNesterov8bit',
],
2023-03-02 00:24:11 +00:00
value='AdamW8bit',
interactive=True,
)
2023-02-23 01:32:57 +00:00
with gr.Row():
optimizer_args = gr.Textbox(
2023-03-02 00:24:11 +00:00
label='Optimizer extra arguments',
placeholder='(Optional) eg: relative_step=True scale_parameter=True warmup_init=True',
2023-02-23 01:32:57 +00:00
)
2023-01-16 00:59:40 +00:00
return (
learning_rate,
lr_scheduler,
lr_warmup,
train_batch_size,
epoch,
save_every_n_epochs,
mixed_precision,
save_precision,
num_cpu_threads_per_process,
seed,
caption_extension,
cache_latents,
optimizer,
2023-02-23 01:32:57 +00:00
optimizer_args,
2023-01-16 00:59:40 +00:00
)
2023-02-06 01:07:00 +00:00
2023-01-16 00:59:40 +00:00
def run_cmd_training(**kwargs):
options = [
f' --learning_rate="{kwargs.get("learning_rate", "")}"'
if kwargs.get('learning_rate')
else '',
f' --lr_scheduler="{kwargs.get("lr_scheduler", "")}"'
if kwargs.get('lr_scheduler')
else '',
f' --lr_warmup_steps="{kwargs.get("lr_warmup_steps", "")}"'
if kwargs.get('lr_warmup_steps')
else '',
f' --train_batch_size="{kwargs.get("train_batch_size", "")}"'
if kwargs.get('train_batch_size')
else '',
f' --max_train_steps="{kwargs.get("max_train_steps", "")}"'
if kwargs.get('max_train_steps')
else '',
f' --save_every_n_epochs="{int(kwargs.get("save_every_n_epochs", 1))}"'
if int(kwargs.get('save_every_n_epochs'))
2023-01-16 00:59:40 +00:00
else '',
f' --mixed_precision="{kwargs.get("mixed_precision", "")}"'
if kwargs.get('mixed_precision')
else '',
f' --save_precision="{kwargs.get("save_precision", "")}"'
if kwargs.get('save_precision')
else '',
f' --seed="{kwargs.get("seed", "")}"'
2023-03-20 12:47:00 +00:00
if kwargs.get('seed') != ''
else '',
2023-01-16 00:59:40 +00:00
f' --caption_extension="{kwargs.get("caption_extension", "")}"'
if kwargs.get('caption_extension')
else '',
' --cache_latents' if kwargs.get('cache_latents') else '',
2023-02-23 01:32:57 +00:00
# ' --use_lion_optimizer' if kwargs.get('optimizer') == 'Lion' else '',
f' --optimizer_type="{kwargs.get("optimizer", "AdamW")}"',
2023-03-02 00:24:11 +00:00
f' --optimizer_args {kwargs.get("optimizer_args", "")}'
if not kwargs.get('optimizer_args') == ''
else '',
2023-01-16 00:59:40 +00:00
]
run_cmd = ''.join(options)
return run_cmd
2023-03-02 00:24:11 +00:00
def gradio_advanced_training():
2023-03-07 12:42:13 +00:00
with gr.Row():
additional_parameters = gr.Textbox(
2023-03-20 12:47:00 +00:00
label='Additional parameters',
2023-03-07 12:42:13 +00:00
placeholder='(Optional) Use to provide additional parameters not handled by the GUI. Eg: --some_parameters "value"',
)
with gr.Row():
2023-02-04 16:55:06 +00:00
keep_tokens = gr.Slider(
label='Keep n tokens', value='0', minimum=0, maximum=32, step=1
)
clip_skip = gr.Slider(
label='Clip skip', value='1', minimum=1, maximum=12, step=1
)
max_token_length = gr.Dropdown(
label='Max Token Length',
choices=[
'75',
'150',
'225',
],
value='75',
)
full_fp16 = gr.Checkbox(
label='Full fp16 training (experimental)', value=False
)
2023-02-04 16:55:06 +00:00
with gr.Row():
gradient_checkpointing = gr.Checkbox(
label='Gradient checkpointing', value=False
)
2023-02-06 01:07:00 +00:00
shuffle_caption = gr.Checkbox(label='Shuffle caption', value=False)
2023-02-04 16:55:06 +00:00
persistent_data_loader_workers = gr.Checkbox(
label='Persistent data loader', value=False
2023-01-27 12:33:44 +00:00
)
2023-02-04 16:55:06 +00:00
mem_eff_attn = gr.Checkbox(
label='Memory efficient attention', value=False
)
with gr.Row():
2023-03-02 00:02:04 +00:00
# This use_8bit_adam element should be removed in a future release as it is no longer used
2023-03-05 15:34:09 +00:00
# use_8bit_adam = gr.Checkbox(
# label='Use 8bit adam', value=False, visible=False
# )
xformers = gr.Checkbox(label='Use xformers', value=True)
2023-02-06 01:07:00 +00:00
color_aug = gr.Checkbox(label='Color augmentation', value=False)
flip_aug = gr.Checkbox(label='Flip augmentation', value=False)
2023-03-28 15:54:42 +00:00
min_snr_gamma = gr.Slider(label='Min SNR gamma', value = 0, minimum=0, maximum=20, step=1)
2023-02-06 01:07:00 +00:00
with gr.Row():
bucket_no_upscale = gr.Checkbox(
label="Don't upscale bucket resolution", value=True
)
bucket_reso_steps = gr.Number(
label='Bucket resolution steps', value=64
)
* 2023/02/06 (v20.7.0) - ``--bucket_reso_steps`` and ``--bucket_no_upscale`` options are added to training scripts (fine tuning, DreamBooth, LoRA and Textual Inversion) and ``prepare_buckets_latents.py``. - ``--bucket_reso_steps`` takes the steps for buckets in aspect ratio bucketing. Default is 64, same as before. - Any value greater than or equal to 1 can be specified; 64 is highly recommended and a value divisible by 8 is recommended. - If less than 64 is specified, padding will occur within U-Net. The result is unknown. - If you specify a value that is not divisible by 8, it will be truncated to divisible by 8 inside VAE, because the size of the latent is 1/8 of the image size. - If ``--bucket_no_upscale`` option is specified, images smaller than the bucket size will be processed without upscaling. - Internally, a bucket smaller than the image size is created (for example, if the image is 300x300 and ``bucket_reso_steps=64``, the bucket is 256x256). The image will be trimmed. - Implementation of [#130](https://github.com/kohya-ss/sd-scripts/issues/130). - Images with an area larger than the maximum size specified by ``--resolution`` are downsampled to the max bucket size. - Now the number of data in each batch is limited to the number of actual images (not duplicated). Because a certain bucket may contain smaller number of actual images, so the batch may contain same (duplicated) images. - ``--random_crop`` now also works with buckets enabled. - Instead of always cropping the center of the image, the image is shifted left, right, up, and down to be used as the training data. This is expected to train to the edges of the image. - Implementation of discussion [#34](https://github.com/kohya-ss/sd-scripts/discussions/34).
2023-02-06 16:04:07 +00:00
random_crop = gr.Checkbox(
label='Random crop instead of center crop', value=False
)
2023-02-23 01:32:57 +00:00
noise_offset = gr.Textbox(
label='Noise offset (0 - 1)', placeholder='(Oprional) eg: 0.1'
)
2023-03-02 00:24:11 +00:00
2023-02-08 01:58:35 +00:00
with gr.Row():
caption_dropout_every_n_epochs = gr.Number(
2023-03-02 00:24:11 +00:00
label='Dropout caption every n epochs', value=0
2023-02-08 01:58:35 +00:00
)
caption_dropout_rate = gr.Slider(
2023-03-02 00:24:11 +00:00
label='Rate of caption dropout', value=0, minimum=0, maximum=1
2023-02-08 01:58:35 +00:00
)
2023-03-22 00:20:57 +00:00
vae_batch_size = gr.Slider(
label='VAE batch size',
minimum=0,
maximum=32,
2023-03-22 16:55:30 +00:00
value=0,
2023-04-01 20:09:40 +00:00
step=1
2023-03-22 00:20:57 +00:00
)
with gr.Row():
save_state = gr.Checkbox(label='Save training state', value=False)
resume = gr.Textbox(
label='Resume from saved training state',
placeholder='path to "last-state" state folder to resume from',
)
resume_button = gr.Button('📂', elem_id='open_folder_small')
2023-03-04 23:56:22 +00:00
resume_button.click(
get_folder_path,
outputs=resume,
show_progress=False,
)
max_train_epochs = gr.Textbox(
label='Max train epoch',
placeholder='(Optional) Override number of epoch',
)
max_data_loader_n_workers = gr.Textbox(
label='Max num workers for DataLoader',
placeholder='(Optional) Override number of epoch. Default: 8',
2023-03-25 11:03:31 +00:00
value="0",
)
return (
2023-03-05 15:34:09 +00:00
# use_8bit_adam,
xformers,
full_fp16,
gradient_checkpointing,
shuffle_caption,
color_aug,
flip_aug,
clip_skip,
mem_eff_attn,
save_state,
resume,
max_token_length,
max_train_epochs,
max_data_loader_n_workers,
2023-01-27 12:33:44 +00:00
keep_tokens,
2023-02-04 16:55:06 +00:00
persistent_data_loader_workers,
2023-02-06 01:07:00 +00:00
bucket_no_upscale,
random_crop,
bucket_reso_steps,
2023-03-02 00:24:11 +00:00
caption_dropout_every_n_epochs,
caption_dropout_rate,
noise_offset,
2023-03-07 12:42:13 +00:00
additional_parameters,
2023-03-22 00:20:57 +00:00
vae_batch_size,
2023-03-28 15:54:42 +00:00
min_snr_gamma,
)
2023-02-06 01:07:00 +00:00
def run_cmd_advanced_training(**kwargs):
options = [
f' --max_train_epochs="{kwargs.get("max_train_epochs", "")}"'
if kwargs.get('max_train_epochs')
else '',
f' --max_data_loader_n_workers="{kwargs.get("max_data_loader_n_workers", "")}"'
if kwargs.get('max_data_loader_n_workers')
else '',
f' --max_token_length={kwargs.get("max_token_length", "")}'
if int(kwargs.get('max_token_length', 75)) > 75
else '',
f' --clip_skip={kwargs.get("clip_skip", "")}'
if int(kwargs.get('clip_skip', 1)) > 1
else '',
f' --resume="{kwargs.get("resume", "")}"'
if kwargs.get('resume')
else '',
2023-01-27 12:33:44 +00:00
f' --keep_tokens="{kwargs.get("keep_tokens", "")}"'
if int(kwargs.get('keep_tokens', 0)) > 0
else '',
2023-03-09 00:33:53 +00:00
f' --caption_dropout_every_n_epochs="{int(kwargs.get("caption_dropout_every_n_epochs", 0))}"'
2023-02-08 01:58:35 +00:00
if int(kwargs.get('caption_dropout_every_n_epochs', 0)) > 0
else '',
2023-03-22 00:20:57 +00:00
f' --caption_dropout_every_n_epochs="{int(kwargs.get("caption_dropout_every_n_epochs", 0))}"'
if int(kwargs.get('caption_dropout_every_n_epochs', 0)) > 0
else '',
f' --vae_batch_size="{kwargs.get("vae_batch_size", 0)}"'
if int(kwargs.get('vae_batch_size', 0)) > 0
2023-02-08 01:58:35 +00:00
else '',
2023-02-06 01:07:00 +00:00
f' --bucket_reso_steps={int(kwargs.get("bucket_reso_steps", 1))}'
if int(kwargs.get('bucket_reso_steps', 64)) >= 1
else '',
2023-03-28 15:54:42 +00:00
f' --min_snr_gamma={int(kwargs.get("min_snr_gamma", 0))}'
if int(kwargs.get('min_snr_gamma', 0)) >= 1
else '',
2023-02-06 01:07:00 +00:00
' --save_state' if kwargs.get('save_state') else '',
' --mem_eff_attn' if kwargs.get('mem_eff_attn') else '',
' --color_aug' if kwargs.get('color_aug') else '',
' --flip_aug' if kwargs.get('flip_aug') else '',
' --shuffle_caption' if kwargs.get('shuffle_caption') else '',
2023-03-28 15:54:42 +00:00
' --gradient_checkpointing' if kwargs.get('gradient_checkpointing')
2023-02-06 01:07:00 +00:00
else '',
' --full_fp16' if kwargs.get('full_fp16') else '',
' --xformers' if kwargs.get('xformers') else '',
2023-03-05 15:34:09 +00:00
# ' --use_8bit_adam' if kwargs.get('use_8bit_adam') else '',
2023-02-06 01:07:00 +00:00
' --persistent_data_loader_workers'
if kwargs.get('persistent_data_loader_workers')
else '',
' --bucket_no_upscale' if kwargs.get('bucket_no_upscale') else '',
' --random_crop' if kwargs.get('random_crop') else '',
2023-02-23 01:32:57 +00:00
f' --noise_offset={float(kwargs.get("noise_offset", 0))}'
if not kwargs.get('noise_offset', '') == ''
else '',
2023-03-20 12:47:00 +00:00
f' {kwargs.get("additional_parameters", "")}',
]
run_cmd = ''.join(options)
return run_cmd