Skip to content

Commit efb20eb

Browse files
committed
Colab notebook
1 parent 0d8a259 commit efb20eb

File tree

1 file changed

+176
-0
lines changed

1 file changed

+176
-0
lines changed

Finetune_VAE.ipynb

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"provenance": [],
7+
"gpuType": "T4"
8+
},
9+
"kernelspec": {
10+
"name": "python3",
11+
"display_name": "Python 3"
12+
},
13+
"language_info": {
14+
"name": "python"
15+
},
16+
"accelerator": "GPU"
17+
},
18+
"cells": [
19+
{
20+
"cell_type": "markdown",
21+
"source": [
22+
"**Install conda, taming-transformers and its dependencies**"
23+
],
24+
"metadata": {
25+
"id": "EZx3a2Dsc_wq"
26+
}
27+
},
28+
{
29+
"cell_type": "code",
30+
"source": [
31+
"!pip install -q condacolab\n",
32+
"import condacolab\n",
33+
"condacolab.install()\n",
34+
"\n",
35+
"!git clone https://github.com/rbbb/taming-transformers\n",
36+
"%cd /content/taming-transformers\n",
37+
"!git checkout stable-diff-vae-finetuning\n",
38+
"\n",
39+
"!conda env update -n taming-transformers -f environment.yaml\n",
40+
"!conda run -n taming-transformers pip install -e ."
41+
],
42+
"metadata": {
43+
"id": "UYDCEq4iE_iF"
44+
},
45+
"execution_count": null,
46+
"outputs": []
47+
},
48+
{
49+
"cell_type": "markdown",
50+
"source": [
51+
"**Put some images in /content/src_img_here/**"
52+
],
53+
"metadata": {
54+
"id": "uyDtJvL-dOWr"
55+
}
56+
},
57+
{
58+
"cell_type": "code",
59+
"source": [
60+
"!mkdir /content/src_img_here/\n",
61+
"#directory /0000/ from danbooru 2020 data containing 3.5k images\n",
62+
"!unzip /content/drive/MyDrive/danbooru_tiny.zip -d /content/src_img_here/"
63+
],
64+
"metadata": {
65+
"id": "Zme4fnUSWwyo"
66+
},
67+
"execution_count": null,
68+
"outputs": []
69+
},
70+
{
71+
"cell_type": "markdown",
72+
"source": [
73+
"**Find all images, put their name in train_img.txt and val_img.txt (10 validation images)**"
74+
],
75+
"metadata": {
76+
"id": "S55xG8OEdYjI"
77+
}
78+
},
79+
{
80+
"cell_type": "code",
81+
"source": [
82+
"import glob\n",
83+
"img_filenames = list(glob.glob(\"/content/src_img_here/**/*.*\", recursive=True))\n",
84+
"img_filenames = [i for i in img_filenames if (i.endswith(\".png\") or i.endswith(\".jpg\") or i.endswith(\".jpeg\"))]\n",
85+
"with open(\"/content/taming-transformers/train_img.txt\",\"w\") as f:\n",
86+
" f.write(\"\\n\".join(img_filenames[10:]))\n",
87+
"with open(\"/content/taming-transformers/val_img.txt\",\"w\") as f:\n",
88+
" f.write(\"\\n\".join(img_filenames[0:10]))\n"
89+
],
90+
"metadata": {
91+
"id": "XvInztU0Lx6y"
92+
},
93+
"execution_count": null,
94+
"outputs": []
95+
},
96+
{
97+
"cell_type": "markdown",
98+
"source": [
99+
"**Get a VAE from civitai, convert from safetensors to ckpt**"
100+
],
101+
"metadata": {
102+
"id": "z71bh_0vdoap"
103+
}
104+
},
105+
{
106+
"cell_type": "code",
107+
"source": [
108+
"!mkdir /content/src_vae_here/\n",
109+
"!wget -O /content/src_vae_here/vae.safetensors https://civitai.com/api/download/models/88156 #Clear VAE\n",
110+
"\n",
111+
"!pip install safetensors torch\n",
112+
"from safetensors import safe_open\n",
113+
"import torch\n",
114+
"vae = {}\n",
115+
"with safe_open(\"/content/src_vae_here/vae.safetensors\", framework=\"pt\", device=0) as f:\n",
116+
" vae = {k: f.get_tensor(k) for k in f.keys()}\n",
117+
"torch.save({'state_dict':vae}, \"/content/src_vae_here/vae.pt\")"
118+
],
119+
"metadata": {
120+
"id": "kPgbHeCpW-Et"
121+
},
122+
"execution_count": null,
123+
"outputs": []
124+
},
125+
{
126+
"cell_type": "markdown",
127+
"source": [
128+
"**Change the parameters in the /content/taming-transformers/configs/finetune_vae.yaml (name of VAE file and batch size)**"
129+
],
130+
"metadata": {
131+
"id": "jg7BpQ4ad5xG"
132+
}
133+
},
134+
{
135+
"cell_type": "code",
136+
"source": [
137+
"import yaml\n",
138+
"with open('/content/taming-transformers/configs/finetune_vae.yaml','r') as file:\n",
139+
" conf = yaml.load(file, Loader=yaml.FullLoader)\n",
140+
"\n",
141+
"conf['model']['params']['ckpt_path'] = \"\\\"/content/src_vae_here/vae.pt\\\"\"\n",
142+
"conf['data']['params']['batch_size'] = \"5\"\n",
143+
"\n",
144+
"with open('/content/taming-transformers/configs/finetune_vae.yaml','w') as file:\n",
145+
" yaml.dump(conf, file)\n",
146+
"\n"
147+
],
148+
"metadata": {
149+
"id": "0AX9Q_6zNu1C"
150+
},
151+
"execution_count": null,
152+
"outputs": []
153+
},
154+
{
155+
"cell_type": "markdown",
156+
"source": [
157+
"**Run**"
158+
],
159+
"metadata": {
160+
"id": "-GXysMjAeLuC"
161+
}
162+
},
163+
{
164+
"cell_type": "code",
165+
"source": [
166+
"%cd /content/taming-transformers\n",
167+
"!conda run --no-capture-output -n taming-transformers python main.py --base configs/finetune_vae.yaml -t True --gpus 0, --accumulate_grad_batches 6"
168+
],
169+
"metadata": {
170+
"id": "GSOiQtWKYzeV"
171+
},
172+
"execution_count": null,
173+
"outputs": []
174+
}
175+
]
176+
}

0 commit comments

Comments
 (0)