nanashisan commited on
Commit
6ad3005
1 Parent(s): 2af2e3c

Upload kohya_SD_PaperSpace.ipynb

Browse files
Files changed (1) hide show
  1. kohya_SD_PaperSpace.ipynb +621 -0
kohya_SD_PaperSpace.ipynb ADDED
@@ -0,0 +1,621 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "cd47645b-3a64-433e-89a0-25fa30217a2c",
6
+ "metadata": {},
7
+ "source": [
8
+ "## 説明"
9
+ ]
10
+ },
11
+ {
12
+ "cell_type": "markdown",
13
+ "id": "06077106-1f0b-406e-8c82-fb127574bebe",
14
+ "metadata": {},
15
+ "source": [
16
+ "Dreambooth-Loraの学習をPeperspaceで動かす為のNotebook \n",
17
+ "本家sd-scripts(https://github.com/kohya-ss/sd-scripts) \n",
18
+ "\n",
19
+ "以下ソースを参考に作成してるで。 \n",
20
+ "sd-scripts(https://github.com/kohya-ss/sd-scripts) \n",
21
+ "colab用kohya-trainer(https://github.com/Linaqruf/kohya-trainer) \n",
22
+ "Peperspace用webui(https://github.com/Engineer-of-Stuff/stable-diffusion-paperspace) \n",
23
+ "\n",
24
+ "学習素材と正規化画像はあらかじめstorageかtmpにアップしてな。 \n",
25
+ "永続Storageがある事と一部ターミナル使う前提になってるから無課金では動かんかもしれんで "
26
+ ]
27
+ },
28
+ {
29
+ "cell_type": "markdown",
30
+ "id": "b07c14b6-b67f-41f3-9a1b-02730b32becf",
31
+ "metadata": {},
32
+ "source": [
33
+ "<span style=\"color: red\">既知の不具合</span> \n",
34
+ "学習実行時に以下の警告メッセージが表示されるで \n",
35
+ "解決策わかったら教えてください \n",
36
+ "- 「--use_8bit_adam 」を有効にすると別パッケージから参照の警告メッセージが表示される。(多分bitsandbytesのパスがおかしい) \n",
37
+ "- 「Could not load dynamic library 'libnvinfer_plugin.so.7';」の警告メッセージが表示される。(libnvinfer_plugin.so.7がpython3.9に無い?) \n",
38
+ "- 「Unable to register cuBLAS factory 」の警告メッセージが表示される。(xpaformer入れる為にcudnnのバージョン下げてるのが怪しい) \n"
39
+ ]
40
+ },
41
+ {
42
+ "cell_type": "markdown",
43
+ "id": "4eb1d725-e55c-41e9-8574-da6dfb641ff0",
44
+ "metadata": {
45
+ "tags": []
46
+ },
47
+ "source": [
48
+ "## 1.SETTING"
49
+ ]
50
+ },
51
+ {
52
+ "cell_type": "markdown",
53
+ "id": "d33d8e53-af14-4033-9ba2-0c4044541763",
54
+ "metadata": {
55
+ "tags": []
56
+ },
57
+ "source": [
58
+ "# 1-0 設定値保存\n",
59
+ "仮想マシン起動時毎回実行する"
60
+ ]
61
+ },
62
+ {
63
+ "cell_type": "code",
64
+ "execution_count": null,
65
+ "id": "e90d2a8f-f497-421d-9a7e-3921caff41c4",
66
+ "metadata": {
67
+ "tags": []
68
+ },
69
+ "outputs": [],
70
+ "source": [
71
+ "#リポジトリ 永続ストレー、一時領域ジシンボリックリンク作成\n",
72
+ "repo_dir = '/notebooks' \n",
73
+ "!ln -s /storage/ /notebooks/\n",
74
+ "!ln -s /tmp/ /notebooks/\n",
75
+ "\n",
76
+ "#その他設定値\n",
77
+ "activate_xformers = True # Enables the xformers optimizations using pre-built wheels.\n",
78
+ "\n",
79
+ "%store repo_dir activate_xformers"
80
+ ]
81
+ },
82
+ {
83
+ "cell_type": "markdown",
84
+ "id": "8f1b1a2f-d1ab-4b84-87dc-c83190bf506d",
85
+ "metadata": {
86
+ "tags": []
87
+ },
88
+ "source": [
89
+ "# 1-1.Git Clone\n",
90
+ "導入時 更新時"
91
+ ]
92
+ },
93
+ {
94
+ "cell_type": "code",
95
+ "execution_count": null,
96
+ "id": "114fc353-213a-4d91-afce-f733ba5a9de2",
97
+ "metadata": {
98
+ "tags": []
99
+ },
100
+ "outputs": [],
101
+ "source": [
102
+ "%cd {repo_dir}\n",
103
+ "\n",
104
+ "import os\n",
105
+ "\n",
106
+ "def clone_kohya_sd_scripts():\n",
107
+ " # Check if the directory already exists\n",
108
+ " if os.path.isdir('/notebooks/sd-scripts'):\n",
109
+ " %cd /notebooks/sd-scripts\n",
110
+ " print(\"This folder already exists, will do a !git pull instead\\n\")\n",
111
+ " !git pull\n",
112
+ " else:\n",
113
+ " !git clone https://github.com/kohya-ss/sd-scripts\n",
114
+ "\n",
115
+ "# Clone or update the Kohya Trainer repository\n",
116
+ "clone_kohya_sd_scripts()"
117
+ ]
118
+ },
119
+ {
120
+ "cell_type": "markdown",
121
+ "id": "b52dd301-0ed2-4ae4-911e-643d39c0f1bf",
122
+ "metadata": {
123
+ "tags": []
124
+ },
125
+ "source": [
126
+ "# 1-2.Install and Setting\n",
127
+ "仮想マシン起動時毎回実行する"
128
+ ]
129
+ },
130
+ {
131
+ "cell_type": "code",
132
+ "execution_count": null,
133
+ "id": "85954927-9497-4a2c-995a-dc4e6ba4b16c",
134
+ "metadata": {},
135
+ "outputs": [],
136
+ "source": [
137
+ "%store -r repo_dir activate_xformers\n",
138
+ "\n",
139
+ "appDir = f'{repo_dir}/sd-scripts'\n",
140
+ "%cd {appDir}\n",
141
+ "\n",
142
+ "!pip install --upgrade pip\n",
143
+ "!pip install --upgrade -r requirements.txt\n",
144
+ "!pip uninstall -y torch torchvision torchaudio # Remove existing pytorch install.\n",
145
+ "!pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113 # Install pytorch for cuda 11.3\n",
146
+ "\n",
147
+ "import os\n",
148
+ "if activate_xformers:\n",
149
+ " print('Installing xformers...')\n",
150
+ " import subprocess\n",
151
+ " def download_release(url):\n",
152
+ " binary = 'xformers-0.0.14.dev0-cp39-cp39-linux_x86_64.whl' # have to save the binary as a specific name that pip likes\n",
153
+ " tmp_dir = subprocess.check_output(['mktemp', '-d']).decode('ascii').strip('\\n')\n",
154
+ " !wget \"{url}\" -O \"{tmp_dir}/{binary}\"\n",
155
+ " return os.path.join(tmp_dir, binary)\n",
156
+ "\n",
157
+ " # Set up pip packages\n",
158
+ " s = subprocess.getoutput('nvidia-smi')\n",
159
+ " if 'A4000' in s:\n",
160
+ " xformers_whl = download_release('https://github.com/Cyberes/xformers-compiled/releases/download/A4000-Oct-28-2022/a4000-xformers-0.0.14.dev0-cp39-cp39-linux_x86_64.whl')\n",
161
+ " elif 'A5000' in s:\n",
162
+ " xformers_whl = download_release('https://github.com/Cyberes/xformers-compiled/releases/download/A5000-Nov-1-2022/a5000-xformers-0.0.14.dev0-cp39-cp39-linux_x86_64.whl')\n",
163
+ " elif 'A6000' in s:\n",
164
+ " xformers_whl = download_release('https://github.com/Cyberes/xformers-compiled/releases/download/A6000-Nov-1-2022/a6000-xformers-0.0.14.dev0-cp39-cp39-linux_x86_64.whl')\n",
165
+ " elif 'P5000' in s:\n",
166
+ " xformers_whl = download_release('https://github.com/Cyberes/xformers-compiled/releases/download/P5000-Nov-1-2022/p5000-xformers-0.0.14.dev0-cp39-cp39-linux_x86_64.whl')\n",
167
+ " elif 'RTX 4000' in s:\n",
168
+ " xformers_whl = download_release('https://github.com/Cyberes/xformers-compiled/releases/download/RTX-4000-Nov-1-2022/rtx4000-xformers-0.0.14.dev0-cp39-cp39-linux_x86_64.whl')\n",
169
+ " elif 'RTX 5000' in s:\n",
170
+ " xformers_whl = download_release('https://github.com/Cyberes/xformers-compiled/releases/download/RTX-5000-Nov-1-2022/rtx5000-xformers-0.0.14.dev0-cp39-cp39-linux_x86_64.whl')\n",
171
+ " elif 'A100' in s:\n",
172
+ " xformers_whl = download_release('https://github.com/Cyberes/xformers-compiled/releases/download/A100-Nov-1-2022/a100-xformers-0.0.14.dev0-cp39-cp39-linux_x86_64.whl')\n",
173
+ " elif 'M4000' in s:\n",
174
+ " print('xformers for M4000 hasn\\'t been built yet.')\n",
175
+ " # xformers_whl = download_release('https://github.com/Cyberes/xformers-compiled/releases/download/A100-Nov-1-2022/a100-xformers-0.0.14.dev0-cp39-cp39-linux_x86_64.whl')\n",
176
+ " else:\n",
177
+ " print('GPU not matched to xformers binary so a one-size-fits-all binary was installed. If you have any issues, please build xformers using the Tools block below.')\n",
178
+ " xformers_whl = download_release('https://raw.githubusercontent.com/Cyberes/xformers-compiled/main/various/xformers-0.0.14.dev0-cp37-cp37m-linux_x86_64.whl')\n",
179
+ " !pip install --force-reinstall \"{xformers_whl}\""
180
+ ]
181
+ },
182
+ {
183
+ "cell_type": "markdown",
184
+ "id": "7076d849-dd45-491d-9e6c-473ed1bdbc6e",
185
+ "metadata": {
186
+ "tags": []
187
+ },
188
+ "source": [
189
+ "# 1-3.Accelerate config作成 \n",
190
+ "導入時初回のみターミナルから実行する。 \n",
191
+ "対話型で選択肢に回答する形式なのでターミナルから実行 \n",
192
+ " cd /notebooks/sd-scripts \n",
193
+ " accelerate config \n",
194
+ "質問回答後下記メッセージが出たら完了 \n",
195
+ "accelerate configuration saved at /root/.cache/huggingface/accelerate/default_config.yaml "
196
+ ]
197
+ },
198
+ {
199
+ "cell_type": "markdown",
200
+ "id": "bf57b0ce-882c-415c-9d80-a923a7026124",
201
+ "metadata": {
202
+ "tags": []
203
+ },
204
+ "source": [
205
+ "# 1-4.accelerate configファイルをsd-scriptsディレクトリにコピーする\n",
206
+ "導入時初回のみ実行する \n",
207
+ "1.3で作ったコンフィグファイルを永続ストレージにコピーする"
208
+ ]
209
+ },
210
+ {
211
+ "cell_type": "code",
212
+ "execution_count": null,
213
+ "id": "9bbd243b-75d3-4492-9bac-196faf55ee97",
214
+ "metadata": {},
215
+ "outputs": [],
216
+ "source": [
217
+ "!cp -r /root/.cache/huggingface/accelerate/ /notebooks/sd-scripts/accelerate/"
218
+ ]
219
+ },
220
+ {
221
+ "cell_type": "markdown",
222
+ "id": "648e5671-b153-4549-96c8-88afb204b3e4",
223
+ "metadata": {},
224
+ "source": [
225
+ "## RUNNING"
226
+ ]
227
+ },
228
+ {
229
+ "cell_type": "markdown",
230
+ "id": "730c4e1b-308f-438c-95a8-e26c671055f5",
231
+ "metadata": {
232
+ "tags": []
233
+ },
234
+ "source": [
235
+ "# 2-0.Dataset Setting"
236
+ ]
237
+ },
238
+ {
239
+ "cell_type": "code",
240
+ "execution_count": null,
241
+ "id": "569d34de-15db-46f4-9af4-7acbfc98e5c8",
242
+ "metadata": {},
243
+ "outputs": [],
244
+ "source": [
245
+ "#起動時。学習素材変更時実行する\n",
246
+ "#Learning checkpointName .ckpt\n",
247
+ "model_file_name = \"wd-1-4-anime_e1.ckpt\" #@param {'type' : 'string'} \n",
248
+ "\n",
249
+ "model_storage_dir =\"/notebooks/storage/models\"\n",
250
+ "\n",
251
+ "model_file_path = f\"{model_storage_dir}/{model_file_name}\"\n",
252
+ "\n",
253
+ "# ===================================================================================================\n",
254
+ "# 正規化データ クラス名\n",
255
+ "reg_count = 1 #@param {type: \"integer\"}\n",
256
+ "reg_class =\"girl\" #@param {type: \"string\"}\n",
257
+ "\n",
258
+ "#学習元データ トークン(インスタンス)名、クラス名\n",
259
+ "train_count = 20 #@param {type: \"integer\"} 1epoch=学習素材 × カウント数のステップを回す(webui版で10の部分)\n",
260
+ "train_token = \"nahida\" #@param {type: \"string\"}\n",
261
+ "train_class = \"girl\" #@param {type: \"string\"}\n",
262
+ "\n",
263
+ "storage_train_dir = \"/notebooks/storage/atelier/dataset/1024_nahidav3\" #@param {type: \"string\"}\n",
264
+ "storage_class_dir = \"/notebooks/storage/atelier/dataset/Classification\" #@param {type: \"string\"}\n",
265
+ "\n",
266
+ "# ===================================================================================================\n",
267
+ "# Save variables to Jupiter's temp storage so we can access it even if the kernel restarts.\n",
268
+ "%store model_storage_dir model_file_path reg_count reg_class train_count train_token train_class storage_train_dir storage_class_dir"
269
+ ]
270
+ },
271
+ {
272
+ "cell_type": "markdown",
273
+ "id": "b017ace2-cd08-427a-89d5-28ad8f7dbfc5",
274
+ "metadata": {},
275
+ "source": [
276
+ "# 2-1 Dreambooth フォルダ削除 \n",
277
+ "学習結果を消すので注意 \n",
278
+ "※学習画像データは消さない "
279
+ ]
280
+ },
281
+ {
282
+ "cell_type": "code",
283
+ "execution_count": null,
284
+ "id": "b5aa4b00-b54d-4f5f-a0da-5153a86c1f87",
285
+ "metadata": {},
286
+ "outputs": [],
287
+ "source": [
288
+ "# 学習結果を消すので注意\n",
289
+ "%cd /notebooks/\n",
290
+ "\n",
291
+ "import os\n",
292
+ "\n",
293
+ "def delete_dreambooth_folder():\n",
294
+ " # Check if the directory already exists\n",
295
+ " if os.path.isdir('/notebooks/dreambooth'):\n",
296
+ " %rm -r /notebooks/dreambooth\n",
297
+ " print(\"dreambooth dataset folder deleted done!!\")\n",
298
+ " else:\n",
299
+ " print(\"dreambooth dataset folder none\")\n",
300
+ "\n",
301
+ "# Delete Dreamboothe Dataset folder\n",
302
+ "delete_dreambooth_folder()"
303
+ ]
304
+ },
305
+ {
306
+ "cell_type": "code",
307
+ "execution_count": null,
308
+ "id": "5a27b535-ccd3-40cc-86b0-cab5cd3d63b7",
309
+ "metadata": {},
310
+ "outputs": [],
311
+ "source": [
312
+ "# 起動時、学習素材変更時実行する\n",
313
+ "#@title Create train and reg folder based on description above\n",
314
+ "%store -r model_storage_dir model_file_path reg_count reg_class train_count train_token train_class storage_train_dir storage_class_dir\n",
315
+ "\n",
316
+ "# Import the os and shutil modules\n",
317
+ "import os\n",
318
+ "import shutil\n",
319
+ "\n",
320
+ "# Change the current working directory to /content\n",
321
+ "%cd /notebooks\n",
322
+ "\n",
323
+ "# Define the dreambooth_directory variable\n",
324
+ "dreambooth_directory = \"/notebooks/dreambooth\"\n",
325
+ "\n",
326
+ "# Check if the dreambooth directory already exists\n",
327
+ "if os.path.isdir(dreambooth_directory):\n",
328
+ " # If the directory exists, do nothing\n",
329
+ " pass\n",
330
+ "else:\n",
331
+ " # If the directory does not exist, create it\n",
332
+ " os.mkdir(dreambooth_directory)\n",
333
+ "\n",
334
+ "#@markdown ### Define the reg_folder variable\n",
335
+ "#reg_count = 1 #@param {type: \"integer\"}\n",
336
+ "#reg_class =\"kasakai_hikaru\" #@param {type: \"string\"}\n",
337
+ "reg_folder = str(reg_count) + \"_\" + reg_class\n",
338
+ "\n",
339
+ "# Define the reg_directory variable\n",
340
+ "reg_directory = f\"{dreambooth_directory}/reg_{reg_class}\"\n",
341
+ "\n",
342
+ "# Check if the reg directory already exists\n",
343
+ "if os.path.isdir(reg_directory):\n",
344
+ " # If the directory exists, do nothing\n",
345
+ " pass\n",
346
+ "else:\n",
347
+ " # If the directory does not exist, create it\n",
348
+ " os.mkdir(reg_directory)\n",
349
+ "\n",
350
+ "# Define the reg_folder_directory variable\n",
351
+ "reg_folder_directory = f\"{reg_directory}/{reg_folder}\"\n",
352
+ "\n",
353
+ "# Check if the reg_folder directory already exists\n",
354
+ "if os.path.isdir(reg_folder_directory):\n",
355
+ " # If the directory exists, do nothing\n",
356
+ " pass\n",
357
+ "else:\n",
358
+ " # If the directory does not exist, create it\n",
359
+ " #os.mkdir(reg_folder_directory)\n",
360
+ " os.symlink(storage_class_dir, reg_folder_directory)\n",
361
+ "\n",
362
+ "#@markdown ### Define the train_folder variable\n",
363
+ "#train_count = 3300 #@param {type: \"integer\"}\n",
364
+ "#train_token = \"sls\" #@param {type: \"string\"}\n",
365
+ "#train_class = \"kasakai_hikaru\" #@param {type: \"string\"}\n",
366
+ "train_folder = str(train_count) + \"_\" + train_token + \"_\" + train_class\n",
367
+ "\n",
368
+ "# Define the train_directory variable\n",
369
+ "train_directory = f\"{dreambooth_directory}/train_{train_class}\"\n",
370
+ "\n",
371
+ "# Check if the train directory already exists\n",
372
+ "if os.path.isdir(train_directory):\n",
373
+ " # If the directory exists, do nothing\n",
374
+ " pass\n",
375
+ "else:\n",
376
+ " # If the directory does not exist, create it\n",
377
+ " os.mkdir(train_directory)\n",
378
+ " \n",
379
+ "# Define the train_folder_directory variable\n",
380
+ "train_folder_directory = f\"{train_directory}/{train_folder}\"\n",
381
+ "\n",
382
+ "# Check if the train_folder directory already exists\n",
383
+ "if os.path.isdir(train_folder_directory):\n",
384
+ " # If the directory exists, do nothing\n",
385
+ " pass\n",
386
+ "else:\n",
387
+ " # If the directory does not exist, create it\n",
388
+ " #os.mkdir(train_folder_directory)\n",
389
+ " os.symlink(storage_train_dir, train_folder_directory)\n",
390
+ " \n",
391
+ "%store train_directory train_folder_directory reg_directory reg_folder_directory"
392
+ ]
393
+ },
394
+ {
395
+ "cell_type": "markdown",
396
+ "id": "1bb54f2f-66a8-4a4f-80c3-a38f6eacd9fe",
397
+ "metadata": {},
398
+ "source": [
399
+ "# Lora Train Start\n",
400
+ "Dreambooth-Loraの学習を実行する \n",
401
+ "引数の詳細情報は「sd-scripts/train_network.py」のソースを参照  "
402
+ ]
403
+ },
404
+ {
405
+ "cell_type": "code",
406
+ "execution_count": null,
407
+ "id": "e0b13039-fec7-4002-998a-64429599baca",
408
+ "metadata": {},
409
+ "outputs": [],
410
+ "source": [
411
+ "#@title Training begin Lora\n",
412
+ "%store -r model_storage_dir model_file_path train_directory reg_directory \n",
413
+ "accelerate_config = \"/notebooks/sd-scripts/accelerate/default_config.yaml\"\n",
414
+ "num_cpu_threads_per_process = 8 #@param {'type':'integer'}\n",
415
+ "pre_trained_model_path =model_file_path #@param {'type':'string'}\n",
416
+ "train_data_dir = train_directory #@param {'type':'string'}\n",
417
+ "reg_data_dir = reg_directory #@param {'type':'string'}\n",
418
+ "\n",
419
+ "output_dir =\"/notebooks/dreambooth\" #@param {'type':'string'}\n",
420
+ "train_batch_size = 6 #@param {type: \"slider\", min: 1, max: 10}\n",
421
+ "resolution = \"768,768\" #@param [\"512,512\", \"768,768\"] {allow-input: false}\n",
422
+ "learning_rate =\"1e-4\" #@param {'type':'string'}\n",
423
+ "mixed_precision = \"bf16\" #@param [\"fp16\", \"bf16\"] {allow-input: false}\n",
424
+ "max_train_steps = 3200 #@param {'type':'integer'}\n",
425
+ "save_precision = \"fp16\" #@param [\"float\", \"fp16\", \"bf16\"] {allow-input: false}\n",
426
+ "save_every_n_epochs = 5 #@param {'type':'integer'}\n",
427
+ "use_network_module = \"networks.lora\" #@param {'type':'string'}\n",
428
+ "caption_extension =\".txt\" #@param {'type':'string'}\n",
429
+ "#resme_path ='/notebooks/dreambooth/last-state' #学習再開する場合フォルダを指定する\n",
430
+ "resme_path ='' #学習再開する場合フォルダを指定する\n",
431
+ "resume = f'--resume={resme_path}' if resme_path else '' #@param {'type':'string'}\n",
432
+ "max_token_length = 225 #@param {'type':'integer'}\n",
433
+ "\n",
434
+ "%cd /notebooks/sd-scripts/\n",
435
+ "!accelerate launch --config_file {accelerate_config} --num_cpu_threads_per_process {num_cpu_threads_per_process} train_network.py \\\n",
436
+ " --v2 \\\n",
437
+ " --max_token_length={max_token_length} \\\n",
438
+ " --pretrained_model_name_or_path={pre_trained_model_path} \\\n",
439
+ " --train_data_dir={train_data_dir} \\\n",
440
+ " --reg_data_dir={reg_data_dir} \\\n",
441
+ " --output_dir={output_dir} \\\n",
442
+ " --prior_loss_weight=1.0 \\\n",
443
+ " --resolution={resolution} \\\n",
444
+ " --train_batch_size={train_batch_size}\\\n",
445
+ " --learning_rate={learning_rate}\\\n",
446
+ " --max_train_steps={max_train_steps} \\\n",
447
+ " --use_8bit_adam \\\n",
448
+ " --xformers \\\n",
449
+ " --cache_latents \\\n",
450
+ " --mixed_precision={mixed_precision} \\\n",
451
+ " --gradient_checkpointing \\\n",
452
+ " --save_every_n_epochs={save_every_n_epochs} \\\n",
453
+ " --enable_bucket \\\n",
454
+ " --network_module={use_network_module} \\\n",
455
+ " --caption_extension={caption_extension} \\\n",
456
+ " --save_state {resume}"
457
+ ]
458
+ },
459
+ {
460
+ "cell_type": "markdown",
461
+ "id": "371b43fe-9293-4f1e-a026-72b3f94df6e2",
462
+ "metadata": {
463
+ "jp-MarkdownHeadingCollapsed": true,
464
+ "tags": []
465
+ },
466
+ "source": [
467
+ "# 3.Dataset Labeling (おまけ)\n",
468
+ "FineTune用 Lora学習には使わない。WD14taggerは使うかも"
469
+ ]
470
+ },
471
+ {
472
+ "cell_type": "code",
473
+ "execution_count": null,
474
+ "id": "05528d73-883e-4365-a1e7-d82cf61eee6e",
475
+ "metadata": {},
476
+ "outputs": [],
477
+ "source": [
478
+ "# 3-1.BLIPでキャプションファイル(.caption)を学習素材と同じ場所に作成する\n",
479
+ "%store -r storage_train_dir\n",
480
+ "%cd /notebooks/sd-scripts/\n",
481
+ "batch_size = 8 #@param {'type':'integer'}\n",
482
+ "\n",
483
+ "!python finetune/make_captions.py --batch_size {batch_size} {storage_train_dir}"
484
+ ]
485
+ },
486
+ {
487
+ "cell_type": "code",
488
+ "execution_count": null,
489
+ "id": "7c58ed68-fc08-4d1a-9630-d14c6b0b3db8",
490
+ "metadata": {},
491
+ "outputs": [],
492
+ "source": [
493
+ "# 3-2 WD1.4 taggerでタグテキスト(.txt)を学習素材と同じ場所に作成する\n",
494
+ "#@title Start WD 1.4 Tagger\n",
495
+ "%store -r storage_train_dir\n",
496
+ "%cd /notebooks/sd-scripts/\n",
497
+ "\n",
498
+ "batch_size = 8 #@param {'type':'integer'}\n",
499
+ "caption_extension = \".txt\" #@param [\".txt\",\".caption\"]\n",
500
+ "\n",
501
+ "!python finetune/tag_images_by_wd14_tagger.py \\\n",
502
+ " {storage_train_dir} \\\n",
503
+ " --batch_size {batch_size} \\\n",
504
+ " --caption_extension {caption_extension}"
505
+ ]
506
+ },
507
+ {
508
+ "cell_type": "code",
509
+ "execution_count": null,
510
+ "id": "b4aef070-b6f2-4346-a553-888bb4404e83",
511
+ "metadata": {},
512
+ "outputs": [],
513
+ "source": [
514
+ "# 3-3 キャプションとタグを結合して1つのファイルにまとめる(meta_clean.json作成)\n",
515
+ "#@title Create meta_clean.json \n",
516
+ "# Change the working directory\n",
517
+ "%store -r storage_train_dir\n",
518
+ "%cd /notebooks/sd-scripts/\n",
519
+ "\n",
520
+ "#@markdown ### Define Parameters\n",
521
+ "meta_cap_dd = \"/notebooks/dreambooth/meta_cap_dd.json\" \n",
522
+ "meta_cap = \"/notebooks/dreambooth/meta_cap.json\" \n",
523
+ "meta_clean = \"/notebooks/dreambooth/meta_clean.json\" #@param {'type':'string'}\n",
524
+ "\n",
525
+ "# Check if the train_data_dir exists and is a directory\n",
526
+ "if os.path.isdir(storage_train_dir):\n",
527
+ " # Check if there are any .caption files in the train_data_dir\n",
528
+ " if any(file.endswith('.caption') for file in os.listdir(storage_train_dir)):\n",
529
+ " # Create meta_cap.json from captions\n",
530
+ " !python finetune/merge_captions_to_metadata.py \\\n",
531
+ " {storage_train_dir} \\\n",
532
+ " {meta_cap}\n",
533
+ "\n",
534
+ " # Check if there are any .txtn files in the train_data_dir\n",
535
+ " if any(file.endswith('.txt') for file in os.listdir(storage_train_dir)):\n",
536
+ " # Create meta_cap_dd.json from tags\n",
537
+ " !python finetune/merge_dd_tags_to_metadata.py \\\n",
538
+ " {storage_train_dir} \\\n",
539
+ " {meta_cap_dd}\n",
540
+ "else:\n",
541
+ " print(\"train_data_dir does not exist or is not a directory.\")\n",
542
+ "\n",
543
+ "# Merge meta_cap.json to meta_cap_dd.json\n",
544
+ "if os.path.exists(meta_cap) and os.path.exists(meta_cap_dd):\n",
545
+ " !python finetune/merge_dd_tags_to_metadata.py \\\n",
546
+ " {storage_train_dir} \\\n",
547
+ " --in_json {meta_cap} \\\n",
548
+ " {meta_cap_dd}\n",
549
+ "\n",
550
+ "# Clean meta_cap_dd.json and store it to meta_clean.json\n",
551
+ "if os.path.exists(meta_cap_dd):\n",
552
+ " # Clean captions and tags in meta_cap_dd.json and store the result in meta_clean.json\n",
553
+ " !python finetune/clean_captions_and_tags.py \\\n",
554
+ " {meta_cap_dd} \\\n",
555
+ " {meta_clean}\n",
556
+ "elif os.path.exists(meta_cap):\n",
557
+ " # If meta_cap_dd.json does not exist, clean meta_cap.json and store the result in meta_clean.json\n",
558
+ " !python finetune/clean_captions_and_tags.py \\\n",
559
+ " {meta_cap} \\\n",
560
+ " {meta_clean}\n"
561
+ ]
562
+ },
563
+ {
564
+ "cell_type": "code",
565
+ "execution_count": null,
566
+ "id": "067d56ab-cc17-4acf-af5c-3913c56c722a",
567
+ "metadata": {},
568
+ "outputs": [],
569
+ "source": [
570
+ "# 3-4 latentsの事前取得\n",
571
+ "#@title Aspect Ratio Bucketing\n",
572
+ "%store -r storage_train_dir model_file_path\n",
573
+ "\n",
574
+ "# Change working directory\n",
575
+ "%cd /notebooks/sd-scripts/\n",
576
+ "\n",
577
+ "#@markdown ### Define parameters\n",
578
+ "\n",
579
+ "#model_dir = \"runwayml/stable-diffusion-v1-5\" #@param {'type' : 'string'} \n",
580
+ "model_dir = model_file_path #@param {'type' : 'string'} \n",
581
+ "batch_size = 4 #@param {'type':'integer'}\n",
582
+ "max_resolution = \"768,768\" #@param [\"512,512\", \"768,768\"] {allow-input: false}\n",
583
+ "mixed_precision = \"bf16\" #@param [\"no\", \"fp16\", \"bf16\"] {allow-input: false}\n",
584
+ "meta_clean = \"/notebooks/dreambooth/meta_clean.json\"\n",
585
+ "meta_lat = \"/notebooks/dreambooth/meta_lat.json\"\n",
586
+ "\n",
587
+ "\n",
588
+ "# Run script to prepare buckets and latents\n",
589
+ "!python finetune/prepare_buckets_latents.py \\\n",
590
+ " {storage_train_dir} \\\n",
591
+ " {meta_clean} \\\n",
592
+ " {meta_lat} \\\n",
593
+ " {model_dir} \\\n",
594
+ " --batch_size {batch_size} \\\n",
595
+ " --max_resolution {max_resolution} \\\n",
596
+ " --mixed_precision {mixed_precision}\n"
597
+ ]
598
+ }
599
+ ],
600
+ "metadata": {
601
+ "kernelspec": {
602
+ "display_name": "Python 3 (ipykernel)",
603
+ "language": "python",
604
+ "name": "python3"
605
+ },
606
+ "language_info": {
607
+ "codemirror_mode": {
608
+ "name": "ipython",
609
+ "version": 3
610
+ },
611
+ "file_extension": ".py",
612
+ "mimetype": "text/x-python",
613
+ "name": "python",
614
+ "nbconvert_exporter": "python",
615
+ "pygments_lexer": "ipython3",
616
+ "version": "3.9.13"
617
+ }
618
+ },
619
+ "nbformat": 4,
620
+ "nbformat_minor": 5
621
+ }