From ba9e26a99db9d38534bc2b446c6027e024cf23be Mon Sep 17 00:00:00 2001 From: bmaltais Date: Thu, 9 Mar 2023 07:49:50 -0500 Subject: [PATCH 1/5] LoHa support --- README.md | 1 + library/common_gui.py | 5 ++++- lora_gui.py | 28 +++++++++++++++++++++------- requirements.txt | 2 +- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 24ad0b7..07c775e 100644 --- a/README.md +++ b/README.md @@ -177,6 +177,7 @@ This will store your a backup file with your current locally installed pip packa ## Change History * 2023/03/05 (v21.2.0): + - Add support for LoRA LoHa type. See https://github.com/KohakuBlueleaf/LyCORIS for more detais. - Fix issue https://github.com/bmaltais/kohya_ss/issues/335 - Add option to print LoRA trainer command without executing it - Add support for samples during trainin via a new `Sample images config` accordion in the `Training parameters` tab. diff --git a/library/common_gui.py b/library/common_gui.py index b22594f..477d346 100644 --- a/library/common_gui.py +++ b/library/common_gui.py @@ -58,7 +58,10 @@ def update_my_data(my_data): my_data[key] = int(value) else: my_data[key] = -1 - + + if my_data.get('LoRA_type', 'Standard') == 'LoCon': + my_data['LoRA_type'] = 'LyCORIS/LoCon' + return my_data diff --git a/lora_gui.py b/lora_gui.py index d175b30..23eae18 100644 --- a/lora_gui.py +++ b/lora_gui.py @@ -474,17 +474,29 @@ def train_model( run_cmd += f' --save_model_as={save_model_as}' if not float(prior_loss_weight) == 1.0: run_cmd += f' --prior_loss_weight={prior_loss_weight}' - if LoRA_type == 'LoCon': + if LoRA_type == 'LoCon' or LoRA_type == 'LyCORIS/LoCon': try: - import locon.locon_kohya + import lycoris except ModuleNotFoundError: print( - "\033[1;31mError:\033[0m The required module 'locon' is not installed. Please install by running \033[33mupgrade.ps1\033[0m before running this program." + "\033[1;31mError:\033[0m The required module 'lycoris_lora' is not installed. Please install by running \033[33mupgrade.ps1\033[0m before running this program." ) return - run_cmd += f' --network_module=locon.locon_kohya' + run_cmd += f' --network_module=lycoris.kohya' run_cmd += ( - f' --network_args "conv_dim={conv_dim}" "conv_alpha={conv_alpha}"' + f' --network_args "conv_dim={conv_dim}" "conv_alpha={conv_alpha}" "algo=lora"' + ) + if LoRA_type == 'LyCORIS/LoHa': + try: + import lycoris + except ModuleNotFoundError: + print( + "\033[1;31mError:\033[0m The required module 'lycoris_lora' is not installed. Please install by running \033[33mupgrade.ps1\033[0m before running this program." + ) + return + run_cmd += f' --network_module=lycoris.kohya' + run_cmd += ( + f' --network_args "conv_dim={conv_dim}" "conv_alpha={conv_alpha}" "algo=loha"' ) if LoRA_type == 'Kohya LoCon': run_cmd += f' --network_module=networks.lora' @@ -700,7 +712,9 @@ def lora_tab( label='LoRA type', choices=[ 'Kohya LoCon', - 'LoCon', + # 'LoCon', + 'LyCORIS/LoCon', + 'LyCORIS/LoHa', 'Standard', ], value='Standard', @@ -787,7 +801,7 @@ def lora_tab( # Show of hide LoCon conv settings depending on LoRA type selection def LoRA_type_change(LoRA_type): print('LoRA type changed...') - if LoRA_type == 'LoCon' or LoRA_type == 'Kohya LoCon': + if LoRA_type == 'LoCon' or LoRA_type == 'Kohya LoCon' or LoRA_type == 'LyCORIS/LoHa' or LoRA_type == 'LyCORIS/LoCon': return gr.Group.update(visible=True) else: return gr.Group.update(visible=False) diff --git a/requirements.txt b/requirements.txt index 27bde40..479c7f6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -25,6 +25,6 @@ timm==0.6.12 huggingface-hub==0.12.0 tensorflow==2.10.1 # For locon support -locon==0.0.2 +lycoris_lora==0.0.5 # for kohya_ss library . \ No newline at end of file From e110d4b67d100086cc565c2be6fbb7d09c559f33 Mon Sep 17 00:00:00 2001 From: bmaltais Date: Thu, 9 Mar 2023 12:11:17 -0500 Subject: [PATCH 2/5] Update requirements --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 479c7f6..df32424 100644 --- a/requirements.txt +++ b/requirements.txt @@ -25,6 +25,6 @@ timm==0.6.12 huggingface-hub==0.12.0 tensorflow==2.10.1 # For locon support -lycoris_lora==0.0.5 +lycoris_lora==0.0.9 # for kohya_ss library . \ No newline at end of file From 75c21eabbcdafff96bc58a16a67aabb38ab1d0f1 Mon Sep 17 00:00:00 2001 From: bmaltais Date: Sat, 11 Mar 2023 14:53:43 -0500 Subject: [PATCH 3/5] Update description --- lora_gui.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lora_gui.py b/lora_gui.py index 911652a..89f97fb 100644 --- a/lora_gui.py +++ b/lora_gui.py @@ -796,14 +796,14 @@ def lora_tab( maximum=512, value=1, step=1, - label='LoCon Convolution Rank (Dimension)', + label='Convolution Rank (Dimension)', ) conv_alpha = gr.Slider( minimum=1, maximum=512, value=1, step=1, - label='LoCon Convolution Alpha', + label='Convolution Alpha', ) # Show of hide LoCon conv settings depending on LoRA type selection def LoRA_type_change(LoRA_type): From 79c2c2debe1f393b83e06d518cb78afeeb105935 Mon Sep 17 00:00:00 2001 From: bmaltais Date: Sun, 12 Mar 2023 10:11:41 -0400 Subject: [PATCH 4/5] Add validation that all requirements are met --- README.md | 29 ++++++++++++++++++------ gui.bat | 12 +++++++--- gui.ps1 | 12 ++++++++-- tools/validate_requirements.py | 41 ++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 12 deletions(-) create mode 100644 tools/validate_requirements.py diff --git a/README.md b/README.md index fc49070..393464f 100644 --- a/README.md +++ b/README.md @@ -110,16 +110,29 @@ pip install --use-pep517 --upgrade -r requirements.txt Once the commands have completed successfully you should be ready to use the new version. -## Launching the GUI +## Launching the GUI using gui.bat or gui.ps1 + +The script can be run with several optional command line arguments: + +--listen: the IP address to listen on for connections to Gradio. +--username: a username for authentication. +--password: a password for authentication. +--server_port: the port to run the server listener on. +--inbrowser: opens the Gradio UI in a web browser. +--share: shares the Gradio UI. + +These command line arguments can be passed to the UI function as keyword arguments. To launch the Gradio UI, run the script in a terminal with the desired command line arguments, for example: + +`gui.ps1 --listen 127.0.0.1 --server_port 7860 --inbrowser --share` + +or + +`gui.bat --listen 127.0.0.1 --server_port 7860 --inbrowser --share` + +## Launching the GUI using kohya_gui.py To run the GUI, simply use this command: -``` -.\gui.ps1 -``` - -or you can also do: - ``` .\venv\Scripts\activate @@ -176,6 +189,8 @@ This will store your a backup file with your current locally installed pip packa ## Change History +* 2023.03/12 (v21.2.3): + - Add validation that all requirements are met before starting the GUI. * 2023/03/11 (v21.2.2): - Add support for LoRA LoHa type. See https://github.com/KohakuBlueleaf/LyCORIS for more detais. * 2023/03/10 (v21.2.1): diff --git a/gui.bat b/gui.bat index a7e8d08..d1f2864 100644 --- a/gui.bat +++ b/gui.bat @@ -1,6 +1,12 @@ @echo off -call venv\Scripts\activate.bat -python.exe kohya_gui.py %* +:: Activate the virtual environment +call .\venv\Scripts\activate.bat -pause \ No newline at end of file +:: Validate the requirements and store the exit code +python.exe .\tools\validate_requirements.py + +:: If the exit code is 0, run the kohya_gui.py script with the command-line arguments +if %errorlevel% equ 0 ( + python.exe kohya_gui.py %* +) \ No newline at end of file diff --git a/gui.ps1 b/gui.ps1 index fde5028..81b43a0 100644 --- a/gui.ps1 +++ b/gui.ps1 @@ -1,2 +1,10 @@ -.\venv\Scripts\activate -python.exe kohya_gui.py $args \ No newline at end of file +# Activate the virtual environment +& .\venv\Scripts\activate + +# Validate the requirements and store the exit code +python.exe .\tools\validate_requirements.py + +# If the exit code is 0, run the kohya_gui.py script with the command-line arguments +if ($LASTEXITCODE -eq 0) { + python.exe kohya_gui.py $args +} \ No newline at end of file diff --git a/tools/validate_requirements.py b/tools/validate_requirements.py new file mode 100644 index 0000000..9af2ce4 --- /dev/null +++ b/tools/validate_requirements.py @@ -0,0 +1,41 @@ +import sys +import pkg_resources + +print("Validating that requirements are satisfied.") + +# Load the requirements from the requirements.txt file +with open('requirements.txt') as f: + requirements = f.readlines() + +# Check each requirement against the installed packages +missing_requirements = [] +wrong_version_requirements = [] +for requirement in requirements: + requirement = requirement.strip() + if requirement == ".": + # Skip the current requirement if it is a dot (.) + continue + try: + pkg_resources.require(requirement) + except pkg_resources.DistributionNotFound: + missing_requirements.append(requirement) + except pkg_resources.VersionConflict as e: + wrong_version_requirements.append((requirement, str(e.req), e.dist.version)) + +# If there are any missing or wrong version requirements, print an error message and exit with a non-zero exit code +if missing_requirements or wrong_version_requirements: + if missing_requirements: + print("Error: The following packages are missing:") + for requirement in missing_requirements: + print(f" - {requirement}") + if wrong_version_requirements: + print("Error: The following packages have the wrong version:") + for requirement, expected_version, actual_version in wrong_version_requirements: + print(f" - {requirement} (expected version {expected_version}, found version {actual_version})") + print('\nRun \033[33mupgrade.ps1\033[0m or \033[33mpip install -U -r requirements.txt\033[0m to resolve the missing requirements listed above...') + + sys.exit(1) + +# All requirements satisfied +print("All requirements satisfied.") +sys.exit(0) From 0f692bed9fcf86a6e93294a0f659104a02d3988a Mon Sep 17 00:00:00 2001 From: bmaltais Date: Sun, 12 Mar 2023 10:14:32 -0400 Subject: [PATCH 5/5] Fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 393464f..d416ac8 100644 --- a/README.md +++ b/README.md @@ -192,7 +192,7 @@ This will store your a backup file with your current locally installed pip packa * 2023.03/12 (v21.2.3): - Add validation that all requirements are met before starting the GUI. * 2023/03/11 (v21.2.2): - - Add support for LoRA LoHa type. See https://github.com/KohakuBlueleaf/LyCORIS for more detais. + - Add support for LoRA LoHa type. See https://github.com/KohakuBlueleaf/LyCORIS for more details. * 2023/03/10 (v21.2.1): - Update to latest sd-script code - Add support for SVD based LoRA merge