Skip to content

Commit 0ca93a7

Browse files
committed
v18.1: Model conversion utility
1 parent f459c32 commit 0ca93a7

15 files changed

+209
-1177
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ venv
22
venv1
33
mytraining.ps
44
__pycache__
5-
.vscode
5+
.vscode
6+
*.egg-info
7+
build

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ Drop by the discord server for support: https://discord.com/channels/10415185624
130130

131131
## Change history
132132

133+
* 12/18 (v18.1) update:
134+
- Add Stable Diffusion model conversion utility. Make sure to run `pip upgrade -U -r requirements.txt` after updating to this release as this introduce new pip requirements.
133135
* 12/17 (v18) update:
134136
- Save model as option added to train_db_fixed.py
135137
- Save model as option added to GUI

dreambooth_gui.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010
import subprocess
1111
import pathlib
1212
import shutil
13-
from dreambooth_gui.dreambooth_folder_creation import gradio_dreambooth_folder_creation_tab
14-
from dreambooth_gui.caption_gui import gradio_caption_gui_tab
15-
from dreambooth_gui.dataset_balancing import gradio_dataset_balancing_tab
16-
from dreambooth_gui.common_gui import (
13+
from library.dreambooth_folder_creation_gui import gradio_dreambooth_folder_creation_tab
14+
from library.caption_gui import gradio_caption_gui_tab
15+
from library.dataset_balancing_gui import gradio_dataset_balancing_tab
16+
from library.common_gui import (
1717
get_folder_path,
1818
remove_doublequote,
1919
get_file_path,
2020
)
21+
from library.convert_model_gui import gradio_convert_model_tab
2122
from easygui import filesavebox, msgbox
2223

2324
folder_symbol = '\U0001f4c2' # 📂
@@ -699,6 +700,7 @@ def set_pretrained_model_name_or_path_input(value, v2, v_parameterization):
699700
# Captionning tab
700701
gradio_caption_gui_tab()
701702
gradio_dataset_balancing_tab()
703+
gradio_convert_model_tab()
702704
# with gr.Tab('Model conversion'):
703705
# convert_to_safetensors_input = gr.Checkbox(
704706
# label='Convert to SafeTensors', value=True
File renamed without changes.
File renamed without changes.
File renamed without changes.

library/convert_model_gui.py

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
import gradio as gr
2+
from easygui import msgbox
3+
import subprocess
4+
import os
5+
import shutil
6+
from .common_gui import get_folder_path, get_file_path
7+
8+
folder_symbol = '\U0001f4c2' # 📂
9+
refresh_symbol = '\U0001f504' # 🔄
10+
save_style_symbol = '\U0001f4be' # 💾
11+
document_symbol = '\U0001F4C4' # 📄
12+
13+
def convert_model(source_model_input, source_model_type, target_model_folder_input, target_model_name_input, target_model_type, target_save_precision_type):
14+
# Check for caption_text_input
15+
if source_model_type == "":
16+
msgbox("Invalid source model type")
17+
return
18+
19+
# Check if source model exist
20+
if os.path.isfile(source_model_input):
21+
print('The provided source model is a file')
22+
elif os.path.isdir(source_model_input):
23+
print('The provided model is a folder')
24+
else:
25+
msgbox("The provided source model is neither a file nor a folder")
26+
return
27+
28+
# Check if source model exist
29+
if os.path.isdir(target_model_folder_input):
30+
print('The provided model folder exist')
31+
else:
32+
msgbox("The provided target folder does not exist")
33+
return
34+
35+
run_cmd = f'.\\venv\Scripts\python.exe "tools/convert_diffusers20_original_sd.py"'
36+
37+
v1_models = [
38+
'runwayml/stable-diffusion-v1-5',
39+
'CompVis/stable-diffusion-v1-4',
40+
]
41+
42+
# check if v1 models
43+
if str(source_model_type) in v1_models:
44+
print('SD v1 model specified. Setting --v1 parameter')
45+
run_cmd += ' --v1'
46+
else:
47+
print('SD v2 model specified. Setting --v2 parameter')
48+
run_cmd += ' --v2'
49+
50+
if not target_save_precision_type == 'unspecified':
51+
run_cmd += f' --{target_save_precision_type}'
52+
53+
if target_model_type == "diffuser":
54+
run_cmd += f' --reference_model="{source_model_type}"'
55+
56+
run_cmd += f' "{source_model_input}"'
57+
58+
if target_model_type == "diffuser":
59+
target_model_path = os.path.join(target_model_folder_input, target_model_name_input)
60+
run_cmd += f' "{target_model_path}"'
61+
else:
62+
target_model_path = os.path.join(target_model_folder_input, f'{target_model_name_input}.{target_model_type}')
63+
run_cmd += f' "{target_model_path}"'
64+
65+
print(run_cmd)
66+
67+
# Run the command
68+
subprocess.run(run_cmd)
69+
70+
if not target_model_type == "diffuser":
71+
72+
v2_models = ['stabilityai/stable-diffusion-2-1-base',
73+
'stabilityai/stable-diffusion-2-base',]
74+
v_parameterization =[
75+
'stabilityai/stable-diffusion-2-1',
76+
'stabilityai/stable-diffusion-2',]
77+
78+
if str(source_model_type) in v2_models:
79+
inference_file = os.path.join(target_model_folder_input, f'{target_model_name_input}.yaml')
80+
print(f'Saving v2-inference.yaml as {inference_file}')
81+
shutil.copy(
82+
f'./v2_inference/v2-inference.yaml',
83+
f'{inference_file}',
84+
)
85+
86+
if str(source_model_type) in v_parameterization:
87+
inference_file = os.path.join(target_model_folder_input, f'{target_model_name_input}.yaml')
88+
print(f'Saving v2-inference-v.yaml as {inference_file}')
89+
shutil.copy(
90+
f'./v2_inference/v2-inference-v.yaml',
91+
f'{inference_file}',
92+
)
93+
94+
# parser = argparse.ArgumentParser()
95+
# parser.add_argument("--v1", action='store_true',
96+
# help='load v1.x model (v1 or v2 is required to load checkpoint) / 1.xのモデルを読み込む')
97+
# parser.add_argument("--v2", action='store_true',
98+
# help='load v2.0 model (v1 or v2 is required to load checkpoint) / 2.0のモデルを読み込む')
99+
# parser.add_argument("--fp16", action='store_true',
100+
# help='load as fp16 (Diffusers only) and save as fp16 (checkpoint only) / fp16形式で読み込み(Diffusers形式のみ対応)、保存する(checkpointのみ対応)')
101+
# parser.add_argument("--bf16", action='store_true', help='save as bf16 (checkpoint only) / bf16形式で保存する(checkpointのみ対応)')
102+
# parser.add_argument("--float", action='store_true',
103+
# help='save as float (checkpoint only) / float(float32)形式で保存する(checkpointのみ対応)')
104+
# parser.add_argument("--epoch", type=int, default=0, help='epoch to write to checkpoint / checkpointに記録するepoch数の値')
105+
# parser.add_argument("--global_step", type=int, default=0,
106+
# help='global_step to write to checkpoint / checkpointに記録するglobal_stepの値')
107+
# parser.add_argument("--reference_model", type=str, default=None,
108+
# help="reference model for schduler/tokenizer, required in saving Diffusers, copy schduler/tokenizer from this / scheduler/tokenizerのコピー元のDiffusersモデル、Diffusers形式で保存するときに必要")
109+
110+
# parser.add_argument("model_to_load", type=str, default=None,
111+
# help="model to load: checkpoint file or Diffusers model's directory / 読み込むモデル、checkpointかDiffusers形式モデルのディレクトリ")
112+
# parser.add_argument("model_to_save", type=str, default=None,
113+
# help="model to save: checkpoint (with extension) or Diffusers model's directory (without extension) / 変換後のモデル、拡張子がある場合はcheckpoint、ない場合はDiffusesモデルとして保存")
114+
115+
116+
###
117+
# Gradio UI
118+
###
119+
120+
121+
def gradio_convert_model_tab():
122+
with gr.Tab('Convert model'):
123+
gr.Markdown(
124+
'This utility can be used to convert from one stable diffusion model format to another.'
125+
)
126+
with gr.Row():
127+
source_model_input = gr.Textbox(
128+
label='Source model',
129+
placeholder='path to source model folder of file to convert...',
130+
interactive=True,
131+
)
132+
button_source_model_dir = gr.Button(
133+
folder_symbol, elem_id='open_folder_small'
134+
)
135+
button_source_model_dir.click(
136+
get_folder_path, outputs=source_model_input
137+
)
138+
139+
button_source_model_file = gr.Button(
140+
document_symbol, elem_id='open_folder_small'
141+
)
142+
button_source_model_file.click(
143+
get_file_path, inputs=[source_model_input], outputs=source_model_input
144+
)
145+
146+
source_model_type = gr.Dropdown(label="Source model type", choices=[
147+
'stabilityai/stable-diffusion-2-1-base',
148+
'stabilityai/stable-diffusion-2-base',
149+
'stabilityai/stable-diffusion-2-1',
150+
'stabilityai/stable-diffusion-2',
151+
'runwayml/stable-diffusion-v1-5',
152+
'CompVis/stable-diffusion-v1-4',
153+
],)
154+
with gr.Row():
155+
target_model_folder_input = gr.Textbox(
156+
label='Target model folder',
157+
placeholder='path to target model folder of file name to create...',
158+
interactive=True,
159+
)
160+
button_target_model_folder = gr.Button(
161+
folder_symbol, elem_id='open_folder_small'
162+
)
163+
button_target_model_folder.click(
164+
get_folder_path, outputs=target_model_folder_input
165+
)
166+
167+
target_model_name_input = gr.Textbox(
168+
label='Target model name',
169+
placeholder='target model name...',
170+
interactive=True,
171+
)
172+
target_model_type = gr.Dropdown(label="Target model type", choices=[
173+
'diffuser',
174+
'ckpt',
175+
'safetensors',
176+
],)
177+
target_save_precision_type = gr.Dropdown(label="Target model precison", choices=[
178+
'unspecified',
179+
'fp16',
180+
'bf16',
181+
'float'
182+
], value='unspecified')
183+
184+
185+
convert_button = gr.Button('Convert model')
186+
187+
convert_button.click(
188+
convert_model,
189+
inputs=[source_model_input, source_model_type, target_model_folder_input, target_model_name_input, target_model_type, target_save_precision_type
190+
],
191+
)

dreambooth_gui/dataset_balancing.py renamed to library/dataset_balancing_gui.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ def gradio_dataset_balancing_tab():
102102

103103
total_repeats_number = gr.Number(
104104
value=1000,
105-
min=1,
106105
interactive=True,
107106
label='Training steps per concept per epoch',
108107
)
File renamed without changes.

0 commit comments

Comments
 (0)