File size: 12,136 Bytes
be3b1f2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"source": [
"#**PLAYGROUND V2.5 LORA CREATOR - Adapted from SD XL Lora training by linoytsaban*"
],
"metadata": {
"id": "RvbNnswz1Bwy"
}
},
{
"cell_type": "markdown",
"source": [
"# Install necessary libs."
],
"metadata": {
"id": "Pxl1NJ4ew_22"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "krdOGpbewX6D"
},
"outputs": [],
"source": [
"!pip install huggingface_hub datasets pillow xformers bitsandbytes transformers accelerate wandb dadaptation prodigyopt torch -q\n"
]
},
{
"cell_type": "code",
"source": [
"!pip install peft -q"
],
"metadata": {
"id": "LmsPVnS8xpWB"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!pip install git+https://github.com/huggingface/diffusers.git -q"
],
"metadata": {
"id": "yAKkbjNsxp4n"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Downloading the script!"
],
"metadata": {
"id": "gYT0P8sMxtOE"
}
},
{
"cell_type": "code",
"source": [
"!wget https://raw.githubusercontent.com/huggingface/diffusers/main/examples/advanced_diffusion_training/train_dreambooth_lora_sdxl_advanced.py"
],
"metadata": {
"id": "ehtWgVcdxwJT"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!wget https://gist.githubusercontent.com/artificialguybr/a1c58ad578d0446d493c9793093196e1/raw/b8cede5d9ad5f9911de70ac187c6b078eea693c8/sd-dataset-to-hf-dataset.py"
],
"metadata": {
"id": "rvrh-vLexwlI"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!accelerate config default"
],
"metadata": {
"id": "cepNwXbXyBa-"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Login with your HuggingFace Account!"
],
"metadata": {
"id": "PTJdDX4AxE2m"
}
},
{
"cell_type": "code",
"source": [
"from huggingface_hub import notebook_login\n",
"notebook_login()"
],
"metadata": {
"id": "nKtKBrgVxGpb"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Convert your currently SD XL (Kohya) style Dataset to HF Dataset."
],
"metadata": {
"id": "HLDTiVlJyI4g"
}
},
{
"cell_type": "code",
"source": [
"# Required imports\n",
"import os\n",
"from huggingface_hub import HfApi, login\n",
"from datasets import Dataset, Features, Value, Image\n",
"from PIL import Image as PILImage\n",
"import io\n",
"\n",
"## Configuration\n",
"# Define the directory where your images and texts are stored\n",
"data_folder = \"\" # Example: \"/content/drive/MyDrive/dataset\"\n",
"\n",
"# Name your dataset as desired\n",
"dataset_name = \"\" # Example: \"my-first-dataset\"\n",
"dataset_repo_id = \"\" # Example: \"your_username/my-first-dataset\"\n",
"hf_token = \"\" # Your Hugging Face token\n",
"\n",
"## Initialize the Hugging Face API\n",
"api = HfApi()\n",
"login(hf_token)\n",
"\n",
"## Define the dataset features\n",
"features = Features({\n",
" \"image\": Image(),\n",
" \"text\": Value(\"string\"),\n",
"})\n",
"\n",
"## Create a dictionary to store the corresponding images and texts\n",
"data = {\"image\": [], \"text\": []}\n",
"\n",
"## Accepted image file extensions\n",
"image_extensions = [\".png\", \".jpg\", \".jpeg\", \".gif\"]\n",
"\n",
"## Iterate through image files\n",
"for filename in os.listdir(data_folder):\n",
" # Check if the file has an accepted image extension\n",
" if any(filename.lower().endswith(ext) for ext in image_extensions):\n",
" image_path = os.path.join(data_folder, filename)\n",
" txt_filename = os.path.splitext(filename)[0] + \".txt\"\n",
" txt_path = os.path.join(data_folder, txt_filename)\n",
"\n",
" if os.path.exists(txt_path):\n",
" # Read the content of the text file\n",
" with open(txt_path, \"r\") as txt_file:\n",
" text = txt_file.read()\n",
"\n",
" # Load the image using the pillow library\n",
" with PILImage.open(image_path) as image:\n",
" # Convert the image to the format expected by Hugging Face Datasets\n",
" image_bytes = io.BytesIO()\n",
" image.save(image_bytes, format='PNG')\n",
" image_bytes = image_bytes.getvalue()\n",
"\n",
" # Add the image and text to the dictionary\n",
" data[\"image\"].append(image_bytes)\n",
" data[\"text\"].append(text)\n",
"\n",
"## Create the dataset on Hugging Face\n",
"dataset = Dataset.from_dict(data, features=features)\n",
"dataset.push_to_hub(dataset_repo_id, private=True) # Set private=False if you want the dataset to be public\n",
"\n",
"print(\"Dataset successfully uploaded to Hugging Face!\")\n"
],
"metadata": {
"id": "jYTgzfRwyDfL"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!pip install python-slugify\n",
"from slugify import slugify\n",
"\n",
"model_name = \"3d icon SDXL LoRA\" # @param\n",
"output_dir = slugify(model_name)"
],
"metadata": {
"id": "I-EcR3g6zS7o"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# **Instance & Validation Prompt**\n",
"* `instance_prompt` -\n",
" * when custom captions are enabled this prompt is still used in case there are missing captions, as well as in the model's readme.\n",
" * If custom captions are not used, this prompt will be used as the caption for all training images.\n",
"* `validation_prompt` -\n",
" * this prompt is used to generate images throught the training process, this way you can see the models learning curve during training.\n",
" * you can also change `num_validation_images` (4 by default) and `validation_epochs` (50 by default) to control the amount images generated with the validation prompt, and the number of ephochs between each dreambooth validation. "
],
"metadata": {
"id": "LyuzvbWIzbXC"
}
},
{
"cell_type": "code",
"source": [
"instance_prompt = \"3d icon in the style of TOK\" # @param\n",
"validation_prompt = \"a TOK icon of an astronaut riding a horse, in the style of TOK\" # @param"
],
"metadata": {
"id": "xIopxUKHzYcU"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"**Set your LoRA rank**\n",
"The rank of your LoRA is linked to its expressiveness.\n",
"The bigger the rank the closer we are to regular dreambooth, and in theory we have more expressive power (and heavier weights).\n",
"\n",
"For a very simple concept that you have a good high quality image set for (e.g. a pet, a generic object), a rank as low as 4 can be enough to get great results. We reccomend going between 8 and 64 depending on your concept and how much of a priortiy it is for you to keep the LoRA small or not. "
],
"metadata": {
"id": "sb0kvKjtzh9w"
}
},
{
"cell_type": "code",
"source": [
"rank = 8 # @param"
],
"metadata": {
"id": "58pTgSOIziw3"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#Dataset Name/Local/HF"
],
"metadata": {
"id": "DviZbC9mz0RX"
}
},
{
"cell_type": "code",
"source": [
"dataset_name=./3d_icon # @param\n",
"caption_column=prompt # @param"
],
"metadata": {
"id": "VZnq1H7fz2dM"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#!/usr/bin/env bash\n",
"!accelerate launch train_dreambooth_lora_sdxl_advanced.py \\\n",
" --pretrained_model_name_or_path=\"playgroundai/playground-v2.5-1024px-aesthetic\" \\\n",
" --dataset_name=\"$dataset_name\" \\\n",
" --instance_prompt=\"$instance_prompt\" \\\n",
" --validation_prompt=\"$validation_prompt\" \\\n",
" --output_dir=\"$output_dir\" \\\n",
" --caption_column=\"$caption_column\" \\\n",
" --do_edm_style_training \\\n",
" --mixed_precision=\"bf16\" \\\n",
" --resolution=1024 \\\n",
" --train_batch_size=3 \\\n",
" --repeats=1 \\\n",
" --report_to=\"wandb\"\\\n",
" --gradient_accumulation_steps=1 \\\n",
" --gradient_checkpointing \\\n",
" --learning_rate=1e-5 \\\n",
" --optimizer=\"AdamW\"\\\n",
" --lr_scheduler=\"constant\" \\\n",
" --rank=\"$rank\" \\\n",
" --max_train_steps=2000 \\\n",
" --checkpointing_steps=2000 \\\n",
" --seed=\"0\" \\\n",
" --push_to_hub"
],
"metadata": {
"id": "P3IAGgF9zmig"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from huggingface_hub import whoami\n",
"from pathlib import Path\n",
"from IPython.display import display, Markdown\n",
"\n",
"username = whoami(token=Path(\"/root/.cache/huggingface/\"))[\"name\"]\n",
"repo_id = f\"{username}/{output_dir}\"\n",
"\n",
"link_to_model = f\"https://huggingface.co/{repo_id}\"\n",
"\n",
"display(Markdown(\"### Your model has finished training.\\nAccess it here: {}\".format(link_to_model)))"
],
"metadata": {
"id": "NkY5Sviy0fHJ"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#LOAD INFERENCE"
],
"metadata": {
"id": "t8gtC0kf0WX0"
}
},
{
"cell_type": "code",
"source": [
"import torch\n",
"from huggingface_hub import hf_hub_download, upload_file\n",
"from diffusers import DiffusionPipeline\n",
"from diffusers.models import AutoencoderKL\n",
"from safetensors.torch import load_file\n",
"\n",
"pipe = DiffusionPipeline.from_pretrained(\n",
" \"playgroundai/playground-v2.5-1024px-aesthetic\",\n",
" torch_dtype=torch.float16,\n",
" variant=\"fp16\",\n",
").to(\"cuda\")\n",
"\n",
"pipe.load_lora_weights(repo_id, weight_name=\"pytorch_lora_weights.safetensors\")\n"
],
"metadata": {
"id": "Ib7aaWVQ0mdT"
},
"execution_count": null,
"outputs": []
}
]
} |