comdoleger commited on
Commit
4e36f4e
·
verified ·
1 Parent(s): 983db42

Upload notebooks/FLUX_1_dev_LoRA_Training.ipynb with huggingface_hub

Browse files
notebooks/FLUX_1_dev_LoRA_Training.ipynb ADDED
@@ -0,0 +1,291 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {
6
+ "collapsed": false,
7
+ "id": "zl-S0m3pkQC5"
8
+ },
9
+ "source": [
10
+ "# AI Toolkit by Ostris\n",
11
+ "## FLUX.1-dev Training\n"
12
+ ]
13
+ },
14
+ {
15
+ "cell_type": "code",
16
+ "execution_count": null,
17
+ "metadata": {},
18
+ "outputs": [],
19
+ "source": [
20
+ "!nvidia-smi"
21
+ ]
22
+ },
23
+ {
24
+ "cell_type": "code",
25
+ "execution_count": null,
26
+ "metadata": {
27
+ "id": "BvAG0GKAh59G"
28
+ },
29
+ "outputs": [],
30
+ "source": [
31
+ "!git clone https://github.com/ostris/ai-toolkit\n",
32
+ "!mkdir -p /content/dataset"
33
+ ]
34
+ },
35
+ {
36
+ "cell_type": "markdown",
37
+ "metadata": {
38
+ "id": "UFUW4ZMmnp1V"
39
+ },
40
+ "source": [
41
+ "Put your image dataset in the `/content/dataset` folder"
42
+ ]
43
+ },
44
+ {
45
+ "cell_type": "code",
46
+ "execution_count": null,
47
+ "metadata": {
48
+ "id": "XGZqVER_aQJW"
49
+ },
50
+ "outputs": [],
51
+ "source": [
52
+ "!cd ai-toolkit && git submodule update --init --recursive && pip install -r requirements.txt\n"
53
+ ]
54
+ },
55
+ {
56
+ "cell_type": "markdown",
57
+ "metadata": {
58
+ "id": "OV0HnOI6o8V6"
59
+ },
60
+ "source": [
61
+ "## Model License\n",
62
+ "Training currently only works with FLUX.1-dev. Which means anything you train will inherit the non-commercial license. It is also a gated model, so you need to accept the license on HF before using it. Otherwise, this will fail. Here are the required steps to setup a license.\n",
63
+ "\n",
64
+ "Sign into HF and accept the model access here [black-forest-labs/FLUX.1-dev](https://huggingface.co/black-forest-labs/FLUX.1-dev)\n",
65
+ "\n",
66
+ "[Get a READ key from huggingface](https://huggingface.co/settings/tokens/new?) and place it in the next cell after running it."
67
+ ]
68
+ },
69
+ {
70
+ "cell_type": "code",
71
+ "execution_count": null,
72
+ "metadata": {
73
+ "id": "3yZZdhFRoj2m"
74
+ },
75
+ "outputs": [],
76
+ "source": [
77
+ "import getpass\n",
78
+ "import os\n",
79
+ "\n",
80
+ "# Prompt for the token\n",
81
+ "hf_token = getpass.getpass('Enter your HF access token and press enter: ')\n",
82
+ "\n",
83
+ "# Set the environment variable\n",
84
+ "os.environ['HF_TOKEN'] = hf_token\n",
85
+ "\n",
86
+ "print(\"HF_TOKEN environment variable has been set.\")"
87
+ ]
88
+ },
89
+ {
90
+ "cell_type": "code",
91
+ "execution_count": null,
92
+ "metadata": {
93
+ "id": "9gO2EzQ1kQC8"
94
+ },
95
+ "outputs": [],
96
+ "source": [
97
+ "import os\n",
98
+ "import sys\n",
99
+ "sys.path.append('/content/ai-toolkit')\n",
100
+ "from toolkit.job import run_job\n",
101
+ "from collections import OrderedDict\n",
102
+ "from PIL import Image\n",
103
+ "import os\n",
104
+ "os.environ[\"HF_HUB_ENABLE_HF_TRANSFER\"] = \"1\""
105
+ ]
106
+ },
107
+ {
108
+ "cell_type": "markdown",
109
+ "metadata": {
110
+ "id": "N8UUFzVRigbC"
111
+ },
112
+ "source": [
113
+ "## Setup\n",
114
+ "\n",
115
+ "This is your config. It is documented pretty well. Normally you would do this as a yaml file, but for colab, this will work. This will run as is without modification, but feel free to edit as you want."
116
+ ]
117
+ },
118
+ {
119
+ "cell_type": "code",
120
+ "execution_count": null,
121
+ "metadata": {
122
+ "id": "_t28QURYjRQO"
123
+ },
124
+ "outputs": [],
125
+ "source": [
126
+ "from collections import OrderedDict\n",
127
+ "\n",
128
+ "job_to_run = OrderedDict([\n",
129
+ " ('job', 'extension'),\n",
130
+ " ('config', OrderedDict([\n",
131
+ " # this name will be the folder and filename name\n",
132
+ " ('name', 'my_first_flux_lora_v1'),\n",
133
+ " ('process', [\n",
134
+ " OrderedDict([\n",
135
+ " ('type', 'sd_trainer'),\n",
136
+ " # root folder to save training sessions/samples/weights\n",
137
+ " ('training_folder', '/content/output'),\n",
138
+ " # uncomment to see performance stats in the terminal every N steps\n",
139
+ " #('performance_log_every', 1000),\n",
140
+ " ('device', 'cuda:0'),\n",
141
+ " # if a trigger word is specified, it will be added to captions of training data if it does not already exist\n",
142
+ " # alternatively, in your captions you can add [trigger] and it will be replaced with the trigger word\n",
143
+ " # ('trigger_word', 'image'),\n",
144
+ " ('network', OrderedDict([\n",
145
+ " ('type', 'lora'),\n",
146
+ " ('linear', 16),\n",
147
+ " ('linear_alpha', 16)\n",
148
+ " ])),\n",
149
+ " ('save', OrderedDict([\n",
150
+ " ('dtype', 'float16'), # precision to save\n",
151
+ " ('save_every', 250), # save every this many steps\n",
152
+ " ('max_step_saves_to_keep', 4) # how many intermittent saves to keep\n",
153
+ " ])),\n",
154
+ " ('datasets', [\n",
155
+ " # datasets are a folder of images. captions need to be txt files with the same name as the image\n",
156
+ " # for instance image2.jpg and image2.txt. Only jpg, jpeg, and png are supported currently\n",
157
+ " # images will automatically be resized and bucketed into the resolution specified\n",
158
+ " OrderedDict([\n",
159
+ " ('folder_path', '/content/dataset'),\n",
160
+ " ('caption_ext', 'txt'),\n",
161
+ " ('caption_dropout_rate', 0.05), # will drop out the caption 5% of time\n",
162
+ " ('shuffle_tokens', False), # shuffle caption order, split by commas\n",
163
+ " ('cache_latents_to_disk', True), # leave this true unless you know what you're doing\n",
164
+ " ('resolution', [512, 768, 1024]) # flux enjoys multiple resolutions\n",
165
+ " ])\n",
166
+ " ]),\n",
167
+ " ('train', OrderedDict([\n",
168
+ " ('batch_size', 1),\n",
169
+ " ('steps', 2000), # total number of steps to train 500 - 4000 is a good range\n",
170
+ " ('gradient_accumulation_steps', 1),\n",
171
+ " ('train_unet', True),\n",
172
+ " ('train_text_encoder', False), # probably won't work with flux\n",
173
+ " ('content_or_style', 'balanced'), # content, style, balanced\n",
174
+ " ('gradient_checkpointing', True), # need the on unless you have a ton of vram\n",
175
+ " ('noise_scheduler', 'flowmatch'), # for training only\n",
176
+ " ('optimizer', 'adamw8bit'),\n",
177
+ " ('lr', 1e-4),\n",
178
+ "\n",
179
+ " # uncomment this to skip the pre training sample\n",
180
+ " # ('skip_first_sample', True),\n",
181
+ "\n",
182
+ " # uncomment to completely disable sampling\n",
183
+ " # ('disable_sampling', True),\n",
184
+ "\n",
185
+ " # uncomment to use new vell curved weighting. Experimental but may produce better results\n",
186
+ " # ('linear_timesteps', True),\n",
187
+ "\n",
188
+ " # ema will smooth out learning, but could slow it down. Recommended to leave on.\n",
189
+ " ('ema_config', OrderedDict([\n",
190
+ " ('use_ema', True),\n",
191
+ " ('ema_decay', 0.99)\n",
192
+ " ])),\n",
193
+ "\n",
194
+ " # will probably need this if gpu supports it for flux, other dtypes may not work correctly\n",
195
+ " ('dtype', 'bf16')\n",
196
+ " ])),\n",
197
+ " ('model', OrderedDict([\n",
198
+ " # huggingface model name or path\n",
199
+ " ('name_or_path', 'black-forest-labs/FLUX.1-dev'),\n",
200
+ " ('is_flux', True),\n",
201
+ " ('quantize', True), # run 8bit mixed precision\n",
202
+ " #('low_vram', True), # uncomment this if the GPU is connected to your monitors. It will use less vram to quantize, but is slower.\n",
203
+ " ])),\n",
204
+ " ('sample', OrderedDict([\n",
205
+ " ('sampler', 'flowmatch'), # must match train.noise_scheduler\n",
206
+ " ('sample_every', 250), # sample every this many steps\n",
207
+ " ('width', 1024),\n",
208
+ " ('height', 1024),\n",
209
+ " ('prompts', [\n",
210
+ " # you can add [trigger] to the prompts here and it will be replaced with the trigger word\n",
211
+ " #'[trigger] holding a sign that says \\'I LOVE PROMPTS!\\'',\n",
212
+ " 'woman with red hair, playing chess at the park, bomb going off in the background',\n",
213
+ " 'a woman holding a coffee cup, in a beanie, sitting at a cafe',\n",
214
+ " 'a horse is a DJ at a night club, fish eye lens, smoke machine, lazer lights, holding a martini',\n",
215
+ " 'a man showing off his cool new t shirt at the beach, a shark is jumping out of the water in the background',\n",
216
+ " 'a bear building a log cabin in the snow covered mountains',\n",
217
+ " 'woman playing the guitar, on stage, singing a song, laser lights, punk rocker',\n",
218
+ " 'hipster man with a beard, building a chair, in a wood shop',\n",
219
+ " 'photo of a man, white background, medium shot, modeling clothing, studio lighting, white backdrop',\n",
220
+ " 'a man holding a sign that says, \\'this is a sign\\'',\n",
221
+ " 'a bulldog, in a post apocalyptic world, with a shotgun, in a leather jacket, in a desert, with a motorcycle'\n",
222
+ " ]),\n",
223
+ " ('neg', ''), # not used on flux\n",
224
+ " ('seed', 42),\n",
225
+ " ('walk_seed', True),\n",
226
+ " ('guidance_scale', 4),\n",
227
+ " ('sample_steps', 20)\n",
228
+ " ]))\n",
229
+ " ])\n",
230
+ " ])\n",
231
+ " ])),\n",
232
+ " # you can add any additional meta info here. [name] is replaced with config name at top\n",
233
+ " ('meta', OrderedDict([\n",
234
+ " ('name', '[name]'),\n",
235
+ " ('version', '1.0')\n",
236
+ " ]))\n",
237
+ "])\n"
238
+ ]
239
+ },
240
+ {
241
+ "cell_type": "markdown",
242
+ "metadata": {
243
+ "id": "h6F1FlM2Wb3l"
244
+ },
245
+ "source": [
246
+ "## Run it\n",
247
+ "\n",
248
+ "Below does all the magic. Check your folders to the left. Items will be in output/LoRA/your_name_v1 In the samples folder, there are preiodic sampled. This doesnt work great with colab. They will be in /content/output"
249
+ ]
250
+ },
251
+ {
252
+ "cell_type": "code",
253
+ "execution_count": null,
254
+ "metadata": {
255
+ "id": "HkajwI8gteOh"
256
+ },
257
+ "outputs": [],
258
+ "source": [
259
+ "run_job(job_to_run)\n"
260
+ ]
261
+ },
262
+ {
263
+ "cell_type": "markdown",
264
+ "metadata": {
265
+ "id": "Hblgb5uwW5SD"
266
+ },
267
+ "source": [
268
+ "## Done\n",
269
+ "\n",
270
+ "Check your ourput dir and get your slider\n"
271
+ ]
272
+ }
273
+ ],
274
+ "metadata": {
275
+ "accelerator": "GPU",
276
+ "colab": {
277
+ "gpuType": "A100",
278
+ "machine_shape": "hm",
279
+ "provenance": []
280
+ },
281
+ "kernelspec": {
282
+ "display_name": "Python 3",
283
+ "name": "python3"
284
+ },
285
+ "language_info": {
286
+ "name": "python"
287
+ }
288
+ },
289
+ "nbformat": 4,
290
+ "nbformat_minor": 0
291
+ }