| <!--Copyright 2023 The HuggingFace Team. All rights reserved. | |
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | |
| the License. You may obtain a copy of the License at | |
| http://www.apache.org/licenses/LICENSE-2.0 | |
| Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | |
| an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | |
| specific language governing permissions and limitations under the License. | |
| --> | |
| # Unconditional ์ด๋ฏธ์ง ์์ฑ | |
| unconditional ์ด๋ฏธ์ง ์์ฑ์ text-to-image ๋๋ image-to-image ๋ชจ๋ธ๊ณผ ๋ฌ๋ฆฌ ํ ์คํธ๋ ์ด๋ฏธ์ง์ ๋ํ ์กฐ๊ฑด์ด ์์ด ํ์ต ๋ฐ์ดํฐ ๋ถํฌ์ ์ ์ฌํ ์ด๋ฏธ์ง๋ง์ ์์ฑํฉ๋๋ค. | |
| <iframe | |
| src="https://stevhliu-ddpm-butterflies-128.hf.space" | |
| frameborder="0" | |
| width="850" | |
| height="550" | |
| ></iframe> | |
| ์ด ๊ฐ์ด๋์์๋ ๊ธฐ์กด์ ์กด์ฌํ๋ ๋ฐ์ดํฐ์ ๊ณผ ์์ ๋ง์ ์ปค์คํ ๋ฐ์ดํฐ์ ์ ๋ํด unconditional image generation ๋ชจ๋ธ์ ํ๋ จํ๋ ๋ฐฉ๋ฒ์ ์ค๋ช ํฉ๋๋ค. ํ๋ จ ์ธ๋ถ ์ฌํญ์ ๋ํด ๋ ์์ธํ ์๊ณ ์ถ๋ค๋ฉด unconditional image generation์ ์ํ ๋ชจ๋ ํ์ต ์คํฌ๋ฆฝํธ๋ฅผ [์ฌ๊ธฐ](https://github.com/huggingface/diffusers/tree/main/examples/unconditional_image_generation)์์ ํ์ธํ ์ ์์ต๋๋ค. | |
| ์คํฌ๋ฆฝํธ๋ฅผ ์คํํ๊ธฐ ์ , ๋จผ์ ์์กด์ฑ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ ์ค์นํด์ผ ํฉ๋๋ค. | |
| ```bash | |
| pip install diffusers[training] accelerate datasets | |
| ``` | |
| ๊ทธ ๋ค์ ๐ค [Accelerate](https://github.com/huggingface/accelerate/) ํ๊ฒฝ์ ์ด๊ธฐํํฉ๋๋ค. | |
| ```bash | |
| accelerate config | |
| ``` | |
| ๋ณ๋์ ์ค์ ์์ด ๊ธฐ๋ณธ ์ค์ ์ผ๋ก ๐ค [Accelerate](https://github.com/huggingface/accelerate/) ํ๊ฒฝ์ ์ด๊ธฐํํด๋ด ์๋ค. | |
| ```bash | |
| accelerate config default | |
| ``` | |
| ๋ ธํธ๋ถ๊ณผ ๊ฐ์ ๋ํํ ์์ ์ง์ํ์ง ์๋ ํ๊ฒฝ์ ๊ฒฝ์ฐ, ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํด๋ณผ ์๋ ์์ต๋๋ค. | |
| ```py | |
| from accelerate.utils import write_basic_config | |
| write_basic_config() | |
| ``` | |
| ## ๋ชจ๋ธ์ ํ๋ธ์ ์ ๋ก๋ํ๊ธฐ | |
| ํ์ต ์คํฌ๋ฆฝํธ์ ๋ค์ ์ธ์๋ฅผ ์ถ๊ฐํ์ฌ ํ๋ธ์ ๋ชจ๋ธ์ ์ ๋ก๋ํ ์ ์์ต๋๋ค. | |
| ```bash | |
| --push_to_hub | |
| ``` | |
| ## ์ฒดํฌํฌ์ธํธ ์ ์ฅํ๊ณ ๋ถ๋ฌ์ค๊ธฐ | |
| ํ๋ จ ์ค ๋ฌธ์ ๊ฐ ๋ฐ์ํ ๊ฒฝ์ฐ๋ฅผ ๋๋นํ์ฌ ์ฒดํฌํฌ์ธํธ๋ฅผ ์ ๊ธฐ์ ์ผ๋ก ์ ์ฅํ๋ ๊ฒ์ด ์ข์ต๋๋ค. ์ฒดํฌํฌ์ธํธ๋ฅผ ์ ์ฅํ๋ ค๋ฉด ํ์ต ์คํฌ๋ฆฝํธ์ ๋ค์ ์ธ์๋ฅผ ์ ๋ฌํฉ๋๋ค: | |
| ```bash | |
| --checkpointing_steps=500 | |
| ``` | |
| ์ ์ฒด ํ๋ จ ์ํ๋ 500์คํ ๋ง๋ค `output_dir`์ ํ์ ํด๋์ ์ ์ฅ๋๋ฉฐ, ํ์ต ์คํฌ๋ฆฝํธ์ `--resume_from_checkpoint` ์ธ์๋ฅผ ์ ๋ฌํจ์ผ๋ก์จ ์ฒดํฌํฌ์ธํธ๋ฅผ ๋ถ๋ฌ์ค๊ณ ํ๋ จ์ ์ฌ๊ฐํ ์ ์์ต๋๋ค. | |
| ```bash | |
| --resume_from_checkpoint="checkpoint-1500" | |
| ``` | |
| ## ํ์ธํ๋ | |
| ์ด์ ํ์ต ์คํฌ๋ฆฝํธ๋ฅผ ์์ํ ์ค๋น๊ฐ ๋์์ต๋๋ค! `--dataset_name` ์ธ์์ ํ์ธํ๋ํ ๋ฐ์ดํฐ์ ์ด๋ฆ์ ์ง์ ํ ๋ค์, `--output_dir` ์ธ์์ ์ง์ ๋ ๊ฒฝ๋ก๋ก ์ ์ฅํฉ๋๋ค. ๋ณธ์ธ๋ง์ ๋ฐ์ดํฐ์ ๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด, [ํ์ต์ฉ ๋ฐ์ดํฐ์ ๋ง๋ค๊ธฐ](create_dataset) ๊ฐ์ด๋๋ฅผ ์ฐธ์กฐํ์ธ์. | |
| ํ์ต ์คํฌ๋ฆฝํธ๋ `diffusion_pytorch_model.bin` ํ์ผ์ ์์ฑํ๊ณ , ๊ทธ๊ฒ์ ๋น์ ์ ๋ฆฌํฌ์งํ ๋ฆฌ์ ์ ์ฅํฉ๋๋ค. | |
| <Tip> | |
| ๐ก ์ ์ฒด ํ์ต์ V100 GPU 4๊ฐ๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ, 2์๊ฐ์ด ์์๋ฉ๋๋ค. | |
| </Tip> | |
| ์๋ฅผ ๋ค์ด, [Oxford Flowers](https://huggingface.co/datasets/huggan/flowers-102-categories) ๋ฐ์ดํฐ์ ์ ์ฌ์ฉํด ํ์ธํ๋ํ ๊ฒฝ์ฐ: | |
| ```bash | |
| accelerate launch train_unconditional.py \ | |
| --dataset_name="huggan/flowers-102-categories" \ | |
| --resolution=64 \ | |
| --output_dir="ddpm-ema-flowers-64" \ | |
| --train_batch_size=16 \ | |
| --num_epochs=100 \ | |
| --gradient_accumulation_steps=1 \ | |
| --learning_rate=1e-4 \ | |
| --lr_warmup_steps=500 \ | |
| --mixed_precision=no \ | |
| --push_to_hub | |
| ``` | |
| <div class="flex justify-center"> | |
| <img src="https://user-images.githubusercontent.com/26864830/180248660-a0b143d0-b89a-42c5-8656-2ebf6ece7e52.png"/> | |
| </div> | |
| [Pokemon](https://huggingface.co/datasets/huggan/pokemon) ๋ฐ์ดํฐ์ ์ ์ฌ์ฉํ ๊ฒฝ์ฐ: | |
| ```bash | |
| accelerate launch train_unconditional.py \ | |
| --dataset_name="huggan/pokemon" \ | |
| --resolution=64 \ | |
| --output_dir="ddpm-ema-pokemon-64" \ | |
| --train_batch_size=16 \ | |
| --num_epochs=100 \ | |
| --gradient_accumulation_steps=1 \ | |
| --learning_rate=1e-4 \ | |
| --lr_warmup_steps=500 \ | |
| --mixed_precision=no \ | |
| --push_to_hub | |
| ``` | |
| <div class="flex justify-center"> | |
| <img src="https://user-images.githubusercontent.com/26864830/180248200-928953b4-db38-48db-b0c6-8b740fe6786f.png"/> | |
| </div> | |
| ### ์ฌ๋ฌ๊ฐ์ GPU๋ก ํ๋ จํ๊ธฐ | |
| `accelerate`์ ์ฌ์ฉํ๋ฉด ์ํํ ๋ค์ค GPU ํ๋ จ์ด ๊ฐ๋ฅํฉ๋๋ค. `accelerate`์ ์ฌ์ฉํ์ฌ ๋ถ์ฐ ํ๋ จ์ ์คํํ๋ ค๋ฉด [์ฌ๊ธฐ](https://huggingface.co/docs/accelerate/basic_tutorials/launch) ์ง์นจ์ ๋ฐ๋ฅด์ธ์. ๋ค์์ ๋ช ๋ น์ด ์์ ์ ๋๋ค. | |
| ```bash | |
| accelerate launch --mixed_precision="fp16" --multi_gpu train_unconditional.py \ | |
| --dataset_name="huggan/pokemon" \ | |
| --resolution=64 --center_crop --random_flip \ | |
| --output_dir="ddpm-ema-pokemon-64" \ | |
| --train_batch_size=16 \ | |
| --num_epochs=100 \ | |
| --gradient_accumulation_steps=1 \ | |
| --use_ema \ | |
| --learning_rate=1e-4 \ | |
| --lr_warmup_steps=500 \ | |
| --mixed_precision="fp16" \ | |
| --logger="wandb" \ | |
| --push_to_hub | |
| ``` | |