Instructions to use lsmpp/kontextrefiner with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use lsmpp/kontextrefiner with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("lsmpp/kontextrefiner", dtype=torch.bfloat16, device_map="cuda") prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] - Notebooks
- Google Colab
- Kaggle
File size: 5,599 Bytes
1b7e5b9 | 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 2025 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
```
์์ฑ๋ ์ด๋ฏธ์ง:

์ฌ์ง์์ ์ ์ ์๋ฏ์ด, "๊ณต"์ ์ด๋ฏธ์ง์ ์์ต๋๋ค. ์ด ๋ถ๋ถ์ ๊ฐ์กฐํด ๋ณผ๊น์!
๋จผ์ `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
```
์ด์ "๊ณต"์ด ์๋ ๊ทธ๋ฆผ์ ์ถ๋ ฅํ ์ ์์ต๋๋ค!

๋ง์ฐฌ๊ฐ์ง๋ก `--` ์ ๋ฏธ์ฌ๋ฅผ ๋จ์ด์ ์ฌ์ฉํ์ฌ ๋ฌธ์ฅ์ ์ผ๋ถ๋ฅผ ๊ฐ์กฐํ์ง ์์ ์ ์์ต๋๋ค. ํ๋ฒ ์๋ํด ๋ณด์ธ์!
์ฆ๊ฒจ์ฐพ๋ ํ์ดํ๋ผ์ธ์ `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) ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ์ธ์.
|