File size: 7,510 Bytes
e1a8883
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Dependencies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "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",
    "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",
   "metadata": {},
   "source": [
    "# Download the model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Run the cell to download the model\n",
    "\n",
    "#-------------\n",
    "MODEL_NAMExl=dls_xlf(\"\", \"\", \"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Create/Load a Session"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Session_Name = \"Example-Session\"\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",
   "metadata": {},
   "source": [
    "# Instance Images\n",
    "The most important step is to rename the instance pictures to one unique unknown identifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "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",
    "Crop_size = 1024\n",
    "\n",
    "# 1024 is the native resolution\n",
    "\n",
    "\n",
    "#--------------------------------------------\n",
    "\n",
    "# Disabled when \"Smart_crop_images\" is set to \"True\"\n",
    "\n",
    "Resize_to_1024_and_keep_aspect_ratio = False\n",
    "\n",
    "# Will resize the smallest dimension to 1024 without cropping while keeping the aspect ratio (make sure you have enough VRAM)\n",
    "\n",
    "\n",
    "# Check out this example for naming : https://i.imgur.com/d2lD3rz.jpeg\n",
    "\n",
    "#-----------------\n",
    "uplder(Remove_existing_instance_images, Smart_crop_images, Crop_size, Resize_to_1024_and_keep_aspect_ratio, IMAGES_FOLDER_OPTIONAL, INSTANCE_DIR, CAPTIONS_DIR)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Manual Captioning"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Open a tool to manually caption the instance images.\n",
    "\n",
    "#-----------------\n",
    "caption(CAPTIONS_DIR, INSTANCE_DIR)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Train LoRA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Training Settings\n",
    "\n",
    "# Epoch = Number of steps/images\n",
    "\n",
    "\n",
    "UNet_Training_Epochs= 120\n",
    "\n",
    "UNet_Learning_Rate= \"1e-6\"\n",
    "\n",
    "# Keep the learning rate between 1e-6 and 3e-6\n",
    "\n",
    "\n",
    "Text_Encoder_Training_Epochs= 40\n",
    "\n",
    "# The training is highly affected by this value, a total of 300 steps (not epochs) is enough, set to 0 if enhancing existing concepts\n",
    "\n",
    "Text_Encoder_Learning_Rate= \"1e-6\"\n",
    "\n",
    "# Keep the learning rate at 1e-6 or lower\n",
    "\n",
    "\n",
    "External_Captions= False\n",
    "\n",
    "# Load the captions from a text file for each instance image\n",
    "\n",
    "\n",
    "LoRA_Dim = 64\n",
    "\n",
    "# Dimension of the LoRa model, between 64 and 128 is good enough\n",
    "\n",
    "\n",
    "Save_VRAM = False\n",
    "\n",
    "# Use as low as 10GB VRAM with Dim = 64\n",
    "\n",
    "\n",
    "Intermediary_Save_Epoch = \"[30,60]\"\n",
    "\n",
    "# [30,60] means it will save intermediary models at epoch 30 and epoch 60, you can add as many as you want like [30,60,80,100]\n",
    "\n",
    "\n",
    "#-----------------\n",
    "dbtrainxl(UNet_Training_Epochs, Text_Encoder_Training_Epochs, UNet_Learning_Rate, Text_Encoder_Learning_Rate, LoRA_Dim, False, 1024, MODEL_NAMExl, SESSION_DIR, INSTANCE_DIR, CAPTIONS_DIR, External_Captions, INSTANCE_NAME, Session_Name, OUTPUT_DIR, 0, Save_VRAM, Intermediary_Save_Epoch)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test the Trained Model"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ComfyUI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Args=\"--listen --port 3000 --preview-method auto\"\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, restored)\n",
    "!python /workspace/ComfyUI/main.py $Args"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# A1111"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "User = \"\"\n",
    "\n",
    "Password= \"\"\n",
    "\n",
    "# Add credentials to your Gradio interface (optional).\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, restoreda)\n",
    "!python /workspace/sd/stable-diffusion-webui/webui.py $configf"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Free up space"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}