NEXAS commited on
Commit
2d055e9
1 Parent(s): 69f7194

Upload Finetune_.ipynb

Browse files
Files changed (1) hide show
  1. Finetune_.ipynb +234 -0
Finetune_.ipynb ADDED
@@ -0,0 +1,234 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "source": [
6
+ "#@title 🤗 AutoTrain DreamBooth\n",
7
+ "# @markdown In order to use this colab\n",
8
+ "# @markdown - upload images to a folder named `images/`\n",
9
+ "# @markdown - choose a project name if you wish\n",
10
+ "# @markdown - change model if you wish, you can also select sd2/2.1 or sd1.5\n",
11
+ "# @markdown - update prompt and remember it. choose keywords that don't usually appear in dictionaries\n",
12
+ "# @markdown - add huggingface information (token and repo_id) if you wish to push trained model to huggingface hub\n",
13
+ "# @markdown - update hyperparameters if you wish\n",
14
+ "# @markdown - click `Runtime > Run all` or run each cell individually\n",
15
+ "\n",
16
+ "import os\n",
17
+ "!pip install -U autotrain-advanced > install_logs.txt\n",
18
+ "!autotrain setup > setup_logs.txt"
19
+ ],
20
+ "metadata": {
21
+ "cellView": "code",
22
+ "id": "9iClNdQayIv5"
23
+ },
24
+ "execution_count": null,
25
+ "outputs": []
26
+ },
27
+ {
28
+ "cell_type": "markdown",
29
+ "source": [
30
+ "# New Section"
31
+ ],
32
+ "metadata": {
33
+ "id": "BqqPQXPhRgU2"
34
+ }
35
+ },
36
+ {
37
+ "cell_type": "code",
38
+ "source": [
39
+ "from google.colab import drive\n",
40
+ "drive.mount('/content/drive')"
41
+ ],
42
+ "metadata": {
43
+ "colab": {
44
+ "base_uri": "https://localhost:8080/"
45
+ },
46
+ "id": "DL29zZgjRtll",
47
+ "outputId": "ced02a72-8dda-48c1-c111-a682dd5e2881"
48
+ },
49
+ "execution_count": 3,
50
+ "outputs": [
51
+ {
52
+ "output_type": "stream",
53
+ "name": "stdout",
54
+ "text": [
55
+ "Mounted at /content/drive\n"
56
+ ]
57
+ }
58
+ ]
59
+ },
60
+ {
61
+ "cell_type": "code",
62
+ "execution_count": 5,
63
+ "metadata": {
64
+ "id": "A2-_lkBS1WKA",
65
+ "cellView": "code"
66
+ },
67
+ "outputs": [],
68
+ "source": [
69
+ "#@markdown ---\n",
70
+ "#@markdown #### Project Config\n",
71
+ "project_name = 'my_dreambooth_project' # @param {type:\"string\"}\n",
72
+ "model_name = 'stabilityai/stable-diffusion-xl-base-1.0' # @param [\"stabilityai/stable-diffusion-xl-base-1.0\", \"runwayml/stable-diffusion-v1-5\", \"stabilityai/stable-diffusion-2-1\", \"stabilityai/stable-diffusion-2-1-base\"]\n",
73
+ "prompt = 'photo of a nkl person' # @param {type: \"string\"}\n",
74
+ "\n",
75
+ "#@markdown ---\n",
76
+ "#@markdown #### Push to Hub?\n",
77
+ "#@markdown Use these only if you want to push your trained model to a private repo in your Hugging Face Account\n",
78
+ "#@markdown If you dont use these, the model will be saved in Google Colab and you are required to download it manually.\n",
79
+ "#@markdown Please enter your Hugging Face write token. The trained model will be saved to your Hugging Face account.\n",
80
+ "#@markdown You can find your token here: https://huggingface.co/settings/tokens\n",
81
+ "push_to_hub = True # @param [\"False\", \"True\"] {type:\"raw\"}\n",
82
+ "hf_token = \"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\" #@param {type:\"string\"}\n",
83
+ "repo_id = \"NEXAS/stable_diff_custom\" #@param {type:\"string\"}\n",
84
+ "\n",
85
+ "#@markdown ---\n",
86
+ "#@markdown #### Hyperparameters\n",
87
+ "learning_rate = 1e-4 # @param {type:\"number\"}\n",
88
+ "num_steps = 500 #@param {type:\"number\"}\n",
89
+ "batch_size = 1 # @param {type:\"slider\", min:1, max:32, step:1}\n",
90
+ "gradient_accumulation = 4 # @param {type:\"slider\", min:1, max:32, step:1}\n",
91
+ "resolution = 1024 # @param {type:\"slider\", min:128, max:1024, step:128}\n",
92
+ "use_8bit_adam = True # @param [\"False\", \"True\"] {type:\"raw\"}\n",
93
+ "use_xformers = True # @param [\"False\", \"True\"] {type:\"raw\"}\n",
94
+ "use_fp16 = True # @param [\"False\", \"True\"] {type:\"raw\"}\n",
95
+ "train_text_encoder = False # @param [\"False\", \"True\"] {type:\"raw\"}\n",
96
+ "gradient_checkpointing = True # @param [\"False\", \"True\"] {type:\"raw\"}\n",
97
+ "os.environ[\"PROJECT_NAME\"] = project_name\n",
98
+ "os.environ[\"MODEL_NAME\"] = model_name\n",
99
+ "os.environ[\"PROMPT\"] = prompt\n",
100
+ "os.environ[\"PUSH_TO_HUB\"] = str(push_to_hub)\n",
101
+ "os.environ[\"HF_TOKEN\"] = hf_token\n",
102
+ "os.environ[\"REPO_ID\"] = repo_id\n",
103
+ "os.environ[\"LEARNING_RATE\"] = str(learning_rate)\n",
104
+ "os.environ[\"NUM_STEPS\"] = str(num_steps)\n",
105
+ "os.environ[\"BATCH_SIZE\"] = str(batch_size)\n",
106
+ "os.environ[\"GRADIENT_ACCUMULATION\"] = str(gradient_accumulation)\n",
107
+ "os.environ[\"RESOLUTION\"] = str(resolution)\n",
108
+ "os.environ[\"USE_8BIT_ADAM\"] = str(use_8bit_adam)\n",
109
+ "os.environ[\"USE_XFORMERS\"] = str(use_xformers)\n",
110
+ "os.environ[\"USE_FP16\"] = str(use_fp16)\n",
111
+ "os.environ[\"TRAIN_TEXT_ENCODER\"] = str(train_text_encoder)\n",
112
+ "os.environ[\"GRADIENT_CHECKPOINTING\"] = str(gradient_checkpointing)"
113
+ ]
114
+ },
115
+ {
116
+ "cell_type": "code",
117
+ "execution_count": null,
118
+ "metadata": {
119
+ "collapsed": true,
120
+ "id": "g3cd_ED_yXXt"
121
+ },
122
+ "outputs": [],
123
+ "source": [
124
+ "!autotrain dreambooth \\\n",
125
+ "--model ${MODEL_NAME} \\\n",
126
+ "--output ${PROJECT_NAME} \\\n",
127
+ "--image-path images/ \\\n",
128
+ "--prompt \"${PROMPT}\" \\\n",
129
+ "--resolution ${RESOLUTION} \\\n",
130
+ "--batch-size ${BATCH_SIZE} \\\n",
131
+ "--num-steps ${NUM_STEPS} \\\n",
132
+ "--gradient-accumulation ${GRADIENT_ACCUMULATION} \\\n",
133
+ "--lr ${LEARNING_RATE} \\\n",
134
+ "$( [[ \"$USE_FP16\" == \"True\" ]] && echo \"--fp16\" ) \\\n",
135
+ "$( [[ \"$USE_XFORMERS\" == \"True\" ]] && echo \"--xformers\" ) \\\n",
136
+ "$( [[ \"$TRAIN_TEXT_ENCODER\" == \"True\" ]] && echo \"--train-text-encoder\" ) \\\n",
137
+ "$( [[ \"$USE_8BIT_ADAM\" == \"True\" ]] && echo \"--use-8bit-adam\" ) \\\n",
138
+ "$( [[ \"$GRADIENT_CHECKPOINTING\" == \"True\" ]] && echo \"--gradient-checkpointing\" ) \\\n",
139
+ "$( [[ \"$PUSH_TO_HUB\" == \"True\" ]] && echo \"--push-to-hub --hub-token ${HF_TOKEN} --hub-model-id ${REPO_ID}\" )"
140
+ ]
141
+ },
142
+ {
143
+ "cell_type": "code",
144
+ "source": [
145
+ "# Inference\n",
146
+ "# this is the inference code that you can use after you have trained your model\n",
147
+ "# Unhide code below and change prj_path to your repo or local path (e.g. my_dreambooth_project)\n",
148
+ "#\n",
149
+ "#\n",
150
+ "#\n",
151
+ "from diffusers import DiffusionPipeline\n",
152
+ "import torch\n",
153
+ "\n",
154
+ "prj_path = \"/content/my_dreambooth_project\"\n",
155
+ "model = \"stabilityai/stable-diffusion-xl-base-1.0\"\n",
156
+ "pipe = DiffusionPipeline.from_pretrained(\n",
157
+ " model,\n",
158
+ " torch_dtype=torch.float16,\n",
159
+ ")\n",
160
+ "pipe.to(\"cuda\")\n",
161
+ "pipe.load_lora_weights(prj_path, weight_name=\"pytorch_lora_weights.safetensors\")\n",
162
+ "\n",
163
+ "prompt = \"potrait Photo of a nkl person, in anime style\"\n",
164
+ "\n",
165
+ "for seed in range(5):\n",
166
+ " generator = torch.Generator(\"cuda\").manual_seed(seed)\n",
167
+ " image = pipe(prompt=prompt, generator=generator,num_inference_steps=25).images[0]\n",
168
+ " image.save(f\"images/{seed}.png\")"
169
+ ],
170
+ "metadata": {
171
+ "id": "L2Zn_1Knlmgs"
172
+ },
173
+ "execution_count": null,
174
+ "outputs": []
175
+ },
176
+ {
177
+ "cell_type": "code",
178
+ "source": [
179
+ "# Inference\n",
180
+ "# this is the inference code that you can use after you have trained your model\n",
181
+ "# Unhide code below and change prj_path to your repo or local path (e.g. my_dreambooth_project)\n",
182
+ "#\n",
183
+ "#\n",
184
+ "#\n",
185
+ "from diffusers import DiffusionPipeline,StableDiffusionXLImg2ImgPipeline\n",
186
+ "import torch\n",
187
+ "\n",
188
+ "prj_path = \"NEXAS/stable_diff_custom\"\n",
189
+ "model = \"stabilityai/stable-diffusion-xl-base-1.0\"\n",
190
+ "pipe = DiffusionPipeline.from_pretrained(\n",
191
+ " model,\n",
192
+ " torch_dtype=torch.float16,\n",
193
+ " )\n",
194
+ "pipe.to(\"cuda\")\n",
195
+ "pipe.load_lora_weights(prj_path, weight_name=\"pytorch_lora_weights.safetensors\")\n",
196
+ "\n",
197
+ "refiner = StableDiffusionXLImg2ImgPipeline.from_pretrained(\n",
198
+ " \"stabilityai/stable-diffusion-xl-refiner-1.0\",\n",
199
+ " torch_dtype=torch.float16,\n",
200
+ " )\n",
201
+ "refiner.to(\"cuda\")\n",
202
+ "\n",
203
+ "prompt = \"photo of a nkl person,in a black suit 4k\"\n",
204
+ "\n",
205
+ "seed = 42\n",
206
+ "generator = torch.Generator(\"cuda\").manual_seed(seed)\n",
207
+ "image = pipe(prompt=prompt, generator=generator).images[0]\n",
208
+ "image = refiner(prompt=prompt, generator=generator, image=image).images[0]\n",
209
+ "image.save(f\"generated_image.png\")"
210
+ ],
211
+ "metadata": {
212
+ "id": "M8i_ae_obcGe"
213
+ },
214
+ "execution_count": null,
215
+ "outputs": []
216
+ }
217
+ ],
218
+ "metadata": {
219
+ "accelerator": "GPU",
220
+ "colab": {
221
+ "gpuType": "T4",
222
+ "provenance": []
223
+ },
224
+ "kernelspec": {
225
+ "display_name": "Python 3",
226
+ "name": "python3"
227
+ },
228
+ "language_info": {
229
+ "name": "python"
230
+ }
231
+ },
232
+ "nbformat": 4,
233
+ "nbformat_minor": 0
234
+ }