{
"cells": [
{
"cell_type": "markdown",
"source": [
"# Notice:\n",
"To train Dreambooth Stable Diffusion requires a recommended minimum of 16 GB of GPU RAM. Trying to train Dreambooth with less will trigger an Out of Memory error. This Notebook is defaulted to set up to run on the RTX5000 GPU, which requires a paid account on Gradient. \n",
"\n",
"Change the URL at the top where it says \"machine?=RTX5000\" to any GPU with at least 16 GB of RAM to run training. The P6000, V100, V100-32G, RTX5000, A4000, A5000, A100, and A100-80G powered machines will all be able to run this training. \n",
"\n",
"You can do so by changing the following URL where it says \"(YOUR-GPU-CHOICE)\":\n",
"\n",
" https://console.paperspace.com/github/gradient-ai/dreambooth-stable-diffusion/blob/main/sd_dreambooth_gradient.ipynb?machine=(YOUR-GPU-CHOICE)\n"
],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
"# Dreambooth fine-tuning for Stable Diffusion using d🧨ffusers with Gradient Notebooks\n",
"\n",
"This notebook shows how to \"teach\" Stable Diffusion a new concept via Dreambooth using 🤗 Hugging Face [🧨 Diffusers library](https://github.com/huggingface/diffusers). \n",
"\n",
"\n",
"_By using just 3-5 images you can teach new concepts to Stable Diffusion and personalize the model on your own images_ \n",
"\n",
"Differently from Textual Inversion, this approach trains the whole model, which can yield better results to the cost of bigger models.\n",
"\n",
"For a general introduction to the Stable Diffusion model please refer to this [Gradient Notebook](https://console.paperspace.com/github/gradient-ai/stable-diffusion/blob/main/stable-diffusion-notebook.ipynb?machine=A4000).\n",
"\n"
],
"metadata": {
"id": "tAZq3vFDcFiT"
}
},
{
"cell_type": "markdown",
"source": [
"## Installation"
],
"metadata": {
"id": "KbzZ9xe6dWwf"
}
},
{
"cell_type": "code",
"execution_count": 1,
"source": [
"#@title Install the required libs\n",
"!pip install -qq accelerate tensorboard ftfy\n",
"!pip install -qq --upgrade diffusers[torch] --no-cache-dir\n",
"!pip install -qq -U transformers\n",
"!pip install -qq bitsandbytes\n",
"!pip install gradio\n",
"!mkdir inputs\n",
"!git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui"
],
"outputs": [],
"metadata": {
"execution": {
"iopub.execute_input": "2022-10-28T17:34:36.733188Z",
"iopub.status.busy": "2022-10-28T17:34:36.732409Z",
"iopub.status.idle": "2022-10-28T17:34:46.339367Z",
"shell.execute_reply": "2022-10-28T17:34:46.338666Z",
"shell.execute_reply.started": "2022-10-28T17:34:36.733122Z"
},
"id": "30lu8LWXmg5j"
}
},
{
"cell_type": "markdown",
"source": [
"# Loading in the Stable Diffusion v1-5 models\n",
"\n",
"To make accessing the Stable Diffusion models easy and not take up any storage, we have added the Stable Diffusion models v1-5 as mountable public datasets! To access the models this way, simply navigate to the \"Data Sources\" tab using the navigator on the far left of the page. Then click \"Public\" to switch into the Gradient Public Datasets, and scroll down until you find \"stable-diffusion\" near the bottom of the list. Then, click \"mount\" to make them accessible from the \"datasets\" directory. This directory is in the root folder, so access it with the path `../datasets/stable-diffusion-diffusers/stable-diffusion-v1-5`\n",
"\n",
"> Note that these public dataset files will not count toward the file storage limits \n",
"\n",
"\n",
""
],
"metadata": {}
},