{ "cells": [ { "cell_type": "markdown", "id": "1d22b3c2-c883-4947-aff6-0f23aa7b14e7", "metadata": {}, "source": [ "# Stable Diffusion\n", "\n", "Using a pre-trained text-to-image model." ] }, { "cell_type": "code", "execution_count": null, "id": "de9ec933-22dd-4018-ad17-67de41b5e3e5", "metadata": {}, "outputs": [], "source": [ "# pip3 install diffusers transformers accelerate scipy safetensors xformers" ] }, { "cell_type": "code", "execution_count": 7, "id": "9173c5b8-e9f1-44da-a9e0-2fe36214e602", "metadata": {}, "outputs": [], "source": [ "import torch\n", "from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler\n", "import os" ] }, { "cell_type": "code", "execution_count": 2, "id": "645dcf47-fe61-4291-92a5-9abe7c762221", "metadata": {}, "outputs": [], "source": [ "model_id = \"stabilityai/stable-diffusion-2-1\"" ] }, { "cell_type": "code", "execution_count": 12, "id": "78581804-40dc-4453-9052-5f4d27bf84d4", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Loading pipeline components...: 100%|█████████████████████████████████████████████████████| 6/6 [00:00<00:00, 16.38it/s]\n" ] } ], "source": [ "# Use the DPMSolverMultistepScheduler (DPM-Solver++) scheduler here instead\n", "# Using float32 instead of float16+cuda to compute with CPU rather than GPU\n", "pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float32)\n", "pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)\n", "# pipe = pipe.to(\"cuda\")" ] }, { "cell_type": "code", "execution_count": 13, "id": "9cadef32-3304-49f0-87c3-fac7caafab12", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "100%|███████████████████████████████████████████████████████████████████████████████████| 50/50 [11:10<00:00, 13.41s/it]\n", "/opt/homebrew/lib/python3.11/site-packages/diffusers/image_processor.py:88: RuntimeWarning: invalid value encountered in cast\n", " images = (images * 255).round().astype(\"uint8\")\n" ] } ], "source": [ "prompt = \"a photo of a ninja crouched on a torii on a cliff above the sea\"\n", "image = pipe(prompt).images[0]" ] }, { "cell_type": "code", "execution_count": 15, "id": "02e99341-5467-46e8-869b-431fb4946864", "metadata": {}, "outputs": [], "source": [ "image.save(\"generated_image.png\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" } }, "nbformat": 4, "nbformat_minor": 5 }