{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" }, "accelerator": "GPU", "gpuClass": "standard" }, "cells": [ { "cell_type": "markdown", "source": [ "# **Stable Diffusion Colab Notebook for Capricon**" ], "metadata": { "id": "fkjJJppwmVjA" } }, { "cell_type": "markdown", "source": [ "**Step 1**: Before connecting, make sure you are using a GPU runtime. If the nvidia-smi command fails, click the Runtime menu item above and select Change runtime type and for hardware accelerator, make sure GPU is selected. Then re-run the command." ], "metadata": { "id": "UcuLSH3GLdVE" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "s1KNNcaELcT3", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "3dd95058-1a94-4a98-eecf-180a6896d1d9" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Thu Feb 2 15:29:00 2023 \n", "+-----------------------------------------------------------------------------+\n", "| NVIDIA-SMI 510.47.03 Driver Version: 510.47.03 CUDA Version: 11.6 |\n", "|-------------------------------+----------------------+----------------------+\n", "| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |\n", "| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |\n", "| | | MIG M. |\n", "|===============================+======================+======================|\n", "| 0 Tesla T4 Off | 00000000:00:04.0 Off | 0 |\n", "| N/A 40C P0 22W / 70W | 0MiB / 15360MiB | 0% Default |\n", "| | | N/A |\n", "+-------------------------------+----------------------+----------------------+\n", " \n", "+-----------------------------------------------------------------------------+\n", "| Processes: |\n", "| GPU GI CI PID Type Process name GPU Memory |\n", "| ID ID Usage |\n", "|=============================================================================|\n", "| No running processes found |\n", "+-----------------------------------------------------------------------------+\n" ] } ], "source": [ "!nvidia-smi" ] }, { "cell_type": "markdown", "source": [ "**Step 2**. Use PIP to install the Stable Diffusion diffusers and the accelerate, scipy, ftfy and transformers libraries. " ], "metadata": { "id": "6yVn7ZN4Mb1t" } }, { "cell_type": "code", "source": [ "!pip install diffusers==0.11.1\n", "!pip install transformers scipy ftfy accelerate" ], "metadata": { "id": "X6OUZ3_2Muo2" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "**Step 3**. Import the necessary libraries and set up the Stable Diffusion pipeline. Note that libraries only need to be imported once." ], "metadata": { "id": "40tVvVX1Mwy-" } }, { "cell_type": "code", "source": [ "import random # for seed value randomization (optional)\n", "import torch # only need to import libraries once\n", "from diffusers import StableDiffusionPipeline\n", "\n", "pipe = StableDiffusionPipeline.from_pretrained(\"CompVis/stable-diffusion-v1-4\", torch_dtype=torch.float16) \n", "\n", "pipe = pipe.to(\"cuda\") # set the pipe to use CUDA (enabled by connecting to a GPU)\n" ], "metadata": { "id": "_kdlZ1FXNLlj" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "Step 4: Create your prompt and generate an image using the system defaults." ], "metadata": { "id": "8gknHEonNMHa" } }, { "cell_type": "code", "source": [ "# VARIABLES SECTION\n", "thePrompt = \"Put your prompt here\" #@param {type:\"string\"}\n", "image = pipe(thePrompt).images[0] # image here is in [PIL format](https://pillow.readthedocs.io/en/stable/)\n", "\n", "# Now to display an image you can either save it such as:\n", "#image.save(f\"astronaut_rides_horse.png\") \n", "\n", "image # display the image inline in colab" ], "metadata": { "id": "M2DgYVERP_8R" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "**Step 4 Option 2:** Add a random seed value so that you can control whether or not to keep the same seed value for each run. The combination of the same seed value and the same prompt will generate the same image each time." ], "metadata": { "id": "43vFBXbGURkl" } }, { "cell_type": "code", "source": [ "# VARIABLES SECTION\n", "thePrompt = \"Put your prompt here\" #@param {type:\"string\"}\n", "# set to -1 to have the random function create a random seed value\n", "seedValue = -1 #@param {type:\"integer\"}\n", "\n", "if seedValue < 0:\n", " seedValue = random.randint(0,999999)\n", "print(\"Seed Value = \",seedValue)\n", "\n", "generator = torch.Generator(\"cuda\").manual_seed(seedValue)\n", "\n", "image = pipe(thePrompt, generator=generator).images[0]\n", "\n", "image # display the image inline in colab" ], "metadata": { "id": "kp6V8E9URZZn" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "**Step 4 Option 3:** Add the ability to control the number of inference steps using the num_inference_steps argument." ], "metadata": { "id": "yucAoqsXSM7z" } }, { "cell_type": "code", "source": [ "# VARIABLES SECTION\n", "thePrompt = \"Put your prompt here\" #@param {type:\"string\"}\n", "# set to -1 to have the random function create a random seed value\n", "seedValue = -1 #@param {type:\"integer\"}\n", "# set the number of inference steps \n", "inferenceSteps = 10 #@param {type:\"slider\", min:0, max:250, step:1}\n", "\n", "# Set the random seed value if so desire\n", "if seedValue < 0:\n", " seedValue = random.randint(0,999999)\n", "print(\"Seed Value= \",seedValue)\n", "\n", "# Build the image\n", "generator = torch.Generator(\"cuda\").manual_seed(seedValue)\n", "image = pipe(thePrompt, num_inference_steps=inferenceSteps, generator=generator).images[0]\n", "\n", "# Display the image\n", "image # display the image inline in colab\n", "# print(\"\\n Done\") a print() here kills the image output functionality" ], "metadata": { "id": "Y4IhjIx7SN_L" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "**Step 4 Option 4:** Add the ability to control the guidance_scale\n" ], "metadata": { "id": "-A3jmLY1W6RU" } }, { "cell_type": "code", "source": [ "# VARIABLES SECTION\n", "thePrompt = \"Put your prompt here\" #@param {type:\"string\"}\n", "# set to -1 to have the random function create a random seed value\n", "seedValue = -1 #@param {type:\"integer\"}\n", "# set the number of inference steps \n", "inferenceSteps = 10 #@param {type:\"slider\", min:0, max:250, step:1}\n", "# guidance scale range is 1 to 30 \n", "guidanceScale = 7.5 #@param {type:\"slider\", min:1, max:30, step:0.5}\n", "\n", "if seedValue < 0:\n", " seedValue = random.randint(0,999999)\n", "print(\"Seed Value= \",seedValue)\n", "\n", "# Build the image\n", "generator = torch.Generator(\"cuda\").manual_seed(seedValue)\n", "\n", "image = pipe(thePrompt, guidance_scale=guidanceScale, num_inference_steps=inferenceSteps, generator=generator).images[0]\n", "\n", "image # display the image inline in colab\n" ], "metadata": { "id": "cJ0_GfxfW94G" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "Step 4 Option 5: Add the ability to create multiple images" ], "metadata": { "id": "pFg6WZucYQLN" } }, { "cell_type": "code", "source": [ "# the following library is used to output images to the screen\n", "from IPython.display import display\n", "# VARIABLES SECTION\n", "thePrompt = \"Put your prompt here\" #@param {type:\"string\"}\n", "# set to -1 to have the random function create a random seed value\n", "seedValue = -1 #@param {type:\"integer\"}\n", "# set the number of inference steps \n", "inferenceSteps = 10 #@param {type:\"slider\", min:0, max:250, step:1}\n", "# guidance scale range is 1 to 30 \n", "guidanceScale = 7.5 #@param {type:\"slider\", min:1, max:30, step:0.5}\n", "# number of Images\n", "numberImages = 3 #@param {type:\"integer\"}\n", "\n", "promptlist = [thePrompt] * numberImages\n", "\n", "# Build the image\n", "for ndx in range(numberImages):\n", " if seedValue < 0:\n", " seedValue = random.randint(0,999999)\n", " #print(\"Seed Value= \",seedValue)\n", " generator = torch.Generator(\"cuda\").manual_seed(seedValue)\n", " image = pipe(prompt=promptlist, \n", " guidance_scale=guidanceScale, \n", " num_inference_steps=inferenceSteps, \n", " generator=generator).images\n", "\n", "for img in image:\n", " display(img)\n", " print(\"-----\")" ], "metadata": { "id": "Qycz_co5YUzF" }, "execution_count": null, "outputs": [] } ] }