colab notebook work

This commit is contained in:
Victor Hall 2023-01-22 13:01:18 -05:00
parent 33936c1626
commit f58c305cf2
1 changed files with 154 additions and 108 deletions

View File

@ -3,8 +3,8 @@
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": { "metadata": {
"id": "view-in-github", "colab_type": "text",
"colab_type": "text" "id": "view-in-github"
}, },
"source": [ "source": [
"<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>" "<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>"
@ -23,8 +23,8 @@
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {
"id": "WsYIcz9HY9lx", "cellView": "form",
"cellView": "form" "id": "WsYIcz9HY9lx"
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
@ -40,40 +40,42 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"source": [
"#@title verify python version\n",
"!python --version"
],
"metadata": {
"id": "f2cdMtCt9Wb6",
"cellView": "form"
},
"execution_count": null, "execution_count": null,
"outputs": [] "metadata": {
"cellView": "form",
"id": "f2cdMtCt9Wb6"
},
"outputs": [],
"source": [
"#@title Verify python version, should be 3.10.something\n",
"!python --version"
]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "d1di4EC6ygw1"
},
"outputs": [],
"source": [ "source": [
"#@title Optional connect Gdrive\n", "#@title Optional connect Gdrive\n",
"#@markdown This will let you put all your training data and checkpoints directly on your drive. Much faster/easier to continue later, less setup time.\n",
"\n",
"#@markdown Creates /content/drive/MyDrive/everydreamlogs/ckpt\n",
"from google.colab import drive\n", "from google.colab import drive\n",
"drive.mount('/content/drive')\n", "drive.mount('/content/drive')\n",
"\n", "\n",
"\n", "!mkdir -p /content/drive/MyDrive/everydreamlogs/ckpt"
"!mkdir /content/drive/MyDrive/logs/ckpt" ]
],
"metadata": {
"id": "d1di4EC6ygw1",
"cellView": "form"
},
"execution_count": null,
"outputs": []
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {
"id": "hAuBbtSvGpau", "cellView": "form",
"cellView": "form" "id": "hAuBbtSvGpau"
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
@ -81,26 +83,26 @@
"from IPython.display import clear_output\n", "from IPython.display import clear_output\n",
"from subprocess import getoutput\n", "from subprocess import getoutput\n",
"s = getoutput('nvidia-smi')\n", "s = getoutput('nvidia-smi')\n",
"!pip install torch==1.12.1+cu116 torchvision==0.13.1+cu116 --extra-index-url \"https://download.pytorch.org/whl/cu116\"\n", "!pip install -q torch==1.12.1+cu116 torchvision==0.13.1+cu116 --extra-index-url \"https://download.pytorch.org/whl/cu116\"\n",
"!pip install transformers==4.25.1\n", "!pip install -q transformers==4.25.1\n",
"!pip install diffusers[torch]==0.10.2\n", "!pip install -q diffusers[torch]==0.10.2\n",
"!pip install pynvml==11.4.1\n", "!pip install -q pynvml==11.4.1\n",
"!pip install bitsandbytes==0.35.0\n", "!pip install -q bitsandbytes==0.35.0\n",
"!pip install ftfy==6.1.1\n", "!pip install -q ftfy==6.1.1\n",
"!pip install aiohttp==3.8.3\n", "!pip install -q aiohttp==3.8.3\n",
"!pip install tensorboard>=2.11.0\n", "!pip install -q tensorboard>=2.11.0\n",
"!pip install protobuf==3.20.1\n", "!pip install -q protobuf==3.20.1\n",
"!pip install wandb==0.13.6\n", "!pip install -q wandb==0.13.6\n",
"!pip install pyre-extensions==0.0.23\n", "!pip install -q pyre-extensions==0.0.23\n",
"if \"A100\" in s:\n", "if \"A100\" in s:\n",
" !pip install https://huggingface.co/industriaditat/xformers_precompiles/blob/main/A100_13dev/xformers-0.0.13.dev0-py3-none-any.whl\n", " !pip install -q https://huggingface.co/industriaditat/xformers_precompiles/blob/main/A100_13dev/xformers-0.0.13.dev0-py3-none-any.whl\n",
"else:\n", "else:\n",
" !pip install https://huggingface.co/industriaditat/xformers_precompiles/resolve/main/T4_13dev/xformers-0.0.13.dev0-py3-none-any.whl\n", " !pip install -q https://huggingface.co/industriaditat/xformers_precompiles/resolve/main/T4_13dev/xformers-0.0.13.dev0-py3-none-any.whl\n",
"!pip install pytorch-lightning==1.6.5\n", "!pip install -q pytorch-lightning==1.6.5\n",
"!pip install OmegaConf==2.2.3\n", "!pip install -q OmegaConf==2.2.3\n",
"!pip install numpy==1.23.5\n", "!pip install -q numpy==1.23.5\n",
"!pip install colorama\n", "!pip install -q colorama\n",
"!pip install keyboard\n", "!pip install -q keyboard\n",
"clear_output()\n", "clear_output()\n",
"!git clone https://github.com/victorchall/EveryDream2trainer.git\n", "!git clone https://github.com/victorchall/EveryDream2trainer.git\n",
"%cd /content/EveryDream2trainer\n", "%cd /content/EveryDream2trainer\n",
@ -113,104 +115,139 @@
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {
"id": "unaffeqGP_0A", "cellView": "form",
"cellView": "form" "id": "unaffeqGP_0A"
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"#@title Get A Base Model\n", "#@title Get A Base Model\n",
"#@markdown in this space you can type any url however i provide 2 options in the drop down\n", "#@markdown Choose SD1.5 or Waifu Diffusion 1.3 from the dropdown, or paste your own URL in the box\n",
"\n",
"#@markdown If you already did this once with Gdrive connected, you can skip this step as the cached copy is on your gdrive\n",
"from IPython.display import clear_output\n", "from IPython.display import clear_output\n",
"!mkdir input\n", "!mkdir input\n",
"%cd /content/EveryDream2trainer\n", "%cd /content/EveryDream2trainer\n",
"!python utils/get_yamls.py\n", "!python utils/get_yamls.py\n",
"!rm train.py\n",
"!wget \"https://github.com/nawnie/EveryDream2trainer/blob/e605cc59fb1f84f4f8e9290d88f7ede7b4c4fe0c/train_colab.py\"\n",
"!mkdir Ckpt_cache\n",
"MODEL_URL = \"https://huggingface.co/panopstor/EveryDream/resolve/main/sd_v1-5_vae.ckpt\" #@param [\"https://huggingface.co/panopstor/EveryDream/resolve/main/sd_v1-5_vae.ckpt\", \"https://huggingface.co/hakurei/waifu-diffusion-v1-3/resolve/main/wd-v1-3-float16.ckpt\"] {allow-input: true}\n", "MODEL_URL = \"https://huggingface.co/panopstor/EveryDream/resolve/main/sd_v1-5_vae.ckpt\" #@param [\"https://huggingface.co/panopstor/EveryDream/resolve/main/sd_v1-5_vae.ckpt\", \"https://huggingface.co/hakurei/waifu-diffusion-v1-3/resolve/main/wd-v1-3-float16.ckpt\"] {allow-input: true}\n",
"print(\"Downloading \")\n", "print(\"Downloading \")\n",
"!wget $MODEL_URL\n", "!wget $MODEL_URL\n",
"\n", "\n",
"%cd /content/EveryDream2trainer\n", "%cd /content/EveryDream2trainer\n",
"\n", "\n",
"\n",
"clear_output()\n", "clear_output()\n",
"print(\"DONE!\")" "print(\"DONE!\")"
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"source": [
"In order to train, you need a base model on which to train. This is a one-time setup to configure base models when you want to use a particular base.\n"
],
"metadata": { "metadata": {
"id": "nEzuEYH0536C" "id": "nEzuEYH0536C"
} },
"source": [
"In order to train, you need a base model on which to train. This is a one-time setup to configure base models when you want to use a particular base.\n"
]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "tPvQSo6ScF2c"
},
"outputs": [],
"source": [ "source": [
"import os\n", "import os\n",
"#@title Convert to diffuers currently only 1.5 based models\n", "#@title Setup conversion\n",
"#@markdown * This Path should be to an ckpt you want to use the one we just downloaded will be inside EveryDream2trainer\n",
"\n", "\n",
"Base_path = \"/content/EveryDream2trainer/sd_v1-5_vae.ckpt\" #@param {type:\"string\"}\n", "#@markdown If you already did this once with Gdrive connected, you can skip this step as the cached copy is on your gdrive. If you are not sure, look in your Gdrive for `logs/ckpt` and see if you have a folder with the `save_name` below.\n",
"#@markdown * the name that you will use when selecting this model in the future traning sessons.\n", "\n",
"save_name = \"SD_1.5\" #@param{type:\"string\"}\n", "#@markdown Pick the `model_type` in the dropdown. This is the model type that you are converting and you downloaded above. This is important as it will determine the model architecture and the correct settings to use.\n",
"\n",
"#@markdown * `SD1x` is all SD1.x based models *(SD1.4, SD1.5, Waifu Diffusion 1.3, etc)*\n",
"\n",
"#@markdown * `SD2_512_base` is the SD2 512 base model\n",
"\n",
"#@markdown * `SD21` is all SD2 768 models. *(ex. SD2.1 768, or trained models based on that)*\n",
"\n",
"#@markdown If you are not sure, double check the model author's page or ask for help on [Discord](https://discord.gg/uheqxU6sXN).\n",
"model_type = \"SD1x\" #@param [\"SD1x\", \"SD2_512_base\", \"SD21\"]\n",
"\n",
"#@markdown This is the temporary ckpt file that was downloaded above. If you downloaded a different model, you can change this. *Hint: look at your file manager in the EveryDream2trainer folder for .ckpt files*.\n",
"base_path = \"/content/EveryDream2trainer/sd_v1-5_vae.ckpt\" #@param {type:\"string\"}\n",
"\n",
"#@markdown The name that you will use when selecting this model in the future training sessons.\n",
"save_name = \"SD15\" #@param{type:\"string\"}\n",
"\n",
"#@markdown If you are using Gdrive, this will save the converted model to your Gdrive for future use so you can skip downloading and converting the model.\n",
"cache_to_gdrive = True #@param{type:\"boolean\"}\n",
"\n",
"if cache_to_gdrive:\n",
" save_name = os.path.join(\"/content/drive/MyDrive/everydreamlogs/ckpt\", save_name)\n",
"\n",
"img_size = 512\n",
"upscale_attention = False\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",
" inference_yaml = \"v2-inference-v.yaml\"\n",
" img_size = 768\n",
"\n",
"print(base_path)\n",
"print(inference_yaml)\n",
"\n", "\n",
"!python utils/convert_original_stable_diffusion_to_diffusers.py --scheduler_type ddim \\\n", "!python utils/convert_original_stable_diffusion_to_diffusers.py --scheduler_type ddim \\\n",
"--original_config_file v1-inference.yaml \\\n", "--original_config_file {inference_yaml} \\\n",
"--image_size 512 \\\n", "--image_size {img_size} \\\n",
"--checkpoint_path \"$Base_path\" \\\n", "--checkpoint_path {base_path} \\\n",
"--prediction_type epsilon \\\n", "--prediction_type epsilon \\\n",
"--upcast_attn False \\\n", "--upcast_attn False \\\n",
"--dump_path \"$save_name\"\n" "--dump_path {save_name}"
], ]
"metadata": {
"id": "tPvQSo6ScF2c",
"cellView": "form"
},
"execution_count": null,
"outputs": []
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "bLpcvpGJB4Gu"
},
"outputs": [],
"source": [ "source": [
"#@title Resume from a diffuser model saved to your Gdrive\n", "#@title Resume from a diffusers model saved to your Gdrive\n",
"#@markdown * if you have preveiously saved diffusers on your drive you can slect them here\n", "#@markdown * if you have preveiously saved diffusers on your drive you can slect them here\n",
"#@markdown ex. */content/drive/MyDrive/everydreamlogs/myproject_202208/ckpts/interrupted-gs023*\n",
"Resume_Model = \"\" #@param{type:\"string\"} \n", "Resume_Model = \"\" #@param{type:\"string\"} \n",
"save_name= Resume_Model" "save_name= Resume_Model"
], ]
"metadata": {
"id": "bLpcvpGJB4Gu",
"cellView": "form"
},
"execution_count": null,
"outputs": []
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {
"id": "JXVu-W2lCjwX"
},
"source": [ "source": [
"For a more indepth Explanation of each of these paramaters check out /content/EveryDream2trainer/doc.\n", "For a more indepth Explanation of each of these paramaters check out /content/EveryDream2trainer/doc.\n",
"\n", "\n",
"\n", "\n",
"After youve tried a few models you will find /content/EveryDream2trainer/doc/ATWEAKING.md to be extremly helpful." "After youve tried a few models you will find /content/EveryDream2trainer/doc/ATWEAKING.md to be extremly helpful."
], ]
"metadata": {
"id": "JXVu-W2lCjwX"
}
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {
"id": "j9pEI69WXS9w", "cellView": "form",
"cellView": "form" "id": "j9pEI69WXS9w"
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"#@title \n", "#@title \n",
"#@markdown # Run Everydream 2\n", "#@markdown # Run Everydream 2\n",
"#@markdown If you want to use a .json config or upload your own, skip this cell and run the cell below instead\n",
"\n", "\n",
"Save_to_Gdrive = True #@param{type:\"boolean\"}\n", "Save_to_Gdrive = True #@param{type:\"boolean\"}\n",
"#@markdown * Use resume to contnue training you just ran\n", "#@markdown * Use resume to contnue training you just ran\n",
@ -223,8 +260,8 @@
"shuffle_tags = False #@param{type:\"boolean\"}\n", "shuffle_tags = False #@param{type:\"boolean\"}\n",
"#@markdown * you can stop the text encoder to attempt to reduce overfitting when resuming an unfinished model\n", "#@markdown * you can stop the text encoder to attempt to reduce overfitting when resuming an unfinished model\n",
"Disable_text_Encoder= False #@param{type:\"boolean\"}\n", "Disable_text_Encoder= False #@param{type:\"boolean\"}\n",
"Project_Name = \"\" #@param{type: 'string'}\n", "Project_Name = \"my_project\" #@param{type: 'string'}\n",
"Max_Epochs = 100 #@param {type:\"slider\", min:0, max:200, step:10}\n", "Max_Epochs = 100 #@param {type:\"slider\", min:0, max:200, step:5}\n",
"\n", "\n",
"#@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. See Advanced Tweaking for more info. Once you have started, the learning rate is a good first knob to turn as you move into more advanced tweaking.\n", "#@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. See Advanced Tweaking for more info. Once you have started, the learning rate is a good first knob to turn as you move into more advanced tweaking.\n",
"\n", "\n",
@ -234,12 +271,14 @@
"\n", "\n",
"#@markdown * The constant scheduler is the default and keeps your LR set to the value you set in the command line. That's really it for constant! I recommend sticking with it until you are comfortable with general training.\n", "#@markdown * The constant scheduler is the default and keeps your LR set to the value you set in the command line. That's really it for constant! I recommend sticking with it until you are comfortable with general training.\n",
"\n", "\n",
"Schedule = \"polynomial\" #@param [\"constant\", \"polynomial\", \"linear\", \"cosine\"] {allow-input: true}\n", "Schedule = \"constant\" #@param [\"constant\", \"polynomial\", \"linear\", \"cosine\"] {allow-input: true}\n",
"Resolution = 512#@param{type:\"integer\"} \n", "\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", "\n",
"#@markdown * Batch size is also another \"hyperparamter\" 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", "#@markdown * Batch size is also another \"hyperparamter\" 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", "\n",
"#@markdown * While very small batch sizes can impact performance negatively, at some point larger sizes have little impact on overall speed as well, so shooting for the moon is not always advisable. Changing batch size may also impact what learning rate you use, with typically larger batch_size requiring a slightly higher learning rate.\n", "#@markdown * Batch size impacts VRAM use. 4 should work on SD1.x models and 3 for SD2.x models at 512 resolution. Lower this if you get CUDA out of memory errors.\n",
"\n", "\n",
"Batch_Size = 4 #@param{type: 'number'}\n", "Batch_Size = 4 #@param{type: 'number'}\n",
"\n", "\n",
@ -247,22 +286,23 @@
"#@markdown * 1 or 5 steps will take the same vram as a batch of 1\n", "#@markdown * 1 or 5 steps will take the same vram as a batch of 1\n",
"#@markdown * in colab free teir you can expect the fastest proformance from a batch of 4 and a step of 2 giving us a total batch size of 8 at 512 resolution \n", "#@markdown * in colab free teir you can expect the fastest proformance from a batch of 4 and a step of 2 giving us a total batch size of 8 at 512 resolution \n",
"#@markdown * Due to bucketng you may need to decresse batch size to 3\n", "#@markdown * Due to bucketng you may need to decresse batch size to 3\n",
"#@markdown * Remember fast Doesnt always mean better nor does slow experement\n", "#@markdown * Remember fast doesn't always mean better\n",
"\n", "\n",
"Gradient_steps = 2 #@param{type: 'number'}\n", "Gradient_steps = 1 #@param{type:\"slider\", min:1, max:10, step:1}\n",
"Dataset_Location = \"/content/drive/MyDrive/Zmanda\" #@param {type:\"string\"}\n", "Dataset_Location = \"/content/drive/MyDrive/training_samples\" #@param {type:\"string\"}\n",
"dataset = Dataset_Location\n", "dataset = Dataset_Location\n",
"model = save_name\n", "model = save_name\n",
"\n", "\n",
"Save_every_N_epoch = 25#@param{type:\"integer\"}\n", "#@markdown * How often to save checkpoints.\n",
"Save_every_N_epoch = 20 #@param{type:\"integer\"}\n",
"\n", "\n",
"#@markdown You can set your own sample prompts by adding them, one line at a time, to sample_prompts.txt.\n", "#@markdown You can set your own sample prompts by adding them, one line at a time, to sample_prompts.txt.\n",
"\n", "\n",
"Steps_between_samples = 30#@param{type:\"integer\"}\n", "Steps_between_samples = 300 #@param{type:\"integer\"}\n",
"\n", "\n",
"Drive=\"\"\n", "Drive=\"\"\n",
"if Save_to_Gdrive:\n", "if Save_to_Gdrive:\n",
" Drive = \"--logdir /content/drive/MyDrive/logs --save_ckpt_dir /content/drive/MyDrive/logs/ckpt\"\n", " Drive = \"--logdir /content/drive/MyDrive/everydreamlogs --save_ckpt_dir /content/drive/MyDrive/everydreamlogs/ckpt\"\n",
"\n", "\n",
"if Max_Epochs==0:\n", "if Max_Epochs==0:\n",
" Max_Epoch=1\n", " Max_Epoch=1\n",
@ -270,7 +310,6 @@
"if resume:\n", "if resume:\n",
" model = \"findlast\"\n", " model = \"findlast\"\n",
"\n", "\n",
"\n",
"Gradient = \"\"\n", "Gradient = \"\"\n",
"if Gradient_checkpointing:\n", "if Gradient_checkpointing:\n",
" Gradient = \"--gradient_checkpointing \"\n", " Gradient = \"--gradient_checkpointing \"\n",
@ -289,7 +328,6 @@
"if Disable_text_Encoder:\n", "if Disable_text_Encoder:\n",
" textencode = \"--disable_textenc_training Train_text \"\n", " textencode = \"--disable_textenc_training Train_text \"\n",
"\n", "\n",
"\n",
"!python train_colab.py --resume_ckpt \"$model\" \\\n", "!python train_colab.py --resume_ckpt \"$model\" \\\n",
" $textencode \\\n", " $textencode \\\n",
" $Gradient \\\n", " $Gradient \\\n",
@ -301,7 +339,7 @@
" --grad_accum 2 \\\n", " --grad_accum 2 \\\n",
" --cond_dropout 0.00 \\\n", " --cond_dropout 0.00 \\\n",
" --data_root \"$dataset\" \\\n", " --data_root \"$dataset\" \\\n",
" --flip_p 0.01 \\\n", " --flip_p 0.00 \\\n",
" --lr $Learning_Rate \\\n", " --lr $Learning_Rate \\\n",
" --lr_decay_steps 0 \\\n", " --lr_decay_steps 0 \\\n",
" --lr_scheduler \"$Schedule\" \\\n", " --lr_scheduler \"$Schedule\" \\\n",
@ -320,32 +358,40 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"source": [ "execution_count": null,
"#@title Adv Setup use the .Json to setup your paramaters\n",
"!python train.py --config train.json"
],
"metadata": { "metadata": {
"id": "Iuoa_1B9jRGU" "id": "Iuoa_1B9jRGU"
}, },
"execution_count": null, "outputs": [],
"outputs": [] "source": [
"#@title Alternate startup script\n",
"#@markdown Edit train.json to setup your paramaters\n",
"!python train.py --config train.json"
]
} }
], ],
"metadata": { "metadata": {
"accelerator": "GPU", "accelerator": "GPU",
"colab": { "colab": {
"provenance": [], "include_colab_link": true,
"include_colab_link": true "provenance": []
}, },
"gpuClass": "standard", "gpuClass": "standard",
"kernelspec": { "kernelspec": {
"display_name": "Python 3", "display_name": "venv",
"language": "python",
"name": "python3" "name": "python3"
}, },
"language_info": { "language_info": {
"name": "python" "name": "python",
"version": "3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)]"
},
"vscode": {
"interpreter": {
"hash": "e602395b73d27e246c3f66de86a1ed4dc1e5a85e8356fd1a2f027b9d2f1f8162"
}
} }
}, },
"nbformat": 4, "nbformat": 4,
"nbformat_minor": 0 "nbformat_minor": 0
} }