Skip to content

Commit 988f327

Browse files
authored
Merge pull request #41 from JoePenna/feature/google-colab-notebook
Feature/google colab notebook
2 parents b0bfaed + 745aba4 commit 988f327

File tree

4 files changed

+357
-181
lines changed

4 files changed

+357
-181
lines changed

dreambooth_colab_joepenna.ipynb

+245
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"provenance": [],
7+
"collapsed_sections": []
8+
},
9+
"kernelspec": {
10+
"name": "python3",
11+
"display_name": "Python 3"
12+
},
13+
"language_info": {
14+
"name": "python"
15+
}
16+
},
17+
"cells": [
18+
{
19+
"cell_type": "code",
20+
"execution_count": null,
21+
"metadata": {
22+
"id": "qeTrc2vOeiNh"
23+
},
24+
"outputs": [],
25+
"source": [
26+
"#@title BUILD ENV\n",
27+
"!git clone https://github.com/JoePenna/Dreambooth-Stable-Diffusion\n",
28+
"%cd Dreambooth-Stable-Diffusion\n",
29+
"\n",
30+
"!git checkout feature/google-colab-notebook\n",
31+
"\n",
32+
"!pip install omegaconf\n",
33+
"!pip install einops\n",
34+
"!pip install pytorch-lightning==1.6.5\n",
35+
"!pip install test-tube\n",
36+
"!pip install transformers\n",
37+
"!pip install kornia\n",
38+
"!pip install -e git+https://github.com/CompVis/taming-transformers.git@master#egg=taming-transformers\n",
39+
"!pip install -e git+https://github.com/openai/CLIP.git@main#egg=clip\n",
40+
"!pip install setuptools==59.5.0\n",
41+
"!pip install pillow==9.0.1\n",
42+
"!pip install torchmetrics==0.6.0\n",
43+
"!pip install -e .\n",
44+
"!pip install protobuf==3.20.1\n",
45+
"!pip install gdown\n",
46+
"!pip install pydrive\n",
47+
"!pip install -qq diffusers[\"training\"]==0.3.0 transformers ftfy\n",
48+
"!pip install -qq \"ipywidgets>=7,<8\"\n",
49+
"!pip install huggingface_hub\n",
50+
"!pip install ipywidgets==7.7.1\n",
51+
"\n",
52+
"import os\n",
53+
"os._exit(00)\n"
54+
]
55+
},
56+
{
57+
"cell_type": "code",
58+
"execution_count": null,
59+
"outputs": [],
60+
"source": [
61+
"#@title # Required - Navigate back to the directory.\n",
62+
"%cd Dreambooth-Stable-Diffusion"
63+
],
64+
"metadata": {
65+
"collapsed": false
66+
}
67+
},
68+
{
69+
"cell_type": "code",
70+
"source": [
71+
"#@markdown Hugging Face Login\n",
72+
"from huggingface_hub import notebook_login\n",
73+
"\n",
74+
"notebook_login()"
75+
],
76+
"metadata": {
77+
"id": "6tjx0HcjesFo"
78+
},
79+
"execution_count": 1,
80+
"outputs": []
81+
},
82+
{
83+
"cell_type": "code",
84+
"source": [
85+
"#@markdown Download the 1.4 sd model\n",
86+
"from IPython.display import clear_output\n",
87+
"\n",
88+
"from huggingface_hub import hf_hub_download\n",
89+
"downloaded_model_path = hf_hub_download(\n",
90+
" repo_id=\"CompVis/stable-diffusion-v-1-4-original\",\n",
91+
" filename=\"sd-v1-4.ckpt\",\n",
92+
" use_auth_token=True\n",
93+
")\n",
94+
"\n",
95+
"# Move the sd-v1-4.ckpt to the root of this directory as \"model.ckpt\"\n",
96+
"actual_locations_of_model_blob = !readlink -f {downloaded_model_path}\n",
97+
"!mv {actual_locations_of_model_blob[-1]} model.ckpt\n",
98+
"clear_output()\n",
99+
"print(\"✅ model.ckpt successfully downloaded\")\n"
100+
],
101+
"metadata": {
102+
"id": "O15vMMhCevib"
103+
},
104+
"execution_count": null,
105+
"outputs": []
106+
},
107+
{
108+
"cell_type": "code",
109+
"source": [
110+
"#@title # Download Regularization Images\n",
111+
"#@markdown We’ve created the following image sets\n",
112+
"#@markdown - `man_euler` - provided by Niko Pueringer (Corridor Digital) - euler @ 40 steps, CFG 7.5\n",
113+
"#@markdown - `man_unsplash` - pictures from various photographers\n",
114+
"#@markdown - `person_ddim`\n",
115+
"#@markdown - `woman_ddim` - provided by David Bielejeski - ddim @ 50 steps, CFG 10.0 <br />\n",
116+
"#@markdown - `blonde_woman` - provided by David Bielejeski - ddim @ 50 steps, CFG 10.0 <br />\n",
117+
"\n",
118+
"dataset=\"person_ddim\" #@param [\"man_euler\", \"man_unsplash\", \"person_ddim\", \"woman_ddim\", \"blonde_woman\"]\n",
119+
"!git clone https://github.com/djbielejeski/Stable-Diffusion-Regularization-Images-{dataset}.git\n",
120+
"\n",
121+
"!mkdir -p regularization_images/{dataset}\n",
122+
"!mv -v Stable-Diffusion-Regularization-Images-{dataset}/{dataset}/*.* regularization_images/{dataset}"
123+
],
124+
"metadata": {
125+
"id": "N96aedTtfBjO"
126+
},
127+
"execution_count": 2,
128+
"outputs": []
129+
},
130+
{
131+
"cell_type": "code",
132+
"source": [
133+
"#@title # Training Images\n",
134+
"#@markdown ## Upload your training images\n",
135+
"#@markdown WARNING: Be sure to upload an even amount of images, otherwise the training inexplicably stops at 1500 steps. <br />\n",
136+
"#@markdown - 2-3 full body\n",
137+
"#@markdown - 3-5 upper body\n",
138+
"#@markdown - 5-12 close-up on face <br /> <br />\n",
139+
"#@markdown The images should be as close as possible to the kind of images you’re trying to make (most of the time, that means no selfies).\n",
140+
"from google.colab import files\n",
141+
"from IPython.display import clear_output\n",
142+
"\n",
143+
"# Create the directory\n",
144+
"!rm -rf training_images\n",
145+
"!mkdir -p training_images\n",
146+
"\n",
147+
"# Upload the files\n",
148+
"uploaded = files.upload()\n",
149+
"for filename in uploaded.keys():\n",
150+
" updated_file_name = filename.replace(\" \", \"_\")\n",
151+
" !mv \"{filename}\" \"training_images/{updated_file_name}\"\n",
152+
" clear_output()\n",
153+
"\n",
154+
"# Tell the user what is going on\n",
155+
"training_images_file_paths = !find training_images/*\n",
156+
"if len(training_images_file_paths) == 0:\n",
157+
" print(\"❌ no training images found. Please upload images to training_images\")\n",
158+
"else:\n",
159+
" print(\"\" + str(len(training_images_file_paths)) + \" training images found\")\n"
160+
],
161+
"metadata": {
162+
"id": "A7hmdOdOfGzs"
163+
},
164+
"execution_count": null,
165+
"outputs": []
166+
},
167+
{
168+
"cell_type": "code",
169+
"source": [
170+
"#@title # Training\n",
171+
"\n",
172+
"#@markdown This isn't used for training, just to help you remember what your trained into the model.\n",
173+
"project_name = \"project_name\" #@param {type:\"string\"}\n",
174+
"\n",
175+
"# MAX STEPS\n",
176+
"#@markdown How many steps do you want to train for?\n",
177+
"max_training_steps = 2000 #@param {type:\"integer\"}\n",
178+
"\n",
179+
"#@markdown Match class_word to the category of the regularization images you chose above.\n",
180+
"class_word = \"person\" #@param [\"man\", \"person\", \"woman\"] {allow-input: true}\n",
181+
"\n",
182+
"#@markdown This is the unique token you are incorporating into the stable diffusion model.\n",
183+
"token = \"firstNameLastName\" #@param {type:\"string\"}\n",
184+
"reg_data_root = \"/content/Dreambooth-Stable-Diffusion/regularization_images/\" + dataset\n",
185+
"\n",
186+
"!rm -rf training_images/.ipynb_checkpoints\n",
187+
"!python \"main.py\" \\\n",
188+
" --base configs/stable-diffusion/v1-finetune_unfrozen.yaml \\\n",
189+
" -t \\\n",
190+
" --actual_resume \"model.ckpt\" \\\n",
191+
" --reg_data_root {reg_data_root} \\\n",
192+
" -n {project_name} \\\n",
193+
" --gpus 0, \\\n",
194+
" --data_root \"/content/Dreambooth-Stable-Diffusion/training_images\" \\\n",
195+
" --max_training_steps {max_training_steps} \\\n",
196+
" --class_word {class_word} \\\n",
197+
" --token {token} \\\n",
198+
" --no-test"
199+
],
200+
"metadata": {
201+
"id": "m2o_fFFvfxHi"
202+
},
203+
"execution_count": null,
204+
"outputs": []
205+
},
206+
{
207+
"cell_type": "code",
208+
"source": [
209+
"#@title # Prune the Model\n",
210+
"\n",
211+
"directory_paths = !ls -d logs/*\n",
212+
"last_checkpoint_file = directory_paths[-1] + \"/checkpoints/last.ckpt\"\n",
213+
"!python \"prune_ckpt.py\" --ckpt {last_checkpoint_file}\n",
214+
"last_checkpoint_file_pruned = directory_paths[-1] + \"/checkpoints/last-pruned.ckpt\"\n",
215+
"training_images = !find training_images/*\n",
216+
"date_string = !date +\"%Y-%m-%dT%H-%M-%S\"\n",
217+
"file_name = date_string[-1] + \"_\" + project_name + \"_\" + str(len(training_images)) + \"_training_images_\" + str(max_training_steps) + \"_max_training_steps_\" + token + \"_token_\" + class_word + \"_class_word.ckpt\"\n",
218+
"!mkdir -p trained_models\n",
219+
"!mv {last_checkpoint_file_pruned} trained_models/{file_name}\n",
220+
"\n",
221+
"print(\"Download your trained model file from trained_models/\" + file_name + \" and use in your favorite Stable Diffusion repo!\")"
222+
],
223+
"metadata": {
224+
"id": "Ll_ZIFNUulKJ"
225+
},
226+
"execution_count": null,
227+
"outputs": []
228+
},
229+
{
230+
"cell_type": "code",
231+
"source": [
232+
"#@title Save model in google drive\n",
233+
"from google.colab import drive\n",
234+
"drive.mount('/content/drive')\n",
235+
"\n",
236+
"!cp trained_models/{file_name} /content/drive/MyDrive/{file_name}"
237+
],
238+
"metadata": {
239+
"id": "mkidEm4evn1J"
240+
},
241+
"execution_count": null,
242+
"outputs": []
243+
}
244+
]
245+
}

0 commit comments

Comments
 (0)