summaryrefslogtreecommitdiff
path: root/diffusion/tutorials/01_tut.ipynb
blob: bcb90776b29d33d4dab5d1042f7c70c918919fff (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3306ed06",
   "metadata": {},
   "source": [
    "## 1. 注册及token"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c132f4d2",
   "metadata": {},
   "source": [
    "- 注册:\n",
    "    - https://huggingface.co\n",
    "- token\n",
    "    - https://huggingface.co/settings/tokens\n",
    "\n",
    "- 安装 huggingface 命令行工具:\n",
    "    - `$ python -m pip install huggingface_hub`\n",
    "    - `$ huggingface-cli login`\n",
    "\n",
    "- huggingface 获取 CompVis 相关模型的访问权限\n",
    "    - https://huggingface.co/CompVis/stable-diffusion-v1-4"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99e0b5e9",
   "metadata": {},
   "source": [
    "## 2. github 项目"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f8ee18a3",
   "metadata": {},
   "source": [
    "- git clone 到本地\n",
    "    - `git clone https://github.com/CompVis/stable-diffusion`\n",
    "- 配置虚拟环境 `ldm`(latent diffusion model)\n",
    " \n",
    "```\n",
    "conda env create -f environment.yaml\n",
    "conda activate ldm\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77e4ea71",
   "metadata": {},
   "source": [
    "## 3. 生成第一个图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0bca3d05",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/chzhang/anaconda3/envs/ldm/lib/python3.8/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    }
   ],
   "source": [
    "from torch import autocast"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "83d22a58",
   "metadata": {},
   "outputs": [],
   "source": [
    "from diffusers import StableDiffusionPipeline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "eac64ab0",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Fetching 16 files: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 16/16 [00:00<00:00, 29127.11it/s]\n",
      "The config attributes {'clip_sample': False} were passed to PNDMScheduler, but are not expected and will be ignored. Please verify your scheduler_config.json configuration file.\n"
     ]
    }
   ],
   "source": [
    "pipe = StableDiffusionPipeline.from_pretrained(\n",
    "\t\"CompVis/stable-diffusion-v1-4\",\n",
    "#     revision='fp32',\n",
    "#     revision='fp16'\n",
    "\tuse_auth_token=True\n",
    ").to(\"cuda\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "55d7a9ec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "StableDiffusionPipeline {\n",
       "  \"_class_name\": \"StableDiffusionPipeline\",\n",
       "  \"_diffusers_version\": \"0.6.0\",\n",
       "  \"feature_extractor\": [\n",
       "    \"transformers\",\n",
       "    \"CLIPFeatureExtractor\"\n",
       "  ],\n",
       "  \"safety_checker\": [\n",
       "    \"stable_diffusion\",\n",
       "    \"StableDiffusionSafetyChecker\"\n",
       "  ],\n",
       "  \"scheduler\": [\n",
       "    \"diffusers\",\n",
       "    \"PNDMScheduler\"\n",
       "  ],\n",
       "  \"text_encoder\": [\n",
       "    \"transformers\",\n",
       "    \"CLIPTextModel\"\n",
       "  ],\n",
       "  \"tokenizer\": [\n",
       "    \"transformers\",\n",
       "    \"CLIPTokenizer\"\n",
       "  ],\n",
       "  \"unet\": [\n",
       "    \"diffusers\",\n",
       "    \"UNet2DConditionModel\"\n",
       "  ],\n",
       "  \"vae\": [\n",
       "    \"diffusers\",\n",
       "    \"AutoencoderKL\"\n",
       "  ]\n",
       "}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pipe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "4f07fd46",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = 'a photo of a chinese woman riding a horse on mars'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "8a30d00a",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 51/51 [00:12<00:00,  4.19it/s]\n"
     ]
    }
   ],
   "source": [
    "with autocast('cuda'):\n",
    "    output = pipe(prompt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "547d0e63",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "StableDiffusionPipelineOutput(images=[<PIL.Image.Image image mode=RGB size=512x512 at 0x7F5E81449CD0>], nsfw_content_detected=[False])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "679a12f1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<PIL.Image.Image image mode=RGB size=512x512>]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "output['images']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "668beeef",
   "metadata": {},
   "outputs": [],
   "source": [
    "from PIL import Image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "ca824027",
   "metadata": {},
   "outputs": [],
   "source": [
    "output['images'][0].show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "01f4eee8",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'output' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn [1], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43moutput\u001b[49m[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mimages\u001b[39m\u001b[38;5;124m'\u001b[39m][\u001b[38;5;241m0\u001b[39m]\u001b[38;5;241m.\u001b[39msave(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m./output/1.png\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
      "\u001b[0;31mNameError\u001b[0m: name 'output' is not defined"
     ]
    }
   ],
   "source": [
    "output['images'][0].save('./output/1.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "479e3705",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}