Skip to content

Commit

Permalink
Merge branch 'dev' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
bmaltais authored Mar 4, 2023
2 parents 129cd5f + e93de7f commit fa92a5c
Show file tree
Hide file tree
Showing 8 changed files with 459 additions and 10 deletions.
4 changes: 2 additions & 2 deletions dreambooth_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
gradio_config,
gradio_source_model,
set_legacy_8bitadam,
update_optimizer,
my_data,
)
from library.tensorboard_gui import (
gradio_tensorboard,
Expand Down Expand Up @@ -214,7 +214,7 @@ def open_configuration(
my_data = json.load(f)
print('Loading config...')
# Update values to fix deprecated use_8bit_adam checkbox and set appropriate optimizer if it is set to True
my_data = update_optimizer(my_data)
my_data = my_data(my_data)
else:
file_path = original_file_path # In case a file_path was provided and the user decide to cancel the open action
my_data = {}
Expand Down
4 changes: 2 additions & 2 deletions finetune_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
color_aug_changed,
run_cmd_training,
set_legacy_8bitadam,
update_optimizer,
my_data,
)
from library.tensorboard_gui import (
gradio_tensorboard,
Expand Down Expand Up @@ -217,7 +217,7 @@ def open_config_file(
my_data_db = json.load(f)
print('Loading config...')
# Update values to fix deprecated use_8bit_adam checkbox and set appropriate optimizer if it is set to True
my_data = update_optimizer(my_data)
my_data = my_data(my_data)
else:
file_path = original_file_path # In case a file_path was provided and the user decide to cancel the open action
my_data_db = {}
Expand Down
6 changes: 5 additions & 1 deletion library/common_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
document_symbol = '\U0001F4C4' # 📄


def update_optimizer(my_data):
def my_data(my_data):
if my_data.get('use_8bit_adam', False):
my_data['optimizer'] = 'AdamW8bit'
my_data['use_8bit_adam'] = False

if my_data.get('model_list', 'custom') == []:
print('Old config with empty model list. Setting to custom...')
my_data['model_list'] = 'custom'
return my_data


Expand Down
2 changes: 1 addition & 1 deletion library/git_caption_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def caption_images(
return

print(f'GIT captioning files in {train_data_dir}...')
run_cmd = f'{PYTHON} "finetune/make_captions.py"'
run_cmd = f'{PYTHON} "finetune/make_captions_by_git.py"'
if not model_id == '':
run_cmd += f' --model_id="{model_id}"'
run_cmd += f' --batch_size="{int(batch_size)}"'
Expand Down
4 changes: 2 additions & 2 deletions lora_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
gradio_source_model,
run_cmd_training,
set_legacy_8bitadam,
update_optimizer,
my_data,
)
from library.dreambooth_folder_creation_gui import (
gradio_dreambooth_folder_creation_tab,
Expand Down Expand Up @@ -239,7 +239,7 @@ def open_configuration(
my_data = json.load(f)
print('Loading config...')
# Update values to fix deprecated use_8bit_adam checkbox and set appropriate optimizer if it is set to True
my_data = update_optimizer(my_data)
my_data = my_data(my_data)
else:
file_path = original_file_path # In case a file_path was provided and the user decide to cancel the open action
my_data = {}
Expand Down
4 changes: 2 additions & 2 deletions textual_inversion_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
gradio_config,
gradio_source_model,
set_legacy_8bitadam,
update_optimizer,
my_data,
)
from library.tensorboard_gui import (
gradio_tensorboard,
Expand Down Expand Up @@ -226,7 +226,7 @@ def open_configuration(
my_data_db = json.load(f)
print('Loading config...')
# Update values to fix deprecated use_8bit_adam checkbox and set appropriate optimizer if it is set to True
my_data = update_optimizer(my_data)
my_data = my_data(my_data)
else:
file_path = original_file_path # In case a file_path was provided and the user decide to cancel the open action
my_data_db = {}
Expand Down
106 changes: 106 additions & 0 deletions tools/extract_locon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#
# From: https://raw.githubusercontent.com/KohakuBlueleaf/LoCon/main/extract_locon.py
#

import argparse

def get_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"base_model", help="The model which use it to train the dreambooth model",
default='', type=str
)
parser.add_argument(
"db_model", help="the dreambooth model you want to extract the locon",
default='', type=str
)
parser.add_argument(
"output_name", help="the output model",
default='./out.pt', type=str
)
parser.add_argument(
"--is_v2", help="Your base/db model is sd v2 or not",
default=False, action="store_true"
)
parser.add_argument(
"--device", help="Which device you want to use to extract the locon",
default='cpu', type=str
)
parser.add_argument(
"--mode",
help=(
'extraction mode, can be "fixed", "threshold", "ratio", "percentile". '
'If not "fixed", network_dim and conv_dim will be ignored'
),
default='fixed', type=str
)
parser.add_argument(
"--linear_dim", help="network dim for linear layer in fixed mode",
default=1, type=int
)
parser.add_argument(
"--conv_dim", help="network dim for conv layer in fixed mode",
default=1, type=int
)
parser.add_argument(
"--linear_threshold", help="singular value threshold for linear layer in threshold mode",
default=0., type=float
)
parser.add_argument(
"--conv_threshold", help="singular value threshold for conv layer in threshold mode",
default=0., type=float
)
parser.add_argument(
"--linear_ratio", help="singular ratio for linear layer in ratio mode",
default=0., type=float
)
parser.add_argument(
"--conv_ratio", help="singular ratio for conv layer in ratio mode",
default=0., type=float
)
parser.add_argument(
"--linear_percentile", help="singular value percentile for linear layer percentile mode",
default=1., type=float
)
parser.add_argument(
"--conv_percentile", help="singular value percentile for conv layer percentile mode",
default=1., type=float
)
return parser.parse_args()
ARGS = get_args()

from locon.utils import extract_diff
from locon.kohya_model_utils import load_models_from_stable_diffusion_checkpoint

import torch


def main():
args = ARGS
base = load_models_from_stable_diffusion_checkpoint(args.is_v2, args.base_model)
db = load_models_from_stable_diffusion_checkpoint(args.is_v2, args.db_model)

linear_mode_param = {
'fixed': args.linear_dim,
'threshold': args.linear_threshold,
'ratio': args.linear_ratio,
'percentile': args.linear_percentile,
}[args.mode]
conv_mode_param = {
'fixed': args.conv_dim,
'threshold': args.conv_threshold,
'ratio': args.conv_ratio,
'percentile': args.conv_percentile,
}[args.mode]

state_dict = extract_diff(
base, db,
args.mode,
linear_mode_param, conv_mode_param,
args.device
)
torch.save(state_dict, args.output_name)


if __name__ == '__main__':
main()
Loading

0 comments on commit fa92a5c

Please sign in to comment.