# ๐Ÿงจ Diffusers ํ•™์Šต ์˜ˆ์‹œ ์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ๋‹ค์–‘ํ•œ ์œ ์ฆˆ์ผ€์ด์Šค๋“ค์— ๋Œ€ํ•œ ์˜ˆ์ œ ์ฝ”๋“œ๋“ค์„ ํ†ตํ•ด ์–ด๋–ป๊ฒŒํ•˜๋ฉด ํšจ๊ณผ์ ์œผ๋กœ `diffusers` ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์„๊นŒ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. **Note**: ํ˜น์‹œ ์˜คํ”ผ์…œํ•œ ์˜ˆ์‹œ์ฝ”๋“œ๋ฅผ ์ฐพ๊ณ  ์žˆ๋‹ค๋ฉด, [์—ฌ๊ธฐ](https://github.com/huggingface/diffusers/tree/main/src/diffusers/pipelines)๋ฅผ ์ฐธ๊ณ ํ•ด๋ณด์„ธ์š”! ์—ฌ๊ธฐ์„œ ๋‹ค๋ฃฐ ์˜ˆ์‹œ๋“ค์€ ๋‹ค์Œ์„ ์ง€ํ–ฅํ•ฉ๋‹ˆ๋‹ค. - **์†์‰ฌ์šด ๋””ํŽœ๋˜์‹œ ์„ค์น˜** (Self-contained) : ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉ๋  ์˜ˆ์‹œ ์ฝ”๋“œ๋“ค์˜ ๋””ํŽœ๋˜์‹œ ํŒจํ‚ค์ง€๋“ค์€ ์ „๋ถ€ `pip install` ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์„ค์น˜ ๊ฐ€๋Šฅํ•œ ํŒจํ‚ค์ง€๋“ค์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ ์นœ์ ˆํ•˜๊ฒŒ `requirements.txt` ํŒŒ์ผ์— ํ•ด๋‹น ํŒจํ‚ค์ง€๋“ค์ด ๋ช…์‹œ๋˜์–ด ์žˆ์–ด, `pip install -r requirements.txt`๋กœ ๊ฐ„ํŽธํ•˜๊ฒŒ ํ•ด๋‹น ๋””ํŽœ๋˜์‹œ๋“ค์„ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ์‹œ: [train_unconditional.py](https://github.com/huggingface/diffusers/blob/main/examples/unconditional_image_generation/train_unconditional.py), [requirements.txt](https://github.com/huggingface/diffusers/blob/main/examples/unconditional_image_generation/requirements.txt) - **์†์‰ฌ์šด ์ˆ˜์ •** (Easy-to-tweak) : ์ €ํฌ๋Š” ๊ฐ€๋Šฅํ•˜๋ฉด ๋งŽ์€ ์œ ์ฆˆ ์ผ€์ด์Šค๋“ค์„ ์ œ๊ณตํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์˜ˆ์‹œ๋Š” ๊ฒฐ๊ตญ ๊ทธ์ € ์˜ˆ์‹œ๋ผ๋Š” ์ ๋“ค ๊ธฐ์–ตํ•ด์ฃผ์„ธ์š”. ์—ฌ๊ธฐ์„œ ์ œ๊ณต๋˜๋Š” ์˜ˆ์‹œ์ฝ”๋“œ๋“ค์„ ๊ทธ์ € ๋‹จ์ˆœํžˆ ๋ณต์‚ฌ-๋ถ™ํ˜€๋„ฃ๊ธฐํ•˜๋Š” ์‹์œผ๋กœ๋Š” ์—ฌ๋Ÿฌ๋ถ„์ด ๋งˆ์ฃผํ•œ ๋ฌธ์ œ๋“ค์„ ์†์‰ฝ๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆœ ์—†์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋งํ•ด ์–ด๋Š ์ •๋„๋Š” ์—ฌ๋Ÿฌ๋ถ„์˜ ์ƒํ™ฉ๊ณผ ๋‹ˆ์ฆˆ์— ๋งž์ถฐ ์ฝ”๋“œ๋ฅผ ์ผ์ • ๋ถ€๋ถ„ ๊ณ ์ณ๋‚˜๊ฐ€์•ผ ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋Œ€๋ถ€๋ถ„์˜ ํ•™์Šต ์˜ˆ์‹œ๋“ค์€ ๋ฐ์ดํ„ฐ์˜ ์ „์ฒ˜๋ฆฌ ๊ณผ์ •๊ณผ ํ•™์Šต ๊ณผ์ •์— ๋Œ€ํ•œ ์ฝ”๋“œ๋“ค์„ ํ•จ๊ป˜ ์ œ๊ณตํ•จ์œผ๋กœ์จ, ์‚ฌ์šฉ์ž๊ฐ€ ๋‹ˆ์ฆˆ์— ๋งž๊ฒŒ ์†์‰ฌ์šด ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋•๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. - **์ž…๋ฌธ์ž ์นœํ™”์ ์ธ** (Beginner-friendly) : ์ด๋ฒˆ ์ฑ•ํ„ฐ๋Š” diffusion ๋ชจ๋ธ๊ณผ `diffusers` ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋Œ€ํ•œ ์ „๋ฐ˜์ ์ธ ์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•ด ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ diffusion ๋ชจ๋ธ์— ๋Œ€ํ•œ ์ตœ์‹  SOTA (state-of-the-art) ๋ฐฉ๋ฒ•๋ก ๋“ค ๊ฐ€์šด๋ฐ์„œ๋„, ์ž…๋ฌธ์ž์—๊ฒŒ๋Š” ๋งŽ์ด ์–ด๋ ค์šธ ์ˆ˜ ์žˆ๋‹ค๊ณ  ํŒ๋‹จ๋˜๋ฉด, ํ•ด๋‹น ๋ฐฉ๋ฒ•๋ก ๋“ค์€ ์—ฌ๊ธฐ์„œ ๋‹ค๋ฃจ์ง€ ์•Š์œผ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. - **ํ•˜๋‚˜์˜ ํƒœ์Šคํฌ๋งŒ ํฌํ•จํ•  ๊ฒƒ**(One-purpose-only): ์—ฌ๊ธฐ์„œ ๋‹ค๋ฃฐ ์˜ˆ์‹œ๋“ค์€ ํ•˜๋‚˜์˜ ํƒœ์Šคํฌ๋งŒ ํฌํ•จํ•˜๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ฌผ๋ก  ์ด๋ฏธ์ง€ ์ดˆํ•ด์ƒํ™”(super-resolution)์™€ ์ด๋ฏธ์ง€ ๋ณด์ •(modification)๊ณผ ๊ฐ™์€ ์œ ์‚ฌํ•œ ๋ชจ๋ธ๋ง ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ฐ–๋Š” ํƒœ์Šคํฌ๋“ค์ด ์กด์žฌํ•˜๊ฒ ์ง€๋งŒ, ํ•˜๋‚˜์˜ ์˜ˆ์ œ์— ํ•˜๋‚˜์˜ ํƒœ์Šคํฌ๋งŒ์„ ๋‹ด๋Š” ๊ฒƒ์ด ๋” ์ดํ•ดํ•˜๊ธฐ ์šฉ์ดํ•˜๋‹ค๊ณ  ํŒ๋‹จํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ €ํฌ๋Š” diffusion ๋ชจ๋ธ์˜ ๋Œ€ํ‘œ์ ์ธ ํƒœ์Šคํฌ๋“ค์„ ๋‹ค๋ฃจ๋Š” ๊ณต์‹ ์˜ˆ์ œ๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. *๊ณต์‹* ์˜ˆ์ œ๋Š” ํ˜„์žฌ ์ง„ํ–‰ํ˜•์œผ๋กœ `diffusers` ๊ด€๋ฆฌ์ž๋“ค(maintainers)์— ์˜ํ•ด ๊ด€๋ฆฌ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ €ํฌ๋Š” ์•ž์„œ ์ •์˜ํ•œ ์ €ํฌ์˜ ์ฒ ํ•™์„ ์—„๊ฒฉํ•˜๊ฒŒ ๋”ฐ๋ฅด๊ณ ์ž ๋…ธ๋ ฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜น์‹œ ์—ฌ๋Ÿฌ๋ถ„๊ป˜์„œ ์ด๋Ÿฌํ•œ ์˜ˆ์‹œ๊ฐ€ ๋ฐ˜๋“œ์‹œ ํ•„์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐ๋˜์‹ ๋‹ค๋ฉด, ์–ธ์ œ๋“ ์ง€ [Feature Request](https://github.com/huggingface/diffusers/issues/new?assignees=&labels=&template=feature_request.md&title=) ํ˜น์€ ์ง์ ‘ [Pull Request](https://github.com/huggingface/diffusers/compare)๋ฅผ ์ฃผ์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์ €ํฌ๋Š” ์–ธ์ œ๋‚˜ ํ™˜์˜์ž…๋‹ˆ๋‹ค! ํ•™์Šต ์˜ˆ์‹œ๋“ค์€ ๋‹ค์–‘ํ•œ ํƒœ์Šคํฌ๋“ค์— ๋Œ€ํ•ด diffusion ๋ชจ๋ธ์„ ์‚ฌ์ „ํ•™์Šต(pretrain)ํ•˜๊ฑฐ๋‚˜ ํŒŒ์ธํŠœ๋‹(fine-tuning)ํ•˜๋Š” ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ํ˜„์žฌ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜ˆ์ œ๋“ค์„ ์ง€์›ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. - [Unconditional Training](./unconditional_training) - [Text-to-Image Training](./text2image) - [Text Inversion](./text_inversion) - [Dreambooth](./dreambooth) memory-efficient attention ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด, ๊ฐ€๋Šฅํ•˜๋ฉด [xFormers](../optimization/xformers)๋ฅผ ์„ค์น˜ํ•ด์ฃผ์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ํ•™์Šต ์†๋„๋ฅผ ๋Š˜๋ฆฌ๊ณ  ๋ฉ”๋ชจ๋ฆฌ์— ๋Œ€ํ•œ ๋ถ€๋‹ด์„ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. | Task | ๐Ÿค— Accelerate | ๐Ÿค— Datasets | Colab |---|---|:---:|:---:| | [**Unconditional Image Generation**](./unconditional_training) | โœ… | โœ… | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/training_example.ipynb) | [**Text-to-Image fine-tuning**](./text2image) | โœ… | โœ… | | [**Textual Inversion**](./text_inversion) | โœ… | - | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/sd_textual_inversion_training.ipynb) | [**Dreambooth**](./dreambooth) | โœ… | - | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/notebooks/blob/main/diffusers/sd_dreambooth_training.ipynb) | [**Training with LoRA**](./lora) | โœ… | - | - | | [**ControlNet**](./controlnet) | โœ… | โœ… | - | | [**InstructPix2Pix**](./instructpix2pix) | โœ… | โœ… | - | | [**Custom Diffusion**](./custom_diffusion) | โœ… | โœ… | - | ## ์ปค๋ฎค๋‹ˆํ‹ฐ ๊ณต์‹ ์˜ˆ์ œ ์™ธ์—๋„ **์ปค๋ฎค๋‹ˆํ‹ฐ ์˜ˆ์ œ** ์—ญ์‹œ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ์˜ˆ์ œ๋“ค์€ ์šฐ๋ฆฌ์˜ ์ปค๋ฎค๋‹ˆํ‹ฐ์— ์˜ํ•ด ๊ด€๋ฆฌ๋ฉ๋‹ˆ๋‹ค. ์ปค๋ฎค๋‹ˆํ‹ฐ ์˜ˆ์ฉจ๋Š” ํ•™์Šต ์˜ˆ์‹œ๋‚˜ ์ถ”๋ก  ํŒŒ์ดํ”„๋ผ์ธ์œผ๋กœ ๊ตฌ์„ฑ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ปค๋ฎค๋‹ˆํ‹ฐ ์˜ˆ์‹œ๋“ค์˜ ๊ฒฝ์šฐ, ์•ž์„œ ์ •์˜ํ–ˆ๋˜ ์ฒ ํ•™๋“ค์„ ์ข€ ๋” ๊ด€๋Œ€ํ•˜๊ฒŒ ์ ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ด๋Ÿฌํ•œ ์ปค๋ฎค๋‹ˆํ‹ฐ ์˜ˆ์‹œ๋“ค์˜ ๊ฒฝ์šฐ, ๋ชจ๋“  ์ด์Šˆ๋“ค์— ๋Œ€ํ•œ ์œ ์ง€๋ณด์ˆ˜๋ฅผ ๋ณด์žฅํ•  ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค. ์œ ์šฉํ•˜๊ธด ํ•˜์ง€๋งŒ, ์•„์ง์€ ๋Œ€์ค‘์ ์ด์ง€ ๋ชปํ•˜๊ฑฐ๋‚˜ ์ €ํฌ์˜ ์ฒ ํ•™์— ๋ถ€ํ•ฉํ•˜์ง€ ์•Š๋Š” ์˜ˆ์ œ๋“ค์€ [community examples](https://github.com/huggingface/diffusers/tree/main/examples/community) ํด๋”์— ๋‹ด๊ธฐ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. **Note**: ์ปค๋ฎค๋‹ˆํ‹ฐ ์˜ˆ์ œ๋Š” `diffusers`์— ๊ธฐ์—ฌ(contribution)๋ฅผ ํฌ๋งํ•˜๋Š” ๋ถ„๋“ค์—๊ฒŒ [์•„์ฃผ ์ข‹์€ ๊ธฐ์—ฌ ์ˆ˜๋‹จ](https://github.com/huggingface/diffusers/issues?q=is%3Aopen+is%3Aissue+label%3A%22good+first+issue%22)์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ## ์ฃผ๋ชฉํ•  ์‚ฌํ•ญ๋“ค ์ตœ์‹  ๋ฒ„์ „์˜ ์˜ˆ์‹œ ์ฝ”๋“œ๋“ค์˜ ์„ฑ๊ณต์ ์ธ ๊ตฌ๋™์„ ๋ณด์žฅํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”, ๋ฐ˜๋“œ์‹œ **์†Œ์Šค์ฝ”๋“œ๋ฅผ ํ†ตํ•ด `diffusers`๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•˜๋ฉฐ,** ํ•ด๋‹น ์˜ˆ์‹œ ์ฝ”๋“œ๋“ค์ด ์š”๊ตฌํ•˜๋Š” ๋””ํŽœ๋˜์‹œ๋“ค ์—ญ์‹œ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์ƒˆ๋กœ์šด ๊ฐ€์ƒ ํ™˜๊ฒฝ์„ ๊ตฌ์ถ•ํ•˜๊ณ  ๋‹ค์Œ์˜ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ```bash git clone https://github.com/huggingface/diffusers cd diffusers pip install . ``` ๊ทธ ๋‹ค์Œ `cd` ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ์˜ˆ์ œ ๋””๋ ‰ํ† ๋ฆฌ์— ์ ‘๊ทผํ•ด์„œ ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ```bash pip install -r requirements.txt ```