File size: 8,625 Bytes
a0da56a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b01332d1-1384-4405-8af6-335c768da6e2",
   "metadata": {},
   "source": [
    "## SDXL LoRA Trainer by TheLastBen https://github.com/TheLastBen/fast-stable-diffusion, if you encounter any issues, feel free to discuss them."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f82bb3b-76de-4e2c-9251-df918f8f2cbe",
   "metadata": {},
   "source": [
    "# Dependencies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3d144e06-1f7a-467b-9cf1-452bf773f0ab",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d1e84d74d92c46f8aa78c03f50a0d0d8",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Button(button_style='success', description='Done!', disabled=True, icon='check', style=ButtonStyle(), tooltip=…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "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",
    "with open('/dev/null', 'w') as devnull:import requests, os, time, importlib;open('/workspace/sdxllorarunpod.py', 'wb').write(requests.get('https://huggingface.co/datasets/TheLastBen/RNPD/raw/main/Scripts/sdxllorarunpod.py').content);os.chdir('/workspace');import sdxllorarunpod;importlib.reload(sdxllorarunpod);from sdxllorarunpod import *;restored=False;restoreda=False;Deps(force_reinstall)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "461b7686-e4aa-4fa8-ab6f-5a6acbf4c601",
   "metadata": {},
   "source": [
    "# Download the model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2f705bd1-35c9-49bd-84fd-03a1348cbe83",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1;32mUsing SDXL model\n"
     ]
    }
   ],
   "source": [
    "# Run the cell to download the model\n",
    "\n",
    "#-------------\n",
    "MODEL_NAMExl=dls_xlf(\"\", \"\", \"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e22327b-e0c3-424c-82e1-fb7f8a815c0b",
   "metadata": {},
   "source": [
    "# Create/Load a Session"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ac69c221-205a-40d2-b42e-6c8d515a43cc",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1;32mCreating session...\n",
      "\u001b[1;32mSession created, proceed to uploading instance images\n"
     ]
    }
   ],
   "source": [
    "Session_Name = \"aether_skin_230808_SDXL_LoRA_128_dim_50_epochs\"\n",
    "\n",
    "# Enter the session name, it if it exists, it will load it, otherwise it'll create an new session.\n",
    "\n",
    "#-----------------\n",
    "[WORKSPACE, Session_Name, INSTANCE_NAME, OUTPUT_DIR, SESSION_DIR, INSTANCE_DIR, CAPTIONS_DIR, MDLPTH, MODEL_NAMExl]=sess_xl(Session_Name, MODEL_NAMExl if 'MODEL_NAMExl' in locals() else \"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d239e77-f7fd-404b-8006-081f15326412",
   "metadata": {},
   "source": [
    "# Train LoRA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c54a7335-8402-42f2-9a71-9da99f6ea604",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[34m'########:'########:::::'###::::'####:'##::: ##:'####:'##::: ##::'######:::\n",
      "... ##..:: ##.... ##:::'## ##:::. ##:: ###:: ##:. ##:: ###:: ##:'##... ##::\n",
      "::: ##:::: ##:::: ##::'##:. ##::: ##:: ####: ##:: ##:: ####: ##: ##:::..:::\n",
      "::: ##:::: ########::'##:::. ##:: ##:: ## ## ##:: ##:: ## ## ##: ##::'####:\n",
      "::: ##:::: ##.. ##::: #########:: ##:: ##. ####:: ##:: ##. ####: ##::: ##::\n",
      "::: ##:::: ##::. ##:: ##.... ##:: ##:: ##:. ###:: ##:: ##:. ###: ##::: ##::\n",
      "::: ##:::: ##:::. ##: ##:::: ##:'####: ##::. ##:'####: ##::. ##:. ######:::\n",
      ":::..:::::..:::::..::..:::::..::....::..::::..::....::..::::..:::......::::\n",
      "\u001b[0m\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Progress:  71%|███████   | 676/950 [06:22<02:23,  1.91it/s, loss=0.245, lr=5.75e-7]  "
     ]
    }
   ],
   "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",
    "Training_Epochs= 50\n",
    "\n",
    "# Epoch = Number of steps/images.\n",
    "\n",
    "Learning_Rate= \"3e-6\"\n",
    "\n",
    "# keep it between 1e-6 and 6e-6\n",
    "\n",
    "\n",
    "External_Captions= True\n",
    "\n",
    "# Load the captions from a text file for each instance image.\n",
    "\n",
    "\n",
    "LoRA_Dim = 128\n",
    "\n",
    "# Dimension of the LoRa model, between 64 and 128 is good enough.\n",
    "\n",
    "\n",
    "Resolution= 1024\n",
    "\n",
    "# 1024 is the native resolution.\n",
    "\n",
    "\n",
    "Save_VRAM = False\n",
    "\n",
    "# Use as low as 9.7GB VRAM with Dim = 64, but slightly slower training.\n",
    "\n",
    "#-----------------\n",
    "dbtrainxl(Resume_Training, Training_Epochs, Learning_Rate, LoRA_Dim, False, Resolution, MODEL_NAMExl, SESSION_DIR, INSTANCE_DIR, CAPTIONS_DIR, External_Captions, INSTANCE_NAME, Session_Name, OUTPUT_DIR, 0.03, Save_VRAM)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2751798-508e-47ad-8e54-95188bdab051",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true,
    "tags": []
   },
   "source": [
    "# Test the Trained Model"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1bc48d6-1526-44c6-ab7c-cc1538c7f61c",
   "metadata": {},
   "source": [
    "# ComfyUI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "26272665-16de-4042-a7a4-6b9205ff3309",
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "Args=\"--listen --port 3000\"\n",
    "\n",
    "\n",
    "Download_SDXL_Model= True\n",
    "\n",
    "\n",
    "Huggingface_token_optional= \"\"\n",
    "\n",
    "# Restore your backed-up Comfy folder by entering your huggingface token, leave it empty to start fresh or continue with the existing sd folder (if any).\n",
    "\n",
    "#--------------------\n",
    "restored=sdcmff(Huggingface_token_optional, MDLPTH, Download_SDXL_Model, restored)\n",
    "!python /workspace/ComfyUI/main.py $Args"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "410520ca-7352-4fc4-907b-cb53f661074e",
   "metadata": {},
   "source": [
    "# A1111"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "351f18d5-f723-4d25-b1ae-1296a22c6d8c",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "User = \"\"\n",
    "\n",
    "Password= \"\"\n",
    "\n",
    "# Add credentials to your Gradio interface (optional).\n",
    "\n",
    "Download_SDXL_Model= True\n",
    "\n",
    "\n",
    "Huggingface_token_optional= \"\"\n",
    "\n",
    "# Restore your backed-up SD folder by entering your huggingface token, leave it empty to start fresh or continue with the existing sd folder (if any).\n",
    "\n",
    "#-----------------\n",
    "configf, restoreda=test(MDLPTH, User, Password, Huggingface_token_optional, Download_SDXL_Model, restoreda)\n",
    "!python /workspace/sd/stable-diffusion-webui/webui.py $configf"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "093d64a7-3d4e-4197-8075-4ed11c7f0ae8",
   "metadata": {},
   "source": [
    "# Free up space"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "370ba58a-d58d-4a80-9575-8c6e094e2626",
   "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.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}