2022-08-28 00:20:44 +00:00
{
"cells": [
{
"cell_type": "markdown",
2022-09-14 11:01:58 +00:00
"metadata": {
"id": "ycYWcsEKc6w7"
},
2022-08-28 00:20:44 +00:00
"source": [
2022-10-12 00:46:16 +00:00
"# Stable Diffusion AI Notebook (Release 2.0.0)\n",
2022-08-28 00:20:44 +00:00
"\n",
"<img src=\"https://user-images.githubusercontent.com/60411196/186547976-d9de378a-9de8-4201-9c25-c057a9c59bad.jpeg\" alt=\"stable-diffusion-ai\" width=\"170px\"/> <br>\n",
"#### Instructions:\n",
"1. Execute each cell in order to mount a Dream bot and create images from text. <br>\n",
2022-09-07 17:42:26 +00:00
"2. Once cells 1-8 were run correctly you'll be executing a terminal in cell #9, you'll need to enter `python scripts/dream.py` command to run Dream bot.<br> \n",
2022-08-28 00:20:44 +00:00
"3. After launching dream bot, you'll see: <br> `Dream > ` in terminal. <br> Insert a command, eg. `Dream > Astronaut floating in a distant galaxy`, or type `-h` for help.\n",
2022-09-07 17:42:26 +00:00
"3. After completion you'll see your generated images in path `stable-diffusion/outputs/img-samples/`, you can also show last generated images in cell #10.\n",
2022-08-28 00:20:44 +00:00
"4. To quit Dream bot use `q` command. <br> \n",
"---\n",
"<font color=\"red\">Note:</font> It takes some time to load, but after installing all dependencies you can use the bot all time you want while colab instance is up. <br>\n",
2022-09-07 17:42:26 +00:00
"<font color=\"red\">Requirements:</font> For this notebook to work you need to have [Stable-Diffusion-v-1-4](https://huggingface.co/CompVis/stable-diffusion-v-1-4-original) stored in your Google Drive, it will be needed in cell #7\n",
2022-10-09 19:37:45 +00:00
"##### For more details visit Github repository: [invoke-ai/InvokeAI](https://github.com/invoke-ai/InvokeAI)\n",
2022-08-28 00:20:44 +00:00
"---\n"
2022-09-14 11:01:58 +00:00
]
2022-08-28 00:20:44 +00:00
},
2022-09-07 17:42:26 +00:00
{
"cell_type": "markdown",
"metadata": {
"id": "dr32VLxlnouf"
2022-09-14 11:01:58 +00:00
},
"source": [
"## ◢ Installation"
]
2022-09-07 17:42:26 +00:00
},
2022-08-28 00:20:44 +00:00
{
"cell_type": "code",
2022-09-14 11:01:58 +00:00
"execution_count": null,
2022-08-28 00:20:44 +00:00
"metadata": {
"cellView": "form",
"id": "a2Z5Qu_o8VtQ"
},
2022-09-14 11:01:58 +00:00
"outputs": [],
"source": [
"#@title 1. Check current GPU assigned\n",
"!nvidia-smi -L\n",
"!nvidia-smi"
]
2022-08-28 00:20:44 +00:00
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "vbI9ZsQHzjqF"
},
"outputs": [],
"source": [
"#@title 2. Download stable-diffusion Repository\n",
"from os.path import exists\n",
"\n",
2022-10-09 19:37:45 +00:00
"!git clone --quiet https://github.com/invoke-ai/InvokeAI.git # Original repo\n",
2022-10-12 00:24:27 +00:00
"%cd /content/InvokeAI/\n",
2022-10-12 00:46:16 +00:00
"!git checkout --quiet tags/v2.0.0"
2022-08-28 00:20:44 +00:00
]
},
{
"cell_type": "code",
2022-09-14 11:01:58 +00:00
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "QbXcGXYEFSNB"
},
"outputs": [],
2022-08-28 00:20:44 +00:00
"source": [
2022-09-07 17:42:26 +00:00
"#@title 3. Install dependencies\n",
2022-08-28 00:20:44 +00:00
"import gc\n",
2022-09-07 17:42:26 +00:00
"\n",
2022-11-25 01:41:31 +00:00
"!wget https://raw.githubusercontent.com/invoke-ai/InvokeAI/development/environments-and-requirements/requirements-base.txt\n",
"!wget https://raw.githubusercontent.com/invoke-ai/InvokeAI/development/environments-and-requirements/requirements-win-colab-cuda.txt\n",
2022-09-07 17:42:26 +00:00
"!pip install colab-xterm\n",
2022-09-14 11:01:58 +00:00
"!pip install -r requirements-lin-win-colab-CUDA.txt\n",
"!pip install clean-fid torchtext\n",
2022-10-12 00:24:27 +00:00
"!pip install transformers\n",
2022-08-28 00:20:44 +00:00
"gc.collect()"
2022-09-14 11:01:58 +00:00
]
2022-08-28 00:20:44 +00:00
},
{
"cell_type": "code",
2022-09-14 11:01:58 +00:00
"execution_count": null,
2022-08-28 00:20:44 +00:00
"metadata": {
"cellView": "form",
2022-09-14 11:01:58 +00:00
"id": "8rSMhgnAttQa"
2022-08-28 00:20:44 +00:00
},
2022-09-14 11:01:58 +00:00
"outputs": [],
"source": [
"#@title 4. Restart Runtime\n",
"exit()"
]
2022-08-28 00:20:44 +00:00
},
{
"cell_type": "code",
2022-09-14 11:01:58 +00:00
"execution_count": null,
2022-09-07 17:42:26 +00:00
"metadata": {
"cellView": "form",
2022-09-14 11:01:58 +00:00
"id": "ChIDWxLVHGGJ"
2022-09-07 17:42:26 +00:00
},
2022-09-14 11:01:58 +00:00
"outputs": [],
"source": [
"#@title 5. Load small ML models required\n",
"import gc\n",
2022-10-12 00:24:27 +00:00
"%cd /content/InvokeAI/\n",
2022-09-14 11:01:58 +00:00
"!python scripts/preload_models.py\n",
"gc.collect()"
]
2022-09-07 17:42:26 +00:00
},
{
"cell_type": "markdown",
"metadata": {
"id": "795x1tMoo8b1"
2022-09-14 11:01:58 +00:00
},
"source": [
"## ◢ Configuration"
]
2022-09-07 17:42:26 +00:00
},
{
"cell_type": "code",
2022-09-14 11:01:58 +00:00
"execution_count": null,
2022-08-28 00:20:44 +00:00
"metadata": {
"cellView": "form",
"id": "YEWPV-sF1RDM"
},
2022-09-14 11:01:58 +00:00
"outputs": [],
"source": [
"#@title 6. Mount google Drive\n",
"from google.colab import drive\n",
"drive.mount('/content/drive')"
]
2022-08-28 00:20:44 +00:00
},
{
"cell_type": "code",
2022-09-14 11:01:58 +00:00
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "zRTJeZ461WGu"
},
"outputs": [],
2022-08-28 00:20:44 +00:00
"source": [
2022-09-07 17:42:26 +00:00
"#@title 7. Drive Path to model\n",
2022-08-28 00:20:44 +00:00
"#@markdown Path should start with /content/drive/path-to-your-file <br>\n",
"#@markdown <font color=\"red\">Note:</font> Model should be downloaded from https://huggingface.co <br>\n",
"#@markdown Lastest release: [Stable-Diffusion-v-1-4](https://huggingface.co/CompVis/stable-diffusion-v-1-4-original)\n",
"from os.path import exists\n",
"\n",
"model_path = \"\" #@param {type:\"string\"}\n",
2022-09-14 11:01:58 +00:00
"if exists(model_path):\n",
" print(\"✅ Valid directory\")\n",
2022-08-28 00:20:44 +00:00
"else: \n",
2022-09-14 11:01:58 +00:00
" print(\"❌ File doesn't exist\")"
]
2022-08-28 00:20:44 +00:00
},
{
"cell_type": "code",
2022-09-14 11:01:58 +00:00
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "UY-NNz4I8_aG"
},
"outputs": [],
2022-08-28 00:20:44 +00:00
"source": [
2022-09-07 17:42:26 +00:00
"#@title 8. Symlink to model\n",
2022-08-28 00:20:44 +00:00
"\n",
"from os.path import exists\n",
"import os \n",
"\n",
"# Folder creation if it doesn't exist\n",
2022-10-12 00:24:27 +00:00
"if exists(\"/content/InvokeAI/models/ldm/stable-diffusion-v1\"):\n",
2022-09-14 11:01:58 +00:00
" print(\"❗ Dir stable-diffusion-v1 already exists\")\n",
2022-08-28 00:20:44 +00:00
"else:\n",
2022-10-12 00:24:27 +00:00
" %mkdir /content/InvokeAI/models/ldm/stable-diffusion-v1\n",
2022-09-14 11:01:58 +00:00
" print(\"✅ Dir stable-diffusion-v1 created\")\n",
2022-08-28 00:20:44 +00:00
"\n",
"# Symbolic link if it doesn't exist\n",
2022-10-12 00:24:27 +00:00
"if exists(\"/content/InvokeAI/models/ldm/stable-diffusion-v1/model.ckpt\"):\n",
2022-09-14 11:01:58 +00:00
" print(\"❗ Symlink already created\")\n",
2022-08-28 00:20:44 +00:00
"else: \n",
2022-09-14 11:01:58 +00:00
" src = model_path\n",
2022-10-12 00:24:27 +00:00
" dst = '/content/InvokeAI/models/ldm/stable-diffusion-v1/model.ckpt'\n",
2022-09-14 11:01:58 +00:00
" os.symlink(src, dst) \n",
" print(\"✅ Symbolic link created successfully\")"
]
2022-08-28 00:20:44 +00:00
},
{
2022-09-07 17:42:26 +00:00
"cell_type": "markdown",
2022-08-28 00:20:44 +00:00
"metadata": {
2022-09-07 17:42:26 +00:00
"id": "Mc28N0_NrCQH"
2022-09-14 11:01:58 +00:00
},
"source": [
"## ◢ Execution"
]
2022-08-28 00:20:44 +00:00
},
{
"cell_type": "code",
2022-09-14 11:01:58 +00:00
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "ir4hCrMIuUpl"
},
"outputs": [],
2022-08-28 00:20:44 +00:00
"source": [
"#@title 9. Run Terminal and Execute Dream bot\n",
"#@markdown <font color=\"blue\">Steps:</font> <br>\n",
2022-10-12 00:46:16 +00:00
"#@markdown 1. Execute command `python scripts/invoke.py` to run InvokeAI.<br>\n",
2022-08-28 00:20:44 +00:00
"#@markdown 2. After initialized you'll see `Dream>` line.<br>\n",
"#@markdown 3. Example text: `Astronaut floating in a distant galaxy` <br>\n",
"#@markdown 4. To quit Dream bot use: `q` command.<br>\n",
"\n",
2022-09-07 17:42:26 +00:00
"%load_ext colabxterm\n",
2022-08-28 00:20:44 +00:00
"%xterm\n",
"gc.collect()"
2022-09-14 11:01:58 +00:00
]
2022-08-28 00:20:44 +00:00
},
{
"cell_type": "code",
2022-09-14 11:01:58 +00:00
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "qnLohSHmKoGk"
},
"outputs": [],
2022-08-28 00:20:44 +00:00
"source": [
2022-09-07 17:42:26 +00:00
"#@title 10. Show the last 15 generated images\n",
2022-08-28 00:20:44 +00:00
"import glob\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib.image as mpimg\n",
"%matplotlib inline\n",
"\n",
"images = []\n",
2022-10-12 00:24:27 +00:00
"for img_path in sorted(glob.glob('/content/InvokeAI/outputs/img-samples/*.png'), reverse=True):\n",
2022-08-28 00:20:44 +00:00
" images.append(mpimg.imread(img_path))\n",
"\n",
2022-09-07 17:42:26 +00:00
"images = images[:15] \n",
2022-08-28 00:20:44 +00:00
"\n",
"plt.figure(figsize=(20,10))\n",
"\n",
"columns = 5\n",
"for i, image in enumerate(images):\n",
" ax = plt.subplot(len(images) / columns + 1, columns, i + 1)\n",
" ax.axes.xaxis.set_visible(False)\n",
" ax.axes.yaxis.set_visible(False)\n",
" ax.axis('off')\n",
" plt.imshow(image)\n",
" gc.collect()\n",
"\n"
2022-09-14 11:01:58 +00:00
]
2022-08-28 00:20:44 +00:00
}
2022-09-14 11:01:58 +00:00
],
"metadata": {
"accelerator": "GPU",
"colab": {
"collapsed_sections": [],
"private_outputs": true,
"provenance": []
},
"gpuClass": "standard",
"kernelspec": {
2022-11-25 01:41:31 +00:00
"display_name": "Python 3.9.12 64-bit",
2022-11-17 02:54:26 +00:00
"language": "python",
2022-09-14 11:01:58 +00:00
"name": "python3"
},
"language_info": {
2022-11-17 02:54:26 +00:00
"name": "python",
2022-11-25 01:41:31 +00:00
"version": "3.9.12"
2022-11-17 02:54:26 +00:00
},
"vscode": {
"interpreter": {
2022-11-25 01:41:31 +00:00
"hash": "4e870c5c5fe42db7e2c5647ae5af656ff3391bf8c2b729cbf7fa0e16ca8cb5af"
2022-11-17 02:54:26 +00:00
}
2022-09-14 11:01:58 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 0
2022-09-07 17:42:26 +00:00
}