Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,4 +4,522 @@ def greet(name):
|
|
4 |
return "Hello " + name + "!!"
|
5 |
|
6 |
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
7 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
return "Hello " + name + "!!"
|
5 |
|
6 |
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
7 |
+
iface.launch()
|
8 |
+
|
9 |
+
import os
|
10 |
+
import re
|
11 |
+
import toml
|
12 |
+
import shutil
|
13 |
+
import zipfile
|
14 |
+
from time import time
|
15 |
+
from IPython.display import Markdown, display
|
16 |
+
|
17 |
+
# These carry information from past executions
|
18 |
+
if "model_url" in globals():
|
19 |
+
old_model_url = model_url
|
20 |
+
else:
|
21 |
+
old_model_url = None
|
22 |
+
if "dependencies_installed" not in globals():
|
23 |
+
dependencies_installed = False
|
24 |
+
if "model_file" not in globals():
|
25 |
+
model_file = None
|
26 |
+
|
27 |
+
# These may be set by other cells, some are legacy
|
28 |
+
if "custom_dataset" not in globals():
|
29 |
+
custom_dataset = None
|
30 |
+
if "override_dataset_config_file" not in globals():
|
31 |
+
override_dataset_config_file = None
|
32 |
+
if "override_config_file" not in globals():
|
33 |
+
override_config_file = None
|
34 |
+
if "optimizer" not in globals():
|
35 |
+
optimizer = "AdamW8bit"
|
36 |
+
if "optimizer_args" not in globals():
|
37 |
+
optimizer_args = None
|
38 |
+
if "continue_from_lora" not in globals():
|
39 |
+
continue_from_lora = ""
|
40 |
+
if "weighted_captions" not in globals():
|
41 |
+
weighted_captions = False
|
42 |
+
if "adjust_tags" not in globals():
|
43 |
+
adjust_tags = False
|
44 |
+
if "keep_tokens_weight" not in globals():
|
45 |
+
keep_tokens_weight = 1.0
|
46 |
+
|
47 |
+
COLAB = True # low ram
|
48 |
+
COMMIT = "e6ad3cbc66130fdc3bf9ecd1e0272969b1d613f7"
|
49 |
+
BETTER_EPOCH_NAMES = True
|
50 |
+
LOAD_TRUNCATED_IMAGES = True
|
51 |
+
|
52 |
+
#@title ## 🚩 Start Here
|
53 |
+
|
54 |
+
#@markdown ### ▶️ Setup
|
55 |
+
#@markdown Your project name will be the same as the folder containing your images. Spaces aren't allowed.
|
56 |
+
project_name = "" #@param {type:"string"}
|
57 |
+
#@markdown The folder structure doesn't matter and is purely for comfort. Make sure to always pick the same one. I like organizing by project.
|
58 |
+
folder_structure = "Organize by project (MyDrive/Loras/project_name/dataset)" #@param ["Organize by category (MyDrive/lora_training/datasets/project_name)", "Organize by project (MyDrive/Loras/project_name/dataset)"]
|
59 |
+
#@markdown Decide the model that will be downloaded and used for training. These options should produce clean and consistent results. You can also choose your own by pasting its download link.
|
60 |
+
training_model = "AnyLora (AnyLoRA_noVae_fp16-pruned.ckpt)" #@param ["Anime (animefull-final-pruned-fp16.safetensors)", "AnyLora (AnyLoRA_noVae_fp16-pruned.ckpt)", "Stable Diffusion (sd-v1-5-pruned-noema-fp16.safetensors)"]
|
61 |
+
optional_custom_training_model_url = "" #@param {type:"string"}
|
62 |
+
custom_model_is_based_on_sd2 = False #@param {type:"boolean"}
|
63 |
+
|
64 |
+
if optional_custom_training_model_url:
|
65 |
+
model_url = optional_custom_training_model_url
|
66 |
+
elif "AnyLora" in training_model:
|
67 |
+
model_url = "https://huggingface.co/Lykon/AnyLoRA/resolve/main/AnyLoRA_noVae_fp16-pruned.ckpt"
|
68 |
+
elif "Anime" in training_model:
|
69 |
+
model_url = "https://huggingface.co/hollowstrawberry/stable-diffusion-guide/resolve/main/models/animefull-final-pruned-fp16.safetensors"
|
70 |
+
else:
|
71 |
+
model_url = "https://huggingface.co/hollowstrawberry/stable-diffusion-guide/resolve/main/models/sd-v1-5-pruned-noema-fp16.safetensors"
|
72 |
+
|
73 |
+
#@markdown ### ▶️ Processing
|
74 |
+
#@markdown Resolution of 512 is standard for Stable Diffusion 1.5. Higher resolution training is much slower but can lead to better details. <p>
|
75 |
+
#@markdown Images will be automatically scaled while training to produce the best results, so you don't need to crop or resize anything yourself.
|
76 |
+
resolution = 1024 #@param {type:"slider", min:512, max:1024, step:128}
|
77 |
+
#@markdown This option will train your images both normally and flipped, for no extra cost, to learn more from them. Turn it on specially if you have less than 20 images. <p>
|
78 |
+
#@markdown **Turn it off if you care about asymmetrical elements in your Lora**.
|
79 |
+
flip_aug = False #@param {type:"boolean"}
|
80 |
+
#markdown Leave empty for no captions.
|
81 |
+
caption_extension = ".txt" #param {type:"string"}
|
82 |
+
#@markdown Shuffling anime tags in place improves learning and prompting. An activation tag goes at the start of every text file and will not be shuffled.
|
83 |
+
shuffle_tags = True #@param {type:"boolean"}
|
84 |
+
shuffle_caption = shuffle_tags
|
85 |
+
activation_tags = "1" #@param [0,1,2,3]
|
86 |
+
keep_tokens = int(activation_tags)
|
87 |
+
|
88 |
+
#@markdown ### ▶️ Steps <p>
|
89 |
+
#@markdown Your images will repeat this number of times during training. I recommend that your images multiplied by their repeats is between 200 and 400.
|
90 |
+
num_repeats = 10 #@param {type:"number"}
|
91 |
+
#@markdown Choose how long you want to train for. A good starting point is around 10 epochs or around 2000 steps.<p>
|
92 |
+
#@markdown One epoch is a number of steps equal to: your number of images multiplied by their repeats, divided by batch size. <p>
|
93 |
+
preferred_unit = "Epochs" #@param ["Epochs", "Steps"]
|
94 |
+
how_many = 10 #@param {type:"number"}
|
95 |
+
max_train_epochs = how_many if preferred_unit == "Epochs" else None
|
96 |
+
max_train_steps = how_many if preferred_unit == "Steps" else None
|
97 |
+
#@markdown Saving more epochs will let you compare your Lora's progress better.
|
98 |
+
save_every_n_epochs = 1 #@param {type:"number"}
|
99 |
+
keep_only_last_n_epochs = 10 #@param {type:"number"}
|
100 |
+
if not save_every_n_epochs:
|
101 |
+
save_every_n_epochs = max_train_epochs
|
102 |
+
if not keep_only_last_n_epochs:
|
103 |
+
keep_only_last_n_epochs = max_train_epochs
|
104 |
+
#@markdown Increasing the batch size makes training faster, but may make learning worse. Recommended 2 or 3.
|
105 |
+
train_batch_size = 2 #@param {type:"slider", min:1, max:8, step:1}
|
106 |
+
|
107 |
+
#@markdown ### ▶️ Learning
|
108 |
+
#@markdown The learning rate is the most important for your results. If you want to train slower with lots of images, or if your dim and alpha are high, move the unet to 2e-4 or lower. <p>
|
109 |
+
#@markdown The text encoder helps your Lora learn concepts slightly better. It is recommended to make it half or a fifth of the unet. If you're training a style you can even set it to 0.
|
110 |
+
unet_lr = 3e-4 #@param {type:"number"}
|
111 |
+
text_encoder_lr = 1e-4 #@param {type:"number"}
|
112 |
+
#@markdown The scheduler is the algorithm that guides the learning rate. If you're not sure, pick `constant` and ignore the number. I personally recommend `cosine_with_restarts` with 3 restarts.
|
113 |
+
lr_scheduler = "cosine_with_restarts" #@param ["constant", "cosine", "cosine_with_restarts", "constant_with_warmup", "linear", "polynomial"]
|
114 |
+
lr_scheduler_number = 3 #@param {type:"number"}
|
115 |
+
lr_scheduler_num_cycles = lr_scheduler_number if lr_scheduler == "cosine_with_restarts" else 0
|
116 |
+
lr_scheduler_power = lr_scheduler_number if lr_scheduler == "polynomial" else 0
|
117 |
+
#@markdown Steps spent "warming up" the learning rate during training for efficiency. I recommend leaving it at 5%.
|
118 |
+
lr_warmup_ratio = 0.05 #@param {type:"slider", min:0.0, max:0.5, step:0.01}
|
119 |
+
lr_warmup_steps = 0
|
120 |
+
#@markdown New feature that adjusts loss over time, makes learning much more efficient, and training can be done with about half as many epochs. Uses a value of 5.0 as recommended by [the paper](https://arxiv.org/abs/2303.09556).
|
121 |
+
min_snr_gamma = True #@param {type:"boolean"}
|
122 |
+
min_snr_gamma_value = 5.0 if min_snr_gamma else None
|
123 |
+
|
124 |
+
#@markdown ### ▶️ Structure
|
125 |
+
#@markdown LoRA is the classic type, while LoCon is good with styles. Lycoris require [this extension](https://github.com/KohakuBlueleaf/a1111-sd-webui-lycoris) for webui to work like normal loras. More info [here](https://github.com/KohakuBlueleaf/Lycoris).
|
126 |
+
lora_type = "LoRA" #@param ["LoRA", "LoCon Lycoris", "LoHa Lycoris"]
|
127 |
+
|
128 |
+
#@markdown Below are some recommended values for the following settings:
|
129 |
+
|
130 |
+
#@markdown | type | network_dim | network_alpha | conv_dim | conv_alpha |
|
131 |
+
#@markdown | :---: | :---: | :---: | :---: | :---: |
|
132 |
+
#@markdown | LoRA | 32 | 16 | | |
|
133 |
+
#@markdown | LoCon | 16 | 8 | 8 | 1 |
|
134 |
+
#@markdown | LoHa | 8 | 4 | 4 | 1 |
|
135 |
+
|
136 |
+
#@markdown More dim means larger Lora, it can hold more information but more isn't always better. A dim between 8-32 is recommended, and alpha equal to half the dim.
|
137 |
+
network_dim = 32 #@param {type:"slider", min:1, max:128, step:1}
|
138 |
+
network_alpha = 16 #@param {type:"slider", min:1, max:128, step:1}
|
139 |
+
#@markdown The following values don't affect LoRA. They work like dim/alpha but only for the additional learning layers of Lycoris.
|
140 |
+
conv_dim = 8 #@param {type:"slider", min:1, max:64, step:1}
|
141 |
+
conv_alpha = 1 #@param {type:"slider", min:1, max:64, step:1}
|
142 |
+
conv_compression = False #@param {type:"boolean"}
|
143 |
+
|
144 |
+
network_module = "lycoris.kohya" if "Lycoris" in lora_type else "networks.lora"
|
145 |
+
network_args = None if lora_type == "LoRA" else [
|
146 |
+
f"conv_dim={conv_dim}",
|
147 |
+
f"conv_alpha={conv_alpha}",
|
148 |
+
]
|
149 |
+
if "Lycoris" in lora_type:
|
150 |
+
network_args.append(f"algo={'loha' if 'LoHa' in lora_type else 'lora'}")
|
151 |
+
network_args.append(f"disable_conv_cp={str(not conv_compression)}")
|
152 |
+
|
153 |
+
#markdown ### ▶️ Experimental
|
154 |
+
#markdown Save additional data equaling ~1 GB allowing you to resume training later.
|
155 |
+
save_state = False #param {type:"boolean"}
|
156 |
+
#markdown Resume training if a save state is found.
|
157 |
+
resume = False #param {type:"boolean"}
|
158 |
+
|
159 |
+
#@markdown ### ▶️ Ready
|
160 |
+
#@markdown You can now run this cell to cook your Lora. Good luck! <p>
|
161 |
+
|
162 |
+
|
163 |
+
# 👩💻 Cool code goes here
|
164 |
+
|
165 |
+
if optimizer == "DAdaptation":
|
166 |
+
optimizer_args = ["decouple=True","weight_decay=0.02","betas=[0.9,0.99]"]
|
167 |
+
unet_lr = 0.5
|
168 |
+
text_encoder_lr = 0.5
|
169 |
+
lr_scheduler = "constant_with_warmup"
|
170 |
+
network_alpha = network_dim
|
171 |
+
|
172 |
+
root_dir = "/content" if COLAB else "~/Loras"
|
173 |
+
deps_dir = os.path.join(root_dir, "deps")
|
174 |
+
repo_dir = os.path.join(root_dir, "kohya-trainer")
|
175 |
+
|
176 |
+
if "/Loras" in folder_structure:
|
177 |
+
main_dir = os.path.join(root_dir, "drive/MyDrive/Loras") if COLAB else root_dir
|
178 |
+
log_folder = os.path.join(main_dir, "_logs")
|
179 |
+
config_folder = os.path.join(main_dir, project_name)
|
180 |
+
images_folder = os.path.join(main_dir, project_name, "dataset")
|
181 |
+
output_folder = os.path.join(main_dir, project_name, "output")
|
182 |
+
else:
|
183 |
+
main_dir = os.path.join(root_dir, "drive/MyDrive/lora_training") if COLAB else root_dir
|
184 |
+
images_folder = os.path.join(main_dir, "datasets", project_name)
|
185 |
+
output_folder = os.path.join(main_dir, "output", project_name)
|
186 |
+
config_folder = os.path.join(main_dir, "config", project_name)
|
187 |
+
log_folder = os.path.join(main_dir, "log")
|
188 |
+
|
189 |
+
config_file = os.path.join(config_folder, "training_config.toml")
|
190 |
+
dataset_config_file = os.path.join(config_folder, "dataset_config.toml")
|
191 |
+
accelerate_config_file = os.path.join(repo_dir, "accelerate_config/config.yaml")
|
192 |
+
|
193 |
+
def clone_repo():
|
194 |
+
os.chdir(root_dir)
|
195 |
+
!git clone https://github.com/kohya-ss/sd-scripts {repo_dir}
|
196 |
+
os.chdir(repo_dir)
|
197 |
+
if COMMIT:
|
198 |
+
!git reset --hard {COMMIT}
|
199 |
+
!wget https://raw.githubusercontent.com/hollowstrawberry/kohya-colab/main/requirements.txt -q -O requirements.txt
|
200 |
+
|
201 |
+
def install_dependencies():
|
202 |
+
clone_repo()
|
203 |
+
!apt -y update -qq
|
204 |
+
!apt -y install aria2 -qq
|
205 |
+
!pip -q install --upgrade -r requirements.txt --extra-index-url https://download.pytorch.org/whl/cu118
|
206 |
+
|
207 |
+
# patch kohya for minor stuff
|
208 |
+
if COLAB:
|
209 |
+
!sed -i "s@cpu@cuda@" library/model_util.py # low ram
|
210 |
+
if LOAD_TRUNCATED_IMAGES:
|
211 |
+
!sed -i 's/from PIL import Image/from PIL import Image, ImageFile\nImageFile.LOAD_TRUNCATED_IMAGES=True/g' library/train_util.py # fix truncated jpegs error
|
212 |
+
if BETTER_EPOCH_NAMES:
|
213 |
+
!sed -i 's/{:06d}/{:02d}/g' library/train_util.py # make epoch names shorter
|
214 |
+
!sed -i 's/"." + args.save_model_as)/"-{:02d}.".format(num_train_epochs) + args.save_model_as)/g' train_network.py # name of the last epoch will match the rest
|
215 |
+
|
216 |
+
from accelerate.utils import write_basic_config
|
217 |
+
if not os.path.exists(accelerate_config_file):
|
218 |
+
write_basic_config(save_location=accelerate_config_file)
|
219 |
+
|
220 |
+
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
|
221 |
+
os.environ["BITSANDBYTES_NOWELCOME"] = "1"
|
222 |
+
os.environ["SAFETENSORS_FAST_GPU"] = "1"
|
223 |
+
|
224 |
+
def validate_dataset():
|
225 |
+
global lr_warmup_steps, lr_warmup_ratio, caption_extension, keep_tokens, keep_tokens_weight, weighted_captions, adjust_tags
|
226 |
+
supported_types = (".png", ".jpg", ".jpeg", ".webp", ".bmp")
|
227 |
+
|
228 |
+
print("\n💿 Checking dataset...")
|
229 |
+
if not project_name.strip() or any(c in project_name for c in " .()\"'\\/"):
|
230 |
+
print("💥 Error: Please choose a valid project name.")
|
231 |
+
return
|
232 |
+
|
233 |
+
if custom_dataset:
|
234 |
+
try:
|
235 |
+
datconf = toml.loads(custom_dataset)
|
236 |
+
datasets = [d for d in datconf["datasets"][0]["subsets"]]
|
237 |
+
except:
|
238 |
+
print(f"💥 Error: Your custom dataset is invalid or contains an error! Please check the original template.")
|
239 |
+
return
|
240 |
+
reg = [d for d in datasets if d.get("is_reg", False)]
|
241 |
+
for r in reg:
|
242 |
+
print("📁"+r["image_dir"].replace("/content/drive/", "") + " (Regularization)")
|
243 |
+
datasets = [d for d in datasets if d not in reg]
|
244 |
+
datasets_dict = {d["image_dir"]: d["num_repeats"] for d in datasets}
|
245 |
+
folders = datasets_dict.keys()
|
246 |
+
files = [f for folder in folders for f in os.listdir(folder)]
|
247 |
+
images_repeats = {folder: (len([f for f in os.listdir(folder) if f.lower().endswith(supported_types)]), datasets_dict[folder]) for folder in folders}
|
248 |
+
else:
|
249 |
+
folders = [images_folder]
|
250 |
+
files = os.listdir(images_folder)
|
251 |
+
images_repeats = {images_folder: (len([f for f in files if f.lower().endswith(supported_types)]), num_repeats)}
|
252 |
+
|
253 |
+
for folder in folders:
|
254 |
+
if not os.path.exists(folder):
|
255 |
+
print(f"💥 Error: The folder {folder.replace('/content/drive/', '')} doesn't exist.")
|
256 |
+
return
|
257 |
+
for folder, (img, rep) in images_repeats.items():
|
258 |
+
if not img:
|
259 |
+
print(f"💥 Error: Your {folder.replace('/content/drive/', '')} folder is empty.")
|
260 |
+
return
|
261 |
+
for f in files:
|
262 |
+
if not f.lower().endswith(".txt") and not f.lower().endswith(supported_types):
|
263 |
+
print(f"💥 Error: Invalid file in dataset: \"{f}\". Aborting.")
|
264 |
+
return
|
265 |
+
|
266 |
+
if not [txt for txt in files if txt.lower().endswith(".txt")]:
|
267 |
+
caption_extension = ""
|
268 |
+
if continue_from_lora and not (continue_from_lora.endswith(".safetensors") and os.path.exists(continue_from_lora)):
|
269 |
+
print(f"💥 Error: Invalid path to existing Lora. Example: /content/drive/MyDrive/Loras/example.safetensors")
|
270 |
+
return
|
271 |
+
|
272 |
+
pre_steps_per_epoch = sum(img*rep for (img, rep) in images_repeats.values())
|
273 |
+
steps_per_epoch = pre_steps_per_epoch/train_batch_size
|
274 |
+
total_steps = max_train_steps or int(max_train_epochs*steps_per_epoch)
|
275 |
+
estimated_epochs = int(total_steps/steps_per_epoch)
|
276 |
+
lr_warmup_steps = int(total_steps*lr_warmup_ratio)
|
277 |
+
|
278 |
+
for folder, (img, rep) in images_repeats.items():
|
279 |
+
print("📁"+folder.replace("/content/drive/", ""))
|
280 |
+
print(f"📈 Found {img} images with {rep} repeats, equaling {img*rep} steps.")
|
281 |
+
print(f"📉 Divide {pre_steps_per_epoch} steps by {train_batch_size} batch size to get {steps_per_epoch} steps per epoch.")
|
282 |
+
if max_train_epochs:
|
283 |
+
print(f"🔮 There will be {max_train_epochs} epochs, for around {total_steps} total training steps.")
|
284 |
+
else:
|
285 |
+
print(f"🔮 There will be {total_steps} steps, divided into {estimated_epochs} epochs and then some.")
|
286 |
+
|
287 |
+
if total_steps > 10000:
|
288 |
+
print("💥 Error: Your total steps are too high. You probably made a mistake. Aborting...")
|
289 |
+
return
|
290 |
+
|
291 |
+
if adjust_tags:
|
292 |
+
print(f"\n📎 Weighted tags: {'ON' if weighted_captions else 'OFF'}")
|
293 |
+
if weighted_captions:
|
294 |
+
print(f"📎 Will use {keep_tokens_weight} weight on {keep_tokens} activation tag(s)")
|
295 |
+
print("📎 Adjusting tags...")
|
296 |
+
adjust_weighted_tags(folders, keep_tokens, keep_tokens_weight, weighted_captions)
|
297 |
+
|
298 |
+
return True
|
299 |
+
|
300 |
+
def adjust_weighted_tags(folders, keep_tokens: int, keep_tokens_weight: float, weighted_captions: bool):
|
301 |
+
weighted_tag = re.compile(r"\((.+?):[.\d]+\)(,|$)")
|
302 |
+
for folder in folders:
|
303 |
+
for txt in [f for f in os.listdir(folder) if f.lower().endswith(".txt")]:
|
304 |
+
with open(os.path.join(folder, txt), 'r') as f:
|
305 |
+
content = f.read()
|
306 |
+
# reset previous changes
|
307 |
+
content = content.replace('\\', '')
|
308 |
+
content = weighted_tag.sub(r'\1\2', content)
|
309 |
+
if weighted_captions:
|
310 |
+
# re-apply changes
|
311 |
+
content = content.replace(r'(', r'\(').replace(r')', r'\)').replace(r':', r'\:')
|
312 |
+
if keep_tokens_weight > 1:
|
313 |
+
tags = [s.strip() for s in content.split(",")]
|
314 |
+
for i in range(min(keep_tokens, len(tags))):
|
315 |
+
tags[i] = f'({tags[i]}:{keep_tokens_weight})'
|
316 |
+
content = ", ".join(tags)
|
317 |
+
with open(os.path.join(folder, txt), 'w') as f:
|
318 |
+
f.write(content)
|
319 |
+
|
320 |
+
def create_config():
|
321 |
+
global dataset_config_file, config_file, model_file
|
322 |
+
|
323 |
+
if resume:
|
324 |
+
resume_points = [f.path for f in os.scandir(output_folder) if f.is_dir()]
|
325 |
+
resume_points.sort()
|
326 |
+
last_resume_point = resume_points[-1] if resume_points else None
|
327 |
+
else:
|
328 |
+
last_resume_point = None
|
329 |
+
|
330 |
+
if override_config_file:
|
331 |
+
config_file = override_config_file
|
332 |
+
print(f"\n⭕ Using custom config file {config_file}")
|
333 |
+
else:
|
334 |
+
config_dict = {
|
335 |
+
"additional_network_arguments": {
|
336 |
+
"unet_lr": unet_lr,
|
337 |
+
"text_encoder_lr": text_encoder_lr,
|
338 |
+
"network_dim": network_dim,
|
339 |
+
"network_alpha": network_alpha,
|
340 |
+
"network_module": network_module,
|
341 |
+
"network_args": network_args,
|
342 |
+
"network_train_unet_only": True if text_encoder_lr == 0 else None,
|
343 |
+
"network_weights": continue_from_lora if continue_from_lora else None
|
344 |
+
},
|
345 |
+
"optimizer_arguments": {
|
346 |
+
"learning_rate": unet_lr,
|
347 |
+
"lr_scheduler": lr_scheduler,
|
348 |
+
"lr_scheduler_num_cycles": lr_scheduler_num_cycles if lr_scheduler == "cosine_with_restarts" else None,
|
349 |
+
"lr_scheduler_power": lr_scheduler_power if lr_scheduler == "polynomial" else None,
|
350 |
+
"lr_warmup_steps": lr_warmup_steps if lr_scheduler != "constant" else None,
|
351 |
+
"optimizer_type": optimizer,
|
352 |
+
"optimizer_args": optimizer_args if optimizer_args else None,
|
353 |
+
},
|
354 |
+
"training_arguments": {
|
355 |
+
"max_train_steps": max_train_steps,
|
356 |
+
"max_train_epochs": max_train_epochs,
|
357 |
+
"save_every_n_epochs": save_every_n_epochs,
|
358 |
+
"save_last_n_epochs": keep_only_last_n_epochs,
|
359 |
+
"train_batch_size": train_batch_size,
|
360 |
+
"noise_offset": None,
|
361 |
+
"clip_skip": 2,
|
362 |
+
"min_snr_gamma": min_snr_gamma_value,
|
363 |
+
"weighted_captions": weighted_captions,
|
364 |
+
"seed": 42,
|
365 |
+
"max_token_length": 225,
|
366 |
+
"xformers": True,
|
367 |
+
"lowram": COLAB,
|
368 |
+
"max_data_loader_n_workers": 8,
|
369 |
+
"persistent_data_loader_workers": True,
|
370 |
+
"save_precision": "fp16",
|
371 |
+
"mixed_precision": "fp16",
|
372 |
+
"output_dir": output_folder,
|
373 |
+
"logging_dir": log_folder,
|
374 |
+
"output_name": project_name,
|
375 |
+
"log_prefix": project_name,
|
376 |
+
"save_state": save_state,
|
377 |
+
"save_last_n_epochs_state": 1 if save_state else None,
|
378 |
+
"resume": last_resume_point
|
379 |
+
},
|
380 |
+
"model_arguments": {
|
381 |
+
"pretrained_model_name_or_path": model_file,
|
382 |
+
"v2": custom_model_is_based_on_sd2,
|
383 |
+
"v_parameterization": True if custom_model_is_based_on_sd2 else None,
|
384 |
+
},
|
385 |
+
"saving_arguments": {
|
386 |
+
"save_model_as": "safetensors",
|
387 |
+
},
|
388 |
+
"dreambooth_arguments": {
|
389 |
+
"prior_loss_weight": 1.0,
|
390 |
+
},
|
391 |
+
"dataset_arguments": {
|
392 |
+
"cache_latents": True,
|
393 |
+
},
|
394 |
+
}
|
395 |
+
|
396 |
+
for key in config_dict:
|
397 |
+
if isinstance(config_dict[key], dict):
|
398 |
+
config_dict[key] = {k: v for k, v in config_dict[key].items() if v is not None}
|
399 |
+
|
400 |
+
with open(config_file, "w") as f:
|
401 |
+
f.write(toml.dumps(config_dict))
|
402 |
+
print(f"\n📄 Config saved to {config_file}")
|
403 |
+
|
404 |
+
if override_dataset_config_file:
|
405 |
+
dataset_config_file = override_dataset_config_file
|
406 |
+
print(f"⭕ Using custom dataset config file {dataset_config_file}")
|
407 |
+
else:
|
408 |
+
dataset_config_dict = {
|
409 |
+
"general": {
|
410 |
+
"resolution": resolution,
|
411 |
+
"shuffle_caption": shuffle_caption,
|
412 |
+
"keep_tokens": keep_tokens,
|
413 |
+
"flip_aug": flip_aug,
|
414 |
+
"caption_extension": caption_extension,
|
415 |
+
"enable_bucket": True,
|
416 |
+
"bucket_reso_steps": 64,
|
417 |
+
"bucket_no_upscale": False,
|
418 |
+
"min_bucket_reso": 320 if resolution > 640 else 256,
|
419 |
+
"max_bucket_reso": 1280 if resolution > 640 else 1024,
|
420 |
+
},
|
421 |
+
"datasets": toml.loads(custom_dataset)["datasets"] if custom_dataset else [
|
422 |
+
{
|
423 |
+
"subsets": [
|
424 |
+
{
|
425 |
+
"num_repeats": num_repeats,
|
426 |
+
"image_dir": images_folder,
|
427 |
+
"class_tokens": None if caption_extension else project_name
|
428 |
+
}
|
429 |
+
]
|
430 |
+
}
|
431 |
+
]
|
432 |
+
}
|
433 |
+
|
434 |
+
for key in dataset_config_dict:
|
435 |
+
if isinstance(dataset_config_dict[key], dict):
|
436 |
+
dataset_config_dict[key] = {k: v for k, v in dataset_config_dict[key].items() if v is not None}
|
437 |
+
|
438 |
+
with open(dataset_config_file, "w") as f:
|
439 |
+
f.write(toml.dumps(dataset_config_dict))
|
440 |
+
print(f"📄 Dataset config saved to {dataset_config_file}")
|
441 |
+
|
442 |
+
def download_model():
|
443 |
+
global old_model_url, model_url, model_file
|
444 |
+
real_model_url = model_url.strip()
|
445 |
+
|
446 |
+
if real_model_url.lower().endswith((".ckpt", ".safetensors")):
|
447 |
+
model_file = f"/content{real_model_url[real_model_url.rfind('/'):]}"
|
448 |
+
else:
|
449 |
+
model_file = "/content/downloaded_model.safetensors"
|
450 |
+
if os.path.exists(model_file):
|
451 |
+
!rm "{model_file}"
|
452 |
+
|
453 |
+
if m := re.search(r"(?:https?://)?(?:www\.)?huggingface\.co/[^/]+/[^/]+/blob", model_url):
|
454 |
+
real_model_url = real_model_url.replace("blob", "resolve")
|
455 |
+
elif m := re.search(r"(?:https?://)?(?:www\.)?civitai\.com/models/([0-9]+)", model_url):
|
456 |
+
real_model_url = f"https://civitai.com/api/download/models/{m.group(1)}"
|
457 |
+
|
458 |
+
!aria2c "{real_model_url}" --console-log-level=warn -c -s 16 -x 16 -k 10M -d / -o "{model_file}"
|
459 |
+
|
460 |
+
if model_file.lower().endswith(".safetensors"):
|
461 |
+
from safetensors.torch import load_file as load_safetensors
|
462 |
+
try:
|
463 |
+
test = load_safetensors(model_file)
|
464 |
+
del test
|
465 |
+
except Exception as e:
|
466 |
+
#if "HeaderTooLarge" in str(e):
|
467 |
+
new_model_file = os.path.splitext(model_file)[0]+".ckpt"
|
468 |
+
!mv "{model_file}" "{new_model_file}"
|
469 |
+
model_file = new_model_file
|
470 |
+
print(f"Renamed model to {os.path.splitext(model_file)[0]}.ckpt")
|
471 |
+
|
472 |
+
if model_file.lower().endswith(".ckpt"):
|
473 |
+
from torch import load as load_ckpt
|
474 |
+
try:
|
475 |
+
test = load_ckpt(model_file)
|
476 |
+
del test
|
477 |
+
except Exception as e:
|
478 |
+
return False
|
479 |
+
|
480 |
+
return True
|
481 |
+
|
482 |
+
def main():
|
483 |
+
global dependencies_installed
|
484 |
+
|
485 |
+
if COLAB and not os.path.exists('/content/drive'):
|
486 |
+
from google.colab import drive
|
487 |
+
print("📂 Connecting to Google Drive...")
|
488 |
+
drive.mount('/content/drive')
|
489 |
+
|
490 |
+
for dir in (main_dir, deps_dir, repo_dir, log_folder, images_folder, output_folder, config_folder):
|
491 |
+
os.makedirs(dir, exist_ok=True)
|
492 |
+
|
493 |
+
if not validate_dataset():
|
494 |
+
return
|
495 |
+
|
496 |
+
if not dependencies_installed:
|
497 |
+
print("\n🏭 Installing dependencies...\n")
|
498 |
+
t0 = time()
|
499 |
+
install_dependencies()
|
500 |
+
t1 = time()
|
501 |
+
dependencies_installed = True
|
502 |
+
print(f"\n✅ Installation finished in {int(t1-t0)} seconds.")
|
503 |
+
else:
|
504 |
+
print("\n✅ Dependencies already installed.")
|
505 |
+
|
506 |
+
if old_model_url != model_url or not model_file or not os.path.exists(model_file):
|
507 |
+
print("\n🔄 Downloading model...")
|
508 |
+
if not download_model():
|
509 |
+
print("\n💥 Error: The model you selected is invalid or corrupted, or couldn't be downloaded. You can use a civitai or huggingface link, or any direct download link.")
|
510 |
+
return
|
511 |
+
print()
|
512 |
+
else:
|
513 |
+
print("\n🔄 Model already downloaded.\n")
|
514 |
+
|
515 |
+
create_config()
|
516 |
+
|
517 |
+
print("\n⭐ Starting trainer...\n")
|
518 |
+
os.chdir(repo_dir)
|
519 |
+
|
520 |
+
!accelerate launch --config_file={accelerate_config_file} --num_cpu_threads_per_process=1 train_network.py --dataset_config={dataset_config_file} --config_file={config_file}
|
521 |
+
|
522 |
+
if not get_ipython().__dict__['user_ns']['_exit_code']:
|
523 |
+
display(Markdown("### ✅ Done! [Go download your Lora(s) from Google Drive](https://drive.google.com/drive/my-drive)"))
|
524 |
+
|
525 |
+
main()
|