File size: 5,599 Bytes
87d40d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<!--Copyright 2024 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.
-->

# ํ”„๋กฌํ”„ํŠธ์— ๊ฐ€์ค‘์น˜ ๋ถ€์—ฌํ•˜๊ธฐ

[[open-in-colab]]

ํ…์ŠคํŠธ ๊ฐ€์ด๋“œ ๊ธฐ๋ฐ˜์˜ diffusion ๋ชจ๋ธ์€ ์ฃผ์–ด์ง„ ํ…์ŠคํŠธ ํ”„๋กฌํ”„ํŠธ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
ํ…์ŠคํŠธ ํ”„๋กฌํ”„ํŠธ์—๋Š” ๋ชจ๋ธ์ด ์ƒ์„ฑํ•ด์•ผ ํ•˜๋Š” ์—ฌ๋Ÿฌ ๊ฐœ๋…์ด ํฌํ•จ๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ ํ”„๋กฌํ”„ํŠธ์˜ ํŠน์ • ๋ถ€๋ถ„์— ๊ฐ€์ค‘์น˜๋ฅผ ๋ถ€์—ฌํ•˜๋Š” ๊ฒƒ์ด ๋ฐ”๋žŒ์งํ•œ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Šต๋‹ˆ๋‹ค.

Diffusion ๋ชจ๋ธ์€ ๋ฌธ๋งฅํ™”๋œ ํ…์ŠคํŠธ ์ž„๋ฒ ๋”ฉ์œผ๋กœ diffusion ๋ชจ๋ธ์˜ cross attention ๋ ˆ์ด์–ด๋ฅผ ์กฐ์ ˆํ•จ์œผ๋กœ์จ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.
([๋” ๋งŽ์€ ์ •๋ณด๋ฅผ ์œ„ํ•œ Stable Diffusion Guide](https://huggingface.co/docs/optimum-neuron/main/en/package_reference/modeling#stable-diffusion)๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”).
๋”ฐ๋ผ์„œ ํ”„๋กฌํ”„ํŠธ์˜ ํŠน์ • ๋ถ€๋ถ„์„ ๊ฐ•์กฐํ•˜๋Š”(๋˜๋Š” ๊ฐ•์กฐํ•˜์ง€ ์•Š๋Š”) ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ ํ”„๋กฌํ”„ํŠธ์˜ ๊ด€๋ จ ๋ถ€๋ถ„์— ํ•ด๋‹นํ•˜๋Š” ํ…์ŠคํŠธ ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ์˜ ํฌ๊ธฐ๋ฅผ ๋Š˜๋ฆฌ๊ฑฐ๋‚˜ ์ค„์ด๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
์ด๊ฒƒ์€ "ํ”„๋กฌํ”„ํŠธ ๊ฐ€์ค‘์น˜ ๋ถ€์—ฌ" ๋ผ๊ณ  ํ•˜๋ฉฐ, ์ปค๋ฎค๋‹ˆํ‹ฐ์—์„œ ๊ฐ€์žฅ ์š”๊ตฌํ•˜๋Š” ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค.([์ด๊ณณ](https://github.com/huggingface/diffusers/issues/2431)์˜ issue๋ฅผ ๋ณด์„ธ์š” ).

## Diffusers์—์„œ ํ”„๋กฌํ”„ํŠธ ๊ฐ€์ค‘์น˜ ๋ถ€์—ฌํ•˜๋Š” ๋ฐฉ๋ฒ•

์šฐ๋ฆฌ๋Š” `diffusers`์˜ ์—ญํ• ์ด ๋‹ค๋ฅธ ํ”„๋กœ์ ํŠธ๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋Š” ํ•„์ˆ˜์ ์ธ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” toolbex๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.
[InvokeAI](https://github.com/invoke-ai/InvokeAI) ๋‚˜ [diffuzers](https://github.com/abhishekkrthakur/diffuzers) ๊ฐ™์€ ๊ฐ•๋ ฅํ•œ UI๋ฅผ ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
ํ”„๋กฌํ”„ํŠธ๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ง€์›ํ•˜๊ธฐ ์œ„ํ•ด, `diffusers` ๋Š”
[StableDiffusionPipeline](https://huggingface.co/docs/diffusers/v0.18.2/en/api/pipelines/stable_diffusion/text2img#diffusers.StableDiffusionPipeline)์™€ ๊ฐ™์€
๋งŽ์€ ํŒŒ์ดํ”„๋ผ์ธ์— [prompt_embeds](https://huggingface.co/docs/diffusers/v0.14.0/en/api/pipelines/stable_diffusion/text2img#diffusers.StableDiffusionPipeline.__call__.prompt_embeds)
์ธ์ˆ˜๋ฅผ ๋…ธ์ถœ์‹œ์ผœ, "prompt-weighted"/์ถ•์ฒ™๋œ ํ…์ŠคํŠธ ์ž„๋ฒ ๋”ฉ์„ ํŒŒ์ดํ”„๋ผ์ธ์— ๋ฐ”๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

[Compel ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ](https://github.com/damian0815/compel)๋Š” ํ”„๋กฌํ”„ํŠธ์˜ ์ผ๋ถ€๋ฅผ ๊ฐ•์กฐํ•˜๊ฑฐ๋‚˜ ๊ฐ•์กฐํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋Š” ์‰ฌ์šด ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
์ž„๋ฒ ๋”ฉ์„ ์ง์ ‘ ์ค€๋น„ํ•˜๋Š” ๊ฒƒ ๋Œ€์‹  ์ด ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ฐ•๋ ฅํžˆ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค.

๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
๋‹ค์Œ๊ณผ ๊ฐ™์ด `"๊ณต์„ ๊ฐ–๊ณ  ๋…ธ๋Š” ๋ถ‰์€์ƒ‰ ๊ณ ์–‘์ด"` ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค:

```py
from diffusers import StableDiffusionPipeline, UniPCMultistepScheduler

pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)

prompt = "a red cat playing with a ball"

generator = torch.Generator(device="cpu").manual_seed(33)

image = pipe(prompt, generator=generator, num_inference_steps=20).images[0]
image
```

์ƒ์„ฑ๋œ ์ด๋ฏธ์ง€:

![img](https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/compel/forest_0.png)

์‚ฌ์ง„์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด, "๊ณต"์€ ์ด๋ฏธ์ง€์— ์—†์Šต๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„์„ ๊ฐ•์กฐํ•ด ๋ณผ๊นŒ์š”!

๋จผ์ € `compel` ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค:

```sh
pip install compel
```

๊ทธ๋Ÿฐ ๋‹ค์Œ์—๋Š” `Compel` ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค:

```py
from compel import Compel

compel_proc = Compel(tokenizer=pipe.tokenizer, text_encoder=pipe.text_encoder)
```

์ด์ œ `"++"` ๋ฅผ ์‚ฌ์šฉํ•ด์„œ "๊ณต" ์„ ๊ฐ•์กฐํ•ด ๋ด…์‹œ๋‹ค:

```py
prompt = "a red cat playing with a ball++"
```

๊ทธ๋ฆฌ๊ณ  ์ด ํ”„๋กฌํ”„ํŠธ๋ฅผ ํŒŒ์ดํ”„๋ผ์ธ์— ๋ฐ”๋กœ ์ „๋‹ฌํ•˜์ง€ ์•Š๊ณ , `compel_proc` ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฒ˜๋ฆฌํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค:

```py
prompt_embeds = compel_proc(prompt)
```

ํŒŒ์ดํ”„๋ผ์ธ์— `prompt_embeds` ๋ฅผ ๋ฐ”๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

```py
generator = torch.Generator(device="cpu").manual_seed(33)

images = pipe(prompt_embeds=prompt_embeds, generator=generator, num_inference_steps=20).images[0]
image
```

์ด์ œ "๊ณต"์ด ์žˆ๋Š” ๊ทธ๋ฆผ์„ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

![img](https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/compel/forest_1.png)

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ `--` ์ ‘๋ฏธ์‚ฌ๋ฅผ ๋‹จ์–ด์— ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์žฅ์˜ ์ผ๋ถ€๋ฅผ ๊ฐ•์กฐํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ๋ฒˆ ์‹œ๋„ํ•ด ๋ณด์„ธ์š”!

์ฆ๊ฒจ์ฐพ๋Š” ํŒŒ์ดํ”„๋ผ์ธ์— `prompt_embeds` ์ž…๋ ฅ์ด ์—†๋Š” ๊ฒฝ์šฐ issue๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์ฃผ์„ธ์š”.
Diffusers ํŒ€์€ ์ตœ๋Œ€ํ•œ ๋Œ€์‘ํ•˜๋ ค๊ณ  ๋…ธ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

Compel 1.1.6 ๋Š” textual inversions์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹จ์ˆœํ™”ํ•˜๋Š” ์œ ํ‹ฐ๋ฆดํ‹ฐ ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
`DiffusersTextualInversionManager`๋ฅผ ์ธ์Šคํ„ด์Šคํ™” ํ•œ ํ›„ ์ด๋ฅผ Compel init์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค:

```
textual_inversion_manager = DiffusersTextualInversionManager(pipe)
compel = Compel(
    tokenizer=pipe.tokenizer,
    text_encoder=pipe.text_encoder,
    textual_inversion_manager=textual_inversion_manager)
```

๋” ๋งŽ์€ ์ •๋ณด๋ฅผ ์–ป๊ณ  ์‹ถ๋‹ค๋ฉด [compel](https://github.com/damian0815/compel) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”.