
{
"cell_type": "markdown",
"source": [
"# Alternate access: log in to HuggingFace for online access to models\n",
"\n",
"In order to access the models from CompVis for Stable Diffusion, you must follow three steps:\n",
"\n",
"1. You must acknowledge and agree to their user requirements and license for their models. you can do so by reading the instructions found on this page: https://huggingface.co/runwayml/stable-diffusion-v1-5\n",
"\n",
"2. You must login to Huggingface, and then create and retrieve an access token (found here: https://huggingface.co/settings/tokens)\n",
"\n",
"3. Finally, replace the segment of the cell below `` with your own token, and run the cell. \n",
"\n",
"If you follow these steps, you will be able to access the model for free!\n",
"\n",
"If you are using this method, be sure to change the path to `runwayml/stable-diffusion-v1-5`\n",
"\n"
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 2,
"source": [
"# !wget https://raw.githubusercontent.com/gradient-ai/stable-diffusion/main/login.py\n",
"# !python login.py --token "
],
"outputs": [],
"metadata": {
"execution": {
"iopub.execute_input": "2022-10-28T17:34:46.340804Z",
"iopub.status.busy": "2022-10-28T17:34:46.340614Z",
"iopub.status.idle": "2022-10-28T17:34:46.758162Z",
"shell.execute_reply": "2022-10-28T17:34:46.757334Z",
"shell.execute_reply.started": "2022-10-28T17:34:46.340786Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"## Imports and setup"
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 1,
"source": [
"#@title Import required libraries\n",
"import argparse\n",
"import itertools\n",
"import math\n",
"\n",
"import os\n",
"from contextlib import nullcontext\n",
"import random\n",
"\n",
"import numpy as np\n",
"import torch\n",
"import torch.nn.functional as F\n",
"import torch.utils.checkpoint\n",
"from torch.utils.data import Dataset\n",
"\n",
"import PIL\n",
"from accelerate import Accelerator\n",
"from accelerate.logging import get_logger\n",
"from accelerate.utils import set_seed\n",
"from diffusers import AutoencoderKL, DDPMScheduler, PNDMScheduler, StableDiffusionPipeline, UNet2DConditionModel\n",
"from diffusers.optimization import get_scheduler\n",
"from diffusers.pipelines.stable_diffusion import StableDiffusionSafetyChecker\n",
"from PIL import Image\n",
"from torchvision import transforms\n",
"from tqdm.auto import tqdm\n",
"from transformers import CLIPFeatureExtractor, CLIPTextModel, CLIPTokenizer\n",
"\n",
"import bitsandbytes as bnb\n",
"\n",
"\n",
"# Helper function\n",
"\n",
"def image_grid(imgs, rows, cols):\n",
" assert len(imgs) == rows*cols\n",
"\n",
" w, h = imgs[0].size\n",
" grid = Image.new('RGB', size=(cols*w, rows*h))\n",
" grid_w, grid_h = grid.size\n",
" \n",
" for i, img in enumerate(imgs):\n",
" grid.paste(img, box=(i%cols*w, i//cols*h))\n",
" return grid"
],
"outputs": [],
"metadata": {
"cellView": "form",
"colab": {
"base_uri": "https://localhost:8080/"
},
"execution": {
"iopub.execute_input": "2022-10-28T17:39:42.305061Z",
"iopub.status.busy": "2022-10-28T17:39:42.304381Z",
"iopub.status.idle": "2022-10-28T17:39:44.179822Z",
"shell.execute_reply": "2022-10-28T17:39:44.179165Z",
"shell.execute_reply.started": "2022-10-28T17:39:42.304996Z"
},
"id": "1_h0kO-VnQog",
"outputId": "96e5e3cf-0da9-4a0c-b506-35de5e724d8d"
}
},
{
"cell_type": "markdown",
"source": [
"## Settings for teaching your new concept\n",
"\n",
"When setting up your data, you can use the demo data provided below to create your first inversion. \n",
"\n",
"### Display your video output in markdown\n",
"If you would like to use your own images, hash out the cells below and add them to the `inputs` directory.\n",
"\n",
"