cmc/data-science

Personal data science learning files.

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

b5590008a2a8a1dba0defca9f6f3bb0377c6365b

unsigned

author: Christian Cleberg <hello@cleberg.net> · 2023-09-19T04:10:17Z

add StableDiffusion notebook
 .DS_Store                       | Bin 0 -> 6148 bytes
 notebooks/.DS_Store             | Bin 0 -> 6148 bytes
 notebooks/StableDiffusion.ipynb | 120 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 120 insertions(+)

diff --git a/.DS_Store b/.DS_Store
new file mode 100644
index 0000000..3e17dcc
Binary files /dev/null and b/.DS_Store differ
diff --git a/notebooks/.DS_Store b/notebooks/.DS_Store
new file mode 100644
index 0000000..5008ddf
Binary files /dev/null and b/notebooks/.DS_Store differ
diff --git a/notebooks/StableDiffusion.ipynb b/notebooks/StableDiffusion.ipynb
new file mode 100644
index 0000000..58074fd
--- /dev/null
+++ b/notebooks/StableDiffusion.ipynb
@@ -0,0 +1,120 @@
+{
+ "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
+}