Skip to content

Instantly share code, notes, and snippets.

@tibaes
Created December 1, 2022 17:53
Show Gist options
  • Save tibaes/588ccdba75b29c86408b39e41aceb755 to your computer and use it in GitHub Desktop.
Save tibaes/588ccdba75b29c86408b39e41aceb755 to your computer and use it in GitHub Desktop.
stable-diffusion.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "stable-diffusion.ipynb",
"private_outputs": true,
"provenance": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"accelerator": "GPU",
"gpuClass": "standard"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/tibaes/588ccdba75b29c86408b39e41aceb755/stable-diffusion.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "QuFz5uGi-h6G"
},
"outputs": [],
"source": [
"%pip install --quiet --upgrade diffusers transformers scipy mediapy accelerate ftfy spacy"
]
},
{
"cell_type": "code",
"source": [
"import subprocess\n",
"\n",
"# The xformers package is mandatory to be able to create several 768x768 images.\n",
"github_url = \"https://github.com/TheLastBen/fast-stable-diffusion\"\n",
"xformers_wheels = \"xformers-0.0.13.dev0-py3-none-any.whl\"\n",
"\n",
"# Obtain GPU info\n",
"\n",
"nvidia_output = subprocess.run(['nvidia-smi', '-q'], capture_output=True).stdout\n",
"\n",
"gpu_info = [\n",
" str(line) for line in str(nvidia_output).split('\\\\n')\n",
" if \"Product Name\" in line\n",
" ]\n",
"\n",
"print(gpu_info)\n",
"\n",
"# Identify your GPU\n",
"\n",
"gpu_name = None\n",
"\n",
"for gpu_test in ['A100', 'K80', 'P100', 'T4', 'V100']:\n",
" if any(gpu_test in line for line in gpu_info):\n",
" gpu_name = gpu_test\n",
" break\n",
"\n",
"# Install xformers using pre-compiled Python wheels\n",
"%pip install -q {github_url}/raw/main/precompiled/{gpu_name}/{xformers_wheels}"
],
"metadata": {
"id": "oP_dBQpSCIkY"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# model_id = \"stabilityai/stable-diffusion-2-base\"\n",
"model_id = \"stabilityai/stable-diffusion-2\""
],
"metadata": {
"id": "GR4vF2bw-sHR"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from diffusers import PNDMScheduler, DDIMScheduler, LMSDiscreteScheduler, EulerDiscreteScheduler\n",
"\n",
"# scheduler = PNDMScheduler.from_pretrained(model_id, subfolder=\"scheduler\")\n",
"# scheduler = DDIMScheduler.from_pretrained(model_id, subfolder=\"scheduler\")\n",
"# scheduler = LMSDiscreteScheduler.from_pretrained(model_id, subfolder=\"scheduler\")\n",
"scheduler = EulerDiscreteScheduler.from_pretrained(model_id, subfolder=\"scheduler\")"
],
"metadata": {
"id": "vF9Q0xKX8gLR"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import mediapy as media\n",
"import torch\n",
"from diffusers import StableDiffusionPipeline\n",
"\n",
"device = \"cuda\"\n",
"\n",
"pipe = StableDiffusionPipeline.from_pretrained(\n",
" model_id,\n",
" scheduler=scheduler,\n",
" torch_dtype=torch.float16,\n",
" revision=\"fp16\",\n",
" )\n",
"pipe = pipe.to(device)\n",
"pipe.enable_xformers_memory_efficient_attention()\n",
"\n",
"if model_id.endswith('-base'):\n",
" image_length = 512\n",
"else:\n",
" image_length = 768\n",
"\n"
],
"metadata": {
"id": "bG2hkmSEvByV"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"prompt = \"A pikachu fine dining with a view to the Eiffel Tower\"\n",
"num_images = 4\n",
"\n",
"images = pipe(\n",
" prompt,\n",
" num_images_per_prompt=num_images,\n",
" guidance_scale=9,\n",
" num_inference_steps=25,\n",
" height=image_length,\n",
" width=image_length,\n",
" ).images\n",
" \n",
"media.show_images(images)\n",
"images[0].save(\"output.jpg\")"
],
"metadata": {
"id": "AUc4QJfE-uR9"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "AxmBhekRzzUt"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment