cmc/data-science

Personal data science learning files.

clone: git clone https://gitbay.org/cmc/data-science.git

main: notebooks/StableDiffusion.ipynb · raw

  1{
  2 "cells": [
  3  {
  4   "cell_type": "markdown",
  5   "id": "1d22b3c2-c883-4947-aff6-0f23aa7b14e7",
  6   "metadata": {},
  7   "source": [
  8    "# Stable Diffusion\n",
  9    "\n",
 10    "Using a pre-trained text-to-image model."
 11   ]
 12  },
 13  {
 14   "cell_type": "code",
 15   "execution_count": null,
 16   "id": "de9ec933-22dd-4018-ad17-67de41b5e3e5",
 17   "metadata": {},
 18   "outputs": [],
 19   "source": [
 20    "# pip3 install diffusers transformers accelerate scipy safetensors xformers"
 21   ]
 22  },
 23  {
 24   "cell_type": "code",
 25   "execution_count": 7,
 26   "id": "9173c5b8-e9f1-44da-a9e0-2fe36214e602",
 27   "metadata": {},
 28   "outputs": [],
 29   "source": [
 30    "import torch\n",
 31    "from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler\n",
 32    "import os"
 33   ]
 34  },
 35  {
 36   "cell_type": "code",
 37   "execution_count": 2,
 38   "id": "645dcf47-fe61-4291-92a5-9abe7c762221",
 39   "metadata": {},
 40   "outputs": [],
 41   "source": [
 42    "model_id = \"stabilityai/stable-diffusion-2-1\""
 43   ]
 44  },
 45  {
 46   "cell_type": "code",
 47   "execution_count": 12,
 48   "id": "78581804-40dc-4453-9052-5f4d27bf84d4",
 49   "metadata": {},
 50   "outputs": [
 51    {
 52     "name": "stderr",
 53     "output_type": "stream",
 54     "text": [
 55      "Loading pipeline components...: 100%|█████████████████████████████████████████████████████| 6/6 [00:00<00:00, 16.38it/s]\n"
 56     ]
 57    }
 58   ],
 59   "source": [
 60    "# Use the DPMSolverMultistepScheduler (DPM-Solver++) scheduler here instead\n",
 61    "# Using float32 instead of float16+cuda to compute with CPU rather than GPU\n",
 62    "pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float32)\n",
 63    "pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)\n",
 64    "# pipe = pipe.to(\"cuda\")"
 65   ]
 66  },
 67  {
 68   "cell_type": "code",
 69   "execution_count": 13,
 70   "id": "9cadef32-3304-49f0-87c3-fac7caafab12",
 71   "metadata": {},
 72   "outputs": [
 73    {
 74     "name": "stderr",
 75     "output_type": "stream",
 76     "text": [
 77      "100%|███████████████████████████████████████████████████████████████████████████████████| 50/50 [11:10<00:00, 13.41s/it]\n",
 78      "/opt/homebrew/lib/python3.11/site-packages/diffusers/image_processor.py:88: RuntimeWarning: invalid value encountered in cast\n",
 79      "  images = (images * 255).round().astype(\"uint8\")\n"
 80     ]
 81    }
 82   ],
 83   "source": [
 84    "prompt = \"a photo of a ninja crouched on a torii on a cliff above the sea\"\n",
 85    "image = pipe(prompt).images[0]"
 86   ]
 87  },
 88  {
 89   "cell_type": "code",
 90   "execution_count": 15,
 91   "id": "02e99341-5467-46e8-869b-431fb4946864",
 92   "metadata": {},
 93   "outputs": [],
 94   "source": [
 95    "image.save(\"generated_image.png\")"
 96   ]
 97  }
 98 ],
 99 "metadata": {
100  "kernelspec": {
101   "display_name": "Python 3 (ipykernel)",
102   "language": "python",
103   "name": "python3"
104  },
105  "language_info": {
106   "codemirror_mode": {
107    "name": "ipython",
108    "version": 3
109   },
110   "file_extension": ".py",
111   "mimetype": "text/x-python",
112   "name": "python",
113   "nbconvert_exporter": "python",
114   "pygments_lexer": "ipython3",
115   "version": "3.11.5"
116  }
117 },
118 "nbformat": 4,
119 "nbformat_minor": 5
120}