English
Japanese
a
Phantom-Artist commited on
Commit
db276de
1 Parent(s): f7bc562

Added the notebook I used to train WD

Browse files
DreamBooth_Stable_Diffusion_works_with_SD_2.ipynb ADDED
@@ -0,0 +1,703 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": null,
6
+ "metadata": {
7
+ "cellView": "form",
8
+ "colab": {
9
+ "base_uri": "https://localhost:8080/"
10
+ },
11
+ "id": "XU7NuMAA2drw",
12
+ "outputId": "b71fda33-9ca0-4d63-9f20-472db963db22"
13
+ },
14
+ "outputs": [
15
+ {
16
+ "name": "stdout",
17
+ "output_type": "stream",
18
+ "text": [
19
+ "NVIDIA A100-SXM4-40GB, 40960 MiB, 40536 MiB\n"
20
+ ]
21
+ }
22
+ ],
23
+ "source": [
24
+ "#@markdown Check type of GPU and VRAM available.\n",
25
+ "!nvidia-smi --query-gpu=name,memory.total,memory.free --format=csv,noheader"
26
+ ]
27
+ },
28
+ {
29
+ "cell_type": "code",
30
+ "execution_count": null,
31
+ "metadata": {
32
+ "colab": {
33
+ "base_uri": "https://localhost:8080/"
34
+ },
35
+ "id": "9rIK1aqT1Cf0",
36
+ "outputId": "0769ad3a-6f27-4c7a-bf32-a1513bffef63"
37
+ },
38
+ "outputs": [
39
+ {
40
+ "name": "stdout",
41
+ "output_type": "stream",
42
+ "text": [
43
+ "Mounted at /content/drive\n"
44
+ ]
45
+ }
46
+ ],
47
+ "source": [
48
+ "from google.colab import drive\n",
49
+ "drive.mount('/content/drive')"
50
+ ]
51
+ },
52
+ {
53
+ "cell_type": "markdown",
54
+ "metadata": {
55
+ "id": "BzM7j0ZSc_9c"
56
+ },
57
+ "source": [
58
+ "https://github.com/ShivamShrirao/diffusers/tree/main/examples/dreambooth"
59
+ ]
60
+ },
61
+ {
62
+ "cell_type": "markdown",
63
+ "metadata": {
64
+ "id": "wnTMyW41cC1E"
65
+ },
66
+ "source": [
67
+ "## Install Requirements"
68
+ ]
69
+ },
70
+ {
71
+ "cell_type": "code",
72
+ "execution_count": null,
73
+ "metadata": {
74
+ "colab": {
75
+ "base_uri": "https://localhost:8080/"
76
+ },
77
+ "id": "aLWXPZqjsZVV",
78
+ "outputId": "e97c0864-61b4-4d81-8f65-773e32aef9ac"
79
+ },
80
+ "outputs": [
81
+ {
82
+ "name": "stdout",
83
+ "output_type": "stream",
84
+ "text": [
85
+ " Installing build dependencies ... \u001b[?25l\u001b[?25hdone\n",
86
+ " Getting requirements to build wheel ... \u001b[?25l\u001b[?25hdone\n",
87
+ " Preparing metadata (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n",
88
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m190.3/190.3 KB\u001b[0m \u001b[31m17.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
89
+ "\u001b[?25h Building wheel for diffusers (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n",
90
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m18.7/18.7 MB\u001b[0m \u001b[31m79.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
91
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m144.0/144.0 KB\u001b[0m \u001b[31m15.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
92
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m6.3/6.3 MB\u001b[0m \u001b[31m103.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
93
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m53.1/53.1 KB\u001b[0m \u001b[31m6.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
94
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m76.3/76.3 MB\u001b[0m \u001b[31m21.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
95
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m14.2/14.2 MB\u001b[0m \u001b[31m76.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
96
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m7.6/7.6 MB\u001b[0m \u001b[31m118.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
97
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m55.8/55.8 KB\u001b[0m \u001b[31m7.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
98
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m71.5/71.5 KB\u001b[0m \u001b[31m5.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
99
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m140.6/140.6 KB\u001b[0m \u001b[31m16.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
100
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m107.0/107.0 KB\u001b[0m \u001b[31m14.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
101
+ "\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
102
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m84.5/84.5 KB\u001b[0m \u001b[31m10.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
103
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.1/2.1 MB\u001b[0m \u001b[31m91.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
104
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m56.9/56.9 KB\u001b[0m \u001b[31m7.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
105
+ "\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
106
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m50.5/50.5 KB\u001b[0m \u001b[31m4.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
107
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m64.3/64.3 KB\u001b[0m \u001b[31m9.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
108
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m80.6/80.6 KB\u001b[0m \u001b[31m11.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
109
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m69.6/69.6 KB\u001b[0m \u001b[31m8.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
110
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 KB\u001b[0m \u001b[31m6.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
111
+ "\u001b[?25h Building wheel for ffmpy (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
112
+ " Building wheel for python-multipart (setup.py) ... \u001b[?25l\u001b[?25hdone\n"
113
+ ]
114
+ }
115
+ ],
116
+ "source": [
117
+ "!wget -q https://github.com/ShivamShrirao/diffusers/raw/main/examples/dreambooth/train_dreambooth.py\n",
118
+ "%pip install -qq git+https://github.com/ShivamShrirao/diffusers\n",
119
+ "%pip install -q -U --pre triton\n",
120
+ "%pip install -q accelerate==0.12.0 transformers ftfy bitsandbytes gradio"
121
+ ]
122
+ },
123
+ {
124
+ "cell_type": "code",
125
+ "execution_count": null,
126
+ "metadata": {
127
+ "cellView": "form",
128
+ "colab": {
129
+ "base_uri": "https://localhost:8080/",
130
+ "height": 327
131
+ },
132
+ "id": "y4lqqWT_uxD2",
133
+ "outputId": "8baacd05-301c-4bec-ff16-9fedabac3dd8"
134
+ },
135
+ "outputs": [
136
+ {
137
+ "name": "stdout",
138
+ "output_type": "stream",
139
+ "text": [
140
+ "Login successful\n",
141
+ "Your token has been saved to /root/.huggingface/token\n"
142
+ ]
143
+ }
144
+ ],
145
+ "source": [
146
+ "#@title Login to HuggingFace 🤗\n",
147
+ "\n",
148
+ "#@markdown You need to accept the model license before downloading or using the Stable Diffusion weights. Please, visit the [model card](https://huggingface.co/CompVis/stable-diffusion-v1-4), read the license and tick the checkbox if you agree. You have to be a registered user in 🤗 Hugging Face Hub, and you'll also need to use an access token for the code to work.\n",
149
+ "from huggingface_hub import notebook_login\n",
150
+ "!git config --global credential.helper store\n",
151
+ "notebook_login()"
152
+ ]
153
+ },
154
+ {
155
+ "cell_type": "markdown",
156
+ "metadata": {
157
+ "id": "XfTlc8Mqb8iH"
158
+ },
159
+ "source": [
160
+ "### Install xformers from precompiled wheel."
161
+ ]
162
+ },
163
+ {
164
+ "cell_type": "code",
165
+ "execution_count": null,
166
+ "metadata": {
167
+ "colab": {
168
+ "base_uri": "https://localhost:8080/"
169
+ },
170
+ "id": "n6dcjPnnaiCn",
171
+ "outputId": "515ea33d-bca8-4d59-c04e-ab97382ea848"
172
+ },
173
+ "outputs": [
174
+ {
175
+ "name": "stdout",
176
+ "output_type": "stream",
177
+ "text": [
178
+ "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
179
+ "Collecting xformers\n",
180
+ " Cloning https://github.com/facebookresearch/xformers (to revision 1d31a3a) to /tmp/pip-install-7ejn07wq/xformers_3f27ca8ec2e5417990ad2bfde90dbaaa\n",
181
+ " Running command git clone --filter=blob:none --quiet https://github.com/facebookresearch/xformers /tmp/pip-install-7ejn07wq/xformers_3f27ca8ec2e5417990ad2bfde90dbaaa\n",
182
+ "\u001b[33m WARNING: Did not find branch or tag '1d31a3a', assuming revision or ref.\u001b[0m\u001b[33m\n",
183
+ "\u001b[0m Running command git checkout -q 1d31a3a\n",
184
+ " Resolved https://github.com/facebookresearch/xformers to commit 1d31a3a\n",
185
+ " Running command git submodule update --init --recursive -q\n",
186
+ " Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
187
+ "Requirement already satisfied: torch>=1.12 in /usr/local/lib/python3.8/dist-packages (from xformers) (1.13.1+cu116)\n",
188
+ "Requirement already satisfied: numpy in /usr/local/lib/python3.8/dist-packages (from xformers) (1.21.6)\n",
189
+ "Collecting pyre-extensions==0.0.23\n",
190
+ " Using cached pyre_extensions-0.0.23-py3-none-any.whl (11 kB)\n",
191
+ "Requirement already satisfied: typing-extensions in /usr/local/lib/python3.8/dist-packages (from pyre-extensions==0.0.23->xformers) (4.4.0)\n",
192
+ "Collecting typing-inspect\n",
193
+ " Using cached typing_inspect-0.8.0-py3-none-any.whl (8.7 kB)\n",
194
+ "Collecting mypy-extensions>=0.3.0\n",
195
+ " Using cached mypy_extensions-1.0.0-py3-none-any.whl (4.7 kB)\n",
196
+ "Building wheels for collected packages: xformers\n",
197
+ " Building wheel for xformers (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
198
+ " Created wheel for xformers: filename=xformers-0.0.14.dev0-cp38-cp38-linux_x86_64.whl size=93158187 sha256=5151194336bd969942c59696c06bff6dbd0498d9315e192b6b773b97dadb6ebb\n",
199
+ " Stored in directory: /tmp/pip-ephem-wheel-cache-shg3tlmw/wheels/5f/3f/8d/a1f7db4c46304e4c83f3ce87dd959d5fcc266b04b9095a737c\n",
200
+ "Successfully built xformers\n",
201
+ "Installing collected packages: mypy-extensions, typing-inspect, pyre-extensions, xformers\n",
202
+ "Successfully installed mypy-extensions-1.0.0 pyre-extensions-0.0.23 typing-inspect-0.8.0 xformers-0.0.14.dev0\n"
203
+ ]
204
+ }
205
+ ],
206
+ "source": [
207
+ "#%pip install -q https://github.com/metrolobo/xformers_wheels/releases/download/1d31a3ac_various_6/xformers-0.0.14.dev0-cp37-cp37m-linux_x86_64.whl\n",
208
+ "# These were compiled on Tesla T4, should also work on P100, thanks to https://github.com/metrolobo\n",
209
+ "\n",
210
+ "# If precompiled wheels don't work, install it with the following command. It will take around 40 minutes to compile.\n",
211
+ "%pip install git+https://github.com/facebookresearch/xformers@1d31a3a#egg=xformers"
212
+ ]
213
+ },
214
+ {
215
+ "cell_type": "markdown",
216
+ "metadata": {
217
+ "id": "G0NV324ZcL9L"
218
+ },
219
+ "source": [
220
+ "## Settings and run"
221
+ ]
222
+ },
223
+ {
224
+ "cell_type": "code",
225
+ "execution_count": 195,
226
+ "metadata": {
227
+ "colab": {
228
+ "base_uri": "https://localhost:8080/"
229
+ },
230
+ "id": "Rxg0y5MBudmd",
231
+ "outputId": "eeb1e86d-bf2f-47e6-a4e0-82d26aeb0549"
232
+ },
233
+ "outputs": [
234
+ {
235
+ "output_type": "stream",
236
+ "name": "stdout",
237
+ "text": [
238
+ "Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n",
239
+ "[*] Weights will be saved at /content/drive/MyDrive/stable_diffusion_weights/aitop_roiyaruRIZ_style\n"
240
+ ]
241
+ }
242
+ ],
243
+ "source": [
244
+ "#@markdown Name/Path of the initial model.\n",
245
+ "MODEL_NAME = \"hakurei/waifu-diffusion\" #@param {type:\"string\"}\n",
246
+ "\n",
247
+ "#@markdown A general name for class like dog for dog images.\n",
248
+ "CLASS_NAME = \"roiyaruRIZ_style\" #@param {type:\"string\"}\n",
249
+ "\n",
250
+ "#@markdown Path for images of the concept for training.\n",
251
+ "INSTANCE_DIR = \"/content/data/aitop_\" #@param {type:\"string\"}\n",
252
+ "INSTANCE_DIR += CLASS_NAME\n",
253
+ "!mkdir -p $INSTANCE_DIR\n",
254
+ "CLASS_DIR = f\"/content/data/{CLASS_NAME}\"\n",
255
+ "\n",
256
+ "#@markdown If model weights should be saved directly in google drive (takes around 4-5 GB).\n",
257
+ "save_to_gdrive = True #@param {type:\"boolean\"}\n",
258
+ "if save_to_gdrive:\n",
259
+ " from google.colab import drive\n",
260
+ " drive.mount('/content/drive')\n",
261
+ "\n",
262
+ "#@markdown Enter the directory name to save model at.\n",
263
+ "\n",
264
+ "OUTPUT_DIR = \"stable_diffusion_weights/aitop_\" #@param {type:\"string\"}\n",
265
+ "OUTPUT_DIR += CLASS_NAME\n",
266
+ "if save_to_gdrive:\n",
267
+ " OUTPUT_DIR = \"/content/drive/MyDrive/\" + OUTPUT_DIR\n",
268
+ "else:\n",
269
+ " OUTPUT_DIR = \"/content/\" + OUTPUT_DIR\n",
270
+ "\n",
271
+ "print(f\"[*] Weights will be saved at {OUTPUT_DIR}\")\n",
272
+ "\n",
273
+ "!mkdir -p $OUTPUT_DIR\n",
274
+ "\n",
275
+ "#@markdown sks is a rare identifier, feel free to replace it."
276
+ ]
277
+ },
278
+ {
279
+ "cell_type": "code",
280
+ "execution_count": null,
281
+ "metadata": {
282
+ "id": "fe-GgtnUVO_e"
283
+ },
284
+ "outputs": [],
285
+ "source": [
286
+ "#@markdown Upload your images by running this cell.\n",
287
+ "\n",
288
+ "#@markdown OR\n",
289
+ "\n",
290
+ "#@markdown You can use the file manager on the left panel to upload (drag and drop) to INSTANCE_DIR (it uploads faster)\n",
291
+ "\n",
292
+ "import os\n",
293
+ "from google.colab import files\n",
294
+ "import shutil\n",
295
+ "\n",
296
+ "uploaded = files.upload()\n",
297
+ "for filename in uploaded.keys():\n",
298
+ " dst_path = os.path.join(INSTANCE_DIR, filename)\n",
299
+ " shutil.move(filename, dst_path)"
300
+ ]
301
+ },
302
+ {
303
+ "cell_type": "markdown",
304
+ "metadata": {
305
+ "id": "qn5ILIyDJIcX"
306
+ },
307
+ "source": [
308
+ "# Start Training\n",
309
+ "\n",
310
+ "Use the table below to choose the best flags based on your memory and speed requirements. Tested on Tesla T4 GPU.\n",
311
+ "\n",
312
+ "\n",
313
+ "| `fp16` | `train_batch_size` | `gradient_accumulation_steps` | `gradient_checkpointing` | `use_8bit_adam` | GB VRAM usage | Speed (it/s) |\n",
314
+ "| ---- | ------------------ | ----------------------------- | ----------------------- | --------------- | ---------- | ------------ |\n",
315
+ "| fp16 | 1 | 1 | TRUE | TRUE | 9.92 | 0.93 |\n",
316
+ "| no | 1 | 1 | TRUE | TRUE | 10.08 | 0.42 |\n",
317
+ "| fp16 | 2 | 1 | TRUE | TRUE | 10.4 | 0.66 |\n",
318
+ "| fp16 | 1 | 1 | FALSE | TRUE | 11.17 | 1.14 |\n",
319
+ "| no | 1 | 1 | FALSE | TRUE | 11.17 | 0.49 |\n",
320
+ "| fp16 | 1 | 2 | TRUE | TRUE | 11.56 | 1 |\n",
321
+ "| fp16 | 2 | 1 | FALSE | TRUE | 13.67 | 0.82 |\n",
322
+ "| fp16 | 1 | 2 | FALSE | TRUE | 13.7 | 0.83 |\n",
323
+ "| fp16 | 1 | 1 | TRUE | FALSE | 15.79 | 0.77 |\n"
324
+ ]
325
+ },
326
+ {
327
+ "cell_type": "markdown",
328
+ "metadata": {
329
+ "id": "-ioxxvHoicPs"
330
+ },
331
+ "source": [
332
+ "Add `--gradient_checkpointing` flag for around 9.92 GB VRAM usage.\n",
333
+ "\n",
334
+ "remove `--use_8bit_adam` flag for full precision. Requires 15.79 GB with `--gradient_checkpointing` else 17.8 GB."
335
+ ]
336
+ },
337
+ {
338
+ "cell_type": "code",
339
+ "execution_count": 196,
340
+ "metadata": {
341
+ "colab": {
342
+ "base_uri": "https://localhost:8080/"
343
+ },
344
+ "id": "jjcSXTp-u-Eg",
345
+ "outputId": "4bdae706-74ed-4a40-f821-ce3f765ce87a"
346
+ },
347
+ "outputs": [
348
+ {
349
+ "output_type": "stream",
350
+ "name": "stdout",
351
+ "text": [
352
+ "The following values were not passed to `accelerate launch` and had defaults used instead:\n",
353
+ "\t`--num_processes` was set to a value of `1`\n",
354
+ "\t`--num_machines` was set to a value of `1`\n",
355
+ "\t`--mixed_precision` was set to a value of `'no'`\n",
356
+ "\t`--num_cpu_threads_per_process` was set to `6` to improve out-of-box performance\n",
357
+ "To avoid this warning pass in values for each of the problematic parameters or run `accelerate config`.\n",
358
+ "Fetching 15 files: 100% 15/15 [00:00<00:00, 171897.70it/s]\n",
359
+ "You have disabled the safety checker for <class 'diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline'> by passing `safety_checker=None`. Ensure that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered results in services or applications open to the public. Both the diffusers team and Hugging Face strongly recommend to keep the safety filter enabled in all public facing circumstances, disabling it only for use-cases that involve analyzing network behavior or auditing its results. For more information, please have a look at https://github.com/huggingface/diffusers/pull/254 .\n",
360
+ "Generating class images: 100% 13/13 [00:38<00:00, 2.95s/it]\n",
361
+ "Caching latents: 100% 50/50 [00:05<00:00, 9.18it/s]\n",
362
+ "Steps: 100% 1000/1000 [05:56<00:00, 2.84it/s, loss=0.351, lr=5e-6]\n",
363
+ "Fetching 15 files: 100% 15/15 [00:00<00:00, 33447.40it/s]\n",
364
+ "You have disabled the safety checker for <class 'diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline'> by passing `safety_checker=None`. Ensure that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered results in services or applications open to the public. Both the diffusers team and Hugging Face strongly recommend to keep the safety filter enabled in all public facing circumstances, disabling it only for use-cases that involve analyzing network behavior or auditing its results. For more information, please have a look at https://github.com/huggingface/diffusers/pull/254 .\n",
365
+ "[*] Weights saved at /content/drive/MyDrive/stable_diffusion_weights/aitop_roiyaruRIZ_style/1000\n",
366
+ "Steps: 100% 1000/1000 [06:21<00:00, 2.62it/s, loss=0.351, lr=5e-6]\n"
367
+ ]
368
+ }
369
+ ],
370
+ "source": [
371
+ "!accelerate launch train_dreambooth.py \\\n",
372
+ " --pretrained_model_name_or_path=$MODEL_NAME \\\n",
373
+ " --instance_data_dir=$INSTANCE_DIR \\\n",
374
+ " --class_data_dir=$CLASS_DIR \\\n",
375
+ " --output_dir=$OUTPUT_DIR \\\n",
376
+ " --with_prior_preservation --prior_loss_weight=1.0 \\\n",
377
+ " --instance_prompt=\"aitop {CLASS_NAME}\" \\\n",
378
+ " --class_prompt=\"{CLASS_NAME}\" \\\n",
379
+ " --seed=1337 \\\n",
380
+ " --resolution=512 \\\n",
381
+ " --train_batch_size=1 \\\n",
382
+ " --mixed_precision=\"fp16\" \\\n",
383
+ " --gradient_accumulation_steps=1 \\\n",
384
+ " --gradient_checkpointing \\\n",
385
+ " --learning_rate=5e-6 \\\n",
386
+ " --lr_scheduler=\"constant\" \\\n",
387
+ " --lr_warmup_steps=0 \\\n",
388
+ " --num_class_images=50 \\\n",
389
+ " --sample_batch_size=4 \\\n",
390
+ " --max_train_steps=1000\n",
391
+ " #--use_8bit_adam \\\n"
392
+ ]
393
+ },
394
+ {
395
+ "cell_type": "markdown",
396
+ "metadata": {
397
+ "id": "5V8wgU0HN-Kq"
398
+ },
399
+ "source": [
400
+ "## Convert weights to ckpt to use in web UIs like AUTOMATIC1111."
401
+ ]
402
+ },
403
+ {
404
+ "cell_type": "code",
405
+ "execution_count": null,
406
+ "metadata": {
407
+ "colab": {
408
+ "base_uri": "https://localhost:8080/"
409
+ },
410
+ "id": "baL22PHzOLeP",
411
+ "outputId": "7969edd9-51f6-4f35-ba16-8e04700c132d"
412
+ },
413
+ "outputs": [
414
+ {
415
+ "name": "stdout",
416
+ "output_type": "stream",
417
+ "text": [
418
+ "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
419
+ "Collecting safetensors\n",
420
+ " Downloading safetensors-0.2.8-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB)\n",
421
+ "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.2/1.2 MB\u001b[0m \u001b[31m63.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
422
+ "\u001b[?25hInstalling collected packages: safetensors\n",
423
+ "Successfully installed safetensors-0.2.8\n"
424
+ ]
425
+ }
426
+ ],
427
+ "source": [
428
+ "#@markdown Download script\n",
429
+ "!wget -q https://github.com/ShivamShrirao/diffusers/raw/main/scripts/convert_diffusers_to_original_stable_diffusion.py\n",
430
+ "!pip install safetensors"
431
+ ]
432
+ },
433
+ {
434
+ "cell_type": "code",
435
+ "execution_count": 197,
436
+ "metadata": {
437
+ "colab": {
438
+ "base_uri": "https://localhost:8080/"
439
+ },
440
+ "id": "89Az5NUxOWdy",
441
+ "outputId": "e97e902b-d673-47de-c7fb-99dad404a8ca"
442
+ },
443
+ "outputs": [
444
+ {
445
+ "output_type": "stream",
446
+ "name": "stdout",
447
+ "text": [
448
+ "Reshaping encoder.mid.attn_1.q.weight for SD format\n",
449
+ "Reshaping encoder.mid.attn_1.k.weight for SD format\n",
450
+ "Reshaping encoder.mid.attn_1.v.weight for SD format\n",
451
+ "Reshaping encoder.mid.attn_1.proj_out.weight for SD format\n",
452
+ "Reshaping decoder.mid.attn_1.q.weight for SD format\n",
453
+ "Reshaping decoder.mid.attn_1.k.weight for SD format\n",
454
+ "Reshaping decoder.mid.attn_1.v.weight for SD format\n",
455
+ "Reshaping decoder.mid.attn_1.proj_out.weight for SD format\n",
456
+ "[*] Converted ckpt saved at /content/drive/MyDrive/stable_diffusion_weights/aitop_roiyaruRIZ_style/1000/phantom_roiyaruRIZ_style_Diffusion.ckpt\n"
457
+ ]
458
+ }
459
+ ],
460
+ "source": [
461
+ "#@markdown Run conversion.\n",
462
+ "ckpt_path = OUTPUT_DIR + f\"/1000/phantom_{CLASS_NAME}_Diffusion.ckpt\"\n",
463
+ "output_dir_with_steps = OUTPUT_DIR + \"/1000\"\n",
464
+ "\n",
465
+ "half_arg = \"\"\n",
466
+ "#@markdown Whether to convert to fp16, takes half the space (2GB), might loose some quality.\n",
467
+ "fp16 = False #@param {type: \"boolean\"}\n",
468
+ "if fp16:\n",
469
+ " half_arg = \"--half\"\n",
470
+ "!python convert_diffusers_to_original_stable_diffusion.py --model_path $output_dir_with_steps --checkpoint_path $ckpt_path $half_arg\n",
471
+ "print(f\"[*] Converted ckpt saved at {ckpt_path}\")"
472
+ ]
473
+ },
474
+ {
475
+ "cell_type": "markdown",
476
+ "metadata": {
477
+ "id": "ToNG4fd_dTbF"
478
+ },
479
+ "source": [
480
+ "## Inference"
481
+ ]
482
+ },
483
+ {
484
+ "cell_type": "code",
485
+ "execution_count": null,
486
+ "metadata": {
487
+ "id": "NA8IECI8InNr"
488
+ },
489
+ "outputs": [],
490
+ "source": [
491
+ "import torch\n",
492
+ "from torch import autocast\n",
493
+ "from diffusers import StableDiffusionPipeline\n",
494
+ "from IPython.display import display"
495
+ ]
496
+ },
497
+ {
498
+ "cell_type": "code",
499
+ "execution_count": 199,
500
+ "metadata": {
501
+ "colab": {
502
+ "base_uri": "https://localhost:8080/"
503
+ },
504
+ "id": "gW15FjffdTID",
505
+ "outputId": "92a545d0-f9b6-4b33-d6de-1b0e3e0d00b8"
506
+ },
507
+ "outputs": [
508
+ {
509
+ "output_type": "stream",
510
+ "name": "stderr",
511
+ "text": [
512
+ "You have disabled the safety checker for <class 'diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline'> by passing `safety_checker=None`. Ensure that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered results in services or applications open to the public. Both the diffusers team and Hugging Face strongly recommend to keep the safety filter enabled in all public facing circumstances, disabling it only for use-cases that involve analyzing network behavior or auditing its results. For more information, please have a look at https://github.com/huggingface/diffusers/pull/254 .\n"
513
+ ]
514
+ }
515
+ ],
516
+ "source": [
517
+ "model_path = output_dir_with_steps # If you want to use previously trained model saved in gdrive, replace this with the full path of model in gdrive\n",
518
+ "\n",
519
+ "pipe = StableDiffusionPipeline.from_pretrained(model_path, torch_dtype=torch.float16).to(\"cuda\")\n",
520
+ "g_cuda = None"
521
+ ]
522
+ },
523
+ {
524
+ "cell_type": "code",
525
+ "execution_count": null,
526
+ "metadata": {
527
+ "cellView": "form",
528
+ "colab": {
529
+ "base_uri": "https://localhost:8080/"
530
+ },
531
+ "id": "oIzkltjpVO_f",
532
+ "outputId": "1db9fcaa-2d0f-4966-dc4f-baac60cdb807"
533
+ },
534
+ "outputs": [
535
+ {
536
+ "data": {
537
+ "text/plain": [
538
+ "<torch._C.Generator at 0x7f81b9035570>"
539
+ ]
540
+ },
541
+ "execution_count": 38,
542
+ "metadata": {},
543
+ "output_type": "execute_result"
544
+ }
545
+ ],
546
+ "source": [
547
+ "#@markdown Can set random seed here for reproducibility.\n",
548
+ "g_cuda = torch.Generator(device='cuda')\n",
549
+ "seed = 52362 #@param {type:\"number\"}\n",
550
+ "g_cuda.manual_seed(seed)"
551
+ ]
552
+ },
553
+ {
554
+ "cell_type": "code",
555
+ "execution_count": 1,
556
+ "metadata": {
557
+ "cellView": "form",
558
+ "colab": {
559
+ "base_uri": "https://localhost:8080/",
560
+ "height": 240
561
+ },
562
+ "id": "K6xoHWSsbcS3",
563
+ "outputId": "7bbe13a0-0e31-48a3-cf94-f7200c02ceb7"
564
+ },
565
+ "outputs": [
566
+ {
567
+ "output_type": "error",
568
+ "ename": "NameError",
569
+ "evalue": "ignored",
570
+ "traceback": [
571
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
572
+ "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
573
+ "\u001b[0;32m<ipython-input-1-c0940be36692>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0mwidth\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m512\u001b[0m \u001b[0;31m#@param {type:\"number\"}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 11\u001b[0;31m \u001b[0;32mwith\u001b[0m \u001b[0mautocast\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"cuda\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minference_mode\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 12\u001b[0m images = pipe(\n\u001b[1;32m 13\u001b[0m \u001b[0mprompt\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
574
+ "\u001b[0;31mNameError\u001b[0m: name 'autocast' is not defined"
575
+ ]
576
+ }
577
+ ],
578
+ "source": [
579
+ "#@title Run for generating images.\n",
580
+ "\n",
581
+ "prompt = \"masterpiece, best quality, 1girl, aitop roiyaruRIZ_style\" #@param {type:\"string\"}\n",
582
+ "negative_prompt = \"nsfw, worst quality, low quality, medium quality, deleted, lowres, bad anatomy, bad hands, text, error, missing fingers, extra digits, fewer digits, cropped, jpeg artifacts, signature, watermark, username, blurry, simple background\" #@param {type:\"string\"}\n",
583
+ "num_samples = 4 #@param {type:\"number\"}\n",
584
+ "guidance_scale = 11 #@param {type:\"number\"}\n",
585
+ "num_inference_steps = 50 #@param {type:\"number\"}\n",
586
+ "height = 512 #@param {type:\"number\"}\n",
587
+ "width = 512 #@param {type:\"number\"}\n",
588
+ "\n",
589
+ "with autocast(\"cuda\"), torch.inference_mode():\n",
590
+ " images = pipe(\n",
591
+ " prompt,\n",
592
+ " height=height,\n",
593
+ " width=width,\n",
594
+ " negative_prompt=negative_prompt,\n",
595
+ " num_images_per_prompt=num_samples,\n",
596
+ " num_inference_steps=num_inference_steps,\n",
597
+ " guidance_scale=guidance_scale,\n",
598
+ " generator=g_cuda\n",
599
+ " ).images\n",
600
+ "\n",
601
+ "for img in images:\n",
602
+ " display(img)"
603
+ ]
604
+ },
605
+ {
606
+ "cell_type": "code",
607
+ "execution_count": null,
608
+ "metadata": {
609
+ "cellView": "form",
610
+ "id": "WMCqQ5Tcdsm2"
611
+ },
612
+ "outputs": [],
613
+ "source": [
614
+ "#@markdown Run Gradio UI for generating images.\n",
615
+ "import gradio as gr\n",
616
+ "\n",
617
+ "def inference(prompt, negative_prompt, num_samples, height=512, width=512, num_inference_steps=50, guidance_scale=7.5):\n",
618
+ " with torch.autocast(\"cuda\"), torch.inference_mode():\n",
619
+ " return pipe(\n",
620
+ " prompt, height=int(height), width=int(width),\n",
621
+ " negative_prompt=negative_prompt,\n",
622
+ " num_images_per_prompt=int(num_samples),\n",
623
+ " num_inference_steps=int(num_inference_steps), guidance_scale=guidance_scale,\n",
624
+ " generator=g_cuda\n",
625
+ " ).images\n",
626
+ "\n",
627
+ "with gr.Blocks() as demo:\n",
628
+ " with gr.Row():\n",
629
+ " with gr.Column():\n",
630
+ " prompt = gr.Textbox(label=\"Prompt\", value=\"photo of sks guy, digital painting\")\n",
631
+ " negative_prompt = gr.Textbox(label=\"Negative Prompt\", value=\"\")\n",
632
+ " run = gr.Button(value=\"Generate\")\n",
633
+ " with gr.Row():\n",
634
+ " num_samples = gr.Number(label=\"Number of Samples\", value=4)\n",
635
+ " guidance_scale = gr.Number(label=\"Guidance Scale\", value=7.5)\n",
636
+ " with gr.Row():\n",
637
+ " height = gr.Number(label=\"Height\", value=512)\n",
638
+ " width = gr.Number(label=\"Width\", value=512)\n",
639
+ " num_inference_steps = gr.Slider(label=\"Steps\", value=50)\n",
640
+ " with gr.Column():\n",
641
+ " gallery = gr.Gallery()\n",
642
+ "\n",
643
+ " run.click(inference, inputs=[prompt, negative_prompt, num_samples, height, width, num_inference_steps, guidance_scale], outputs=gallery)\n",
644
+ "\n",
645
+ "demo.launch(debug=True)"
646
+ ]
647
+ },
648
+ {
649
+ "cell_type": "code",
650
+ "execution_count": null,
651
+ "metadata": {
652
+ "id": "lJoOgLQHnC8L"
653
+ },
654
+ "outputs": [],
655
+ "source": [
656
+ "#@title (Optional) Delete diffuser weights and only keep the ckpt to free up drive space (4GB).\n",
657
+ "\n",
658
+ "#@markdown [ ! ] Caution, Only execute if you are sure u want to delete the diffuser format weights and only use the ckpt.\n",
659
+ "import shutil\n",
660
+ "from glob import glob\n",
661
+ "for f in glob(OUTPUT_DIR+\"/*\"):\n",
662
+ " if not f.endswith(\".ckpt\"):\n",
663
+ " try:\n",
664
+ " shutil.rmtree(f)\n",
665
+ " except NotADirectoryError:\n",
666
+ " continue\n",
667
+ " print(\"Deleted\", f)"
668
+ ]
669
+ },
670
+ {
671
+ "cell_type": "code",
672
+ "execution_count": null,
673
+ "metadata": {
674
+ "id": "jXgi8HM4c-DA"
675
+ },
676
+ "outputs": [],
677
+ "source": []
678
+ }
679
+ ],
680
+ "metadata": {
681
+ "accelerator": "GPU",
682
+ "colab": {
683
+ "provenance": []
684
+ },
685
+ "gpuClass": "premium",
686
+ "kernelspec": {
687
+ "display_name": "Python 3.10.7 64-bit",
688
+ "language": "python",
689
+ "name": "python3"
690
+ },
691
+ "language_info": {
692
+ "name": "python",
693
+ "version": "3.10.7"
694
+ },
695
+ "vscode": {
696
+ "interpreter": {
697
+ "hash": "e7370f93d1d0cde622a1f8e1c04877d8463912d04d973331ad4851f04de6915a"
698
+ }
699
+ }
700
+ },
701
+ "nbformat": 4,
702
+ "nbformat_minor": 0
703
+ }