hungchiayu1
initial commit
ffead1e
<!--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.
-->
# ํ›‘์–ด๋ณด๊ธฐ
๐Ÿงจ Diffusers๋กœ ๋น ๋ฅด๊ฒŒ ์‹œ์ž‘ํ•˜๊ณ  ์‹คํ–‰ํ•˜์„ธ์š”!
์ด ํ›‘์–ด๋ณด๊ธฐ๋Š” ์—ฌ๋Ÿฌ๋ถ„์ด ๊ฐœ๋ฐœ์ž, ์ผ๋ฐ˜์‚ฌ์šฉ์ž ์ƒ๊ด€์—†์ด ์‹œ์ž‘ํ•˜๋Š” ๋ฐ ๋„์›€์„ ์ฃผ๋ฉฐ, ์ถ”๋ก ์„ ์œ„ํ•ด [`DiffusionPipeline`] ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.
์‹œ์ž‘ํ•˜๊ธฐ์— ์•ž์„œ์„œ, ํ•„์š”ํ•œ ๋ชจ๋“  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”:
```bash
pip install --upgrade diffusers accelerate transformers
```
- [`accelerate`](https://huggingface.co/docs/accelerate/index)์€ ์ถ”๋ก  ๋ฐ ํ•™์Šต์„ ์œ„ํ•œ ๋ชจ๋ธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์†๋„๋ฅผ ๋†’์ž…๋‹ˆ๋‹ค.
- [`transformers`](https://huggingface.co/docs/transformers/index)๋Š” [Stable Diffusion](https://huggingface.co/docs/diffusers/api/pipelines/stable_diffusion/overview)๊ณผ ๊ฐ™์ด ๊ฐ€์žฅ ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๋Š” ํ™•์‚ฐ ๋ชจ๋ธ์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.
## DiffusionPipeline
[`DiffusionPipeline`]์€ ์ถ”๋ก ์„ ์œ„ํ•ด ์‚ฌ์ „ํ•™์Šต๋œ ํ™•์‚ฐ ์‹œ์Šคํ…œ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ€์žฅ ์‰ฌ์šด ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋‹ค์–‘ํ•œ ์–‘์‹์˜ ๋งŽ์€ ์ž‘์—…์— [`DiffusionPipeline`]์„ ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ง€์›๋˜๋Š” ์ž‘์—…์€ ์•„๋ž˜์˜ ํ‘œ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”:
| **Task** | **Description** | **Pipeline**
|------------------------------|--------------------------------------------------------------------------------------------------------------|-----------------|
| Unconditional Image Generation | ๊ฐ€์šฐ์‹œ์•ˆ ๋…ธ์ด์ฆˆ์—์„œ ์ด๋ฏธ์ง€ ์ƒ์„ฑ | [unconditional_image_generation](./using-diffusers/unconditional_image_generation`) |
| Text-Guided Image Generation | ํ…์ŠคํŠธ ํ”„๋กฌํ”„ํŠธ๋กœ ์ด๋ฏธ์ง€ ์ƒ์„ฑ | [conditional_image_generation](./using-diffusers/conditional_image_generation) |
| Text-Guided Image-to-Image Translation | ํ…์ŠคํŠธ ํ”„๋กฌํ”„ํŠธ์— ๋”ฐ๋ผ ์ด๋ฏธ์ง€ ์กฐ์ • | [img2img](./using-diffusers/img2img) |
| Text-Guided Image-Inpainting | ๋งˆ์Šคํฌ ๋ฐ ํ…์ŠคํŠธ ํ”„๋กฌํ”„ํŠธ๊ฐ€ ์ฃผ์–ด์ง„ ์ด๋ฏธ์ง€์˜ ๋งˆ์Šคํ‚น๋œ ๋ถ€๋ถ„์„ ์ฑ„์šฐ๊ธฐ | [inpaint](./using-diffusers/inpaint) |
| Text-Guided Depth-to-Image Translation | ๊นŠ์ด ์ถ”์ •์„ ํ†ตํ•ด ๊ตฌ์กฐ๋ฅผ ์œ ์ง€ํ•˜๋ฉด์„œ ํ…์ŠคํŠธ ํ”„๋กฌํ”„ํŠธ์— ๋”ฐ๋ผ ์ด๋ฏธ์ง€์˜ ์ผ๋ถ€๋ฅผ ์กฐ์ • | [depth2image](./using-diffusers/depth2image) |
ํ™•์‚ฐ ํŒŒ์ดํ”„๋ผ์ธ์ด ๋‹ค์–‘ํ•œ ์ž‘์—…์— ๋Œ€ํ•ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€๋Š” [**Using Diffusers**](./using-diffusers/overview)๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”.
์˜ˆ๋ฅผ๋“ค์–ด, [`DiffusionPipeline`] ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์‹œ์ž‘ํ•˜๊ณ , ๋‹ค์šด๋กœ๋“œํ•˜๋ ค๋Š” ํŒŒ์ดํ”„๋ผ์ธ ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
๋ชจ๋“  [Diffusers' checkpoint](https://huggingface.co/models?library=diffusers&sort=downloads)์— ๋Œ€ํ•ด [`DiffusionPipeline`]์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ, ์ด ๊ฐ€์ด๋“œ์—์„œ๋Š” [Stable Diffusion](https://huggingface.co/CompVis/stable-diffusion)์„ ์‚ฌ์šฉํ•˜์—ฌ text-to-image๋ฅผ ํ•˜๋Š”๋ฐ [`DiffusionPipeline`]์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
[Stable Diffusion](https://huggingface.co/CompVis/stable-diffusion) ๊ธฐ๋ฐ˜ ๋ชจ๋ธ์„ ์‹คํ–‰ํ•˜๊ธฐ ์ „์— [license](https://huggingface.co/spaces/CompVis/stable-diffusion-license)๋ฅผ ์ฃผ์˜ ๊นŠ๊ฒŒ ์ฝ์œผ์„ธ์š”.
์ด๋Š” ๋ชจ๋ธ์˜ ํ–ฅ์ƒ๋œ ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๊ธฐ๋Šฅ๊ณผ ์ด๊ฒƒ์œผ๋กœ ์ƒ์„ฑ๋  ์ˆ˜ ์žˆ๋Š” ์œ ํ•ดํ•œ ์ฝ˜ํ…์ธ  ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์„ ํƒํ•œ Stable Diffusion ๋ชจ๋ธ(*์˜ˆ*: [`runwayml/stable-diffusion-v1-5`](https://huggingface.co/runwayml/stable-diffusion-v1-5))๋กœ ์ด๋™ํ•˜์—ฌ ๋ผ์ด์„ผ์Šค๋ฅผ ์ฝ์œผ์„ธ์š”.
๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ชจ๋ธ์„ ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:
```python
>>> from diffusers import DiffusionPipeline
>>> pipeline = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
```
[`DiffusionPipeline`]์€ ๋ชจ๋“  ๋ชจ๋ธ๋ง, ํ† ํฐํ™” ๋ฐ ์Šค์ผ€์ค„๋ง ๊ตฌ์„ฑ์š”์†Œ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ์บ์‹œํ•ฉ๋‹ˆ๋‹ค.
๋ชจ๋ธ์€ ์•ฝ 14์–ต๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ GPU์—์„œ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.
PyTorch์—์„œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ƒ์„ฑ๊ธฐ ๊ฐ์ฒด๋ฅผ GPU๋กœ ์˜ฎ๊ธธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
```python
>>> pipeline.to("cuda")
```
์ด์ œ `pipeline`์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:
```python
>>> image = pipeline("An image of a squirrel in Picasso style").images[0]
```
์ถœ๋ ฅ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ [PIL Image object](https://pillow.readthedocs.io/en/stable/reference/Image.html?highlight=image#the-image-class)๋กœ ๋ž˜ํ•‘๋ฉ๋‹ˆ๋‹ค.
๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ด๋ฏธ์ง€๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:
```python
>>> image.save("image_of_squirrel_painting.png")
```
**์ฐธ๊ณ **: ๋‹ค์Œ์„ ํ†ตํ•ด ๊ฐ€์ค‘์น˜๋ฅผ ๋‹ค์šด๋กœ๋“œํ•˜์—ฌ ๋กœ์ปฌ์—์„œ ํŒŒ์ดํ”„๋ผ์ธ์„ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค:
```
git lfs install
git clone https://huggingface.co/runwayml/stable-diffusion-v1-5
```
๊ทธ๋ฆฌ๊ณ  ์ €์žฅ๋œ ๊ฐ€์ค‘์น˜๋ฅผ ํŒŒ์ดํ”„๋ผ์ธ์— ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.
```python
>>> pipeline = DiffusionPipeline.from_pretrained("./stable-diffusion-v1-5")
```
ํŒŒ์ดํ”„๋ผ์ธ ์‹คํ–‰์€ ๋™์ผํ•œ ๋ชจ๋ธ ์•„ํ‚คํ…์ฒ˜์ด๋ฏ€๋กœ ์œ„์˜ ์ฝ”๋“œ์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.
```python
>>> generator.to("cuda")
>>> image = generator("An image of a squirrel in Picasso style").images[0]
>>> image.save("image_of_squirrel_painting.png")
```
ํ™•์‚ฐ ์‹œ์Šคํ…œ์€ ๊ฐ๊ฐ ์žฅ์ ์ด ์žˆ๋Š” ์—ฌ๋Ÿฌ ๋‹ค๋ฅธ [schedulers](./api/schedulers/overview)์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ Stable Diffusion์€ `PNDMScheduler`๋กœ ์‹คํ–‰๋˜์ง€๋งŒ ๋‹ค๋ฅธ ์Šค์ผ€์ค„๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋งค์šฐ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. *์˜ˆ* [`EulerDiscreteScheduler`] ์Šค์ผ€์ค„๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋Š” ๊ฒฝ์šฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:
```python
>>> from diffusers import EulerDiscreteScheduler
>>> pipeline = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
>>> # change scheduler to Euler
>>> pipeline.scheduler = EulerDiscreteScheduler.from_config(pipeline.scheduler.config)
```
์Šค์ผ€์ค„๋Ÿฌ ๋ณ€๊ฒฝ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ [Using Schedulers](./using-diffusers/schedulers) ๊ฐ€์ด๋“œ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”.
[Stability AI's](https://stability.ai/)์˜ Stable Diffusion ๋ชจ๋ธ์€ ์ธ์ƒ์ ์ธ ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๋ชจ๋ธ์ด๋ฉฐ ํ…์ŠคํŠธ์—์„œ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ํ›จ์”ฌ ๋” ๋งŽ์€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” Stable Diffusion๋งŒ์„ ์œ„ํ•œ ์ „์ฒด ๋ฌธ์„œ ํŽ˜์ด์ง€๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค [link](./conceptual/stable_diffusion).
๋งŒ์•ฝ ๋” ์ ์€ ๋ฉ”๋ชจ๋ฆฌ, ๋” ๋†’์€ ์ถ”๋ก  ์†๋„, Mac๊ณผ ๊ฐ™์€ ํŠน์ • ํ•˜๋“œ์›จ์–ด ๋˜๋Š” ONNX ๋Ÿฐํƒ€์ž„์—์„œ ์‹คํ–‰๋˜๋„๋ก Stable Diffusion์„ ์ตœ์ ํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด ์ตœ์ ํ™” ํŽ˜์ด์ง€๋ฅผ ์‚ดํŽด๋ณด์„ธ์š”:
- [Optimized PyTorch on GPU](./optimization/fp16)
- [Mac OS with PyTorch](./optimization/mps)
- [ONNX](./optimization/onnx)
- [OpenVINO](./optimization/open_vino)
ํ™•์‚ฐ ๋ชจ๋ธ์„ ๋ฏธ์„ธ์กฐ์ •ํ•˜๊ฑฐ๋‚˜ ํ•™์Šต์‹œํ‚ค๋ ค๋ฉด, [**training section**](./training/overview)์„ ์‚ดํŽด๋ณด์„ธ์š”.
๋งˆ์ง€๋ง‰์œผ๋กœ, ์ƒ์„ฑ๋œ ์ด๋ฏธ์ง€๋ฅผ ๊ณต๊ฐœ์ ์œผ๋กœ ๋ฐฐํฌํ•  ๋•Œ ์‹ ์ค‘์„ ๊ธฐํ•ด ์ฃผ์„ธ์š” ๐Ÿค—.