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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"nbformat": 4,
|
3 |
+
"nbformat_minor": 0,
|
4 |
+
"metadata": {
|
5 |
+
"colab": {
|
6 |
+
"provenance": []
|
7 |
+
},
|
8 |
+
"kernelspec": {
|
9 |
+
"name": "python3",
|
10 |
+
"display_name": "Python 3"
|
11 |
+
},
|
12 |
+
"language_info": {
|
13 |
+
"name": "python"
|
14 |
+
},
|
15 |
+
"accelerator": "TPU"
|
16 |
+
},
|
17 |
+
"cells": [
|
18 |
+
{
|
19 |
+
"cell_type": "code",
|
20 |
+
"source": [
|
21 |
+
"!pip install git+https://github.com/huggingface/diffusers.git\n",
|
22 |
+
"!pip install -U -r requirements.txt\n",
|
23 |
+
"!pip install huggingface\n",
|
24 |
+
"!pip install diffusers[training]\n",
|
25 |
+
"!pip install diffusers\n",
|
26 |
+
"!pip install torch\n",
|
27 |
+
"!sudo apt -qq install git-lfs\n",
|
28 |
+
"!git config --global credential.helper store\n",
|
29 |
+
"!pip install tqdm"
|
30 |
+
],
|
31 |
+
"metadata": {
|
32 |
+
"id": "aE5NZ-XcU7bC"
|
33 |
+
},
|
34 |
+
"execution_count": null,
|
35 |
+
"outputs": []
|
36 |
+
},
|
37 |
+
{
|
38 |
+
"cell_type": "code",
|
39 |
+
"source": [
|
40 |
+
"from dataclasses import dataclass\n",
|
41 |
+
"\n",
|
42 |
+
"\n",
|
43 |
+
"@dataclass\n",
|
44 |
+
"class TrainingConfig:\n",
|
45 |
+
" image_size = 128 # the generated image resolution\n",
|
46 |
+
" train_batch_size = 16\n",
|
47 |
+
" eval_batch_size = 16 # how many images to sample during evaluation\n",
|
48 |
+
" num_epochs = 50\n",
|
49 |
+
" gradient_accumulation_steps = 1\n",
|
50 |
+
" learning_rate = 1e-4\n",
|
51 |
+
" lr_warmup_steps = 500\n",
|
52 |
+
" save_image_epochs = 10\n",
|
53 |
+
" save_model_epochs = 10\n",
|
54 |
+
" mixed_precision = \"fp16\" # `no` for float32, `fp16` for automatic mixed precision\n",
|
55 |
+
" output_dir = \"ddpm-butterflies-128\" # the model name locally and on the HF Hub\n",
|
56 |
+
"\n",
|
57 |
+
" push_to_hub = True # whether to upload the saved model to the HF Hub\n",
|
58 |
+
" hub_private_repo = False\n",
|
59 |
+
" overwrite_output_dir = True # overwrite the old model when re-running the notebook\n",
|
60 |
+
" seed = 0\n",
|
61 |
+
"\n",
|
62 |
+
"\n",
|
63 |
+
"config = TrainingConfig()"
|
64 |
+
],
|
65 |
+
"metadata": {
|
66 |
+
"id": "faBx8T9NV1Xv"
|
67 |
+
},
|
68 |
+
"execution_count": null,
|
69 |
+
"outputs": []
|
70 |
+
},
|
71 |
+
{
|
72 |
+
"cell_type": "code",
|
73 |
+
"execution_count": null,
|
74 |
+
"metadata": {
|
75 |
+
"id": "d5jOnnaPSKZx"
|
76 |
+
},
|
77 |
+
"outputs": [],
|
78 |
+
"source": [
|
79 |
+
"from datasets import load_dataset\n",
|
80 |
+
"\n",
|
81 |
+
"config.dataset_name = \"Drozdik/tattoo_v0\"\n",
|
82 |
+
"dataset = load_dataset(config.dataset_name, split=\"train\")"
|
83 |
+
]
|
84 |
+
},
|
85 |
+
{
|
86 |
+
"cell_type": "code",
|
87 |
+
"source": [
|
88 |
+
"def transform(examples):\n",
|
89 |
+
" images = [preprocess(image.convert(\"RGB\")) for image in examples[\"image\"]]\n",
|
90 |
+
" return {\"images\": images}\n",
|
91 |
+
"\n"
|
92 |
+
],
|
93 |
+
"metadata": {
|
94 |
+
"id": "CvUPjQmqXsG1"
|
95 |
+
},
|
96 |
+
"execution_count": null,
|
97 |
+
"outputs": []
|
98 |
+
},
|
99 |
+
{
|
100 |
+
"cell_type": "code",
|
101 |
+
"source": [
|
102 |
+
"from diffusers import DDPMPipeline\n",
|
103 |
+
"import math\n",
|
104 |
+
"import os\n",
|
105 |
+
"\n",
|
106 |
+
"def make_grid(images, rows, cols):\n",
|
107 |
+
" w, h = images[0].size\n",
|
108 |
+
" grid = Image.new(\"RGB\", size=(cols * w, rows * h))\n",
|
109 |
+
" for i, image in enumerate(images):\n",
|
110 |
+
" grid.paste(image, box=(i % cols * w, i // cols * h))\n",
|
111 |
+
" return grid\n",
|
112 |
+
"\n",
|
113 |
+
"\n",
|
114 |
+
"def evaluate(config, epoch, pipeline):\n",
|
115 |
+
" images = pipeline(\n",
|
116 |
+
" batch_size=config.eval_batch_size,\n",
|
117 |
+
" generator=torch.manual_seed(config.seed),\n",
|
118 |
+
" ).images\n",
|
119 |
+
"\n",
|
120 |
+
" image_grid = make_grid(images, rows=4, cols=4)\n",
|
121 |
+
"\n",
|
122 |
+
" test_dir = os.path.join(config.output_dir, \"samples\")\n",
|
123 |
+
" os.makedirs(test_dir, exist_ok=True)\n",
|
124 |
+
" image_grid.save(f\"{test_dir}/{epoch:04d}.png\")\n",
|
125 |
+
"\n"
|
126 |
+
],
|
127 |
+
"metadata": {
|
128 |
+
"id": "p6tO2qgGx-m3"
|
129 |
+
},
|
130 |
+
"execution_count": null,
|
131 |
+
"outputs": []
|
132 |
+
},
|
133 |
+
{
|
134 |
+
"cell_type": "code",
|
135 |
+
"source": [
|
136 |
+
"from accelerate import Accelerator\n",
|
137 |
+
"from tqdm.auto import tqdm\n",
|
138 |
+
"from pathlib import Path\n",
|
139 |
+
"import os\n",
|
140 |
+
"\n",
|
141 |
+
"def train_loop(config, model, noise_scheduler, optimizer, train_dataloader, lr_scheduler):\n",
|
142 |
+
" accelerator = Accelerator(\n",
|
143 |
+
" mixed_precision=config.mixed_precision,\n",
|
144 |
+
" gradient_accumulation_steps=config.gradient_accumulation_steps,\n",
|
145 |
+
" log_with=\"tensorboard\",\n",
|
146 |
+
" project_dir=os.path.join(config.output_dir, \"logs\"),\n",
|
147 |
+
" )\n",
|
148 |
+
"\n",
|
149 |
+
" if accelerator.is_main_process:\n",
|
150 |
+
" os.makedirs(config.output_dir,exist_ok=True)\n",
|
151 |
+
" accelerator.init_trackers(\"train_example\")\n",
|
152 |
+
"\n",
|
153 |
+
" model, optimizer, train_dataloader, lr_scheduler = accelerator.prepare(model, optimizer, train_dataloader, lr_scheduler)\n",
|
154 |
+
"\n",
|
155 |
+
" global_step = 0\n",
|
156 |
+
"\n",
|
157 |
+
" for epoch in range(config.num_epochs):\n",
|
158 |
+
" progress_bar = tqdm(total=len(train_dataloader), disable=not accelerator.is_local_main_process)\n",
|
159 |
+
" progress_bar.set_description(f\"Epoch {epoch}\")\n",
|
160 |
+
"\n",
|
161 |
+
" for step, batch in enumerate(train_dataloader):\n",
|
162 |
+
" clean_images = batch[\"images\"]\n",
|
163 |
+
"\n",
|
164 |
+
" noise = torch.randn(clean_images.shape).to(clean_images.device)\n",
|
165 |
+
" bs = clean_images.shape[0]\n",
|
166 |
+
"\n",
|
167 |
+
" timesteps = torch.randint(\n",
|
168 |
+
" 0, noise_scheduler.config.num_train_timesteps, (bs,), device=clean_images.device\n",
|
169 |
+
" ).long()\n",
|
170 |
+
"\n",
|
171 |
+
" noisy_images = noise_scheduler.add_noise(clean_images, noise, timesteps)\n",
|
172 |
+
"\n",
|
173 |
+
" with accelerator.accumulate(model):\n",
|
174 |
+
" noise_pred = model(noisy_images, timesteps, return_dict=False)[0]\n",
|
175 |
+
" loss = F.mse_loss(noise_pred,noise)\n",
|
176 |
+
" accelerator.backward(loss)\n",
|
177 |
+
"\n",
|
178 |
+
" accelerator.clip_grad_norm_(model.parameters(),1.0)\n",
|
179 |
+
" optimizer.step()\n",
|
180 |
+
" lr_scheduler.step()\n",
|
181 |
+
" optimizer.zero_grad()\n",
|
182 |
+
"\n",
|
183 |
+
" progress_bar.update(1)\n",
|
184 |
+
" logs = {\"loss\": loss.detach().item(), \"lr\": lr_scheduler.get_last_lr()[0], \"step\": global_step}\n",
|
185 |
+
" progress_bar.set_postfix(**logs)\n",
|
186 |
+
" accelerator.log(logs, step=global_step)\n",
|
187 |
+
" global_step += 1\n",
|
188 |
+
"\n",
|
189 |
+
" if accelerator.is_main_process:\n",
|
190 |
+
" pipeline = DDPMPipeline(unet=accelerator.unwrap_model(model), scheduler=noise_scheduler)\n",
|
191 |
+
"\n",
|
192 |
+
" if (epoch + 1) % config.save_image_epochs == 0 or epoch == config.num_epochs - 1:\n",
|
193 |
+
" evaluate(config, epoch, pipeline)\n",
|
194 |
+
" if (epoch + 1) % config.save_model_epochs == 0 or epoch == config.num_epochs - 1:\n",
|
195 |
+
" pipeline.save_pretrained(config.output_dir)\n",
|
196 |
+
"\n",
|
197 |
+
"\n",
|
198 |
+
"\n",
|
199 |
+
" upload_folder(\n",
|
200 |
+
" repo_id=repo_id,\n",
|
201 |
+
" folder_path=args.output_dir,\n",
|
202 |
+
" commit_message=\"End of training\",\n",
|
203 |
+
" ignore_patterns=[\"step_*\", \"epoch_*\"],\n",
|
204 |
+
" )\n",
|
205 |
+
"\n"
|
206 |
+
],
|
207 |
+
"metadata": {
|
208 |
+
"id": "Ae7g7TaCsnh7"
|
209 |
+
},
|
210 |
+
"execution_count": null,
|
211 |
+
"outputs": []
|
212 |
+
},
|
213 |
+
{
|
214 |
+
"cell_type": "code",
|
215 |
+
"source": [
|
216 |
+
"from accelerate import notebook_launcher\n",
|
217 |
+
"import torch.nn.functional as F\n",
|
218 |
+
"from diffusers.optimization import get_cosine_schedule_with_warmup\n",
|
219 |
+
"import torch\n",
|
220 |
+
"from PIL import Image\n",
|
221 |
+
"from diffusers import DDPMScheduler\n",
|
222 |
+
"from diffusers import UNet2DModel\n",
|
223 |
+
"import torch\n",
|
224 |
+
"from torchvision import transforms\n",
|
225 |
+
"\n",
|
226 |
+
"\n",
|
227 |
+
"\n",
|
228 |
+
"\n",
|
229 |
+
"preprocess = transforms.Compose(\n",
|
230 |
+
" [\n",
|
231 |
+
" transforms.Resize((config.image_size, config.image_size)),\n",
|
232 |
+
" transforms.RandomHorizontalFlip(),\n",
|
233 |
+
" transforms.ToTensor(),\n",
|
234 |
+
" transforms.Normalize([.5],[.5]),\n",
|
235 |
+
" ]\n",
|
236 |
+
")\n",
|
237 |
+
"\n",
|
238 |
+
"dataset.set_transform(transform)\n",
|
239 |
+
"\n",
|
240 |
+
"train_dataloader = torch.utils.data.DataLoader(dataset, batch_size=config.train_batch_size, shuffle=True)\n",
|
241 |
+
"\n",
|
242 |
+
"model = UNet2DModel(sample_size=config.image_size,in_channels=3, out_channels=3, layers_per_block=2, block_out_channels=(128,128,256,256,512,512), down_block_types=(\"DownBlock2D\",\"DownBlock2D\",\"DownBlock2D\",\"DownBlock2D\",\"AttnDownBlock2D\",\"DownBlock2D\"), up_block_types=(\"UpBlock2D\",\"AttnUpBlock2D\",\"UpBlock2D\",\"UpBlock2D\",\"UpBlock2D\",\"UpBlock2D\"), )\n",
|
243 |
+
"\n",
|
244 |
+
"sample_image = dataset[0][\"images\"].unsqueeze(0)\n",
|
245 |
+
"print(\"Input shape:\", sample_image.shape)\n",
|
246 |
+
"\n",
|
247 |
+
"print(\"Output shape:\", model(sample_image, timestep=0).sample.shape)\n",
|
248 |
+
"\n",
|
249 |
+
"noise_scheduler = DDPMScheduler(num_train_timesteps=1000)\n",
|
250 |
+
"noise = torch.randn(sample_image.shape)\n",
|
251 |
+
"time_steps = torch.LongTensor([50])\n",
|
252 |
+
"noisy_image = noise_scheduler.add_noise(sample_image, noise, time_steps)\n",
|
253 |
+
"Image.fromarray(((noisy_image.permute(0, 2, 3, 1) + 1.0) * 127.5).type(torch.uint8).numpy()[0])\n",
|
254 |
+
"\n",
|
255 |
+
"\n",
|
256 |
+
"\n",
|
257 |
+
"\n",
|
258 |
+
"optimizer = torch.optim.AdamW(model.parameters(), lr=config.learning_rate)\n",
|
259 |
+
"lr_scheduler = get_cosine_schedule_with_warmup(\n",
|
260 |
+
" optimizer=optimizer,\n",
|
261 |
+
" num_warmup_steps=config.lr_warmup_steps,\n",
|
262 |
+
" num_training_steps=(len(train_dataloader)*config.num_epochs),\n",
|
263 |
+
")\n",
|
264 |
+
"\n",
|
265 |
+
"\n",
|
266 |
+
"\n",
|
267 |
+
"args = (config, model, noise_scheduler, optimizer, train_dataloader, lr_scheduler)\n",
|
268 |
+
"\n",
|
269 |
+
"notebook_launcher(train_loop, args, num_processes=1)"
|
270 |
+
],
|
271 |
+
"metadata": {
|
272 |
+
"id": "FnPpL7H2yT8O"
|
273 |
+
},
|
274 |
+
"execution_count": null,
|
275 |
+
"outputs": []
|
276 |
+
},
|
277 |
+
{
|
278 |
+
"cell_type": "code",
|
279 |
+
"source": [
|
280 |
+
"model = UNet2DModel.from_pretrained(config.output_dir, subfolder=\"unet\")\n",
|
281 |
+
"optimizer = torch.optim.AdamW(model.parameters(), lr=config.learning_rate)\n",
|
282 |
+
"lr_scheduler = get_cosine_schedule_with_warmup(\n",
|
283 |
+
" optimizer=optimizer,\n",
|
284 |
+
" num_warmup_steps=config.lr_warmup_steps,\n",
|
285 |
+
" num_training_steps=(len(train_dataloader)*config.num_epochs),\n",
|
286 |
+
")\n",
|
287 |
+
"args = (config, model, noise_scheduler, optimizer, train_dataloader, lr_scheduler)\n",
|
288 |
+
"notebook_launcher(train_loop, args, num_processes=1)"
|
289 |
+
],
|
290 |
+
"metadata": {
|
291 |
+
"id": "K22cx-8snBIV"
|
292 |
+
},
|
293 |
+
"execution_count": null,
|
294 |
+
"outputs": []
|
295 |
+
},
|
296 |
+
{
|
297 |
+
"cell_type": "code",
|
298 |
+
"source": [
|
299 |
+
"!nvidia-smi"
|
300 |
+
],
|
301 |
+
"metadata": {
|
302 |
+
"colab": {
|
303 |
+
"base_uri": "https://localhost:8080/"
|
304 |
+
},
|
305 |
+
"id": "Rqv9HTR22qXe",
|
306 |
+
"outputId": "9480fd9d-5545-4ef8-f91c-f1dc8a02573a"
|
307 |
+
},
|
308 |
+
"execution_count": null,
|
309 |
+
"outputs": [
|
310 |
+
{
|
311 |
+
"output_type": "stream",
|
312 |
+
"name": "stdout",
|
313 |
+
"text": [
|
314 |
+
"Sun Aug 6 08:13:38 2023 \n",
|
315 |
+
"+-----------------------------------------------------------------------------+\n",
|
316 |
+
"| NVIDIA-SMI 525.105.17 Driver Version: 525.105.17 CUDA Version: 12.0 |\n",
|
317 |
+
"|-------------------------------+----------------------+----------------------+\n",
|
318 |
+
"| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |\n",
|
319 |
+
"| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |\n",
|
320 |
+
"| | | MIG M. |\n",
|
321 |
+
"|===============================+======================+======================|\n",
|
322 |
+
"| 0 Tesla T4 Off | 00000000:00:04.0 Off | 0 |\n",
|
323 |
+
"| N/A 77C P0 34W / 70W | 10807MiB / 15360MiB | 0% Default |\n",
|
324 |
+
"| | | N/A |\n",
|
325 |
+
"+-------------------------------+----------------------+----------------------+\n",
|
326 |
+
" \n",
|
327 |
+
"+-----------------------------------------------------------------------------+\n",
|
328 |
+
"| Processes: |\n",
|
329 |
+
"| GPU GI CI PID Type Process name GPU Memory |\n",
|
330 |
+
"| ID ID Usage |\n",
|
331 |
+
"|=============================================================================|\n",
|
332 |
+
"+-----------------------------------------------------------------------------+\n"
|
333 |
+
]
|
334 |
+
}
|
335 |
+
]
|
336 |
+
}
|
337 |
+
]
|
338 |
+
}
|