|
|
|
"""kohya-LoRA-dreambooth-latest.ipynb |
|
|
|
Automatically generated by Colaboratory. |
|
|
|
Original file is located at |
|
https://colab.research.google.com/drive/1V6iv4_OdHivIrJlEqjsNPjeRai_pM9ei |
|
|
|
![visitors](https://visitor-badge.glitch.me/badge?page_id=linaqruf.lora-dreambooth) [![](https://dcbadge.vercel.app/api/shield/850007095775723532?style=flat)](https://lookup.guru/850007095775723532) [![ko-fi](https://img.shields.io/badge/Support%20me%20on%20Ko--fi-F16061?logo=ko-fi&logoColor=white&style=flat)](https://ko-fi.com/linaqruf) <a href="https://saweria.co/linaqruf"><img alt="Saweria" src="https://img.shields.io/badge/Saweria-7B3F00?style=flat&logo=ko-fi&logoColor=white"/></a> |
|
|
|
# **Kohya LoRA Dreambooth** |
|
A Colab Notebook For LoRA Training (Dreambooth Method) |
|
|
|
| Notebook Name | Description | Link | V14 | |
|
| --- | --- | --- | --- | |
|
| [Kohya LoRA Dreambooth](https://github.com/Linaqruf/kohya-trainer/blob/main/kohya-LoRA-dreambooth.ipynb) | LoRA Training (Dreambooth method) | [![](https://img.shields.io/static/v1?message=Open%20in%20Colab&logo=googlecolab&labelColor=5c5c5c&color=0f80c1&label=%20&style=flat)](https://colab.research.google.com/github/Linaqruf/kohya-trainer/blob/main/kohya-LoRA-dreambooth.ipynb) | [![](https://img.shields.io/static/v1?message=Older%20Version&logo=googlecolab&labelColor=5c5c5c&color=e74c3c&label=%20&style=flat)](https://colab.research.google.com/github/Linaqruf/kohya-trainer/blob/ff701379c65380c967cd956e4e9e8f6349563878/kohya-LoRA-dreambooth.ipynb) | |
|
| [Kohya LoRA Fine-Tuning](https://github.com/Linaqruf/kohya-trainer/blob/main/kohya-LoRA-finetuner.ipynb) | LoRA Training (Fine-tune method) | [![](https://img.shields.io/static/v1?message=Open%20in%20Colab&logo=googlecolab&labelColor=5c5c5c&color=0f80c1&label=%20&style=flat)](https://colab.research.google.com/github/Linaqruf/kohya-trainer/blob/main/kohya-LoRA-finetuner.ipynb) | [![](https://img.shields.io/static/v1?message=Older%20Version&logo=googlecolab&labelColor=5c5c5c&color=e74c3c&label=%20&style=flat)](https://colab.research.google.com/github/Linaqruf/kohya-trainer/blob/ff701379c65380c967cd956e4e9e8f6349563878/kohya-LoRA-finetuner.ipynb) | |
|
| [Kohya Trainer](https://github.com/Linaqruf/kohya-trainer/blob/main/kohya-trainer.ipynb) | Native Training | [![](https://img.shields.io/static/v1?message=Open%20in%20Colab&logo=googlecolab&labelColor=5c5c5c&color=0f80c1&label=%20&style=flat)](https://colab.research.google.com/github/Linaqruf/kohya-trainer/blob/main/kohya-trainer.ipynb) | [![](https://img.shields.io/static/v1?message=Older%20Version&logo=googlecolab&labelColor=5c5c5c&color=e74c3c&label=%20&style=flat)](https://colab.research.google.com/github/Linaqruf/kohya-trainer/blob/ff701379c65380c967cd956e4e9e8f6349563878/kohya-trainer.ipynb) | |
|
| [Kohya Dreambooth](https://github.com/Linaqruf/kohya-trainer/blob/main/kohya-dreambooth.ipynb) | Dreambooth Training | [![](https://img.shields.io/static/v1?message=Open%20in%20Colab&logo=googlecolab&labelColor=5c5c5c&color=0f80c1&label=%20&style=flat)](https://colab.research.google.com/github/Linaqruf/kohya-trainer/blob/main/kohya-dreambooth.ipynb) | [![](https://img.shields.io/static/v1?message=Older%20Version&logo=googlecolab&labelColor=5c5c5c&color=e74c3c&label=%20&style=flat)](https://colab.research.google.com/github/Linaqruf/kohya-trainer/blob/ff701379c65380c967cd956e4e9e8f6349563878/kohya-dreambooth.ipynb) | |
|
| [Cagliostro Colab UI](https://github.com/Linaqruf/sd-notebook-collection/blob/main/cagliostro-colab-ui.ipynb) `NEW`| A Customizable Stable Diffusion Web UI| [![](https://img.shields.io/static/v1?message=Open%20in%20Colab&logo=googlecolab&labelColor=5c5c5c&color=0f80c1&label=%20&style=flat)](https://colab.research.google.com/github/Linaqruf/sd-notebook-collection/blob/main/cagliostro-colab-ui.ipynb) | |
|
|
|
# I. Install Kohya Trainer |
|
""" |
|
|
|
|
|
|
|
|
|
import os |
|
import zipfile |
|
import shutil |
|
import time |
|
from subprocess import getoutput |
|
from IPython.utils import capture |
|
from google.colab import drive |
|
|
|
|
|
|
|
|
|
root_dir = "/content" |
|
deps_dir = os.path.join(root_dir, "deps") |
|
repo_dir = os.path.join(root_dir, "kohya-trainer") |
|
training_dir = os.path.join(root_dir, "LoRA") |
|
pretrained_model = os.path.join(root_dir, "pretrained_model") |
|
vae_dir = os.path.join(root_dir, "vae") |
|
config_dir = os.path.join(training_dir, "config") |
|
|
|
|
|
accelerate_config = os.path.join(repo_dir, "accelerate_config/config.yaml") |
|
tools_dir = os.path.join(repo_dir, "tools") |
|
finetune_dir = os.path.join(repo_dir, "finetune") |
|
|
|
for store in [ |
|
"root_dir", |
|
"deps_dir", |
|
"repo_dir", |
|
"training_dir", |
|
"pretrained_model", |
|
"vae_dir", |
|
"accelerate_config", |
|
"tools_dir", |
|
"finetune_dir", |
|
"config_dir", |
|
]: |
|
with capture.capture_output() as cap: |
|
|
|
del cap |
|
|
|
repo_url = "https://github.com/Linaqruf/kohya-trainer" |
|
bitsandytes_main_py = "/usr/local/lib/python3.10/dist-packages/bitsandbytes/cuda_setup/main.py" |
|
branch = "" |
|
mount_drive = True |
|
verbose = False |
|
|
|
def read_file(filename): |
|
with open(filename, "r") as f: |
|
contents = f.read() |
|
return contents |
|
|
|
|
|
def write_file(filename, contents): |
|
with open(filename, "w") as f: |
|
f.write(contents) |
|
|
|
|
|
def clone_repo(url): |
|
if not os.path.exists(repo_dir): |
|
os.chdir(root_dir) |
|
!git clone {url} {repo_dir} |
|
else: |
|
os.chdir(repo_dir) |
|
!git pull origin {branch} if branch else !git pull |
|
|
|
|
|
def install_dependencies(): |
|
s = getoutput('nvidia-smi') |
|
|
|
if 'T4' in s: |
|
!sed -i "s@cpu@cuda@" library/model_util.py |
|
|
|
!pip install {'-q' if not verbose else ''} --upgrade -r requirements.txt |
|
|
|
from accelerate.utils import write_basic_config |
|
|
|
if not os.path.exists(accelerate_config): |
|
write_basic_config(save_location=accelerate_config) |
|
|
|
|
|
def remove_bitsandbytes_message(filename): |
|
welcome_message = """ |
|
def evaluate_cuda_setup(): |
|
print('') |
|
print('='*35 + 'BUG REPORT' + '='*35) |
|
print('Welcome to bitsandbytes. For bug reports, please submit your error trace to: https://github.com/TimDettmers/bitsandbytes/issues') |
|
print('For effortless bug reporting copy-paste your error into this form: https://docs.google.com/forms/d/e/1FAIpQLScPB8emS3Thkp66nvqwmjTEgxp8Y9ufuWTzFyr9kJ5AoI47dQ/viewform?usp=sf_link') |
|
print('='*80)""" |
|
|
|
new_welcome_message = """ |
|
def evaluate_cuda_setup(): |
|
import os |
|
if 'BITSANDBYTES_NOWELCOME' not in os.environ or str(os.environ['BITSANDBYTES_NOWELCOME']) == '0': |
|
print('') |
|
print('=' * 35 + 'BUG REPORT' + '=' * 35) |
|
print('Welcome to bitsandbytes. For bug reports, please submit your error trace to: https://github.com/TimDettmers/bitsandbytes/issues') |
|
print('For effortless bug reporting copy-paste your error into this form: https://docs.google.com/forms/d/e/1FAIpQLScPB8emS3Thkp66nvqwmjTEgxp8Y9ufuWTzFyr9kJ5AoI47dQ/viewform?usp=sf_link') |
|
print('To hide this message, set the BITSANDBYTES_NOWELCOME variable like so: export BITSANDBYTES_NOWELCOME=1') |
|
print('=' * 80)""" |
|
|
|
contents = read_file(filename) |
|
new_contents = contents.replace(welcome_message, new_welcome_message) |
|
write_file(filename, new_contents) |
|
|
|
|
|
def main(): |
|
os.chdir(root_dir) |
|
|
|
if mount_drive: |
|
if not os.path.exists("/content/drive"): |
|
drive.mount("/content/drive") |
|
|
|
for dir in [ |
|
deps_dir, |
|
training_dir, |
|
config_dir, |
|
pretrained_model, |
|
vae_dir |
|
]: |
|
os.makedirs(dir, exist_ok=True) |
|
|
|
clone_repo(repo_url) |
|
|
|
if branch: |
|
os.chdir(repo_dir) |
|
status = os.system(f"git checkout {branch}") |
|
if status != 0: |
|
raise Exception("Failed to checkout branch or commit") |
|
|
|
os.chdir(repo_dir) |
|
|
|
!apt install aria2 {'-qq' if not verbose else ''} |
|
|
|
install_dependencies() |
|
time.sleep(3) |
|
|
|
remove_bitsandbytes_message(bitsandytes_main_py) |
|
|
|
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3" |
|
os.environ["BITSANDBYTES_NOWELCOME"] = "1" |
|
os.environ["SAFETENSORS_FAST_GPU"] = "1" |
|
|
|
cuda_path = "/usr/local/cuda-11.8/targets/x86_64-linux/lib/" |
|
ld_library_path = os.environ.get("LD_LIBRARY_PATH", "") |
|
os.environ["LD_LIBRARY_PATH"] = f"{ld_library_path}:{cuda_path}" |
|
|
|
main() |
|
|
|
|
|
|
|
import threading |
|
from google.colab import output |
|
from imjoy_elfinder.app import main |
|
|
|
open_in_new_tab = False |
|
|
|
def start_file_explorer(root_dir=root_dir, port=8765): |
|
try: |
|
main(["--root-dir=" + root_dir, "--port=" + str(port)]) |
|
except Exception as e: |
|
print("Error starting file explorer:", str(e)) |
|
|
|
|
|
def open_file_explorer(open_in_new_tab=False, root_dir=root_dir, port=8765): |
|
thread = threading.Thread(target=start_file_explorer, args=[root_dir, port]) |
|
thread.start() |
|
|
|
if open_in_new_tab: |
|
output.serve_kernel_port_as_window(port) |
|
else: |
|
output.serve_kernel_port_as_iframe(port, height="500") |
|
|
|
open_file_explorer(open_in_new_tab=open_in_new_tab, root_dir=root_dir, port=8765) |
|
|
|
"""# II. Pretrained Model Selection""" |
|
|
|
|
|
|
|
import os |
|
|
|
|
|
|
|
models = { |
|
"AnyLoRA": "https://huggingface.co/Linaqruf/stolen/resolve/main/pruned-models/AnyLoRA_noVae_fp16-pruned.safetensors", |
|
"AnyLoRA-anime-mix": "https://huggingface.co/Lykon/AnyLoRA/resolve/main/AAM_Anylora_AnimeMix.safetensors", |
|
"AnimePastelDream": "https://huggingface.co/Lykon/AnimePastelDream/resolve/main/AnimePastelDream_Soft_noVae_fp16.safetensors", |
|
"Chillout-mix": "https://huggingface.co/Linaqruf/stolen/resolve/main/pruned-models/chillout_mix-pruned.safetensors", |
|
"DosMix": "https://huggingface.co/scrapware/personal-backup/resolve/main/models/dosmix/dosmix_.safetensors", |
|
"DDosMix_V2": "https://huggingface.co/scrapware/personal-backup/resolve/main/models/dosmix/ddosmix_V2.safetensors", |
|
"RealDosMix": "https://huggingface.co/scrapware/personal-backup/resolve/main/models/dosmix/realdosmix_.safetensors", |
|
"anidosmix_A": "https://huggingface.co/scrapware/personal-backup/resolv/main/models/dosmix/anidosmix_A.safetensors", |
|
"cartoonish_v1": "https://huggingface.co/scrapware/personal-backup/resolve/main/models/dosmix/cartoonish_v1.safetensors", |
|
"cbi_MOMO_v2HQ": "https://huggingface.co/scrapware/personal-backup/resolve/main/specials/cbi_MOMO_v2HQ.safetensors", |
|
"cbi_MOMO_v2.1": "https://huggingface.co/scrapware/personal-backup/resolve/main/specials/cbi_MOMO_v2.1.safetensors", |
|
"Nordrin_little-v3": "https://huggingface.co/scrapware/personal-backup/resolve/main/models/nordrinLittleLittle_v30.safetensors", |
|
"majicMIX-v25": "https://huggingface.co/scrapware/personal-backup/resolve/main/majicmixRealistic_betterV2V25.safetensors", |
|
"Anything-v3-0": "https://huggingface.co/Linaqruf/stolen/resolve/main/pruned-models/anything-v3-0.safetensors", |
|
"Anything-v3-1": "https://huggingface.co/cag/anything-v3-1/resolve/main/anything-v3-1.safetensors", |
|
"Anything-v3-2": "https://huggingface.co/Linaqruf/stolen/resolve/main/pruned-models/anything-v3-2.safetensors", |
|
"Anything-v3-3": "https://huggingface.co/Linaqruf/stolen/resolve/main/pruned-models/anything-v3-3.safetensors", |
|
"OpenJourney-v4": "https://huggingface.co/prompthero/openjourney-v4/resolve/main/openjourney-v4.ckpt", |
|
"Stable-Diffusion-v1-5": "https://huggingface.co/Linaqruf/stolen/resolve/main/pruned-models/stable_diffusion_1_5-pruned.safetensors", |
|
"Animefull-final-pruned": "https://huggingface.co/scrapware/personal-backup/resolve/main/novelailatest-pruned.ckpt", |
|
} |
|
|
|
v2_models = { |
|
"stable-diffusion-2-1-base": "https://huggingface.co/stabilityai/stable-diffusion-2-1-base/resolve/main/v2-1_512-ema-pruned.safetensors", |
|
"stable-diffusion-2-1-768v": "https://huggingface.co/stabilityai/stable-diffusion-2-1/resolve/main/v2-1_768-ema-pruned.safetensors", |
|
"plat-diffusion-v1-3-1": "https://huggingface.co/p1atdev/pd-archive/resolve/main/plat-v1-3-1.safetensors", |
|
"replicant-v1": "https://huggingface.co/gsdf/Replicant-V1.0/resolve/main/Replicant-V1.0.safetensors", |
|
"illuminati-diffusion-v1-0": "https://huggingface.co/IlluminatiAI/Illuminati_Diffusion_v1.0/resolve/main/illuminati_diffusion_v1.0.safetensors", |
|
"illuminati-diffusion-v1-1": "https://huggingface.co/4eJIoBek/Illuminati-Diffusion-v1-1/resolve/main/illuminatiDiffusionV1_v11.safetensors", |
|
"waifu-diffusion-1-4-anime-e2": "https://huggingface.co/hakurei/waifu-diffusion-v1-4/resolve/main/wd-1-4-anime_e2.ckpt", |
|
"waifu-diffusion-1-5-e2": "https://huggingface.co/waifu-diffusion/wd-1-5-beta2/resolve/main/checkpoints/wd-1-5-beta2-fp32.safetensors", |
|
"waifu-diffusion-1-5-e2-aesthetic": "https://huggingface.co/waifu-diffusion/wd-1-5-beta2/resolve/main/checkpoints/wd-1-5-beta2-aesthetic-fp32.safetensors", |
|
} |
|
|
|
installModels = [] |
|
installv2Models = [] |
|
|
|
|
|
model_name = "" |
|
|
|
v2_model_name = "" |
|
|
|
if model_name: |
|
model_url = models.get(model_name) |
|
if model_url: |
|
installModels.append((model_name, model_url)) |
|
elif v2_model_name: |
|
v2_model_url = v2_models.get(v2_model_name) |
|
if v2_model_url: |
|
installv2Models.append((v2_model_name, v2_model_url)) |
|
|
|
|
|
def install(checkpoint_name, url): |
|
ext = "ckpt" if url.endswith(".ckpt") else "safetensors" |
|
|
|
hf_token = "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" |
|
user_header = f'"Authorization: Bearer {hf_token}"' |
|
!aria2c --console-log-level=error --summary-interval=10 --header={user_header} -c -x 16 -k 1M -s 16 -d {pretrained_model} -o {checkpoint_name}.{ext} "{url}" |
|
|
|
|
|
def install_checkpoint(): |
|
for model in installModels: |
|
install(model[0], model[1]) |
|
for v2model in installv2Models: |
|
install(v2model[0], v2model[1]) |
|
|
|
install_checkpoint() |
|
|
|
|
|
|
|
import os |
|
|
|
|
|
|
|
os.chdir(root_dir) |
|
|
|
|
|
modelUrls = "" |
|
|
|
def install(url): |
|
base_name = os.path.basename(url) |
|
|
|
if "drive.google.com" in url: |
|
os.chdir(pretrained_model) |
|
!gdown --fuzzy {url} |
|
elif "huggingface.co" in url: |
|
if "/blob/" in url: |
|
url = url.replace("/blob/", "/resolve/") |
|
|
|
hf_token = "" |
|
user_header = f'"Authorization: Bearer {hf_token}"' |
|
!aria2c --console-log-level=error --summary-interval=10 --header={user_header} -c -x 16 -k 1M -s 16 -d {pretrained_model} -o {base_name} {url} |
|
else: |
|
!aria2c --console-log-level=error --summary-interval=10 -c -x 16 -k 1M -s 16 -d {pretrained_model} {url} |
|
|
|
if modelUrls: |
|
urls = modelUrls.split(",") |
|
for url in urls: |
|
install(url.strip()) |
|
|
|
|
|
|
|
import os |
|
|
|
|
|
|
|
vaes = { |
|
"none": "", |
|
"anime.vae.pt": "https://huggingface.co/scrapware/personal-backup/resolve/main/vae/animevae.pt", |
|
"kl-f8-anime1.ckpt": "https://huggingface.co/scrapware/personal-backup/resolve/main/vae/kl-f8-anime1.ckpt", |
|
"kl-f8-anime2.ckpt": "https://huggingface.co/scrapware/personal-backup/resolve/main/vae/kl-f8-anime2.ckpt", |
|
"waifudiffusion.vae.pt": "https://huggingface.co/hakurei/waifu-diffusion-v1-4/resolve/main/vae/kl-f8-anime.ckpt", |
|
"kl-f8-anime3.ckpt": "https://huggingface.co/scrapware/personal-backup/resolve/main/vae/kl-f8-anime3.ckpt", "blessed2.vae.pt": "https://huggingface.co/scrapware/personal-backup/resolve/main/vae/blessed2.vae.pt", |
|
"vae-ft-ema-560000-ema-pruned.safetensors": "https://huggingface.co/scrapware/personal-backup/resolve/main/vae/vae-ft-ema-560000-ema-pruned.safetensors", |
|
"vae-ft-mse-840000-ema-pruned.safetensors": "https://huggingface.co/scrapware/personal-backup/resolve/main/vae/vae-ft-mse-840000-ema-pruned.safetensors", |
|
"stablediffusion.vae.pt": "https://huggingface.co/stabilityai/sd-vae-ft-mse-original/resolve/main/vae-ft-mse-840000-ema-pruned.ckpt", |
|
"ft-mse-840000-darken.pt": "https://huggingface.co/scrapware/personal-backup/resolve/main/vae/ft-mse-840000-darken.pt", |
|
"ft-mse-840000-deeper.pt": "https://huggingface.co/scrapware/personal-backup/resolve/main/vae/ft-mse-840000-deeper.pt", |
|
"apricots_vae.safetensors": "https://huggingface.co/scrapware/personal-backup/resolve/main/vae/apricots_vae_v1.safetensors", |
|
"apricots_quantizer.safetensors": "https://huggingface.co/scrapware/personal-backup/resolve/main/vae/apricots_quantizer_v1.safetensors", |
|
"twinkle_vae.safetensors": "https://huggingface.co/scrapware/personal-backup/resolve/main/vae/twinkle_vae_v1.safetensors", |
|
"twinkle_v1-darken.vae": "https://huggingface.co/scrapware/personal-backup/resolve/main/vae/twinkle_v1-darken.vae", |
|
"twinkle_v1-deeper.vae": "https://huggingface.co/scrapware/personal-backup/resolve/main/vae/twinkle_v1-deeper.vae", |
|
"tensor_quantizer.safetensors": "https://huggingface.co/scrapware/personal-backup/resolve/main/vae/tensor_quantizer.safetensors", |
|
"mangledMergeVAE_v10.pt": "https://huggingface.co/scrapware/personal-backup/resolve/main/vae/mangledMergeVAE_v10.pt", |
|
} |
|
|
|
install_vaes = [] |
|
|
|
|
|
vae_name = "none" |
|
|
|
if vae_name in vaes: |
|
vae_url = vaes[vae_name] |
|
if vae_url: |
|
install_vaes.append((vae_name, vae_url)) |
|
|
|
def install(vae_name, url): |
|
hf_token = "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" |
|
user_header = f'"Authorization: Bearer {hf_token}"' |
|
!aria2c --console-log-level=error --summary-interval=10 --header={user_header} -c -x 16 -k 1M -s 16 -d {vae_dir} -o {vae_name} "{url}" |
|
|
|
def install_vae(): |
|
for vae in install_vaes: |
|
install(vae[0], vae[1]) |
|
|
|
install_vae() |
|
|
|
"""# III. Data Acquisition |
|
|
|
You have three options for acquiring your dataset: |
|
|
|
1. Uploading it to Colab's local files. |
|
2. Bulk downloading images from Danbooru using the `Simple Booru Scraper`. |
|
3. Locating your dataset from Google Drive. |
|
|
|
""" |
|
|
|
|
|
|
|
|
|
import os |
|
from IPython.utils import capture |
|
|
|
|
|
|
|
train_data_dir = "/content/LoRA/train_data" |
|
reg_data_dir = "/content/LoRA/reg_data" |
|
|
|
for dir in [train_data_dir, reg_data_dir]: |
|
if dir: |
|
with capture.capture_output() as cap: |
|
os.makedirs(dir, exist_ok=True) |
|
|
|
del cap |
|
|
|
print(f"Your train data directory : {train_data_dir}") |
|
if reg_data_dir: |
|
print(f"Your reg data directory : {reg_data_dir}") |
|
|
|
|
|
|
|
import os |
|
import shutil |
|
from pathlib import Path |
|
|
|
|
|
|
|
zipfile_url = "/content/drive/MyDrive/Stable-diffusion/zipfile/xxxx.zip" |
|
zipfile_name = "zipfile.zip" |
|
unzip_to = "/content/LoRA" |
|
|
|
hf_token = "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" |
|
user_header = f'"Authorization: Bearer {hf_token}"' |
|
|
|
if unzip_to: |
|
os.makedirs(unzip_to, exist_ok=True) |
|
else: |
|
unzip_to = train_data_dir |
|
|
|
|
|
def download_dataset(url): |
|
if url.startswith("/content"): |
|
return url |
|
elif "drive.google.com" in url: |
|
os.chdir(root_dir) |
|
!gdown --fuzzy {url} |
|
return f"{root_dir}/{zipfile_name}" |
|
elif "huggingface.co" in url: |
|
if "/blob/" in url: |
|
url = url.replace("/blob/", "/resolve/") |
|
!aria2c --console-log-level=error --summary-interval=10 --header={user_header} -c -x 16 -k 1M -s 16 -d {root_dir} -o {zipfile_name} {url} |
|
return f"{root_dir}/{zipfile_name}" |
|
else: |
|
!aria2c --console-log-level=error --summary-interval=10 -c -x 16 -k 1M -s 16 -d {root_dir} -o {zipfile_name} {url} |
|
return f"{root_dir}/{zipfile_name}" |
|
|
|
|
|
def extract_dataset(zip_file, output_path): |
|
if zip_file.startswith("/content"): |
|
!unzip -o {zip_file} -d "{output_path}" |
|
else: |
|
!unzip -o "{zip_file}" -d "{output_path}" |
|
|
|
|
|
def remove_files(train_dir, files_to_move): |
|
for filename in os.listdir(train_dir): |
|
file_path = os.path.join(train_dir, filename) |
|
if filename in files_to_move: |
|
if not os.path.exists(file_path): |
|
shutil.move(file_path, training_dir) |
|
else: |
|
os.remove(file_path) |
|
|
|
|
|
zip_file = download_dataset(zipfile_url) |
|
extract_dataset(zip_file, unzip_to) |
|
|
|
|
|
files_to_move = ( |
|
"meta_cap.json", |
|
"meta_cap_dd.json", |
|
"meta_lat.json", |
|
"meta_clean.json", |
|
) |
|
|
|
remove_files(train_data_dir, files_to_move) |
|
|
|
|
|
|
|
import os |
|
import html |
|
from IPython.utils import capture |
|
|
|
|
|
os.chdir(root_dir) |
|
|
|
booru = "Danbooru" |
|
prompt = "" |
|
|
|
custom_url = "" |
|
|
|
sub_folder = "" |
|
user_agent = "gdl/1.24.5" |
|
|
|
range = "1-200" |
|
write_tags = True |
|
additional_arguments = "--filename /O --no-part" |
|
|
|
with_aria_2c = True |
|
|
|
tags = prompt.split(',') |
|
tags = '+'.join(tags) |
|
|
|
replacement_dict = {" ": "", "(": "%28", ")": "%29", ":": "%3a"} |
|
tags = ''.join(replacement_dict.get(c, c) for c in tags) |
|
|
|
if sub_folder == "": |
|
image_dir = train_data_dir |
|
elif sub_folder.startswith("/content"): |
|
image_dir = sub_folder |
|
else: |
|
image_dir = os.path.join(train_data_dir, sub_folder) |
|
os.makedirs(image_dir, exist_ok=True) |
|
|
|
if booru == "Danbooru": |
|
url = "https://danbooru.donmai.us/posts?tags={}".format(tags) |
|
elif booru == "Gelbooru": |
|
url = "https://gelbooru.com/index.php?page=post&s=list&tags={}".format(tags) |
|
else: |
|
url = "https://safebooru.org/index.php?page=post&s=list&tags={}".format(tags) |
|
|
|
valid_url = custom_url if custom_url else url |
|
|
|
def scrape(config): |
|
args = "" |
|
for k, v in config.items(): |
|
if k.startswith("_"): |
|
args += f'"{v}" ' |
|
elif isinstance(v, str): |
|
args += f'--{k}="{v}" ' |
|
elif isinstance(v, bool) and v: |
|
args += f"--{k} " |
|
elif isinstance(v, float) and not isinstance(v, bool): |
|
args += f"--{k}={v} " |
|
elif isinstance(v, int) and not isinstance(v, bool): |
|
args += f"--{k}={v} " |
|
|
|
return args |
|
|
|
def pre_process_tags(directory): |
|
for item in os.listdir(directory): |
|
item_path = os.path.join(directory, item) |
|
if os.path.isfile(item_path) and item.endswith(".txt"): |
|
old_path = item_path |
|
new_file_name = os.path.splitext(os.path.splitext(item)[0])[0] + ".txt" |
|
new_path = os.path.join(directory, new_file_name) |
|
|
|
os.rename(old_path, new_path) |
|
|
|
with open(new_path, "r") as f: |
|
contents = f.read() |
|
|
|
contents = html.unescape(contents) |
|
contents = contents.replace("_", " ") |
|
contents = ", ".join(contents.split("\n")) |
|
|
|
with open(new_path, "w") as f: |
|
f.write(contents) |
|
|
|
elif os.path.isdir(item_path): |
|
pre_process_tags(item_path) |
|
|
|
get_url_config = { |
|
"get-urls" : True, |
|
"range" : range if range else None, |
|
"user-agent" : user_agent |
|
} |
|
|
|
scrape_config = { |
|
"directory" : image_dir, |
|
"write-tags" : write_tags, |
|
"range" : range if range else None, |
|
"user-agent" : user_agent |
|
} |
|
|
|
if with_aria_2c: |
|
scraper_text = os.path.join(root_dir, "scrape_this.txt") |
|
with capture.capture_output() as cap: |
|
args = scrape(get_url_config) |
|
!gallery-dl "{valid_url}" {args} {additional_arguments} |
|
with open(scraper_text, "w") as f: |
|
f.write(cap.stdout) |
|
|
|
os.chdir(image_dir) |
|
!aria2c --console-log-level=error --summary-interval=10 -c -x 16 -k 1M -s 16 -i {scraper_text} |
|
|
|
else: |
|
args = scrape(scrape_config) |
|
!gallery-dl "{valid_url}" {args} {additional_arguments} |
|
|
|
if write_tags: |
|
pre_process_tags(train_data_dir) |
|
|
|
"""# IV. Data Preprocessing""" |
|
|
|
|
|
|
|
import os |
|
import random |
|
import concurrent.futures |
|
from tqdm import tqdm |
|
from PIL import Image |
|
|
|
|
|
|
|
os.chdir(root_dir) |
|
|
|
test = os.listdir(train_data_dir) |
|
|
|
|
|
convert = False |
|
|
|
random_color = False |
|
|
|
recursive = False |
|
|
|
|
|
batch_size = 32 |
|
supported_types = [ |
|
".png", |
|
".jpg", |
|
".jpeg", |
|
".webp", |
|
".bmp", |
|
".caption", |
|
".npz", |
|
".txt", |
|
".json", |
|
] |
|
|
|
background_colors = [ |
|
(255, 255, 255), |
|
(0, 0, 0), |
|
(255, 0, 0), |
|
(0, 255, 0), |
|
(0, 0, 255), |
|
(255, 255, 0), |
|
(255, 0, 255), |
|
(0, 255, 255), |
|
] |
|
|
|
def clean_directory(directory): |
|
for item in os.listdir(directory): |
|
file_path = os.path.join(directory, item) |
|
if os.path.isfile(file_path): |
|
file_ext = os.path.splitext(item)[1] |
|
if file_ext not in supported_types: |
|
print(f"Deleting file {item} from {directory}") |
|
os.remove(file_path) |
|
elif os.path.isdir(file_path) and recursive: |
|
clean_directory(file_path) |
|
|
|
def process_image(image_path): |
|
img = Image.open(image_path) |
|
img_dir, image_name = os.path.split(image_path) |
|
|
|
if img.mode in ("RGBA", "LA"): |
|
if random_color: |
|
background_color = random.choice(background_colors) |
|
else: |
|
background_color = (255, 255, 255) |
|
bg = Image.new("RGB", img.size, background_color) |
|
bg.paste(img, mask=img.split()[-1]) |
|
|
|
if image_name.endswith(".webp"): |
|
bg = bg.convert("RGB") |
|
new_image_path = os.path.join(img_dir, image_name.replace(".webp", ".jpg")) |
|
bg.save(new_image_path, "JPEG") |
|
os.remove(image_path) |
|
print(f" Converted image: {image_name} to {os.path.basename(new_image_path)}") |
|
else: |
|
bg.save(image_path, "PNG") |
|
print(f" Converted image: {image_name}") |
|
else: |
|
if image_name.endswith(".webp"): |
|
new_image_path = os.path.join(img_dir, image_name.replace(".webp", ".jpg")) |
|
img.save(new_image_path, "JPEG") |
|
os.remove(image_path) |
|
print(f" Converted image: {image_name} to {os.path.basename(new_image_path)}") |
|
else: |
|
img.save(image_path, "PNG") |
|
|
|
def find_images(directory): |
|
images = [] |
|
for root, _, files in os.walk(directory): |
|
for file in files: |
|
if file.endswith(".png") or file.endswith(".webp"): |
|
images.append(os.path.join(root, file)) |
|
return images |
|
|
|
clean_directory(train_data_dir) |
|
images = find_images(train_data_dir) |
|
num_batches = len(images) // batch_size + 1 |
|
|
|
if convert: |
|
with concurrent.futures.ThreadPoolExecutor() as executor: |
|
for i in tqdm(range(num_batches)): |
|
start = i * batch_size |
|
end = start + batch_size |
|
batch = images[start:end] |
|
executor.map(process_image, batch) |
|
|
|
print("All images have been converted") |
|
|
|
"""## 4.2. Data Annotation |
|
You can choose to train a model using captions. We're using [BLIP](https://huggingface.co/spaces/Salesforce/BLIP) for image captioning and [Waifu Diffusion 1.4 Tagger](https://huggingface.co/spaces/SmilingWolf/wd-v1-4-tags) for image tagging similar to Danbooru. |
|
- Use BLIP Captioning for: `General Images` |
|
- Use Waifu Diffusion 1.4 Tagger V2 for: `Anime and Manga-style Images` |
|
""" |
|
|
|
|
|
|
|
import os |
|
|
|
os.chdir(finetune_dir) |
|
|
|
batch_size = 8 |
|
max_data_loader_n_workers = 2 |
|
beam_search = True |
|
min_length = 5 |
|
max_length = 75 |
|
|
|
recursive = False |
|
|
|
verbose_logging = True |
|
|
|
config = { |
|
"_train_data_dir" : train_data_dir, |
|
"batch_size" : batch_size, |
|
"beam_search" : beam_search, |
|
"min_length" : min_length, |
|
"max_length" : max_length, |
|
"debug" : verbose_logging, |
|
"caption_extension" : ".caption", |
|
"max_data_loader_n_workers" : max_data_loader_n_workers, |
|
"recursive" : recursive |
|
} |
|
|
|
args = "" |
|
for k, v in config.items(): |
|
if k.startswith("_"): |
|
args += f'"{v}" ' |
|
elif isinstance(v, str): |
|
args += f'--{k}="{v}" ' |
|
elif isinstance(v, bool) and v: |
|
args += f"--{k} " |
|
elif isinstance(v, float) and not isinstance(v, bool): |
|
args += f"--{k}={v} " |
|
elif isinstance(v, int) and not isinstance(v, bool): |
|
args += f"--{k}={v} " |
|
|
|
final_args = f"python make_captions.py {args}" |
|
|
|
os.chdir(finetune_dir) |
|
!{final_args} |
|
|
|
|
|
|
|
import os |
|
|
|
|
|
os.chdir(finetune_dir) |
|
|
|
|
|
batch_size = 8 |
|
max_data_loader_n_workers = 2 |
|
model = "SmilingWolf/wd-v1-4-convnextv2-tagger-v2" |
|
|
|
recursive = False |
|
|
|
verbose_logging = True |
|
|
|
undesired_tags = "" |
|
|
|
general_threshold = 0.35 |
|
character_threshold = 0.35 |
|
|
|
config = { |
|
"_train_data_dir": train_data_dir, |
|
"batch_size": batch_size, |
|
"repo_id": model, |
|
"recursive": recursive, |
|
"remove_underscore": True, |
|
"general_threshold": general_threshold, |
|
"character_threshold": character_threshold, |
|
"caption_extension": ".txt", |
|
"max_data_loader_n_workers": max_data_loader_n_workers, |
|
"debug": verbose_logging, |
|
"undesired_tags": undesired_tags |
|
} |
|
|
|
args = "" |
|
for k, v in config.items(): |
|
if k.startswith("_"): |
|
args += f'"{v}" ' |
|
elif isinstance(v, str): |
|
args += f'--{k}="{v}" ' |
|
elif isinstance(v, bool) and v: |
|
args += f"--{k} " |
|
elif isinstance(v, float) and not isinstance(v, bool): |
|
args += f"--{k}={v} " |
|
elif isinstance(v, int) and not isinstance(v, bool): |
|
args += f"--{k}={v} " |
|
|
|
final_args = f"python tag_images_by_wd14_tagger.py {args}" |
|
|
|
os.chdir(finetune_dir) |
|
!{final_args} |
|
|
|
|
|
|
|
import os |
|
|
|
|
|
|
|
os.chdir(root_dir) |
|
|
|
|
|
extension = ".txt" |
|
custom_tag = "" |
|
|
|
|
|
sub_folder = "" |
|
|
|
append = False |
|
|
|
remove_tag = False |
|
recursive = False |
|
|
|
if sub_folder == "": |
|
image_dir = train_data_dir |
|
elif sub_folder == "--all": |
|
image_dir = train_data_dir |
|
recursive = True |
|
elif sub_folder.startswith("/content"): |
|
image_dir = sub_folder |
|
else: |
|
image_dir = os.path.join(train_data_dir, sub_folder) |
|
os.makedirs(image_dir, exist_ok=True) |
|
|
|
def read_file(filename): |
|
with open(filename, "r") as f: |
|
contents = f.read() |
|
return contents |
|
|
|
def write_file(filename, contents): |
|
with open(filename, "w") as f: |
|
f.write(contents) |
|
|
|
def process_tags(filename, custom_tag, append, remove_tag): |
|
contents = read_file(filename) |
|
tags = [tag.strip() for tag in contents.split(',')] |
|
custom_tags = [tag.strip() for tag in custom_tag.split(',')] |
|
|
|
for custom_tag in custom_tags: |
|
custom_tag = custom_tag.replace("_", " ") |
|
if remove_tag: |
|
while custom_tag in tags: |
|
tags.remove(custom_tag) |
|
else: |
|
if custom_tag not in tags: |
|
if append: |
|
tags.append(custom_tag) |
|
else: |
|
tags.insert(0, custom_tag) |
|
|
|
contents = ', '.join(tags) |
|
write_file(filename, contents) |
|
|
|
def process_directory(image_dir, tag, append, remove_tag, recursive): |
|
for filename in os.listdir(image_dir): |
|
file_path = os.path.join(image_dir, filename) |
|
|
|
if os.path.isdir(file_path) and recursive: |
|
process_directory(file_path, tag, append, remove_tag, recursive) |
|
elif filename.endswith(extension): |
|
process_tags(file_path, tag, append, remove_tag) |
|
|
|
tag = custom_tag |
|
|
|
if not any( |
|
[filename.endswith(extension) for filename in os.listdir(image_dir)] |
|
): |
|
for filename in os.listdir(image_dir): |
|
if filename.endswith((".png", ".jpg", ".jpeg", ".webp", ".bmp")): |
|
open( |
|
os.path.join(image_dir, filename.split(".")[0] + extension), |
|
"w", |
|
).close() |
|
|
|
if custom_tag: |
|
process_directory(image_dir, tag, append, remove_tag, recursive) |
|
|
|
"""# V. Training Model |
|
|
|
|
|
""" |
|
|
|
|
|
|
|
from google.colab import drive |
|
|
|
v2 = False |
|
v_parameterization = False |
|
project_name = "" |
|
if not project_name: |
|
project_name = "last" |
|
|
|
pretrained_model_name_or_path = "/content/pretrained_model/AnyLoRA.safetensors" |
|
vae = "" |
|
output_dir = "/content/LoRA/output" |
|
|
|
|
|
output_to_drive = True |
|
|
|
if output_to_drive: |
|
output_dir = "/content/drive/MyDrive/LoRA/output" |
|
|
|
if not os.path.exists("/content/drive"): |
|
drive.mount("/content/drive") |
|
|
|
sample_dir = os.path.join(output_dir, "sample") |
|
for dir in [output_dir, sample_dir]: |
|
os.makedirs(dir, exist_ok=True) |
|
|
|
print("Project Name: ", project_name) |
|
print("Model Version: Stable Diffusion V1.x") if not v2 else "" |
|
print("Model Version: Stable Diffusion V2.x") if v2 and not v_parameterization else "" |
|
print("Model Version: Stable Diffusion V2.x 768v") if v2 and v_parameterization else "" |
|
print( |
|
"Pretrained Model Path: ", pretrained_model_name_or_path |
|
) if pretrained_model_name_or_path else print("No Pretrained Model path specified.") |
|
print("VAE Path: ", vae) if vae else print("No VAE path specified.") |
|
print("Output Path: ", output_dir) |
|
|
|
|
|
import os |
|
import toml |
|
import glob |
|
|
|
dataset_repeats = 10 |
|
|
|
activation_word = "" |
|
caption_extension = ".txt" |
|
|
|
resolution = 512 |
|
flip_aug = False |
|
keep_tokens = 0 |
|
|
|
def parse_folder_name(folder_name, default_num_repeats, default_class_token): |
|
folder_name_parts = folder_name.split("_") |
|
|
|
if len(folder_name_parts) == 2: |
|
if folder_name_parts[0].isdigit(): |
|
num_repeats = int(folder_name_parts[0]) |
|
class_token = folder_name_parts[1].replace("_", " ") |
|
else: |
|
num_repeats = default_num_repeats |
|
class_token = default_class_token |
|
else: |
|
num_repeats = default_num_repeats |
|
class_token = default_class_token |
|
|
|
return num_repeats, class_token |
|
|
|
def find_image_files(path): |
|
supported_extensions = (".png", ".jpg", ".jpeg", ".webp", ".bmp") |
|
return [file for file in glob.glob(path + '/**/*', recursive=True) if file.lower().endswith(supported_extensions)] |
|
|
|
def process_data_dir(data_dir, default_num_repeats, default_class_token, is_reg=False): |
|
subsets = [] |
|
|
|
images = find_image_files(data_dir) |
|
if images: |
|
subsets.append({ |
|
"image_dir": data_dir, |
|
"class_tokens": default_class_token, |
|
"num_repeats": default_num_repeats, |
|
**({"is_reg": is_reg} if is_reg else {}), |
|
}) |
|
|
|
for root, dirs, files in os.walk(data_dir): |
|
for folder in dirs: |
|
folder_path = os.path.join(root, folder) |
|
images = find_image_files(folder_path) |
|
|
|
if images: |
|
num_repeats, class_token = parse_folder_name(folder, default_num_repeats, default_class_token) |
|
|
|
subset = { |
|
"image_dir": folder_path, |
|
"class_tokens": class_token, |
|
"num_repeats": num_repeats, |
|
} |
|
|
|
if is_reg: |
|
subset["is_reg"] = True |
|
|
|
subsets.append(subset) |
|
|
|
return subsets |
|
|
|
|
|
train_subsets = process_data_dir(train_data_dir, dataset_repeats, activation_word) |
|
reg_subsets = process_data_dir(reg_data_dir, dataset_repeats, activation_word, is_reg=True) |
|
|
|
subsets = train_subsets + reg_subsets |
|
|
|
config = { |
|
"general": { |
|
"enable_bucket": True, |
|
"caption_extension": caption_extension, |
|
"shuffle_caption": True, |
|
"keep_tokens": keep_tokens, |
|
"bucket_reso_steps": 64, |
|
"bucket_no_upscale": False, |
|
}, |
|
"datasets": [ |
|
{ |
|
"resolution": resolution, |
|
"min_bucket_reso": 320 if resolution > 640 else 256, |
|
"max_bucket_reso": 1280 if resolution > 640 else 1024, |
|
"caption_dropout_rate": 0, |
|
"caption_tag_dropout_rate": 0, |
|
"caption_dropout_every_n_epochs": 0, |
|
"flip_aug": flip_aug, |
|
"color_aug": False, |
|
"face_crop_aug_range": None, |
|
"subsets": subsets, |
|
} |
|
], |
|
} |
|
|
|
dataset_config = os.path.join(config_dir, "dataset_config.toml") |
|
|
|
for key in config: |
|
if isinstance(config[key], dict): |
|
for sub_key in config[key]: |
|
if config[key][sub_key] == "": |
|
config[key][sub_key] = None |
|
elif config[key] == "": |
|
config[key] = None |
|
|
|
config_str = toml.dumps(config) |
|
|
|
with open(dataset_config, "w") as f: |
|
f.write(config_str) |
|
|
|
print(config_str) |
|
|
|
|
|
|
|
|
|
network_category = "LoCon_Lycoris" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
conv_dim = 32 |
|
conv_alpha = 16 |
|
|
|
|
|
network_dim = 32 |
|
network_alpha = 16 |
|
|
|
network_weight = "" |
|
network_module = "lycoris.kohya" if network_category in ["LoHa", "LoCon_Lycoris"] else "networks.lora" |
|
network_args = "" if network_category == "LoRA" else [ |
|
f"conv_dim={conv_dim}", f"conv_alpha={conv_alpha}", |
|
] |
|
|
|
|
|
min_snr_gamma = -1 |
|
|
|
optimizer_type = "AdaFactor" |
|
|
|
optimizer_args = "[\"warmup_init=False\", \"relative_step=True\",]" |
|
|
|
|
|
|
|
train_unet = True |
|
unet_lr = 1e-5 |
|
train_text_encoder = True |
|
text_encoder_lr = 5e-6 |
|
lr_scheduler = "adafactor" |
|
lr_warmup_steps = 0 |
|
|
|
lr_scheduler_num_cycles = 0 |
|
lr_scheduler_power = 0 |
|
|
|
if network_category == "LoHa": |
|
network_args.append("algo=loha") |
|
elif network_category == "LoCon_Lycoris": |
|
network_args.append("algo=lora") |
|
|
|
print("- LoRA Config:") |
|
print(f" - Min-SNR Weighting: {min_snr_gamma}") if not min_snr_gamma == -1 else "" |
|
print(f" - Loading network module: {network_module}") |
|
if not network_category == "LoRA": |
|
print(f" - network args: {network_args}") |
|
print(f" - {network_module} linear_dim set to: {network_dim}") |
|
print(f" - {network_module} linear_alpha set to: {network_alpha}") |
|
if not network_category == "LoRA": |
|
print(f" - {network_module} conv_dim set to: {conv_dim}") |
|
print(f" - {network_module} conv_alpha set to: {conv_alpha}") |
|
|
|
if not network_weight: |
|
print(" - No LoRA weight loaded.") |
|
else: |
|
if os.path.exists(network_weight): |
|
print(f" - Loading LoRA weight: {network_weight}") |
|
else: |
|
print(f" - {network_weight} does not exist.") |
|
network_weight = "" |
|
|
|
print("- Optimizer Config:") |
|
print(f" - Additional network category: {network_category}") |
|
print(f" - Using {optimizer_type} as Optimizer") |
|
if optimizer_args: |
|
print(f" - Optimizer Args: {optimizer_args}") |
|
if train_unet and train_text_encoder: |
|
print(" - Train UNet and Text Encoder") |
|
print(f" - UNet learning rate: {unet_lr}") |
|
print(f" - Text encoder learning rate: {text_encoder_lr}") |
|
if train_unet and not train_text_encoder: |
|
print(" - Train UNet only") |
|
print(f" - UNet learning rate: {unet_lr}") |
|
if train_text_encoder and not train_unet: |
|
print(" - Train Text Encoder only") |
|
print(f" - Text encoder learning rate: {text_encoder_lr}") |
|
print(f" - Learning rate warmup steps: {lr_warmup_steps}") |
|
print(f" - Learning rate Scheduler: {lr_scheduler}") |
|
if lr_scheduler == "cosine_with_restarts": |
|
print(f" - lr_scheduler_num_cycles: {lr_scheduler_num_cycles}") |
|
elif lr_scheduler == "polynomial": |
|
print(f" - lr_scheduler_power: {lr_scheduler_power}") |
|
|
|
|
|
|
|
|
|
import toml |
|
import os |
|
|
|
|
|
lowram = True |
|
enable_sample_prompt = True |
|
sampler = "euler_a" |
|
noise_offset = 0.0 |
|
num_epochs = 10 |
|
vae_batch_size = 6 |
|
train_batch_size = 8 |
|
mixed_precision = "fp16" |
|
save_precision = "fp16" |
|
save_n_epochs_type = "save_every_n_epochs" |
|
save_n_epochs_type_value = 2 |
|
save_model_as = "safetensors" |
|
max_token_length = 225 |
|
clip_skip = 2 |
|
gradient_checkpointing = False |
|
gradient_accumulation_steps = 1 |
|
seed = -1 |
|
logging_dir = "/content/LoRA/logs" |
|
prior_loss_weight = 1.0 |
|
|
|
os.chdir(repo_dir) |
|
|
|
sample_str = f""" |
|
masterpiece, best quality, 1girl, aqua eyes, baseball cap, blonde hair, closed mouth, earrings, green background, hat, hoop earrings, jewelry, looking at viewer, shirt, short hair, simple background, solo, upper body, yellow shirt \ |
|
--n lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry \ |
|
--w 512 \ |
|
--h 768 \ |
|
--l 7 \ |
|
--s 28 |
|
""" |
|
|
|
config = { |
|
"model_arguments": { |
|
"v2": v2, |
|
"v_parameterization": v_parameterization if v2 and v_parameterization else False, |
|
"pretrained_model_name_or_path": pretrained_model_name_or_path, |
|
"vae": vae, |
|
}, |
|
"additional_network_arguments": { |
|
"no_metadata": False, |
|
"unet_lr": float(unet_lr) if train_unet else None, |
|
"text_encoder_lr": float(text_encoder_lr) if train_text_encoder else None, |
|
"network_weights": network_weight, |
|
"network_module": network_module, |
|
"network_dim": network_dim, |
|
"network_alpha": network_alpha, |
|
"network_args": network_args, |
|
"network_train_unet_only": True if train_unet and not train_text_encoder else False, |
|
"network_train_text_encoder_only": True if train_text_encoder and not train_unet else False, |
|
"training_comment": None, |
|
}, |
|
"optimizer_arguments": { |
|
"min_snr_gamma": min_snr_gamma if not min_snr_gamma == -1 else None, |
|
"optimizer_type": optimizer_type, |
|
"learning_rate": unet_lr, |
|
"max_grad_norm": 1.0, |
|
"optimizer_args": eval(optimizer_args) if optimizer_args else None, |
|
"lr_scheduler": lr_scheduler, |
|
"lr_warmup_steps": lr_warmup_steps, |
|
"lr_scheduler_num_cycles": lr_scheduler_num_cycles if lr_scheduler == "cosine_with_restarts" else None, |
|
"lr_scheduler_power": lr_scheduler_power if lr_scheduler == "polynomial" else None, |
|
}, |
|
"dataset_arguments": { |
|
"cache_latents": True, |
|
"debug_dataset": False, |
|
"vae_batch_size": vae_batch_size, |
|
}, |
|
"training_arguments": { |
|
"output_dir": output_dir, |
|
"output_name": project_name, |
|
"save_precision": save_precision, |
|
"save_every_n_epochs": save_n_epochs_type_value if save_n_epochs_type == "save_every_n_epochs" else None, |
|
"save_n_epoch_ratio": save_n_epochs_type_value if save_n_epochs_type == "save_n_epoch_ratio" else None, |
|
"save_last_n_epochs": None, |
|
"save_state": None, |
|
"save_last_n_epochs_state": None, |
|
"resume": None, |
|
"train_batch_size": train_batch_size, |
|
"max_token_length": 225, |
|
"mem_eff_attn": False, |
|
"xformers": True, |
|
"max_train_epochs": num_epochs, |
|
"max_data_loader_n_workers": 8, |
|
"persistent_data_loader_workers": True, |
|
"seed": seed if seed > 0 else None, |
|
"gradient_checkpointing": gradient_checkpointing, |
|
"gradient_accumulation_steps": gradient_accumulation_steps, |
|
"mixed_precision": mixed_precision, |
|
"clip_skip": clip_skip if not v2 else None, |
|
"logging_dir": logging_dir, |
|
"log_prefix": project_name, |
|
"noise_offset": noise_offset if noise_offset > 0 else None, |
|
"lowram": lowram, |
|
}, |
|
"sample_prompt_arguments": { |
|
"sample_every_n_steps": None, |
|
"sample_every_n_epochs": 1 if enable_sample_prompt else 999999, |
|
"sample_sampler": sampler, |
|
}, |
|
"dreambooth_arguments": { |
|
"prior_loss_weight": 1.0, |
|
}, |
|
"saving_arguments": { |
|
"save_model_as": save_model_as |
|
}, |
|
} |
|
|
|
config_path = os.path.join(config_dir, "config_file.toml") |
|
prompt_path = os.path.join(config_dir, "sample_prompt.txt") |
|
|
|
for key in config: |
|
if isinstance(config[key], dict): |
|
for sub_key in config[key]: |
|
if config[key][sub_key] == "": |
|
config[key][sub_key] = None |
|
elif config[key] == "": |
|
config[key] = None |
|
|
|
config_str = toml.dumps(config) |
|
|
|
def write_file(filename, contents): |
|
with open(filename, "w") as f: |
|
f.write(contents) |
|
|
|
write_file(config_path, config_str) |
|
write_file(prompt_path, sample_str) |
|
|
|
print(config_str) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sample_prompt = "/content/LoRA/config/sample_prompt.txt" |
|
config_file = "/content/LoRA/config/config_file.toml" |
|
dataset_config = "/content/LoRA/config/dataset_config.toml" |
|
|
|
accelerate_conf = { |
|
"config_file" : accelerate_config, |
|
"num_cpu_threads_per_process" : 1, |
|
} |
|
|
|
train_conf = { |
|
"sample_prompts" : sample_prompt, |
|
"dataset_config" : dataset_config, |
|
"config_file" : config_file |
|
} |
|
|
|
def train(config): |
|
args = "" |
|
for k, v in config.items(): |
|
if k.startswith("_"): |
|
args += f'"{v}" ' |
|
elif isinstance(v, str): |
|
args += f'--{k}="{v}" ' |
|
elif isinstance(v, bool) and v: |
|
args += f"--{k} " |
|
elif isinstance(v, float) and not isinstance(v, bool): |
|
args += f"--{k}={v} " |
|
elif isinstance(v, int) and not isinstance(v, bool): |
|
args += f"--{k}={v} " |
|
|
|
return args |
|
|
|
accelerate_args = train(accelerate_conf) |
|
train_args = train(train_conf) |
|
final_args = f"accelerate launch {accelerate_args} train_network.py {train_args}" |
|
|
|
os.chdir(repo_dir) |
|
!{final_args} |
|
|
|
"""# VI. Testing""" |
|
|
|
|
|
|
|
training_logs_path = "/content/LoRA/logs" |
|
|
|
os.chdir(repo_dir) |
|
|
|
|
|
|
|
|
|
|
|
import os |
|
import torch |
|
import json |
|
from safetensors.torch import load_file |
|
from safetensors.torch import safe_open |
|
|
|
|
|
network_weight = "" |
|
verbose = False |
|
|
|
def is_safetensors(path): |
|
return os.path.splitext(path)[1].lower() == ".safetensors" |
|
|
|
def load_weight_data(file_path): |
|
if is_safetensors(file_path): |
|
return load_file(file_path) |
|
else: |
|
return torch.load(file_path, map_location="cuda") |
|
|
|
def extract_lora_weights(weight_data): |
|
lora_weights = [ |
|
(key, weight_data[key]) |
|
for key in weight_data.keys() |
|
if "lora_up" in key or "lora_down" in key |
|
] |
|
return lora_weights |
|
|
|
def print_lora_weight_stats(lora_weights): |
|
print(f"Number of LoRA modules: {len(lora_weights)}") |
|
|
|
for key, value in lora_weights: |
|
value = value.to(torch.float32) |
|
print(f"{key}, {torch.mean(torch.abs(value))}, {torch.min(torch.abs(value))}") |
|
|
|
def print_metadata(file_path): |
|
if is_safetensors(file_path): |
|
with safe_open(file_path, framework="pt") as f: |
|
metadata = f.metadata() |
|
if metadata is not None: |
|
print(f"\nLoad metadata for: {file_path}") |
|
print(json.dumps(metadata, indent=4)) |
|
else: |
|
print("No metadata saved, your model is not in safetensors format") |
|
|
|
def main(file_path, verbose: bool): |
|
weight_data = load_weight_data(file_path) |
|
|
|
if verbose: |
|
lora_weights = extract_lora_weights(weight_data) |
|
print_lora_weight_stats(lora_weights) |
|
|
|
print_metadata(file_path) |
|
|
|
if __name__ == "__main__": |
|
main(network_weight, verbose) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
network_weight = "" |
|
network_mul = 0.7 |
|
network_module = "networks.lora" |
|
network_args = "" |
|
|
|
|
|
v2 = False |
|
v_parameterization = False |
|
prompt = "masterpiece, best quality, 1girl, aqua eyes, baseball cap, blonde hair, closed mouth, earrings, green background, hat, hoop earrings, jewelry, looking at viewer, shirt, short hair, simple background, solo, upper body, yellow shirt" |
|
negative = "lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry" |
|
model = "/content/pretrained_model/AnyLoRA.safetensors" |
|
vae = "" |
|
outdir = "/content/tmp" |
|
scale = 7 |
|
sampler = "ddim" |
|
steps = 28 |
|
precision = "fp16" |
|
width = 512 |
|
height = 768 |
|
images_per_prompt = 4 |
|
batch_size = 4 |
|
clip_skip = 2 |
|
seed = -1 |
|
|
|
final_prompt = f"{prompt} --n {negative}" |
|
|
|
config = { |
|
"v2": v2, |
|
"v_parameterization": v_parameterization, |
|
"network_module": network_module, |
|
"network_weight": network_weight, |
|
"network_mul": float(network_mul), |
|
"network_args": eval(network_args) if network_args else None, |
|
"ckpt": model, |
|
"outdir": outdir, |
|
"xformers": True, |
|
"vae": vae if vae else None, |
|
"fp16": True, |
|
"W": width, |
|
"H": height, |
|
"seed": seed if seed > 0 else None, |
|
"scale": scale, |
|
"sampler": sampler, |
|
"steps": steps, |
|
"max_embeddings_multiples": 3, |
|
"batch_size": batch_size, |
|
"images_per_prompt": images_per_prompt, |
|
"clip_skip": clip_skip if not v2 else None, |
|
"prompt": final_prompt, |
|
} |
|
|
|
args = "" |
|
for k, v in config.items(): |
|
if k.startswith("_"): |
|
args += f'"{v}" ' |
|
elif isinstance(v, str): |
|
args += f'--{k}="{v}" ' |
|
elif isinstance(v, bool) and v: |
|
args += f"--{k} " |
|
elif isinstance(v, float) and not isinstance(v, bool): |
|
args += f"--{k}={v} " |
|
elif isinstance(v, int) and not isinstance(v, bool): |
|
args += f"--{k}={v} " |
|
|
|
final_args = f"python gen_img_diffusers.py {args}" |
|
|
|
os.chdir(repo_dir) |
|
!{final_args} |
|
|
|
|
|
import os |
|
import random |
|
import shutil |
|
import zipfile |
|
import time |
|
import json |
|
from google.colab import drive |
|
from datetime import timedelta |
|
from subprocess import getoutput |
|
from IPython.display import clear_output, display, HTML |
|
from IPython.utils import capture |
|
from tqdm import tqdm |
|
|
|
webui_dir = os.path.join(root_dir, "stable-diffusion-webui") |
|
tmp_dir = os.path.join(root_dir, "tmp") |
|
patches_dir = os.path.join(root_dir, "patches") |
|
deps_dir = os.path.join(root_dir, "deps") |
|
extensions_dir = os.path.join(webui_dir, "extensions") |
|
control_dir = os.path.join(webui_dir, "models/ControlNet") |
|
|
|
webui_models_dir = os.path.join(webui_dir, "models/Stable-diffusion") |
|
webui_lora_dir = os.path.join(webui_dir, "models/Lora") |
|
webui_vaes_dir = os.path.join(webui_dir, "models/VAE") |
|
|
|
control_net_max_models_num = 2 |
|
theme = "ogxBGreen" |
|
|
|
default_prompt = "masterpiece, best quality," |
|
default_neg_prompt = "(worst quality, low quality:1.4)" |
|
default_sampler = "DPM++ 2M Karras" |
|
default_steps = 20 |
|
default_width = 512 |
|
default_height = 768 |
|
default_denoising_strength = 0.55 |
|
default_cfg_scale = 7 |
|
|
|
config_file = os.path.join(webui_dir, "config.json") |
|
ui_config_file = os.path.join(webui_dir, "ui-config.json") |
|
webui_style_path = os.path.join(webui_dir, "style.css") |
|
|
|
os.chdir(root_dir) |
|
|
|
for dir in [patches_dir, deps_dir]: |
|
os.makedirs(dir, exist_ok=True) |
|
|
|
package_url = [ |
|
f"https://huggingface.co/Linaqruf/fast-repo/resolve/main/anapnoe-webui.tar.lz4", |
|
f"https://huggingface.co/Linaqruf/fast-repo/resolve/main/anapnoe-webui-deps.tar.lz4", |
|
f"https://huggingface.co/Linaqruf/fast-repo/resolve/main/anapnoe-webui-cache.tar.lz4", |
|
] |
|
|
|
def pre_download(desc): |
|
for package in tqdm(package_url, desc=desc): |
|
with capture.capture_output() as cap: |
|
package_name = os.path.basename(package) |
|
!aria2c --console-log-level=error --summary-interval=10 -c -x 16 -k 1M -s 16 -d {root_dir} -o {package_name} {package} |
|
if package_name == f"anapnoe-webui-deps.tar.lz4": |
|
!tar -xI lz4 -f {package_name} --overwrite-dir --directory=/usr/local/lib/python3.10/dist-packages/ |
|
else: |
|
!tar -xI lz4 -f {package_name} --directory=/ |
|
os.remove(package_name) |
|
del cap |
|
|
|
if os.path.exists("/usr/local/lib/python3.10/dist-packages/ffmpy-0.3.0.dist-info"): |
|
shutil.rmtree("/usr/local/lib/python3.10/dist-packages/ffmpy-0.3.0.dist-info") |
|
|
|
s = getoutput("nvidia-smi") |
|
with capture.capture_output() as cap: |
|
if not "T4" in s: |
|
!pip uninstall -y xformers |
|
!pip install -q xformers==0.0.18 triton |
|
del cap |
|
|
|
|
|
def read_config(filename): |
|
if filename.endswith(".json"): |
|
with open(filename, "r") as f: |
|
config = json.load(f) |
|
else: |
|
with open(filename, 'r') as f: |
|
config = f.read() |
|
return config |
|
|
|
|
|
def write_config(filename, config): |
|
if filename.endswith(".json"): |
|
with open(filename, "w") as f: |
|
json.dump(config, f, indent=4) |
|
else: |
|
with open(filename, 'w', encoding="utf-8") as f: |
|
f.write(config) |
|
|
|
|
|
def open_theme(filename): |
|
themes_folder = os.path.join(webui_dir, "extensions-builtin/sd_theme_editor/themes") |
|
themes_file = os.path.join(themes_folder, f"{filename}.css") |
|
webui_style_path = os.path.join(webui_dir, "style.css") |
|
|
|
style_config = read_config(webui_style_path) |
|
style_css_contents = style_config.split("/*BREAKPOINT_CSS_CONTENT*/")[1] |
|
|
|
theme_config = read_config(themes_file) |
|
style_data = ":host{" + theme_config + "}" + "/*BREAKPOINT_CSS_CONTENT*/" + style_css_contents |
|
write_config(webui_style_path, style_data) |
|
|
|
|
|
def change_config(filename): |
|
config = read_config(filename) |
|
if not "stable-diffusion-webui" in config["disabled_extensions"]: |
|
config["disabled_extensions"].append("stable-diffusion-webui") |
|
config["outdir_txt2img_samples"] = os.path.join(tmp_dir, "outputs/txt2img-images") |
|
config["outdir_img2img_samples"] = os.path.join(tmp_dir, "outputs/img2img-images") |
|
config["outdir_extras_samples"] = os.path.join(tmp_dir, "outputs/extras-images") |
|
config["outdir_txt2img_grids"] = os.path.join(tmp_dir, "outputs/txt2img-grids") |
|
config["outdir_img2img_grids"] = os.path.join(tmp_dir, "outputs/img2img-grids") |
|
config["outdir_save"] = os.path.join(tmp_dir, "log/images") |
|
config["control_net_max_models_num"] = control_net_max_models_num |
|
config["control_net_models_path"] = control_dir |
|
config["control_net_allow_script_control"] = True |
|
config["additional_networks_extra_lora_path"] = webui_lora_dir |
|
config["CLIP_stop_at_last_layers"] = 2 |
|
config["eta_noise_seed_delta"] = 0 |
|
config["show_progress_every_n_steps"] = 10 |
|
config["show_progressbar"] = True |
|
config["quicksettings"] = "sd_model_checkpoint, sd_vae, CLIP_stop_at_last_layers, use_old_karras_scheduler_sigmas, always_discard_next_to_last_sigma" |
|
write_config(filename, config) |
|
|
|
|
|
def change_ui_config(filename): |
|
config = read_config(filename) |
|
config["txt2img/Prompt/value"] = default_prompt |
|
config["txt2img/Negative prompt/value"] = default_neg_prompt |
|
config["txt2img/Sampling method/value"] = default_sampler |
|
config["txt2img/Sampling steps/value"] = default_steps |
|
config["txt2img/Width/value"] = default_width |
|
config["txt2img/Height/value"] = default_height |
|
config["txt2img/Upscaler/value"] = "Latent (nearest-exact)" |
|
config["txt2img/Denoising strength/value"] = default_denoising_strength |
|
config["txt2img/CFG Scale/value"] = default_cfg_scale |
|
config["img2img/Prompt/value"] = default_prompt |
|
config["img2img/Negative prompt/value"] = default_neg_prompt |
|
config["img2img/Sampling method/value"] = default_sampler |
|
config["img2img/Sampling steps/value"] = default_steps |
|
config["img2img/Width/value"] = default_width |
|
config["img2img/Height/value"] = default_height |
|
config["img2img/Denoising strength/value"] = default_denoising_strength |
|
config["img2img/CFG Scale/value"] = default_cfg_scale |
|
write_config(filename, config) |
|
|
|
|
|
def update_extensions(): |
|
start_time = time.time() |
|
extensions_updated = [] |
|
with tqdm( |
|
total=len(os.listdir(extensions_dir)), |
|
desc="[1;32mUpdating extensions", |
|
mininterval=0, |
|
) as pbar: |
|
for dir in os.listdir(extensions_dir): |
|
if os.path.isdir(os.path.join(extensions_dir, dir)): |
|
os.chdir(os.path.join(extensions_dir, dir)) |
|
try: |
|
with capture.capture_output() as cap: |
|
!git fetch origin |
|
!git pull |
|
except Exception as e: |
|
print(f"[1;32mAn error occurred while updating {dir}: {e}") |
|
|
|
output = cap.stdout.strip() |
|
if "Already up to date." not in output: |
|
extensions_updated.append(dir) |
|
pbar.update(1) |
|
|
|
print("\n") |
|
for ext in extensions_updated: |
|
print(f"[1;32m- {ext} updated to new version") |
|
|
|
end_time = time.time() |
|
elapsed_time = int(end_time - start_time) |
|
|
|
if elapsed_time < 60: |
|
print(f"\n[1;32mAll extensions are up to date. Took {elapsed_time} sec") |
|
else: |
|
mins, secs = divmod(elapsed_time, 60) |
|
print(f"\n[1;32mAll extensions are up to date. Took {mins} mins {secs} sec") |
|
|
|
|
|
def main(): |
|
start_time = time.time() |
|
|
|
print("[1;32mInstalling...\n") |
|
|
|
if not os.path.exists(webui_dir): |
|
desc = "[1;32mUnpacking Webui" |
|
pre_download(desc) |
|
else: |
|
print("[1;32mAlready installed, skipping...") |
|
|
|
with capture.capture_output() as cap: |
|
os.chdir(os.path.join(webui_dir, "repositories/stable-diffusion-stability-ai")) |
|
!git apply {patches_dir}/stablediffusion-lowram.patch |
|
|
|
!sed -i "s@os.path.splitext(checkpoint_.*@os.path.splitext(checkpoint_file); map_location='cuda'@" {webui_dir}/modules/sd_models.py |
|
!sed -i 's@ui.create_ui().*@ui.create_ui();shared.demo.queue(concurrency_count=999999,status_update_rate=0.1)@' {webui_dir}/webui.py |
|
|
|
!sed -i "s@'cpu'@'cuda'@" {webui_dir}/modules/extras.py |
|
del cap |
|
|
|
end_time = time.time() |
|
elapsed_time = int(end_time - start_time) |
|
|
|
change_config(config_file) |
|
change_ui_config(ui_config_file) |
|
open_theme(theme) |
|
|
|
if elapsed_time < 60: |
|
print(f"[1;32mFinished unpacking. Took {elapsed_time} sec") |
|
else: |
|
mins, secs = divmod(elapsed_time, 60) |
|
print(f"[1;32mFinished unpacking. Took {mins} mins {secs} sec") |
|
|
|
update_extensions() |
|
|
|
|
|
ngrok_token = "" |
|
ngrok_region = "ap" |
|
|
|
with capture.capture_output() as cap: |
|
for file in os.listdir(output_dir): |
|
file_path = os.path.join(output_dir, file) |
|
if file_path.endswith((".safetensors", ".pt", ".ckpt")): |
|
!ln "{file_path}" {webui_lora_dir} |
|
|
|
for file in os.listdir(pretrained_model): |
|
file_path = os.path.join(pretrained_model, file) |
|
if file_path.endswith((".safetensors", ".ckpt")): |
|
!ln "{file_path}" {webui_models_dir} |
|
|
|
for file in os.listdir(vae_dir): |
|
file_path = os.path.join(vae_dir, file) |
|
if file_path.endswith(".vae.pt"): |
|
!ln "{file_path}" {webui_vaes_dir} |
|
|
|
del cap |
|
|
|
os.chdir(webui_dir) |
|
|
|
print("[1;32m") |
|
|
|
config = { |
|
"enable-insecure-extension-access": True, |
|
"disable-safe-unpickle": True, |
|
"multiple": True if not ngrok_token else False, |
|
"ckpt-dir": webui_models_dir, |
|
"vae-dir": webui_vaes_dir, |
|
"share": True if not ngrok_token else False, |
|
"no-half-vae": True, |
|
"lowram": True, |
|
"gradio-queue": True, |
|
"no-hashing": True, |
|
"disable-console-progressbars": True, |
|
"ngrok": ngrok_token if ngrok_token else None, |
|
"ngrok-region": ngrok_region if ngrok_token else None, |
|
"xformers": True, |
|
"opt-sub-quad-attention": True, |
|
"opt-channelslast": True, |
|
"theme": "dark" |
|
} |
|
|
|
args = "" |
|
for k, v in config.items(): |
|
if k.startswith("_"): |
|
args += f'"{v}" ' |
|
elif isinstance(v, str): |
|
args += f'--{k}="{v}" ' |
|
elif isinstance(v, bool) and v: |
|
args += f"--{k} " |
|
elif isinstance(v, float) and not isinstance(v, bool): |
|
args += f"--{k}={v} " |
|
elif isinstance(v, int) and not isinstance(v, bool): |
|
args += f"--{k}={v} " |
|
|
|
final_args = f"python launch.py {args}" |
|
|
|
os.chdir(webui_dir) |
|
!{final_args} |
|
|
|
main() |
|
|
|
"""# VII. Deployment""" |
|
|
|
|
|
from huggingface_hub import login |
|
from huggingface_hub import HfApi |
|
from huggingface_hub.utils import validate_repo_id, HfHubHTTPError |
|
|
|
|
|
|
|
write_token = "" |
|
|
|
orgs_name = "" |
|
|
|
model_name = "your-model-name" |
|
dataset_name = "your-dataset-name" |
|
make_private = False |
|
|
|
def authenticate(write_token): |
|
login(write_token, add_to_git_credential=True) |
|
api = HfApi() |
|
return api.whoami(write_token), api |
|
|
|
|
|
def create_repo(api, user, orgs_name, repo_name, repo_type, make_private=False): |
|
global model_repo |
|
global datasets_repo |
|
|
|
if orgs_name == "": |
|
repo_id = user["name"] + "/" + repo_name.strip() |
|
else: |
|
repo_id = orgs_name + "/" + repo_name.strip() |
|
|
|
try: |
|
validate_repo_id(repo_id) |
|
api.create_repo(repo_id=repo_id, repo_type=repo_type, private=make_private) |
|
print(f"{repo_type.capitalize()} repo '{repo_id}' didn't exist, creating repo") |
|
except HfHubHTTPError as e: |
|
print(f"{repo_type.capitalize()} repo '{repo_id}' exists, skipping create repo") |
|
|
|
if repo_type == "model": |
|
model_repo = repo_id |
|
print(f"{repo_type.capitalize()} repo '{repo_id}' link: https://huggingface.co/{repo_id}\n") |
|
else: |
|
datasets_repo = repo_id |
|
print(f"{repo_type.capitalize()} repo '{repo_id}' link: https://huggingface.co/datasets/{repo_id}\n") |
|
|
|
user, api = authenticate(write_token) |
|
|
|
if model_name: |
|
create_repo(api, user, orgs_name, model_name, "model", make_private) |
|
if dataset_name: |
|
create_repo(api, user, orgs_name, dataset_name, "dataset", make_private) |
|
|
|
"""## 7.2. Upload with Huggingface Hub""" |
|
|
|
|
|
|
|
from huggingface_hub import HfApi |
|
from pathlib import Path |
|
|
|
|
|
|
|
api = HfApi() |
|
|
|
|
|
model_path = "/content/LoRA/output" |
|
path_in_repo = "" |
|
|
|
|
|
config_path = "/content/LoRA/config" |
|
|
|
|
|
commit_message = "" |
|
|
|
if not commit_message: |
|
commit_message = f"feat: upload {project_name} lora model" |
|
|
|
def upload_to_hf(model_path, is_folder, is_config): |
|
path_obj = Path(model_path) |
|
trained_model = path_obj.parts[-1] |
|
|
|
if path_in_repo: |
|
trained_model = path_in_repo |
|
|
|
if is_config: |
|
trained_model = f"{project_name}_config" |
|
|
|
print(f"Uploading {trained_model} to https://huggingface.co/{model_repo}") |
|
print("Please wait...") |
|
|
|
if is_folder: |
|
api.upload_folder( |
|
folder_path=model_path, |
|
path_in_repo=trained_model, |
|
repo_id=model_repo, |
|
commit_message=commit_message, |
|
ignore_patterns=".ipynb_checkpoints", |
|
) |
|
print(f"Upload success, located at https://huggingface.co/{model_repo}/tree/main\n") |
|
else: |
|
api.upload_file( |
|
path_or_fileobj=model_path, |
|
path_in_repo=trained_model, |
|
repo_id=model_repo, |
|
commit_message=commit_message, |
|
) |
|
print(f"Upload success, located at https://huggingface.co/{model_repo}/blob/main/{trained_model}\n") |
|
|
|
def upload(): |
|
is_model_file = model_path.endswith((".ckpt", ".safetensors", ".pt")) |
|
upload_to_hf(model_path, not is_model_file, False) |
|
|
|
if config_path: |
|
upload_to_hf(config_path, True, True) |
|
|
|
upload() |
|
|
|
|
|
from huggingface_hub import HfApi |
|
from pathlib import Path |
|
import shutil |
|
import zipfile |
|
import os |
|
|
|
api = HfApi() |
|
|
|
|
|
train_data_path = "/content/LoRA/train_data" |
|
|
|
|
|
logs_path = "/content/LoRA/logs" |
|
|
|
tmp_dataset = f"/content/LoRA/{project_name}_dataset" if project_name else "/content/LoRA/tmp_dataset" |
|
tmp_train_data = f"{tmp_dataset}/train_data" |
|
dataset_zip = f"{tmp_dataset}.zip" |
|
|
|
|
|
commit_message = "" |
|
|
|
if not commit_message: |
|
commit_message = f"feat: upload {project_name} dataset and logs" |
|
|
|
os.makedirs(tmp_dataset, exist_ok=True) |
|
os.makedirs(tmp_train_data, exist_ok=True) |
|
|
|
def upload_dataset(dataset_path, is_zip): |
|
path_obj = Path(dataset_path) |
|
dataset_name = path_obj.parts[-1] |
|
|
|
print(f"Uploading {dataset_name} to https://huggingface.co/datasets/{datasets_repo}") |
|
print("Please wait...") |
|
|
|
if is_zip: |
|
api.upload_file( |
|
path_or_fileobj=dataset_path, |
|
path_in_repo=dataset_name, |
|
repo_id=datasets_repo, |
|
repo_type="dataset", |
|
commit_message=commit_message, |
|
) |
|
print(f"Upload success, located at https://huggingface.co/datasets/{datasets_repo}/blob/main/{dataset_name}\n") |
|
else: |
|
api.upload_folder( |
|
folder_path=dataset_path, |
|
path_in_repo=dataset_name, |
|
repo_id=datasets_repo, |
|
repo_type="dataset", |
|
commit_message=commit_message, |
|
ignore_patterns=".ipynb_checkpoints", |
|
) |
|
print(f"Upload success, located at https://huggingface.co/datasets/{datasets_repo}/tree/main/{dataset_name}\n") |
|
|
|
def zip_file(folder_path): |
|
zip_path = f"{folder_path}.zip" |
|
with zipfile.ZipFile(zip_path, "w") as zip_file: |
|
for root, dirs, files in os.walk(folder_path): |
|
for file in files: |
|
zip_file.write(os.path.join(root, file)) |
|
|
|
def move(src_path, dst_path, move_metadata): |
|
metadata_files = [ |
|
"meta_cap.json", |
|
"meta_cap_dd.json", |
|
"meta_lat.json", |
|
"meta_clean.json", |
|
"meta_final.json", |
|
] |
|
|
|
if os.path.exists(src_path): |
|
shutil.move(src_path, dst_path) |
|
|
|
if move_metadata: |
|
parent_meta_path = os.path.dirname(src_path) |
|
|
|
for filename in os.listdir(parent_meta_path): |
|
file_path = os.path.join(parent_meta_path, filename) |
|
if filename in metadata_files: |
|
shutil.move(file_path, dst_path) |
|
|
|
def upload(): |
|
if train_data_path: |
|
move(train_data_path, tmp_train_data, False) |
|
zip_file(tmp_dataset) |
|
upload_dataset(dataset_zip, True) |
|
os.remove(dataset_zip) |
|
if logs_path: |
|
upload_dataset(logs_path, False) |
|
|
|
upload() |
|
|
|
"""## 7.3. Upload with GIT (Alternative)""" |
|
|
|
|
|
|
|
clone_model = True |
|
clone_dataset = True |
|
|
|
def clone_repository(repo_url, local_path): |
|
!git lfs install --skip-smudge |
|
os.environ["GIT_LFS_SKIP_SMUDGE"] = "1" |
|
!git clone {repo_url} {local_path} |
|
|
|
if clone_model: |
|
clone_repository(f"https://huggingface.co/{model_repo}", f"/content/{model_name}") |
|
|
|
if clone_dataset: |
|
clone_repository(f"https://huggingface.co/datasets/{datasets_repo}", f"/content/{dataset_name}") |
|
|
|
|
|
import os |
|
|
|
os.chdir(root_dir) |
|
|
|
|
|
commit_model = True |
|
commit_dataset = True |
|
|
|
commit_message = "" |
|
|
|
if not commit_message: |
|
commit_message = f"feat: upload {project_name} lora model and dataset" |
|
|
|
!git config --global user.email "example@mail.com" |
|
!git config --global user.name "example" |
|
|
|
def commit(repo_folder, commit_message): |
|
os.chdir(os.path.join(root_dir, repo_folder)) |
|
!git lfs install |
|
!huggingface-cli lfs-enable-largefiles . |
|
!git add . |
|
!git commit -m "{commit_message}" |
|
!git push |
|
|
|
|
|
if commit_model: |
|
commit(model_name, commit_message) |
|
|
|
if commit_dataset: |
|
commit(dataset_name, commit_message) |