aboutsummaryrefslogtreecommitdiff
path: root/notebooks/StableDiffusion.ipynb
blob: 58074fdbf1d5d6f24ab44c2ec7192c6d4029b5c5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
}