File size: 12,485 Bytes
19314ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ccaee2
19314ac
 
 
2ccaee2
19314ac
 
 
 
25e906b
 
2ccaee2
25e906b
 
19314ac
2ccaee2
19314ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8434af7
19314ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
866e444
19314ac
866e444
19314ac
 
 
 
866e444
19314ac
 
 
 
 
 
866e444
19314ac
 
 
 
 
 
 
 
 
866e444
19314ac
866e444
19314ac
 
 
 
 
 
 
 
 
866e444
19314ac
866e444
19314ac
 
 
 
866e444
19314ac
 
 
 
 
866e444
19314ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "494d5ce4-5843-4d70-ae96-c1983e21b6e8",
   "metadata": {},
   "source": [
    "## Dreambooth v2 Paperspace Notebook From https://github.com/TheLastBen/fast-stable-diffusion, if you encounter any issues, feel free to discuss them. [Support](https://ko-fi.com/thelastben)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8afdca63-eff3-4a9d-b4d9-127c0f028033",
   "metadata": {
    "tags": []
   },
   "source": [
    "# Dependencies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be74b2d5-da96-4bf4-ae82-4fe4b8abc04c",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Install the dependencies\n",
    "\n",
    "force_reinstall= False\n",
    "\n",
    "# Set to true only if you want to install the dependencies again.\n",
    "\n",
    "\n",
    "#--------------------\n",
    "with open('/dev/null', 'w') as devnull:import requests, os, time, importlib;open('/notebooks/mainpaperspacev2.py', 'wb').write(requests.get('https://huggingface.co/datasets/TheLastBen/PPS/raw/main/Scripts/mainpaperspacev2.py').content);os.chdir('/notebooks');time.sleep(3);import mainpaperspacev2;importlib.reload(mainpaperspacev2);from mainpaperspacev2 import *;Deps(force_reinstall)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a4ef4a2-6863-4603-9254-a1e2a547ee38",
   "metadata": {
    "tags": []
   },
   "source": [
    "# Download the model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a1ba734e-515b-4761-8c88-ef7f165d7971",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "Model_Version = \"768\"\n",
    "\n",
    "# Choices are :  \"512\", \"768\"\n",
    "\n",
    "#-----------------------------------------------------------------------------------------------------------------------------------\n",
    "\n",
    "Custom_Model_Version = \"768\"\n",
    "\n",
    "# Choices are :  \"512\", \"768\"\n",
    "\n",
    "Path_to_HuggingFace= \"\"\n",
    "\n",
    "# Load and finetune a model from Hugging Face, use the format \"profile/model\" like : runwayml/stable-diffusion-v1-5.\n",
    "\n",
    "Model_Path = \"\"\n",
    "\n",
    "# Load a CKPT model from the storage.\n",
    "\n",
    "Model_Link = \"\"\n",
    "\n",
    "# A CKPT direct link, huggingface CKPT link or a shared CKPT from gdrive.\n",
    "\n",
    "\n",
    "safetensors= False\n",
    "\n",
    "# Set to True if the model from the link/path is in safetensors format.\n",
    "\n",
    "\n",
    "#-------------\n",
    "MODEL_NAMEv2=dlv2(Path_to_HuggingFace, Model_Path, Model_Link, Model_Version, Custom_Model_Version, safetensors)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c6c4932-e614-4f5e-8d4a-4feca5ce54f5",
   "metadata": {},
   "source": [
    "# Create/Load a Session"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b6595c37-8ad2-45ff-a055-fe58c6663d2f",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "Session_Name = \"sd-session\"\n",
    "\n",
    "# Enter the session name, it if it exists, it will load it, otherwise it'll create an new session.\n",
    "\n",
    "Session_Link_optional = \"\"\n",
    "\n",
    "# Import a session from another gdrive, the shared gdrive link must point to the specific session's folder that contains the trained CKPT, remove any intermediary CKPT if any.\n",
    "\n",
    "Model_Version = \"768\"\n",
    "\n",
    "# Ignore this if you're not loading a previous session that contains a trained model, choices are :  \"512\", \"768\"\n",
    "\n",
    "\n",
    "#-----------------\n",
    "[PT, WORKSPACE, Session_Name, INSTANCE_NAME, OUTPUT_DIR, SESSION_DIR, CONCEPT_DIR, INSTANCE_DIR, CAPTIONS_DIR, MDLPTH, MODEL_NAMEv2, resumev2]=sessv2(Session_Name, Session_Link_optional, Model_Version, MODEL_NAMEv2 if 'MODEL_NAMEv2' in locals() else \"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5698de61-08d3-4d90-83ef-f882ed956d01",
   "metadata": {},
   "source": [
    "# Instance Images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc2f8f28-226e-45b8-8257-804bbb711f56",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "Remove_existing_instance_images= True\n",
    "\n",
    "# Set to False to keep the existing instance images if any.\n",
    "\n",
    "\n",
    "IMAGES_FOLDER_OPTIONAL=\"\"\n",
    "\n",
    "# If you prefer to specify directly the folder of the pictures instead of uploading, this will add the pictures to the existing (if any) instance images. Leave EMPTY to upload.\n",
    "\n",
    "\n",
    "Smart_crop_images= True\n",
    "\n",
    "# Automatically crop your input images.\n",
    "\n",
    "\n",
    "Crop_size = 768\n",
    "\n",
    "# Choices: \"512\", \"576\", \"640\", \"704\", \"768\", \"832\", \"896\", \"960\", \"1024\"\n",
    "\n",
    "# Check out this example for naming : https://i.imgur.com/d2lD3rz.jpeg\n",
    "\n",
    "\n",
    "#-----------------\n",
    "uplder(Remove_existing_instance_images, Smart_crop_images, Crop_size, IMAGES_FOLDER_OPTIONAL, INSTANCE_DIR, CAPTIONS_DIR, False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e93924f-a6bf-45d5-aa77-915ad7385dcd",
   "metadata": {},
   "source": [
    "# Manual Captioning"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c5dbcb29-b42f-4cfc-9be8-83355838d5a2",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Open a tool to manually caption the instance images.\n",
    "\n",
    "#-----------------\n",
    "caption(CAPTIONS_DIR, INSTANCE_DIR)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c90140c1-6c91-4cae-a222-e1a746957f95",
   "metadata": {},
   "source": [
    "# Concept Images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "55c27688-8601-4943-b61d-fc48b9ded067",
   "metadata": {},
   "outputs": [],
   "source": [
    "Remove_existing_concept_images= True\n",
    "\n",
    "# Set to False to keep the existing concept images if any.\n",
    "\n",
    "\n",
    "IMAGES_FOLDER_OPTIONAL=\"\"\n",
    "\n",
    "# If you prefer to specify directly the folder of the pictures instead of uploading, this will add the pictures to the existing (if any) concept images. Leave EMPTY to upload.\n",
    "\n",
    "\n",
    "#-----------------\n",
    "uplder(Remove_existing_concept_images, True, 512, IMAGES_FOLDER_OPTIONAL, CONCEPT_DIR, CAPTIONS_DIR, True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a4aa42a-fd68-41ad-9ba7-da99f834e2c1",
   "metadata": {},
   "source": [
    "# Dreambooth"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "612d8335-b984-4f34-911d-5457ff98e507",
   "metadata": {},
   "outputs": [],
   "source": [
    "Resume_Training = False\n",
    "\n",
    "# If you're not satisfied with the result, Set to True, run again the cell and it will continue training the current model.\n",
    "\n",
    "\n",
    "UNet_Training_Steps= 1500\n",
    "\n",
    "UNet_Learning_Rate= \"2e-6\"\n",
    "\n",
    "# If you use 10 images, use 650 steps, if you're not satisfied with the result, resume training for another 200 steps with a lower learning rate (8e-6), and so on ...\n",
    "\n",
    "\n",
    "Text_Encoder_Training_Steps= 350\n",
    "\n",
    "Text_Encoder_Learning_Rate= \"1e-6\"\n",
    "\n",
    "# 350-600 steps is enough for a small dataset, keep this number small to avoid overfitting, set to 0 to disable, set it to 0 before resuming training if it is already trained.\n",
    "\n",
    "\n",
    "Text_Encoder_Concept_Training_Steps= 0\n",
    "\n",
    "# Suitable for training a style/concept as it acts as regularization, with a minimum of 300 steps, 1 step/image is enough to train the concept(s), set to 0 to disable, set both the settings above to 0 to fintune only the text_encoder on the concept, set it to 0 before resuming training if it is already trained.\n",
    "\n",
    "\n",
    "External_Captions= False\n",
    "\n",
    "# Get the captions from a text file for each instance image.\n",
    "\n",
    "\n",
    "Offset_Noise= False\n",
    "\n",
    "# Always use it for style training.\n",
    "\n",
    "\n",
    "Resolution = 768\n",
    "\n",
    "# Choices : \"512\", \"576\", \"640\", \"704\", \"768\", \"832\", \"896\", \"960\", \"1024\"\n",
    "# Higher resolution = Higher quality, make sure the instance images are cropped to this selected size (or larger).\n",
    "\n",
    "#---------------------------------------------------------------\n",
    "\n",
    "Save_Checkpoint_Every_n_Steps= False\n",
    "\n",
    "Save_Checkpoint_Every= 500\n",
    "\n",
    "# Minimum 200 steps between each save.\n",
    "\n",
    "\n",
    "Start_saving_from_the_step= 500\n",
    "\n",
    "# Start saving intermediary checkpoints from this step.\n",
    "\n",
    "\n",
    "#-----------------\n",
    "resumev2=dbtrainv2(Resume_Training, UNet_Training_Steps, UNet_Learning_Rate, Text_Encoder_Training_Steps, Text_Encoder_Concept_Training_Steps, Text_Encoder_Learning_Rate, Offset_Noise, Resolution, MODEL_NAMEv2, SESSION_DIR, INSTANCE_DIR, CONCEPT_DIR, CAPTIONS_DIR, External_Captions, INSTANCE_NAME, Session_Name, OUTPUT_DIR, PT, resumev2, Save_Checkpoint_Every_n_Steps, Start_saving_from_the_step, Save_Checkpoint_Every)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf6f2232-60b3-41c5-bea6-b0dcc4aef937",
   "metadata": {},
   "source": [
    "# Test the Trained Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1263a084-b142-4e63-a0aa-2706673a4355",
   "metadata": {},
   "outputs": [],
   "source": [
    "Previous_Session_Name=\"\"\n",
    "\n",
    "# Leave empty if you want to use the current trained model.\n",
    "\n",
    "\n",
    "Custom_Path = \"\"\n",
    "\n",
    "# Input the full path to a desired model.\n",
    "\n",
    "\n",
    "User = \"\" \n",
    "\n",
    "Password= \"\"\n",
    "\n",
    "# Add credentials to your Gradio interface (optional).\n",
    "\n",
    "\n",
    "Use_localtunnel = False\n",
    "\n",
    "# If you have trouble using Gradio server, use this one.\n",
    "\n",
    "\n",
    "#-----------------\n",
    "configf=test(Custom_Path, Previous_Session_Name, Session_Name, User, Password, Use_localtunnel) if 'Session_Name' in locals() else test(Custom_Path, Previous_Session_Name, \"\", User, Password, Use_localtunnel)\n",
    "!python /notebooks/sd/stable-diffusion-webui/webui.py $configf"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53ccbcaf-3319-44f5-967b-ecbdfa9d0e78",
   "metadata": {},
   "source": [
    "# Upload The Trained Model to Hugging Face"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2c9cb205-d828-4e51-9943-f337bd410ea8",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Save it to your personal profile or collaborate to the public [library of concepts](https://huggingface.co/sd-dreambooth-library)\n",
    "\n",
    "Name_of_your_concept = \"\"\n",
    "\n",
    "# Leave empty if you want to name your concept the same as the current session.\n",
    "\n",
    "\n",
    "hf_token_write = \"\"\n",
    "\n",
    "# Create a write access token here : https://huggingface.co/settings/tokens, go to \"New token\" -> Role : Write, a regular read token won't work here.\n",
    "\n",
    "\n",
    "#---------------------------------\n",
    "hfv2(Name_of_your_concept, Save_concept_to, hf_token_write, INSTANCE_NAME, OUTPUT_DIR, Session_Name, MDLPTH)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "881d80a3-4ebf-41bc-b68f-ac1cacb080f3",
   "metadata": {},
   "source": [
    "# Free up space"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7403744d-cc45-419f-88ac-5475fa0f7f45",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Display a list of sessions from which you can remove any session you don't need anymore\n",
    "\n",
    "#-------------------------\n",
    "clean()"
   ]
  }
 ],
 "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.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}