EveryDream2trainer/Train_Colab.ipynb

732 lines
33 KiB
Plaintext
Raw Normal View History

2023-01-21 23:16:30 -07:00
{
"cells": [
{
2023-06-23 20:55:57 -06:00
"attachments": {},
2023-01-21 23:16:30 -07:00
"cell_type": "markdown",
"metadata": {
2023-06-23 20:55:57 -06:00
"colab_type": "text",
"id": "view-in-github"
2023-01-21 23:16:30 -07:00
},
"source": [
2023-01-23 20:03:48 -07:00
"<a href=\"https://colab.research.google.com/github/nawnie/EveryDream2trainer/blob/main/Train_Colab.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
2023-01-21 23:16:30 -07:00
]
},
{
2023-06-23 20:55:57 -06:00
"attachments": {},
2023-01-21 23:16:30 -07:00
"cell_type": "markdown",
"metadata": {
"id": "blaLMSbkPHhG"
},
"source": [
2023-05-24 01:24:38 -06:00
"<p align=\"center\">\n",
" <img src=\"https://github.com/victorchall/EveryDream2trainer/blob/562c4341137d1d9f5bf525e6c56fb4b1eefa2b57/doc/ed_logo_comp.jpg?raw=true\" width=\"600\" height=\"300\">\n",
"</p>\n",
2023-01-22 22:12:03 -07:00
"\n",
2023-05-24 01:24:38 -06:00
"<br>\n",
2023-01-22 22:12:03 -07:00
"\n",
2023-05-24 01:24:38 -06:00
"---\n",
"\n",
"<div align=\"center\">\n",
" <font size=\"6\" color=\"yellow\">Colab Edition</font>\n",
"</div>\n",
"\n",
"---\n",
"\n",
"<br>\n",
"\n",
"Check out the **EveryDream2trainer** documentation and runpod/vastai and local setups here: \n",
"\n",
"[📚 **Documentation**](https://github.com/victorchall/EveryDream2trainer#docs)\n",
"\n",
"And join our vibrant community on Discord:\n",
"\n",
"[💬 **Join the Discord**](https://discord.gg/uheqxU6sXN)\n",
"\n",
"If you find this tool useful, please consider subscribing to the project on Patreon or making a one-time donation on Ko-fi. Your donations keep this project alive as a free open-source tool with ongoing enhancements.\n",
"\n",
"<br>\n",
"\n",
"<p align=\"center\">\n",
" <a href=\"https://www.patreon.com/everydream\">\n",
" <img src=\"https://github.com/victorchall/EveryDream2trainer/raw/main/.github/patreon-medium-button.png?raw=true\" width=\"200\" height=\"50\">\n",
" </a>\n",
"</p>\n",
"\n",
"<br>\n",
"\n",
"<p align=\"center\">\n",
" <a href=\"https://ko-fi.com/everydream\">\n",
" <img src=\"https://github.com/victorchall/EveryDream2trainer/raw/main/.github/kofibutton_sm.png?raw=true\" width=\"75\" height=\"75\">\n",
" </a>\n",
"</p>\n"
2023-01-21 23:16:30 -07:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
2023-06-03 01:20:40 -06:00
"cellView": "form",
"id": "hAuBbtSvGpau"
2023-01-21 23:16:30 -07:00
},
"outputs": [],
"source": [
2023-05-17 14:29:08 -06:00
"#@markdown # Setup and Install Dependencies\n",
2023-05-24 01:24:38 -06:00
"from IPython.display import clear_output, display, HTML\n",
2023-05-17 14:29:08 -06:00
"import subprocess\n",
"import time\n",
"import os \n",
2023-05-24 01:24:38 -06:00
"from tqdm.auto import tqdm\n",
"import PIL\n",
"\n",
"# Defining function for colored text\n",
"def colored(r, g, b, text):\n",
" return f\"\\033[38;2;{r};{g};{b}m{text} \\033[38;2;255;255;255m\"\n",
"\n",
"#@markdown Optional connect Gdrive But strongly recommended\n",
2023-05-24 01:24:38 -06:00
"#@markdown This will let you put all your training data and checkpoints directly on your drive. \n",
"#@markdown Much faster/easier to continue later, less setup time.\n",
2023-02-28 08:35:12 -07:00
"\n",
"#@markdown Creates /content/drive/MyDrive/everydreamlogs/ckpt\n",
"Mount_to_Gdrive = True #@param{type:\"boolean\"} \n",
"\n",
2023-05-24 01:24:38 -06:00
"# Clone the git repository\n",
"print(colored(0, 255, 0, 'Cloning git repository...'))\n",
2023-05-17 14:29:08 -06:00
"!git clone https://github.com/victorchall/EveryDream2trainer.git\n",
"\n",
"if Mount_to_Gdrive:\n",
" from google.colab import drive\n",
" drive.mount('/content/drive')\n",
"\n",
" !mkdir -p /content/drive/MyDrive/everydreamlogs/ckpt\n",
"\n",
2023-05-19 15:18:03 -06:00
"%cd /content/EveryDream2trainer\n",
"\n",
2023-05-24 01:24:38 -06:00
"# Download Arial font\n",
"print(colored(0, 255, 0, 'Downloading Arial font...'))\n",
2023-05-19 15:18:03 -06:00
"!wget -O arial.ttf https://raw.githubusercontent.com/matomo-org/travis-scripts/master/fonts/Arial.ttf\n",
"\n",
"packages = [\n",
2023-06-03 01:20:40 -06:00
" 'transformers==4.29.2',\n",
" 'diffusers[torch]==0.17.1',\n",
" 'pynvml==11.4.1',\n",
" 'bitsandbytes==0.38.1',\n",
" 'ftfy==6.1.1',\n",
2023-05-19 15:18:03 -06:00
" 'aiohttp==3.8.4',\n",
2023-05-17 14:29:08 -06:00
" 'compel~=1.1.3',\n",
2023-06-03 01:20:40 -06:00
" 'protobuf==3.20.1',\n",
" 'wandb==0.15.3',\n",
" 'pyre-extensions==0.0.29',\n",
2023-05-24 01:24:38 -06:00
" 'xformers==0.0.20',\n",
2023-06-03 01:20:40 -06:00
" 'pytorch-lightning==1.6.5',\n",
" 'OmegaConf==2.2.3',\n",
2023-06-03 01:20:40 -06:00
" 'tensorboard>=2.11.0',\n",
" 'tensorrt'\n",
2023-05-17 14:29:08 -06:00
" 'wandb',\n",
" 'colorama',\n",
" 'keyboard',\n",
2023-05-17 14:29:08 -06:00
" 'lion-pytorch'\n",
"]\n",
"\n",
2023-05-24 01:24:38 -06:00
"print(colored(0, 255, 0, 'Installing packages...'))\n",
"for package in tqdm(packages, desc='Installing packages', unit='package', bar_format='{l_bar}{bar}| {n_fmt}/{total_fmt}'):\n",
" if isinstance(package, tuple):\n",
" package_name, extra_index_url = package\n",
2023-05-24 01:24:38 -06:00
" cmd = f\"pip install -I -q {package_name} --extra-index-url {extra_index_url}\"\n",
" else:\n",
" cmd = f\"pip install -q {package}\"\n",
" \n",
" subprocess.run(cmd, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)\n",
"\n",
2023-02-28 08:35:12 -07:00
"clear_output()\n",
"\n",
2023-05-17 14:29:08 -06:00
"\n",
2023-05-24 01:24:38 -06:00
"# Execute Python script\n",
"print(colored(0, 255, 0, 'Executing Python script...'))\n",
2023-02-28 08:35:12 -07:00
"!python utils/get_yamls.py\n",
"clear_output()\n",
2023-05-24 01:24:38 -06:00
"\n",
"print(colored(0, 255, 0, \"DONE! installing dependencies.\"))\n",
"\n",
"# Import pynvml and get GPU details\n",
"import pynvml\n",
"\n",
"pynvml.nvmlInit()\n",
"\n",
"handle = pynvml.nvmlDeviceGetHandleByIndex(0)\n",
"\n",
"gpu_name = pynvml.nvmlDeviceGetName(handle)\n",
"gpu_memory = pynvml.nvmlDeviceGetMemoryInfo(handle).total / 1024**3\n",
"cuda_version_number = pynvml.nvmlSystemGetCudaDriverVersion_v2()\n",
"cuda_version_major = cuda_version_number // 1000\n",
"cuda_version_minor = (cuda_version_number % 1000) // 10\n",
"cuda_version = f\"{cuda_version_major}.{cuda_version_minor}\"\n",
"\n",
"pynvml.nvmlShutdown()\n",
"\n",
2023-05-19 15:18:03 -06:00
"Python_version = !python --version\n",
"import torch\n",
"import torchvision\n",
"import xformers\n",
2023-05-24 01:24:38 -06:00
"\n",
"display(HTML(f\"\"\"\n",
"<table style=\"background-color:transparent;\">\n",
" <tr>\n",
" <td style=\"background-color:transparent;\"><span style=\"color: #FFFF00;\">Python version:</span></td>\n",
" <td style=\"background-color:transparent;\"><span style=\"color: #FFFF00;\">{Python_version[0]}</span></td>\n",
" <td style=\"background-color:transparent;\"><span style=\"color: #FFFF00;\">GPU Name:</span></td>\n",
" <td style=\"background-color:transparent;\"><span style=\"color: #FFFF00;\">{gpu_name}</span></td>\n",
" </tr>\n",
" <tr>\n",
" <td style=\"background-color:transparent;\"><span style=\"color: #FFFF00;\">PyTorch version:</span></td>\n",
" <td style=\"background-color:transparent;\"><span style=\"color: #FFFF00;\">{torch.__version__}</span></td>\n",
" <td style=\"background-color:transparent;\"><span style=\"color: #FFFF00;\">GPU Memory (GB):</span></td>\n",
" <td style=\"background-color:transparent;\"><span style=\"color: #FFFF00;\">{gpu_memory:.2f}</span></td>\n",
" </tr>\n",
" <tr>\n",
" <td style=\"background-color:transparent;\"><span style=\"color: #FFFF00;\">Torchvision version:</span></td>\n",
" <td style=\"background-color:transparent;\"><span style=\"color: #FFFF00;\">{torchvision.__version__}</span></td>\n",
" <td style=\"background-color:transparent;\"><span style=\"color: #FFFF00;\">CUDA version:</span></td>\n",
" <td style=\"background-color:transparent;\"><span style=\"color: #FFFF00;\">{cuda_version}</span></td>\n",
" </tr>\n",
" <tr>\n",
" <td style=\"background-color:transparent;\"><span style=\"color: #FFFF00;\">XFormers version:</span></td>\n",
" <td style=\"background-color:transparent;\"><span style=\"color: #FFFF00;\">{xformers.__version__}</span></td>\n",
" </tr>\n",
"</table>\n",
"\"\"\"))\n",
"\n",
2023-06-03 01:20:40 -06:00
"time.sleep(2)"
2023-02-28 08:35:12 -07:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
2023-06-23 20:55:57 -06:00
"cellView": "form",
"id": "unaffeqGP_0A"
2023-02-28 08:35:12 -07:00
},
"outputs": [],
"source": [
"#@title Get A Base Model\n",
2023-04-03 17:01:10 -06:00
"#@markdown Choose SD1.5, Waifu Diffusion 1.3, SD2.1, or 2.1(512) from the dropdown, or paste your own URL in the box\n",
2023-05-24 01:24:38 -06:00
"#@markdown * Alternately you can link to an HF repo using NAME/MODEL, this does not save to your Gdrive, if you want to save an HF model, use the direct URL\n",
2023-05-17 14:29:08 -06:00
"\n",
"#@markdown * Link to a set of diffusers on your Gdrive\n",
"\n",
2023-05-24 01:24:38 -06:00
"#@markdown * Paste a URL, atm there is no support for .safetensors\n",
2023-02-28 08:35:12 -07:00
"\n",
"from IPython.display import clear_output\n",
"!mkdir input\n",
"%cd /content/EveryDream2trainer\n",
2023-06-03 01:20:40 -06:00
"MODEL_LOCATION = \"panopstor/EveryDream\" #@param [\"sd_v1-5+vae.ckpt\", \"hakurei/waifu-diffusion-v1-3\", \"stabilityai/stable-diffusion-2-1-base\", \"stabilityai/stable-diffusion-2-1\"] {allow-input: true}\n",
2023-05-24 01:24:38 -06:00
"\n",
2023-04-03 17:01:10 -06:00
"if MODEL_LOCATION == \"sd_v1-5+vae.ckpt\":\n",
" MODEL_LOCATION = \"panopstor/EveryDream\"\n",
2023-05-24 01:24:38 -06:00
"\n",
"If_Ckpt = False\n",
2023-02-28 08:35:12 -07:00
"import os\n",
"\n",
"download_path = \"\"\n",
2023-02-28 08:35:12 -07:00
"\n",
2023-05-24 01:24:38 -06:00
"if \".co\" in MODEL_LOCATION or \"https\" in MODEL_LOCATION or \"www\" in MODEL_LOCATION:\n",
" MODEL_URL = MODEL_LOCATION\n",
" print(\"Downloading...\")\n",
2023-04-03 17:01:10 -06:00
" !wget $MODEL_LOCATION\n",
" clear_output()\n",
2023-05-24 01:24:38 -06:00
" print(\"Download completed!\")\n",
" download_path = os.path.join(os.getcwd(), os.path.basename(MODEL_URL))\n",
"else:\n",
2023-05-24 01:24:38 -06:00
" save_name = MODEL_LOCATION\n",
2023-02-28 08:35:12 -07:00
"\n",
"%cd /content/EveryDream2trainer\n",
"\n",
"inference_yaml = \" \"\n",
"\n",
"# Check if the downloaded or copied model is a .ckpt file\n",
2023-05-17 14:29:08 -06:00
"#@markdown Is the model 1.5 or 2.1 based?\n",
2023-06-03 01:20:40 -06:00
"model_type = \"SD1x\" #@param [\"SD1x\", \"SD2_512_base\", \"SD21\"]\n",
"\n",
2023-05-17 14:29:08 -06:00
"if download_path.endswith(\".ckpt\") or MODEL_LOCATION.endswith(\".ckpt\"):\n",
2023-05-24 01:24:38 -06:00
" If_Ckpt = True\n",
" save_path = download_path\n",
" if \".ckpt\" in save_name:\n",
" save_name = save_name.replace(\".ckpt\", \"\")\n",
2023-05-17 14:29:08 -06:00
" \n",
" img_size = 512\n",
" upscale_attention = False\n",
" prediction_type = \"epsilon\"\n",
2023-05-24 01:24:38 -06:00
"\n",
" if model_type == \"SD1x\":\n",
" inference_yaml = \"v1-inference.yaml\"\n",
" elif model_type == \"SD2_512_base\":\n",
" upscale_attention = True\n",
" inference_yaml = \"v2-inference.yaml\"\n",
" elif model_type == \"SD21\":\n",
" upscale_attention = True\n",
" prediction_type = \"v_prediction\"\n",
" inference_yaml = \"v2-inference-v.yaml\"\n",
" img_size = 768\n",
"\n",
2023-05-24 01:24:38 -06:00
" !python utils/convert_original_stable_diffusion_to_diffusers.py --scheduler_type ddim \\\n",
" --original_config_file $inference_yaml \\\n",
" --image_size $img_size \\\n",
2023-05-17 14:29:08 -06:00
" --checkpoint_path $MODEL_LOCATION \\\n",
" --prediction_type $prediction_type \\\n",
" --upcast_attn False \\\n",
" --dump_path $save_name\n",
"\n",
2023-05-24 01:24:38 -06:00
"# Set the save path to the GDrive directory if cache_to_gdrive is True\n",
"if If_Ckpt:\n",
" save_name = os.path.join(\"/content/drive/MyDrive/everydreamlogs/ckpt\", save_name)\n",
2023-05-17 14:29:08 -06:00
"\n",
"if inference_yaml != \" \":\n",
2023-05-24 01:24:38 -06:00
" print(\"Model saved to: \" + save_name + \". The \" + inference_yaml + \" was used!\")\n",
"\n",
2023-06-03 01:20:40 -06:00
"print(\"Model \" + save_name + \" will be used!\")"
2023-02-28 08:35:12 -07:00
]
},
{
2023-06-23 20:55:57 -06:00
"attachments": {},
2023-02-28 08:35:12 -07:00
"cell_type": "markdown",
2023-03-30 07:15:48 -06:00
"metadata": {
"id": "EHyFzKWXX9kB"
},
2023-02-28 08:35:12 -07:00
"source": [
2023-03-09 02:37:17 -07:00
"# Training\n",
2023-02-28 08:35:12 -07:00
"\n",
2023-03-09 02:37:17 -07:00
"For a more indepth Explanation of each of these paramaters check out /content/EveryDream2trainer/doc.\n",
2023-02-28 08:35:12 -07:00
"\n",
2023-04-14 01:11:34 -06:00
"After youve tried a few models you will find /content/EveryDream2trainer/doc/ADVANCED_TWEAKING.md to be extremly helpful."
2023-03-30 07:15:48 -06:00
]
2023-02-28 08:35:12 -07:00
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
2023-06-23 20:55:57 -06:00
"cellView": "form",
"id": "j9pEI69WXS9w"
2023-02-28 08:35:12 -07:00
},
"outputs": [],
"source": [
2023-03-09 02:37:17 -07:00
"from google.colab import runtime\n",
2023-05-17 14:29:08 -06:00
"from IPython.display import clear_output, display\n",
"import time\n",
"import sys\n",
2023-03-09 02:37:17 -07:00
"import time\n",
"import shutil\n",
2023-05-17 14:29:08 -06:00
"import json\n",
"import os\n",
"\n",
2023-03-09 02:37:17 -07:00
"#@title #Run Everydream 2\n",
2023-02-28 08:35:12 -07:00
"%cd /content/EveryDream2trainer\n",
"#@markdown If you want to use a .json config or upload your own, skip this cell and run the cell below instead\n",
2023-04-04 11:40:34 -06:00
"!rm -r /content/EveryDream2trainer/Training_Data\n",
2023-06-23 20:55:57 -06:00
"#@markdown * Save logs and output ckpts to Gdrive (strongly suggested, will need space)\n",
2023-02-28 08:35:12 -07:00
"Save_to_Gdrive = True #@param{type:\"boolean\"}\n",
2023-06-23 20:55:57 -06:00
"#@markdown * Save extra state information to help continue training (if you will resume later, requires high RAM shape)\n",
2023-06-23 11:16:19 -06:00
"Save_optimizer_state = False #@param{type:\"boolean\"}\n",
2023-03-09 02:37:17 -07:00
"#@markdown * Disconnect after training to save Credits \n",
"Disconnect_after_training = False #@param{type:\"boolean\"}\n",
2023-02-28 08:35:12 -07:00
"#@markdown * Use resume to contnue training you just ran, will also find latest diffusers log in your Gdrive to continue.\n",
"resume = False #@param{type:\"boolean\"}\n",
"#@markdown * Name your project so you can find it in your logs\n",
"Project_Name = \"My_Project\" #@param{type: 'string'}\n",
"\n",
2023-06-03 01:20:40 -06:00
"\n",
"\n",
"if model_type == 'SD2_512_base' or model_type == 'SD21':\n",
" file_path = \"/content/EveryDream2trainer/optimizerSD21.json\"\n",
"else:\n",
" file_path = \"/content/EveryDream2trainer/optimizer.json\"\n",
"\n",
"with open(file_path, 'r') as file:\n",
2023-05-17 14:29:08 -06:00
" data = json.load(file)\n",
"\n",
2023-06-03 01:20:40 -06:00
"\n",
2023-02-28 08:35:12 -07:00
"#@markdown * The learning rate affects how much \"training\" is done on the model per training step. It is a very careful balance to select a value that will learn your data and not wreck the model. \n",
"#@markdown Leave this default unless you are very comfortable with training and know what you are doing.\n",
"Learning_Rate = 1e-6 #@param{type: 'number'}\n",
2023-05-17 14:29:08 -06:00
"#@markdown * chosing this will allow you to ignore any settings specific to the text encode and will match it with the Unets settings, recommended for beginers.\n",
"Match_text_to_Unet = False #@param{type:\"boolean\"}\n",
2023-06-03 01:20:40 -06:00
"Text_lr = 5e-7 #@param {type:\"number\"}\n",
2023-02-28 08:35:12 -07:00
"#@markdown * A learning rate scheduler can change your learning rate as training progresses.\n",
2023-05-17 14:29:08 -06:00
"#@markdown * I recommend sticking with constant until you are comfortable with general training. \n",
2023-06-23 20:55:57 -06:00
"Schedule = \"constant\" #@param [\"constant\", \"polynomial\", \"linear\", \"cosine\"] {allow-input: true}\n",
"Text_lr_scheduler = \"cosine\" #@param [\"constant\", \"polynomial\", \"linear\", \"cosine\"] {allow-input: true}\n",
2023-05-17 14:29:08 -06:00
"#@markdown * warm up steps are useful for validation and cosine lrs\n",
"lr_warmup_steps = 0 #@param{type:\"integer\"}\n",
"lr_decay_steps = 0 #@param {type:\"number\"} \n",
"Text_lr_warmup_steps = 0 #@param {type:\"number\"}\n",
"Text_lr_decay_steps = 0 #@param {type:\"number\"} \n",
"\n",
"if Match_text_to_Unet:\n",
" Text_lr = Learning_Rate\n",
" Text_lr_scheduler = Schedule\n",
2023-05-19 15:18:03 -06:00
" Text_lr_warmup_steps = lr_warmup_steps\n",
2023-05-17 14:29:08 -06:00
"\n",
2023-05-20 03:05:56 -06:00
"data['base']['lr'] = Learning_Rate\n",
"data['text_encoder_overrides']['lr'] = Text_lr \n",
"data['base']['lr_scheduler'] = Schedule\n",
"data['text_encoder_overrides']['lr_scheduler'] = Text_lr_scheduler\n",
"data['base']['lr_warmup_steps'] = lr_warmup_steps\n",
"data['base']['lr_decay_steps'] = lr_decay_steps\n",
"data['text_encoder_overrides']['lr_warmup_steps'] = Text_lr_warmup_steps\n",
"data['text_encoder_overrides']['lr_decay_steps'] = Text_lr_decay_steps\n",
2023-05-17 14:29:08 -06:00
"\n",
"# Save the updated JSON data back to the file\n",
2023-06-03 01:20:40 -06:00
"with open(file_path, 'w') as file:\n",
2023-05-17 14:29:08 -06:00
" json.dump(data, file, indent=4)\n",
2023-02-28 08:35:12 -07:00
"\n",
"#@markdown * Resolution to train at (recommend 512). Higher resolution will require lower batch size (below).\n",
"Resolution = 512 #@param {type:\"slider\", min:256, max:768, step:64}\n",
"\n",
"#@markdown * Batch size is also another \"hyperparameter\" of itself and there are tradeoffs. It may not always be best to use the highest batch size possible. Once of the primary reasons to change it is if you get \"CUDA out of memory\" errors where lowering the value may help.\n",
"\n",
"#@markdown * Batch size impacts VRAM use. 8 should work on SD1.x models and 5 for SD2.x models at 512 resolution. Lower this if you get CUDA out of memory errors. You can check resources on your instance and watch the GPU RAM.\n",
"\n",
"Batch_Size = 6 #@param{type: 'number'}\n",
"\n",
"#@markdown * Gradient accumulation is sort of like a virtual batch size increase use this to increase batch size with out increasing vram usage\n",
"#@markdown Increasing from 1 to 2 will have a minor impact on vram use, but more beyond that will not.\n",
"#@markdown In colab free teir you can expect the fastest proformance from a batch of 8 and a gradient step of 1\n",
"#@markdown This is mostly for use if you are training higher resolution on free tier and cannot increase batch size.\n",
"\n",
"Gradient_steps = 1 #@param{type:\"slider\", min:1, max:10, step:1}\n",
"\n",
"#@markdown * Location on your Gdrive where your training images are.\n",
"Dataset_Location = \"/content/drive/MyDrive/training_samples\" #@param {type:\"string\"}\n",
"\n",
2023-05-24 01:24:38 -06:00
"if not resume:\n",
" model = save_name\n",
2023-02-28 08:35:12 -07:00
"\n",
"#@markdown * Max Epochs to train for, this defines how many total times all your training data is used. Default of 100 is a good start if you are training ~30-40 images of one subject. If you have 100 images, you can reduce this to 40-50 and so forth.\n",
"\n",
2023-06-03 01:20:40 -06:00
"Max_Epochs = 100 #@param {type:\"slider\", min:0, max:200, step:1}\n",
2023-02-28 08:35:12 -07:00
"\n",
"#@markdown * How often to save checkpoints.\n",
"Save_every_N_epoch = 20 #@param{type:\"integer\"}\n",
"\n",
"#@markdown * Test sample generation steps, how often to generate samples during training.\n",
"\n",
"#@markdown You can set your own sample prompts by adding them, one line at a time, to `/content/EveryDream2trainer/sample_prompts.txt`. If left empty, it will use the captions from your training images.\n",
"\n",
"#@markdown Use the steps_between_samples to set how often the samples are generated.\n",
"Steps_between_samples = 300 #@param{type:\"integer\"}\n",
"\n",
"#@markdown * That's it! Run the cell! or configure these advance options\n",
"\n",
"#@markdown # ________________ ADV SETTINGS _________________\n",
"#@markdown These are the default Every Dream 2 settings, changing these without learning what they do will likley waste compute credits\n",
"#@markdown please read the doc folder before changing these!\n",
"\n",
"#@markdown * A tip using the sliders, to finely adjust these click them with your mouse then use your keyboard arrows\n",
"\n",
"#@markdown * Using the same seed each time you train allows for more accurate a/b comparison of models, leave at -1 for random\n",
"#@markdown * The seed also effects your training samples, if you want the same seed each sample you will need to change it from -1\n",
"Training_Seed = -1 #@param{type:\"integer\"}\n",
2023-05-17 14:29:08 -06:00
"\n",
2023-02-28 08:35:12 -07:00
"#@markdown * use this option to configure a sample_prompts.json\n",
"#@markdown * check out /content/EveryDream2trainer/doc/logging.md. for more details\n",
"Advance_Samples = False #@param{type:\"boolean\"}\n",
"Sample_File = \"sample_prompts.txt\"\n",
"if Advance_Samples:\n",
" Sample_File = \"sample_prompts.json\"\n",
"#@markdown * Checkpointing Saves Vram to allow larger batch sizes minor slow down on a single batch size but will can allow room for a higher traning resolution (suggested on Colab Free tier, turn off for A100)\n",
"Gradient_checkpointing = True #@param{type:\"boolean\"}\n",
"Disable_Xformers = False #@param{type:\"boolean\"}\n",
"#@markdown * Tag shuffling, mainly for booru training. Best to just read this if interested in shufflng tags /content/EveryDream2trainer/doc/SHUFFLING_TAGS.md\n",
"shuffle_tags = False #@param{type:\"boolean\"}\n",
"#@markdown * You can turn off the text encoder training (generally not suggested)\n",
"Disable_text_Encoder= False #@param{type:\"boolean\"}\n",
"#@markdown * Skip the nth last layer of CLIP.\n",
"Clip_skip = 1 #@param {type:\"slider\", min:0, max:4, step:1}\n",
"#@markdown * ratio of captions dropped from training data.\n",
2023-05-19 15:18:03 -06:00
"Conditional_DropOut = 0.04 #@param {type:\"slider\", min:0, max:0.3, step:0.01}\n",
2023-02-28 08:35:12 -07:00
"#@markdown * Ratio of images randomly to flip horizontally.\n",
"Picture_flip = 0 #@param {type:\"slider\", min:0, max:0.5, step:0.05}\n",
"#@markdown * This can improve contrast in light and dark scenes, Use a ratio between 0-10% for Best results.\n",
2023-05-17 14:29:08 -06:00
"zero_frequency_noise = 0.01 #@param {type:\"slider\", min:0, max:0.12, step:0.01}\n",
2023-02-28 08:35:12 -07:00
"\n",
"#@markdown * Weights and Biases logging token. \n",
"# #@markdown Paste your token here if you have an account so you can use it to track your training progress. If you don't have an account, you can create one for free at https://wandb.ai/site. Log will use your project name from above. This is a free online logging utility.\n",
"# #@markdown Your key is on this page: https://wandb.ai/settings under \"Danger Zone\" \"API Keys\"\n",
"wandb_token = '' #@param{type:\"string\"}\n",
2023-05-17 14:29:08 -06:00
"\n",
2023-02-28 08:35:12 -07:00
"\n",
"#@markdown use validation with wandb\n",
"\n",
"validatation = False #@param{type:\"boolean\"}\n",
2023-05-24 01:24:38 -06:00
"Hide_Warnings = False #@param {type:\"boolean\"}\n",
"\n",
"extensions = ['.zip', '.7z', '.rar', '.tgz']\n",
"uncompressed_dir = 'Training_Data'\n",
"\n",
"if any(ext in Dataset_Location for ext in extensions):\n",
" # Create the uncompressed directory if it doesn't exist\n",
" if not os.path.exists(uncompressed_dir):\n",
" os.makedirs(uncompressed_dir)\n",
" \n",
" # Extract the compressed file to the uncompressed directory\n",
" shutil.unpack_archive(Dataset_Location, uncompressed_dir)\n",
"\n",
" # Set the dataset location to the uncompressed directory\n",
" Dataset_Location = uncompressed_dir\n",
"\n",
"# Use the dataset location in the rest of your code\n",
2023-02-28 08:35:12 -07:00
"dataset = Dataset_Location\n",
"\n",
"Drive=\"\"\n",
"if Save_to_Gdrive:\n",
" Drive = \"--logdir /content/drive/MyDrive/everydreamlogs --save_ckpt_dir /content/drive/MyDrive/everydreamlogs/ckpt\"\n",
"\n",
"if Max_Epochs==0:\n",
" Max_Epoch=1\n",
"\n",
"if resume:\n",
" model = \"findlast\"\n",
"\n",
2023-05-17 14:29:08 -06:00
"validate = \"\"\n",
"if validatation:\n",
" validate = \"--validation_config validation_default.json\"\n",
"\n",
2023-05-24 01:24:38 -06:00
"\n",
"if Hide_Warnings:\n",
" import warnings\n",
" warnings.filterwarnings(\"ignore\")\n",
"\n",
2023-05-17 14:29:08 -06:00
"wandb_settings = \"\"\n",
"if wandb_token:\n",
" !rm /root/.netrc\n",
" !wandb login $wandb_token\n",
" wandb_settings = \"--wandb\"\n",
"\n",
2023-02-28 08:35:12 -07:00
"Gradient = \"\"\n",
"if Gradient_checkpointing:\n",
" Gradient = \"--gradient_checkpointing \"\n",
"\n",
"DX = \"\" \n",
"if Disable_Xformers:\n",
" DX = \"--disable_xformers \"\n",
"\n",
"shuffle = \"\"\n",
"if shuffle_tags:\n",
" shuffle = \"--shuffle_tags \"\n",
"\n",
2023-05-17 14:29:08 -06:00
"Save_optimizer = \"\"\n",
"if Save_optimizer_state:\n",
" Save_optimizer = \"--save_optimizer\"\n",
"\n",
2023-02-28 08:35:12 -07:00
"textencode = \"\"\n",
"if Disable_text_Encoder:\n",
" textencode = \"--disable_textenc_training\"\n",
"\n",
2023-05-17 14:29:08 -06:00
"%run train.py --resume_ckpt \"$model\" \\\n",
" $textencode \\\n",
" $Gradient \\\n",
" $shuffle \\\n",
" $Drive \\\n",
" $DX \\\n",
" $validate \\\n",
" $wandb_settings \\\n",
" $Save_optimizer \\\n",
" --clip_skip $Clip_skip \\\n",
" --batch_size $Batch_Size \\\n",
" --grad_accum $Gradient_steps \\\n",
" --cond_dropout $Conditional_DropOut \\\n",
" --data_root \"$dataset\" \\\n",
" --flip_p $Picture_flip \\\n",
" --log_step 25 \\\n",
2023-05-17 14:29:08 -06:00
" --max_epochs $Max_Epochs \\\n",
" --project_name \"$Project_Name\" \\\n",
" --resolution $Resolution \\\n",
" --sample_prompts \"$Sample_File\" \\\n",
" --sample_steps $Steps_between_samples \\\n",
" --save_every_n_epoch $Save_every_N_epoch \\\n",
" --seed $Training_Seed \\\n",
" --zero_frequency_noise_ratio $zero_frequency_noise\n",
"\n",
"# Finish the training process\n",
2023-05-24 01:24:38 -06:00
"# clear_output()\n",
2023-05-17 14:29:08 -06:00
"time.sleep(2)\n",
2023-05-20 03:05:56 -06:00
"print(\"Training is complete, select a model to start training again\")\n",
"time.sleep(2)\n",
2023-05-17 14:29:08 -06:00
"\n",
2023-03-30 07:15:48 -06:00
"if Disconnect_after_training:\n",
2023-05-19 15:18:03 -06:00
" print(\"Training is complete, in 30 seconds the instance will reset, you will need to chose a model and you can resume training again\")\n",
" time.sleep(40)\n",
2023-05-20 03:05:56 -06:00
" runtime.unassign()\n",
"\n",
2023-06-03 01:20:40 -06:00
"os.kill(os.getpid(), 9)"
2023-02-28 08:35:12 -07:00
]
},
2023-05-17 14:29:08 -06:00
{
"cell_type": "code",
"execution_count": null,
"metadata": {
2023-06-03 01:20:40 -06:00
"cellView": "form",
"id": "8HmIWtODuE6p"
2023-05-17 14:29:08 -06:00
},
"outputs": [],
"source": [
"#@title Test your Diffusers\n",
"import torch\n",
"from diffusers import StableDiffusionPipeline, DiffusionPipeline, DPMSolverMultistepScheduler\n",
"import torch\n",
"#from xformers.ops import MemoryEfficientAttentionFlashAttentionOp\n",
"\n",
"repo_id = \"stabilityai/stable-diffusion-2-base\"\n",
"pipe = DiffusionPipeline.from_pretrained(repo_id, torch_dtype=torch.float16, revision=\"fp16\")\n",
"\n",
"pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)\n",
"pipe = pipe.to(\"cuda\")\n",
"\n",
"prompt = \"High quality photo of an astronaut riding a horse in space\"\n",
"image = pipe(prompt, num_inference_steps=25).images[0]\n",
"image.save(\"astronaut.png\")\n",
"\n",
"#@markdown Path to the diffusers that was trained\n",
"\n",
"#@markdown You can look in the file drawer on the left /content/drive/MyDrive/everydreamlogs and click the three dots to copy the path\n",
"\n",
"#@markdown ex. /content/drive/MyDrive/everydreamlogs/my_project_20230126-023804/ckpts/interrupted-gs86\n",
"\n",
"sd2 = True #@param {type:\"boolean\"}\n",
"diffusers_path=\"stabilityai/stable-diffusion-2-1-base\" #@param{type:\"string\"}\n",
"\n",
"PROMPT= \"a photo of an astronaut on the moon\" #@param{type:\"string\"}\n",
"number_of_inference_steps= 35 #@param{type:\"integer\"}\n",
"sample_name = \"test\" #@param{type:\"string\"}\n",
"n_propmt = \"bad, deformed, ugly, bad anotomy\"\n",
"\n",
"#pipe.load_textual_inversion(\"sd-concepts-library/cat-toy\")\n",
"#pipe.enable_xformers_memory_efficient_attention(attention_op=MemoryEfficientAttentionFlashAttentionOp)\n",
"\n",
"\n",
"\n",
"if sd2:\n",
" pipe = DiffusionPipeline.from_pretrained(repo_id, torch_dtype=torch.float16, revision=\"fp16\")\n",
"else:\n",
" pipe = StableDiffusionPipeline.from_pretrained(diffusers_path, torch_dtype=torch.float16)\n",
"\n",
"pipe = pipe.to(\"cuda\")\n",
"\n",
"image = pipe(PROMPT, num_inference_steps=number_of_inference_steps, negative_prompt=n_propmt).images[0]\n",
"\n",
"\n",
"image.save(sample_name + \".png\")"
]
},
{
2023-06-23 20:55:57 -06:00
"attachments": {},
2023-05-17 14:29:08 -06:00
"cell_type": "markdown",
"metadata": {
"id": "fzXLJVC6OCeP"
2023-05-24 01:24:38 -06:00
},
"source": [
"## Optional NoteBook Features, read all the documentation in /content/EveryDream2trainer/doc before proceeding."
]
2023-05-17 14:29:08 -06:00
},
{
"cell_type": "code",
2023-05-24 01:24:38 -06:00
"execution_count": null,
"metadata": {
"cellView": "form",
2023-06-03 01:20:40 -06:00
"colab": {
"background_save": true
},
2023-05-24 01:24:38 -06:00
"id": "BafdWaYymg0O"
},
"outputs": [],
2023-05-17 14:29:08 -06:00
"source": [
"#@title Remove logs for samples when training (optional) run before training\n",
"file_path = \"/content/EveryDream2trainer/utils/sample_generator.py\"\n",
"\n",
"# Read the file and store the lines\n",
"with open(file_path, \"r\") as file:\n",
" lines = file.readlines()\n",
"\n",
"# The code block to remove\n",
"code_to_remove = ''' with open(f\"{self.log_folder}/samples/gs{global_step:05}-{sample_index}-{clean_prompt[:100]}.txt\", \"w\", encoding='utf-8') as f:\n",
" f.write(str(batch[prompt_idx]))'''\n",
"\n",
"# Join the lines to create a single string\n",
"content = ''.join(lines)\n",
"\n",
"# Replace the code block with an empty string\n",
"content = content.replace(code_to_remove, '')\n",
"\n",
"# Write the modified content back to the file\n",
"with open(file_path, \"w\") as file:\n",
" file.write(content)\n",
"\n",
2023-06-03 01:20:40 -06:00
"print(\"The specified code block has been deleted.\")"
2023-05-24 01:24:38 -06:00
]
2023-05-17 14:29:08 -06:00
},
2023-02-28 08:35:12 -07:00
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "Iuoa_1B9jRGU"
},
"outputs": [],
"source": [
"#@title Alternate startup script\n",
2023-06-03 01:20:40 -06:00
"#@markdown * Edit train.json or chain0.json to setup your paramaters\n",
2023-05-17 14:29:08 -06:00
"\n",
"#@markdown * Edit using a chain length of 0 will use train.json\n",
"\n",
"#@markdown * Edit the above cell saving it as chain1.json to make use of chaining\n",
"\n",
2023-02-28 08:35:12 -07:00
"#@markdown * make sure to check each confguration you will need 1 Json per chain length 3 are provided\n",
2023-05-17 14:29:08 -06:00
"\n",
2023-02-28 08:35:12 -07:00
"#@markdown * your locations in the .json can be done in this format /content/drive/MyDrive/ - then the sub folder you wish to use\n",
"\n",
"%cd /content/EveryDream2trainer\n",
"Chain_Length=0 #@param{type:\"integer\"}\n",
"l = Chain_Length \n",
"I=0 #repeat counter\n",
"if l == None or l == 0:\n",
2023-05-17 14:29:08 -06:00
" train.py --config train.json\n",
2023-02-28 08:35:12 -07:00
"while l > 0:\n",
2023-05-17 14:29:08 -06:00
" !python train.py --config chain{I}.json\n",
2023-02-28 08:35:12 -07:00
" l -= 1\n",
" I =+ 1"
]
2023-06-03 01:20:40 -06:00
},
{
2023-06-23 20:55:57 -06:00
"attachments": {},
2023-06-03 01:20:40 -06:00
"cell_type": "markdown",
"metadata": {
"id": "ls6mX94trxZV"
2023-06-23 20:55:57 -06:00
},
"source": [
"Need some tools to Manage your large datasets check out https://github.com/victorchall/EveryDream for some usefull tools and captioner"
]
2023-02-28 08:35:12 -07:00
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
2023-05-24 01:24:38 -06:00
"gpuType": "T4",
2023-06-23 20:55:57 -06:00
"include_colab_link": true,
"provenance": []
2023-02-28 08:35:12 -07:00
},
"kernelspec": {
2023-06-03 01:20:40 -06:00
"display_name": "Python 3",
2023-02-28 08:35:12 -07:00
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.10.5"
},
"vscode": {
"interpreter": {
"hash": "e602395b73d27e246c3f66de86a1ed4dc1e5a85e8356fd1a2f027b9d2f1f8162"
}
}
},
"nbformat": 4,
"nbformat_minor": 0
}