| # Textual inversion | |
| [[open-in-colab]] | |
| [`StableDiffusionPipeline`]์ textual-inversion์ ์ง์ํ๋๋ฐ, ์ด๋ ๋ช ๊ฐ์ ์ํ ์ด๋ฏธ์ง๋ง์ผ๋ก stable diffusion๊ณผ ๊ฐ์ ๋ชจ๋ธ์ด ์๋ก์ด ์ปจ์ ์ ํ์ตํ ์ ์๋๋ก ํ๋ ๊ธฐ๋ฒ์ ๋๋ค. ์ด๋ฅผ ํตํด ์์ฑ๋ ์ด๋ฏธ์ง๋ฅผ ๋ ์ ์ ์ดํ๊ณ ํน์ ์ปจ์ ์ ๋ง๊ฒ ๋ชจ๋ธ์ ์กฐ์ ํ ์ ์์ต๋๋ค. ์ปค๋ฎค๋ํฐ์์ ๋ง๋ค์ด์ง ์ปจ์ ๋ค์ ์ปฌ๋ ์ ์ [Stable Diffusion Conceptualizer](https://huggingface.co/spaces/sd-concepts-library/stable-diffusion-conceptualizer)๋ฅผ ํตํด ๋น ๋ฅด๊ฒ ์ฌ์ฉํด๋ณผ ์ ์์ต๋๋ค. | |
| ์ด ๊ฐ์ด๋์์๋ Stable Diffusion Conceptualizer์์ ์ฌ์ ํ์ตํ ์ปจ์ ์ ์ฌ์ฉํ์ฌ textual-inversion์ผ๋ก ์ถ๋ก ์ ์คํํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ๋๋ฆฝ๋๋ค. textual-inversion์ผ๋ก ๋ชจ๋ธ์ ์๋ก์ด ์ปจ์ ์ ํ์ต์ํค๋ ๋ฐ ๊ด์ฌ์ด ์์ผ์๋ค๋ฉด, [Textual Inversion](./training/text_inversion) ํ๋ จ ๊ฐ์ด๋๋ฅผ ์ฐธ์กฐํ์ธ์. | |
| Hugging Face ๊ณ์ ์ผ๋ก ๋ก๊ทธ์ธํ์ธ์: | |
| ```py | |
| from huggingface_hub import notebook_login | |
| notebook_login() | |
| ``` | |
| ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋ถ๋ฌ์ค๊ณ ์์ฑ๋ ์ด๋ฏธ์ง๋ฅผ ์๊ฐํํ๊ธฐ ์ํ ๋์ฐ๋ฏธ ํจ์ `image_grid`๋ฅผ ๋ง๋ญ๋๋ค: | |
| ```py | |
| import os | |
| import torch | |
| import PIL | |
| from PIL import Image | |
| from diffusers import StableDiffusionPipeline | |
| from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer | |
| def image_grid(imgs, rows, cols): | |
| assert len(imgs) == rows * cols | |
| w, h = imgs[0].size | |
| grid = Image.new("RGB", size=(cols * w, rows * h)) | |
| grid_w, grid_h = grid.size | |
| for i, img in enumerate(imgs): | |
| grid.paste(img, box=(i % cols * w, i // cols * h)) | |
| return grid | |
| ``` | |
| Stable Diffusion๊ณผ [Stable Diffusion Conceptualizer](https://huggingface.co/spaces/sd-concepts-library/stable-diffusion-conceptualizer)์์ ์ฌ์ ํ์ต๋ ์ปจ์ ์ ์ ํํฉ๋๋ค: | |
| ```py | |
| pretrained_model_name_or_path = "stable-diffusion-v1-5/stable-diffusion-v1-5" | |
| repo_id_embeds = "sd-concepts-library/cat-toy" | |
| ``` | |
| ์ด์ ํ์ดํ๋ผ์ธ์ ๋ก๋ํ๊ณ ์ฌ์ ํ์ต๋ ์ปจ์ ์ ํ์ดํ๋ผ์ธ์ ์ ๋ฌํ ์ ์์ต๋๋ค: | |
| ```py | |
| pipeline = StableDiffusionPipeline.from_pretrained(pretrained_model_name_or_path, torch_dtype=torch.float16).to("cuda") | |
| pipeline.load_textual_inversion(repo_id_embeds) | |
| ``` | |
| ํน๋ณํ placeholder token '`<cat-toy>`'๋ฅผ ์ฌ์ฉํ์ฌ ์ฌ์ ํ์ต๋ ์ปจ์ ์ผ๋ก ํ๋กฌํํธ๋ฅผ ๋ง๋ค๊ณ , ์์ฑํ ์ํ์ ์์ ์ด๋ฏธ์ง ํ์ ์๋ฅผ ์ ํํฉ๋๋ค: | |
| ```py | |
| prompt = "a grafitti in a favela wall with a <cat-toy> on it" | |
| num_samples = 2 | |
| num_rows = 2 | |
| ``` | |
| ๊ทธ๋ฐ ๋ค์ ํ์ดํ๋ผ์ธ์ ์คํํ๊ณ , ์์ฑ๋ ์ด๋ฏธ์ง๋ค์ ์ ์ฅํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ ์ฒ์์ ๋ง๋ค์๋ ๋์ฐ๋ฏธ ํจ์ `image_grid`๋ฅผ ์ฌ์ฉํ์ฌ ์์ฑ ๊ฒฐ๊ณผ๋ค์ ์๊ฐํํฉ๋๋ค. ์ด ๋ `num_inference_steps`์ `guidance_scale`๊ณผ ๊ฐ์ ๋งค๊ฐ ๋ณ์๋ค์ ์กฐ์ ํ์ฌ, ์ด๊ฒ๋ค์ด ์ด๋ฏธ์ง ํ์ง์ ์ด๋ ํ ์ํฅ์ ๋ฏธ์น๋์ง๋ฅผ ์์ ๋กญ๊ฒ ํ์ธํด๋ณด์๊ธฐ ๋ฐ๋๋๋ค. | |
| ```py | |
| all_images = [] | |
| for _ in range(num_rows): | |
| images = pipe(prompt, num_images_per_prompt=num_samples, num_inference_steps=50, guidance_scale=7.5).images | |
| all_images.extend(images) | |
| grid = image_grid(all_images, num_samples, num_rows) | |
| grid | |
| ``` | |
| <div class="flex justify-center"> | |
| <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/textual_inversion_inference.png"> | |
| </div> | |