Buckets:
์ธํ์ธํ
Stable Diffusion ๋ชจ๋ธ์ Stable Diffusion์ ์ฌ์ฉํ์ฌ ๋ง์คํฌ์ ํ ์คํธ ํ๋กฌํํธ๋ฅผ ์ ๊ณตํจ์ผ๋ก์จ ์ด๋ฏธ์ง์ ํน์ ๋ถ๋ถ์ ํธ์งํ ์ ์๋ inpainting์๋ ์ ์ฉํ ์ ์์ต๋๋ค.
ํ
์ธํ์ธํ ์ ์ํด ํน๋ณํ ํ์ธํ๋๋ ์ฒดํฌํฌ์ธํธ, ์๋ฅผ ๋ค์ด stable-diffusion-v1-5/stable-diffusion-inpainting์ ๊ฐ์ ์ฒดํฌํฌ์ธํธ๋ฅผ ์ด ํ์ดํ๋ผ์ธ๊ณผ ํจ๊ป ์ฌ์ฉํ๋ ๊ฒ์ด ๊ถ์ฅ๋ฉ๋๋ค. stable-diffusion-v1-5/stable-diffusion-v1-5์ ๊ฐ์ ๊ธฐ๋ณธ ํ ์คํธ-์ด๋ฏธ์ง Stable Diffusion ์ฒดํฌํฌ์ธํธ๋ ํธํ๋์ง๋ง ์ฑ๋ฅ์ด ๋ฎ์ ์ ์์ต๋๋ค.
์ค์ผ์ค๋ฌ์ ์๋์ ํ์ง ์ฌ์ด์ ํธ๋ ์ด๋์คํ๋ฅผ ํ์ํ๋ ๋ฐฉ๋ฒ๊ณผ ํ์ดํ๋ผ์ธ ๊ตฌ์ฑ ์์๋ฅผ ํจ์จ์ ์ผ๋ก ์ฌ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ฐฐ์ฐ๋ ค๋ฉด Stable Diffusion Tips ์น์ ์ ๊ผญ ํ์ธํ์ธ์!
ํน์ ์์ ์ ๊ณต์ ์ฒดํฌํฌ์ธํธ ์ค ํ๋๋ฅผ ์ฌ์ฉํ๋ ค๋ ๊ฒฝ์ฐ, CompVis ๋ฐ Stability AI Hub ์กฐ์ง์ ํ์ํด ๋ณด์ธ์!
StableDiffusionInpaintPipeline[[diffusers.StableDiffusionInpaintPipeline]]
diffusers.StableDiffusionInpaintPipeline[[diffusers.StableDiffusionInpaintPipeline]]
diffusers.StableDiffusionInpaintPipeline(vae: diffusers.models.autoencoders.autoencoder_kl.AutoencoderKL | diffusers.models.autoencoders.autoencoder_asym_kl.AsymmetricAutoencoderKL, text_encoder: CLIPTextModel, tokenizer: CLIPTokenizer, unet: UNet2DConditionModel, scheduler: KarrasDiffusionSchedulers, safety_checker: StableDiffusionSafetyChecker, feature_extractor: CLIPImageProcessorPil, image_encoder: CLIPVisionModelWithProjection = None, requires_safety_checker: bool = True)
๋งค๊ฐ๋ณ์:
vae ([AutoencoderKL, AsymmetricAutoencoderKL]) : ์ด๋ฏธ์ง๋ฅผ ์ ์ฌ ํํ์ผ๋ก ์ธ์ฝ๋ฉํ๊ณ ์ ์ฌ ํํ์์ ์ด๋ฏธ์ง๋ก ๋์ฝ๋ฉํ๋ Variational Auto-Encoder (VAE) ๋ชจ๋ธ.
text_encoder (CLIPTextModel) : ๊ณ ์ ๋ ํ
์คํธ ์ธ์ฝ๋ (clip-vit-large-patch14).
tokenizer (CLIPTokenizer) : ํ
์คํธ๋ฅผ ํ ํฐํํ๊ธฐ ์ํ CLIPTokenizer์
๋๋ค.
unet (UNet2DConditionModel) : ์ธ์ฝ๋ฉ๋ ์ด๋ฏธ์ง latents์ ๋
ธ์ด์ฆ๋ฅผ ์ ๊ฑฐํ๊ธฐ ์ํ UNet2DConditionModel.
scheduler (SchedulerMixin) : unet์ ํจ๊ป ์ฌ์ฉ๋์ด ์ธ์ฝ๋ฉ๋ ์ด๋ฏธ์ง latents์ ๋
ธ์ด์ฆ๋ฅผ ์ ๊ฑฐํ๋ ์ค์ผ์ค๋ฌ์
๋๋ค. DDIMScheduler, LMSDiscreteScheduler, ๋๋ PNDMScheduler ์ค ํ๋์ผ ์ ์์ต๋๋ค.
safety_checker (StableDiffusionSafetyChecker) : ์์ฑ๋ ์ด๋ฏธ์ง๊ฐ offensiveํ๊ฑฐ๋ ์ ํดํ ๊ฒ์ผ๋ก ๊ฐ์ฃผ๋ ์ ์๋์ง ์ถ์ ํ๋ ๋ถ๋ฅ ๋ชจ๋์
๋๋ค. ๋ชจ๋ธ์ ์ ์ฌ์ ์ ํด์ฑ์ ๋ํ ์์ธํ ๋ด์ฉ์ model card๋ฅผ ์ฐธ๊ณ ํ์ธ์.
feature_extractor (CLIPImageProcessor) : ์์ฑ๋ ์ด๋ฏธ์ง์์ ํน์ง์ ์ถ์ถํ๊ธฐ ์ํ CLIPImageProcessor. safety_checker์ ์
๋ ฅ์ผ๋ก ์ฌ์ฉ๋ฉ๋๋ค.
Stable Diffusion์ ์ฌ์ฉํ ํ ์คํธ ๊ธฐ๋ฐ ์ด๋ฏธ์ง ์ธํ์ธํ ์ ์ํ ํ์ดํ๋ผ์ธ์ ๋๋ค.
์ด ๋ชจ๋ธ์ DiffusionPipeline์์ ์์๋ฉ๋๋ค. ๋ชจ๋ ํ์ดํ๋ผ์ธ์ ๋ํด ๊ตฌํ๋ ์ผ๋ฐ์ ๋ฉ์๋(๋ค์ด๋ก๋, ์ ์ฅ, ํน์ ๋๋ฐ์ด์ค์์ ์คํ ๋ฑ)์ ๋ํด์๋ ์์ ํด๋์ค ๋ฌธ์๋ฅผ ํ์ธํ์ธ์.
ํ์ดํ๋ผ์ธ์ ๋ค์ ๋ก๋ฉ ๋ฉ์๋๋ ์์ํฉ๋๋ค:
- ํ ์คํธ ์ธ๋ฒ์ ์๋ฒ ๋ฉ์ ๋ก๋ํ๋ load_textual_inversion()
- LoRA ๊ฐ์ค์น๋ฅผ ๋ก๋ํ๋ load_lora_weights()
- LoRA ๊ฐ์ค์น๋ฅผ ์ ์ฅํ๋ save_lora_weights()
- IP Adapter๋ฅผ ๋ก๋ํ๊ธฐ ์ํ load_ip_adapter()
.ckptํ์ผ์ ๋ก๋ํ๊ธฐ ์ํ from_single_file()
ํธ์ถ[[diffusers.StableDiffusionInpaintPipeline.call]]
__call__(prompt: str | list[str] = None, image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, list[PIL.Image.Image], list[numpy.ndarray], list[torch.Tensor]] = None, mask_image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, list[PIL.Image.Image], list[numpy.ndarray], list[torch.Tensor]] = None, masked_image_latents: Tensor = None, height: int | None = None, width: int | None = None, padding_mask_crop: int | None = None, strength: float = 1.0, num_inference_steps: int = 50, timesteps: list[int] = None, sigmas: list[float] = None, guidance_scale: float = 7.5, negative_prompt: str | list[str] | None = None, num_images_per_prompt: int | None = 1, eta: float = 0.0, generator: typing.Union[torch.Generator, list[torch.Generator], NoneType] = None, latents: typing.Optional[torch.Tensor] = None, prompt_embeds: typing.Optional[torch.Tensor] = None, negative_prompt_embeds: typing.Optional[torch.Tensor] = None, ip_adapter_image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, list[PIL.Image.Image], list[numpy.ndarray], list[torch.Tensor], NoneType] = None, ip_adapter_image_embeds: list[torch.Tensor] | None = None, output_type: str | None = 'pil', return_dict: bool = True, cross_attention_kwargs: dict[str, typing.Any] | None = None, clip_skip: int = None, callback_on_step_end: typing.Union[typing.Callable[[int, int], NoneType], diffusers.callbacks.PipelineCallback, diffusers.callbacks.MultiPipelineCallbacks, NoneType] = None, callback_on_step_end_tensor_inputs: list[str] = ['latents'], **kwargs)
๋งค๊ฐ๋ณ์:
prompt (str ๋๋ list[str], ์ ํ ์ฌํญ) : ์ด๋ฏธ์ง ์์ฑ์ ์๋ดํ ํ๋กฌํํธ์
๋๋ค. ์ ์๋์ง ์์ ๊ฒฝ์ฐ prompt_embeds๋ฅผ ์ ๋ฌํด์ผ ํฉ๋๋ค.
image (torch.Tensor, PIL.Image.Image, np.ndarray, list[torch.Tensor], list[PIL.Image.Image], ๋๋ list[np.ndarray]) : Image, numpy ๋ฐฐ์ด ๋๋ ํ
์๋ก ํํ๋ inpaint ๋์ ์ด๋ฏธ์ง ๋ฐฐ์น์
๋๋ค(์ด๋ฏธ์ง์ ์ด๋ค ๋ถ๋ถ์ mask_image๋ก ๋ง์คํนํ์ฌ prompt์ ๋ฐ๋ผ ๋ค์ ์ฑ์ธ์ง ์ง์ ). numpy ๋ฐฐ์ด๊ณผ pytorch ํ
์ ๋ชจ๋์์ ์์ ๊ฐ ๋ฒ์๋ [0, 1]์
๋๋ค. ํ
์ ๋๋ ํ
์ ๋ชฉ๋ก์ธ ๊ฒฝ์ฐ ์์ shape์ (B, C, H, W) ๋๋ (C, H, W)์ฌ์ผ ํฉ๋๋ค. numpy ๋ฐฐ์ด ๋๋ ๋ฐฐ์ด ๋ชฉ๋ก์ธ ๊ฒฝ์ฐ ์์ shape์ (B, H, W, C) ๋๋ (H, W, C)์ฌ์ผ ํฉ๋๋ค. image๋ก ์ด๋ฏธ์ง latents๋ ๋ฐ์ ์ ์์ง๋ง, latents๋ฅผ ์ง์ ์ ๋ฌํ๋ ๊ฒฝ์ฐ ๋ค์ ์ธ์ฝ๋ฉ๋์ง ์์ต๋๋ค.
mask_image (torch.Tensor, PIL.Image.Image, np.ndarray, list[torch.Tensor], list[PIL.Image.Image], ๋๋ list[np.ndarray]) : image๋ฅผ ๋ง์คํนํ ์ด๋ฏธ์ง ๋ฐฐ์น(Image, numpy ๋ฐฐ์ด ๋๋ ํ
์)๋ฅผ ๋ํ๋
๋๋ค. ๋ง์คํฌ์ ํฐ์ ํฝ์
์ ๋ค์ ์ฑ์์ง๊ณ ๊ฒ์์ ํฝ์
์ ์ ์ง๋ฉ๋๋ค. mask_image๊ฐ PIL ์ด๋ฏธ์ง์ธ ๊ฒฝ์ฐ, ์ฌ์ฉ ์ ์ ๋จ์ผ ์ฑ๋(๋ช
๋)๋ก ๋ณํ๋ฉ๋๋ค. numpy ๋ฐฐ์ด ๋๋ pytorch ํ
์์ธ ๊ฒฝ์ฐ 3๊ฐ ๋์ ํ๋์ ์์ ์ฑ๋(L)์ ํฌํจํด์ผ ํ๋ฏ๋ก, pytorch ํ
์์ ์์ shape์ (B, 1, H, W), (B, H, W), (1, H, W), (H, W)์
๋๋ค. numpy ๋ฐฐ์ด์ ๊ฒฝ์ฐ (B, H, W, 1), (B, H, W), (H, W, 1), ๋๋ (H, W)์
๋๋ค.
masked_image_latents (torch.Tensor, ์ ํ ์ฌํญ) : ์ธํ์ธํ
์ ์ํ ๋ง์คํน๋ ์ด๋ฏธ์ง์ ์ฌ์ ์ธ์ฝ๋ฉ๋ ์ ์ฌ ๋ณ์(latent).
height (int, ์ ํ ์ฌํญ, ๊ธฐ๋ณธ๊ฐ์ self.unet.config.sample_size * self.vae_scale_factor) : ์์ฑ๋ ์ด๋ฏธ์ง์ ํฝ์
๋จ์ ๋์ด.
width (int, ์ ํ ์ฌํญ, ๊ธฐ๋ณธ๊ฐ์ self.unet.config.sample_size * self.vae_scale_factor) : ์์ฑ๋ ์ด๋ฏธ์ง์ ํฝ์
๋จ์ ๋๋น.
padding_mask_crop (int, ์ ํ ์ฌํญ, ๊ธฐ๋ณธ๊ฐ None) : ์ด๋ฏธ์ง์ ๋ง์คํน์ ์ ์ฉํ ํฌ๋กญ์ ๋ง์ง ํฌ๊ธฐ์
๋๋ค. None์ธ ๊ฒฝ์ฐ ์ด๋ฏธ์ง์ mask_image์ ํฌ๋กญ์ด ์ ์ฉ๋์ง ์์ต๋๋ค. padding_mask_crop์ด None์ด ์๋ ๊ฒฝ์ฐ, ๋จผ์ ์ด๋ฏธ์ง์ ์ข
ํก๋น์ ๋์ผํ๊ณ ๋ชจ๋ ๋ง์คํน ์์ญ์ ํฌํจํ๋ ์ง์ฌ๊ฐํ ์์ญ์ ์ฐพ์ ๋ค์, padding_mask_crop์ ๊ธฐ๋ฐ์ผ๋ก ํด๋น ์์ญ์ ํ์ฅํฉ๋๋ค. ์ดํ ์ธํ์ธํ
์ ์ํด ์๋ ์ด๋ฏธ์ง ํฌ๊ธฐ๋ก ๋ฆฌ์ฌ์ด์ฆํ๊ธฐ ์ ์ ํ์ฅ๋ ์์ญ์ ๊ธฐ์ค์ผ๋ก ์ด๋ฏธ์ง์ mask_image๊ฐ ํฌ๋กญ๋ฉ๋๋ค. ์ด๋ ๋ง์คํน ์์ญ์ด ์๊ณ ์ด๋ฏธ์ง๊ฐ ํฌ๋ฉฐ ๋ฐฐ๊ฒฝ๊ณผ ๊ฐ์ด ์ธํ์ธํ
์ ๊ด๋ จ ์๋ ์ ๋ณด๋ฅผ ํฌํจํ๋ ๊ฒฝ์ฐ์ ์ ์ฉํฉ๋๋ค.
strength (float, ์ ํ ์ฌํญ, ๊ธฐ๋ณธ๊ฐ 1.0) : ์ฐธ์กฐ image๋ฅผ ๋ณํํ ์ ๋๋ฅผ ๋ํ๋
๋๋ค. 0๊ณผ 1 ์ฌ์ด์ฌ์ผ ํฉ๋๋ค. image๋ ์์์ ์ผ๋ก ์ฌ์ฉ๋๋ฉฐ, strength ๊ฐ์ด ๋์์๋ก ๋ ๋ง์ ๋
ธ์ด์ฆ๊ฐ ์ถ๊ฐ๋ฉ๋๋ค. ๋๋
ธ์ด์ง ๋จ๊ณ์ ์๋ ์ด๊ธฐ์ ์ถ๊ฐ๋ ๋
ธ์ด์ฆ์ ์์ ๋ฐ๋ผ ๊ฒฐ์ ๋ฉ๋๋ค. strength๊ฐ 1์ด๋ฉด ์ถ๊ฐ๋๋ ๋
ธ์ด์ฆ๊ฐ ์ต๋๊ฐ ๋๋ฉฐ, num_inference_steps์ ์ง์ ๋ ์ ์ฒด ๋ฐ๋ณต ํ์๋งํผ ๋๋
ธ์ด์ง ํ๋ก์ธ์ค๊ฐ ์คํ๋ฉ๋๋ค. 1์ด๋ผ๋ ๊ฐ์ ๊ธฐ๋ณธ์ ์ผ๋ก image๋ฅผ ๋ฌด์ํ๋ ๊ฒ๊ณผ ๊ฐ์ต๋๋ค.
num_inference_steps (int, ์ ํ ์ฌํญ, ๊ธฐ๋ณธ๊ฐ 50) : ๋๋
ธ์ด์ง ๋จ๊ณ ์์
๋๋ค. ๋๋
ธ์ด์ง ๋จ๊ณ๊ฐ ๋ง์์๋ก ์ผ๋ฐ์ ์ผ๋ก ์ถ๋ก ์๋๊ฐ ๋๋ ค์ง๋ ๋์ ๋ ๋์ ํ์ง์ ์ด๋ฏธ์ง๋ฅผ ์์ฑํฉ๋๋ค. ์ด ๋งค๊ฐ๋ณ์๋ strength์ ์ํด ์กฐ์ ๋ฉ๋๋ค.
timesteps (list[int], ์ ํ ์ฌํญ) : set_timesteps ๋ฉ์๋์ timesteps ์ธ์๋ฅผ ์ง์ํ๋ ์ค์ผ์ค๋ฌ์ ๋
ธ์ด์ฆ ์ ๊ฑฐ ๊ณผ์ ์ ์ฌ์ฉํ ์ปค์คํ
ํ์์คํ
์
๋๋ค. ์ ์๋์ง ์์ ๊ฒฝ์ฐ, num_inference_steps๊ฐ ์ ๋ฌ๋ ๋์ ๊ธฐ๋ณธ ๋์์ด ์ฌ์ฉ๋ฉ๋๋ค. ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌ๋์ด ์์ด์ผ ํฉ๋๋ค.
sigmas (list[float], ์ ํ ์ฌํญ) : set_timesteps ๋ฉ์๋์ sigmas ์ธ์๋ฅผ ์ง์ํ๋ ์ค์ผ์ค๋ฌ์ ๋
ธ์ด์ฆ ์ ๊ฑฐ ๊ณผ์ ์ ์ฌ์ฉํ ์ปค์คํ
์๊ทธ๋ง์
๋๋ค. ์ ์๋์ง ์์ ๊ฒฝ์ฐ, num_inference_steps๊ฐ ์ ๋ฌ๋ ๋์ ๊ธฐ๋ณธ ๋์์ด ์ฌ์ฉ๋ฉ๋๋ค.
guidance_scale (float, ์ ํ ์ฌํญ, ๊ธฐ๋ณธ๊ฐ 7.5) : guidance scale ๊ฐ์ด ๋์์๋ก ๋ชจ๋ธ์ ์ด๋ฏธ์ง ํ์ง์ด ๋ฎ์์ง๋ ๋์ ํ
์คํธ prompt์ ๋ฐ์ ํ๊ฒ ์ฐ๊ฒฐ๋ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ๋๋ก ์ ๋๋ฉ๋๋ค. guidance_scale > 1์ผ ๋ guidance scale์ด ํ์ฑํ๋ฉ๋๋ค.
negative_prompt (str ๋๋ list[str], ์ ํ) : ์ด๋ฏธ์ง ์์ฑ์์ ํฌํจํ์ง ์์ ๋ด์ฉ์ ์๋ดํ๋ ํ๋กฌํํธ์
๋๋ค. ์ ์๋์ง ์์ ๊ฒฝ์ฐ ๋์ negative_prompt_embeds๋ฅผ ์ ๋ฌํด์ผ ํฉ๋๋ค. ๊ฐ์ด๋์ค๋ฅผ ์ฌ์ฉํ์ง ์์ ๋(guidance_scale < 1)์๋ ๋ฌด์๋ฉ๋๋ค.
num_images_per_prompt (int, ์ ํ ์ฌํญ, ๊ธฐ๋ณธ๊ฐ 1) : ํ๋กฌํํธ๋น ์์ฑํ ์ด๋ฏธ์ง์ ์.
eta (float, ์ ํ ์ฌํญ, ๊ธฐ๋ณธ๊ฐ 0.0) : DDIM ๋
ผ๋ฌธ์์ ์ ์ํ ํ๋ผ๋ฏธํฐ eta (ฮท)์ ํด๋นํฉ๋๋ค. DDIMScheduler์๋ง ์ ์ฉ๋๋ฉฐ, ๋ค๋ฅธ ์ค์ผ์ค๋ฌ์์๋ ๋ฌด์๋ฉ๋๋ค.
generator (torch.Generator ๋๋ list[torch.Generator], ์ ํ ์ฌํญ) : ์์ฑ์ ๊ฒฐ์ ์ ์ผ๋ก ๋ง๋ค๊ธฐ ์ํ torch.Generator์
๋๋ค.
latents (torch.Tensor, ์ ํ ์ฌํญ) : ๊ฐ์ฐ์์ ๋ถํฌ์์ ์ํ๋ง๋ ์ฌ์ ์์ฑ๋ ๋
ธ์ด์ฆ๋ latents๋ก, ์ด๋ฏธ์ง ์์ฑ์ ์
๋ ฅ์ผ๋ก ์ฌ์ฉ๋ฉ๋๋ค. ๋์ผํ ์์ฑ์ ๋ค๋ฅธ ํ๋กฌํํธ๋ก ์กฐ์ ํ๋ ๋ฐ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์ ๊ณต๋์ง ์์ผ๋ฉด, ์ ๊ณต๋ ๋๋ค generator๋ฅผ ์ฌ์ฉํ์ฌ ์ํ๋งํ์ฌ latents ํ
์๋ฅผ ์์ฑํฉ๋๋ค.
prompt_embeds (torch.Tensor, ์ ํ ์ฌํญ) : ๋ฏธ๋ฆฌ ์์ฑ๋ ํ
์คํธ ์๋ฒ ๋ฉ์
๋๋ค. ํ
์คํธ ์
๋ ฅ(ํ๋กฌํํธ ๊ฐ์ค์น)์ ์ฝ๊ฒ ์กฐ์ ํ๋ ๋ฐ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์ ๊ณต๋์ง ์์ผ๋ฉด prompt ์
๋ ฅ ์ธ์์์ ํ
์คํธ ์๋ฒ ๋ฉ์ด ์์ฑ๋ฉ๋๋ค.
negative_prompt_embeds (torch.Tensor, ์ ํ ์ฌํญ) : ๋ฏธ๋ฆฌ ์์ฑ๋ ๋ถ์ ํ
์คํธ ์๋ฒ ๋ฉ์
๋๋ค. ํ
์คํธ ์
๋ ฅ(ํ๋กฌํํธ ๊ฐ์ค์น)์ ์ฝ๊ฒ ์กฐ์ ํ๋ ๋ฐ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์ ๊ณต๋์ง ์์ผ๋ฉด negative_prompt ์
๋ ฅ ์ธ์์์ negative_prompt_embeds๊ฐ ์์ฑ๋ฉ๋๋ค.
ip_adapter_image : (PipelineImageInput, ์ ํ ์ฌํญ): IP Adapters์ ํจ๊ป ์ฌ์ฉํ ์ ํ์ ์ด๋ฏธ์ง ์
๋ ฅ์
๋๋ค.
ip_adapter_image_embeds (list[torch.Tensor], ์ ํ) : IP-Adapter๋ฅผ ์ํ ์ฌ์ ์์ฑ๋ ์ด๋ฏธ์ง ์๋ฒ ๋ฉ์
๋๋ค. IP-Adapter์ ์์ ๋์ผํ ๊ธธ์ด์ ๋ฆฌ์คํธ์ฌ์ผ ํฉ๋๋ค. ๊ฐ ์์๋ (batch_size, num_images, emb_dim) ํํ์ ํ
์์ฌ์ผ ํฉ๋๋ค. do_classifier_free_guidance๊ฐ True๋ก ์ค์ ๋ ๊ฒฝ์ฐ, ์์ ์ด๋ฏธ์ง ์๋ฒ ๋ฉ์ ํฌํจํด์ผ ํฉ๋๋ค. ์ ๊ณต๋์ง ์์ผ๋ฉด ip_adapter_image ์
๋ ฅ ์ธ์๋ก๋ถํฐ ์๋ฒ ๋ฉ์ด ๊ณ์ฐ๋ฉ๋๋ค.
output_type (str, ์ ํ ์ฌํญ, ๊ธฐ๋ณธ๊ฐ "pil") : ์์ฑ๋ ์ด๋ฏธ์ง์ ์ถ๋ ฅ ํ์์
๋๋ค. PIL.Image ๋๋ np.array ์ค ํ๋๋ฅผ ์ ํํฉ๋๋ค.
return_dict (bool, ์ ํ ์ฌํญ, ๊ธฐ๋ณธ๊ฐ True) : ์ผ๋ฐ ํํ ๋์ StableDiffusionPipelineOutput์ ๋ฐํํ ์ง ์ฌ๋ถ.
cross_attention_kwargs (dict, ์ ํ ์ฌํญ) : ์ง์ ๋ ๊ฒฝ์ฐ self.processor์์ ์ ์๋ AttentionProcessor๋ก ์ ๋ฌ๋๋ kwargs ์ฌ์ ์
๋๋ค.
clip_skip (int, ์ ํ ์ฌํญ) : ํ๋กฌํํธ ์๋ฒ ๋ฉ์ ๊ณ์ฐํ ๋ CLIP์์ ๊ฑด๋๋ธ ๋ ์ด์ด ์. ๊ฐ์ด 1์ด๋ฉด ์ต์ข
๋ ์ด์ด ๋ฐ๋ก ์ด์ ๋ ์ด์ด์ ์ถ๋ ฅ์ ํ๋กฌํํธ ์๋ฒ ๋ฉ ๊ณ์ฐ์ ์ฌ์ฉํฉ๋๋ค.
callback_on_step_end (Callable, PipelineCallback, MultiPipelineCallbacks, optional) : ์ถ๋ก ์ค ๊ฐ denoising ๋จ๊ณ์ ๋์์ ํธ์ถ๋๋ ํจ์ ๋๋ PipelineCallback ๋๋ MultiPipelineCallbacks์ ํ์ ํด๋์ค์
๋๋ค. ๋ค์ ์ธ์๋ฅผ ๊ฐ์ง๋๋ค: callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict). callback_kwargs์๋ callback_on_step_end_tensor_inputs๋ก ์ง์ ๋ ๋ชจ๋ ํ
์ ๋ชฉ๋ก์ด ํฌํจ๋ฉ๋๋ค.
callback_on_step_end_tensor_inputs (list, ์ ํ ์ฌํญ) : callback_on_step_end ํจ์์ ํ
์ ์
๋ ฅ ๋ชฉ๋ก์
๋๋ค. ๋ชฉ๋ก์ ์ง์ ๋ ํ
์๋ callback_kwargs ์ธ์๋ก ์ ๋ฌ๋ฉ๋๋ค. ํ์ดํ๋ผ์ธ ํด๋์ค์ ._callback_tensor_inputs ์์ฑ์ ๋์ด๋ ๋ณ์๋ง ํฌํจํ ์ ์์ต๋๋ค.
๋ฐํ๊ฐ: StableDiffusionPipelineOutput ๋๋ tuple
If return_dict is True, StableDiffusionPipelineOutput is returned, otherwise a tuple is returned where the first element is a list with the generated images and the second element is a list of bools indicating whether the corresponding generated image contains "not-safe-for-work" (nsfw) content.
์์ฑ์ ์ํ pipeline์ ํธ์ถ ํจ์.
์:
>>> import PIL
>>> import requests
>>> import torch
>>> from io import BytesIO
>>> from diffusers import StableDiffusionInpaintPipeline
>>> def download_image(url):
... response = requests.get(url)
... return PIL.Image.open(BytesIO(response.content)).convert("RGB")
>>> img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png"
>>> mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png"
>>> init_image = download_image(img_url).resize((512, 512))
>>> mask_image = download_image(mask_url).resize((512, 512))
>>> pipe = StableDiffusionInpaintPipeline.from_pretrained(
... "stable-diffusion-v1-5/stable-diffusion-inpainting", torch_dtype=torch.float16
... )
>>> pipe = pipe.to("cuda")
>>> prompt = "Face of a yellow cat, high resolution, sitting on a park bench"
>>> image = pipe(prompt=prompt, image=init_image, mask_image=mask_image).images[0]
enable_attention_slicing[[diffusers.StableDiffusionInpaintPipeline.enable_attention_slicing]]
enable_attention_slicing(slice_size: str | int = 'auto')
๋งค๊ฐ๋ณ์:
slice_size (str ๋๋ int, ์ ํ ์ฌํญ, ๊ธฐ๋ณธ๊ฐ "auto") : "auto"์ธ ๊ฒฝ์ฐ attention heads์ ์ ๋ฌ๋๋ ์
๋ ฅ์ ์ ๋ฐ์ผ๋ก ์ค์ฌ attention์ ๋ ๋จ๊ณ๋ก ๊ณ์ฐํฉ๋๋ค. "max"์ธ ๊ฒฝ์ฐ ํ ๋ฒ์ ํ๋์ ์ฌ๋ผ์ด์ค๋ง ์คํํ์ฌ ์ต๋์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ ์ฝํฉ๋๋ค. ์ซ์๊ฐ ์ ๊ณต๋๋ฉด attention_head_dim // slice_size๋งํผ์ ์ฌ๋ผ์ด์ค๋ฅผ ์ฌ์ฉํฉ๋๋ค. ์ด ๊ฒฝ์ฐ attention_head_dim์ slice_size์ ๋ฐฐ์์ฌ์ผ ํฉ๋๋ค.
์ฌ๋ผ์ด์ค๋ ์ดํ ์ ๊ณ์ฐ์ ํ์ฑํํฉ๋๋ค. ์ด ์ต์ ์ด ํ์ฑํ๋๋ฉด ์ดํ ์ ๋ชจ๋์ ์ ๋ ฅ ํ ์๋ฅผ ์ฌ๋ผ์ด์ค๋ก ๋ถํ ํ์ฌ ์ฌ๋ฌ ๋จ๊ณ์ ๊ฑธ์ณ ์ดํ ์ ์ ๊ณ์ฐํฉ๋๋ค. ์ดํ ์ ํค๋ ํ๋๊ฐ ๋๋ ๊ฒฝ์ฐ, ๊ฐ ํค๋๋ฅผ ์์ฐจ์ ์ผ๋ก ๊ณ์ฐํฉ๋๋ค. ์ด๋ ์๋๊ฐ ์ฝ๊ฐ ๊ฐ์ํ๋ ๋์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ ์ฝํ๋ ๋ฐ ์ ์ฉํฉ๋๋ค.
> โ ๏ธ ์ด๋ฏธ
scaled_dot_product_attention(SDPA)๋ฅผ ์ฌ์ฉ ์ค์ด๋ผ๋ฉด attention slicing์ ํ์ฑํํ์ง ๋ง์ธ์ from PyTorch > 2.0 or xFormers. ์ด๋ฌํ attention ๊ณ์ฐ์ ์ด๋ฏธ ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ฑ์ด ๋งค์ฐ ๋์ ์ด ๊ธฐ๋ฅ์ ํ์ฑํํ ํ์๊ฐ ์์ต๋๋ค. SDPA ๋๋ xFormers์ ํจ๊ป attention slicing์ ํ์ฑํํ๋ฉด ์ฌ๊ฐํ ์ฑ๋ฅ ์ ํ๊ฐ ๋ฐ์ํ ์ ์์ต๋๋ค!
์:
>>> import torch
>>> from diffusers import StableDiffusionPipeline
>>> pipe = StableDiffusionPipeline.from_pretrained(
... "stable-diffusion-v1-5/stable-diffusion-v1-5",
... dtype=torch.float16,
... use_safetensors=True,
... )
>>> prompt = "a photo of an astronaut riding a horse on mars"
>>> pipe.enable_attention_slicing()
>>> image = pipe(prompt).images[0]
disable_attention_slicing[[diffusers.StableDiffusionInpaintPipeline.disable_attention_slicing]]
disable_attention_slicing()
์ฌ๋ผ์ด์ค๋ ์ดํ
์
๊ณ์ฐ ๊ธฐ๋ฅ์ ๋นํ์ฑํํฉ๋๋ค. enable_attention_slicing๊ฐ ์ด์ ์ ํธ์ถ๋ ๊ฒฝ์ฐ, ์ดํ
์
์ ํ ๋ฒ์ ๊ณ์ฐ๋ฉ๋๋ค.
enable_xformers_memory_efficient_attention[[diffusers.StableDiffusionInpaintPipeline.enable_xformers_memory_efficient_attention]]
enable_xformers_memory_efficient_attention(attention_op: typing.Optional[typing.Callable] = None)
๋งค๊ฐ๋ณ์:
attention_op (Callable, ์ ํ ์ฌํญ) : xFormers์ memory_efficient_attention() ํจ์์ op ์ธ์๋ก ์ฌ์ฉ๋ ๋ ๊ธฐ๋ณธ None ์ฐ์ฐ์๋ฅผ ์ฌ์ ์ํฉ๋๋ค.
xFormers์์ ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ ์ธ ์ดํ ์ ์ ํ์ฑํํฉ๋๋ค. ์ด ์ต์ ์ ํ์ฑํํ๋ฉด ์ถ๋ก ์ GPU ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ด ๊ฐ์ํ๊ณ ์๋๊ฐ ํฅ์๋ ์ ์์ต๋๋ค. ํ์ต ์ ์๋ ํฅ์์ ๋ณด์ฅ๋์ง ์์ต๋๋ค.
> โ ๏ธ memory efficient attention๊ณผ sliced attention์ด ๋ชจ๋ ํ์ฑํ๋ ๊ฒฝ์ฐ, memory efficient attention์ > ์ฐ์ ์์๋ฅผ ๊ฐ์ง๋๋ค.
์:
>>> import torch
>>> from diffusers import DiffusionPipeline
>>> from xformers.ops import MemoryEfficientAttentionFlashAttentionOp
>>> pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1", dtype=torch.float16)
>>> pipe = pipe.to("cuda")
>>> pipe.enable_xformers_memory_efficient_attention(attention_op=MemoryEfficientAttentionFlashAttentionOp)
>>> # Workaround for not accepting attention shape using VAE for Flash Attention
>>> pipe.vae.enable_xformers_memory_efficient_attention(attention_op=None)
disable_xformers_memory_efficient_attention[[diffusers.StableDiffusionInpaintPipeline.disable_xformers_memory_efficient_attention]]
disable_xformers_memory_efficient_attention()
xFormers์์ ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ ์ดํ ์ ์ ๋นํ์ฑํํฉ๋๋ค.
load_textual_inversion[[diffusers.StableDiffusionInpaintPipeline.load_textual_inversion]]
load_textual_inversion(pretrained_model_name_or_path: str | list[str] | dict[str, torch.Tensor] | list[dict[str, torch.Tensor]], token: str | list[str] | None = None, tokenizer: 'PreTrainedTokenizer' | None = None, text_encoder: 'PreTrainedModel' | None = None, **kwargs)
๋งค๊ฐ๋ณ์:
pretrained_model_name_or_path (str ๋๋ os.PathLike ๋๋ list[str or os.PathLike] ๋๋ Dict ๋๋ list[Dict]) : ๋ค์ ์ค ํ๋์ด๊ฑฐ๋, ์ด๋ค์ ๋ฆฌ์คํธ์ผ ์ ์์ต๋๋ค: - Hub์ ํธ์คํ
๋ ์ฌ์ ํ์ต ๋ชจ๋ธ์ model id ๋ฌธ์์ด (์: sd-concepts-library/low-poly-hd-logos-icons). - ํ
์คํธ ์ธ๋ฒ์ ๊ฐ์ค์น๋ฅผ ํฌํจํ๋ ๋๋ ํฐ๋ฆฌ ๊ฒฝ๋ก (์: ./my_text_inversion_directory/). - ํ
์คํธ ์ธ๋ฒ์ ๊ฐ์ค์น๋ฅผ ํฌํจํ๋ ํ์ผ ๊ฒฝ๋ก (์: ./my_text_inversions.pt). - torch state dict.
token (str ๋๋ list[str], ์ ํ ์ฌํญ) : ํ
์คํธ ์ธ๋ฒ์ ๊ฐ์ค์น์ ์ฌ์ฉํ ํ ํฐ์ ์ฌ์ ์ํฉ๋๋ค. pretrained_model_name_or_path๊ฐ ๋ฆฌ์คํธ์ธ ๊ฒฝ์ฐ, token๋ ๋์ผํ ๊ธธ์ด์ ๋ฆฌ์คํธ์ฌ์ผ ํฉ๋๋ค.
text_encoder (CLIPTextModel, ์ ํ ์ฌํญ) : ๋๊ฒฐ๋ ํ ์คํธ ์ธ์ฝ๋(clip-vit-large-patch14). ์ง์ ๋์ง ์์ผ๋ฉด ํจ์๊ฐ self.tokenizer๋ฅผ ์ฌ์ฉํฉ๋๋ค.
tokenizer (CLIPTokenizer, ์ ํ ์ฌํญ) : ํ
์คํธ๋ฅผ ํ ํฐํํ CLIPTokenizer์
๋๋ค. ์ง์ ํ์ง ์์ผ๋ฉด ํจ์๊ฐ self.tokenizer๋ฅผ ์ฌ์ฉํฉ๋๋ค.
weight_name (str, ์ ํ ์ฌํญ) : ์ปค์คํ
๊ฐ์ค์น ํ์ผ์ ์ด๋ฆ. ๋ค์ ๊ฒฝ์ฐ์ ์ฌ์ฉํด์ผ ํฉ๋๋ค: - ์ ์ฅ๋ ํ
์คํธ ์ธ๋ฒ์ ํ์ผ์ด ๐ค Diffusers ํ์์ด์ง๋ง text_inv.bin๊ณผ ๊ฐ์ ํน์ ๊ฐ์ค์น ์ด๋ฆ์ผ๋ก ์ ์ฅ๋ ๊ฒฝ์ฐ. - ์ ์ฅ๋ ํ
์คํธ ์ธ๋ฒ์ ํ์ผ์ด Automatic1111 ํ์์ธ ๊ฒฝ์ฐ.
cache_dir (str | os.PathLike, ์ ํ ์ฌํญ) : ํ์ค ์บ์๋ฅผ ์ฌ์ฉํ์ง ์์ ๊ฒฝ์ฐ, ๋ค์ด๋ก๋๋ ์ฌ์ ํ์ต ๋ชจ๋ธ ๊ตฌ์ฑ์ด ์บ์๋๋ ๋๋ ํฐ๋ฆฌ์ ๊ฒฝ๋ก์
๋๋ค.
force_download (bool, ์ ํ ์ฌํญ, ๊ธฐ๋ณธ๊ฐ False) : ์บ์๋ ๋ฒ์ ์ด ์กด์ฌํ๋๋ผ๋ ๋ชจ๋ธ ๊ฐ์ค์น ๋ฐ ๊ตฌ์ฑ ํ์ผ์ (์ฌ)๋ค์ด๋ก๋๋ฅผ ๊ฐ์ ํ ์ง ์ฌ๋ถ.
proxies (dict[str, str], ์ ํ ์ฌํญ) : ํ๋กํ ์ฝ ๋๋ ์๋ํฌ์ธํธ๋ณ๋ก ์ฌ์ฉํ ํ๋ก์ ์๋ฒ์ ์ฌ์ ์
๋๋ค. ์๋ฅผ ๋ค์ด, {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}์ ๊ฐ์ต๋๋ค. ๊ฐ ์์ฒญ์ ํ๋ก์๊ฐ ์ฌ์ฉ๋ฉ๋๋ค.
local_files_only (bool, ์ ํ ์ฌํญ, ๊ธฐ๋ณธ๊ฐ False) : ๋ก์ปฌ ๋ชจ๋ธ ๊ฐ์ค์น์ ๊ตฌ์ฑ ํ์ผ๋ง ๋ก๋ํ ์ง ์ฌ๋ถ๋ฅผ ์ง์ ํฉ๋๋ค. True๋ก ์ค์ ํ๋ฉด Hub์์ ๋ชจ๋ธ์ ๋ค์ด๋ก๋ํ์ง ์์ต๋๋ค.
hf_token (str ๋๋ bool, ์ ํ ์ฌํญ) : ์๊ฒฉ ํ์ผ์ ๋ํ HTTP bearer ์ธ์ฆ์ผ๋ก ์ฌ์ฉํ ํ ํฐ์
๋๋ค. True์ธ ๊ฒฝ์ฐ, diffusers-cli login์์ ์์ฑ๋ ํ ํฐ(~/.huggingface์ ์ ์ฅ๋จ)์ด ์ฌ์ฉ๋ฉ๋๋ค.
revision (str, ์ ํ ์ฌํญ, ๊ธฐ๋ณธ๊ฐ "main") : ์ฌ์ฉํ ํน์ ๋ชจ๋ธ ๋ฒ์ ์
๋๋ค. Git์์ ํ์ฉํ๋ ๋ธ๋์น ์ด๋ฆ, ํ๊ทธ ์ด๋ฆ, ์ปค๋ฐ ID ๋๋ ๊ธฐํ ์๋ณ์์ผ ์ ์์ต๋๋ค.
subfolder (str, ์ ํ ์ฌํญ, ๊ธฐ๋ณธ๊ฐ "") : Hub ๋๋ ๋ก์ปฌ์ ๋ ํฐ ๋ชจ๋ธ ์ ์ฅ์ ๋ด์์ ๋ชจ๋ธ ํ์ผ์ ์๋ธํด๋ ์์น์
๋๋ค.
mirror (str, ์ ํ ์ฌํญ) : ์ค๊ตญ์์ ๋ชจ๋ธ์ ๋ค์ด๋ก๋ํ ๋ ์ ๊ทผ์ฑ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํ ๋ฏธ๋ฌ ์์ค์
๋๋ค. ํด๋น ์์ค์ ์ ์์ฑ์ด๋ ์์ ์ฑ์ ๋ณด์ฅํ์ง ์์ผ๋ฉฐ, ์์ธํ ๋ด์ฉ์ ๋ฏธ๋ฌ ์ฌ์ดํธ๋ฅผ ์ฐธ๊ณ ํ์ธ์.
StableDiffusionPipeline์ ํ ์คํธ ์ธ์ฝ๋์ Textual Inversion ์๋ฒ ๋ฉ์ ๋ก๋ํฉ๋๋ค(๐ค Diffusers์ Automatic1111 ํ์ ๋ชจ๋ ์ง์).
์:
๐ค Diffusers ํ์์ผ๋ก Textual Inversion ์๋ฒ ๋ฉ ๋ฒกํฐ๋ฅผ ๋ก๋ํ๋ ค๋ฉด:
from diffusers import StableDiffusionPipeline
import torch
model_id = "stable-diffusion-v1-5/stable-diffusion-v1-5"
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to("cuda")
pipe.load_textual_inversion("sd-concepts-library/cat-toy")
prompt = "A <cat-toy> backpack"
image = pipe(prompt, num_inference_steps=50).images[0]
image.save("cat-backpack.png")
Automatic1111 ํ์์ Textual Inversion ์๋ฒ ๋ฉ ๋ฒกํฐ๋ฅผ ๋ก๋ํ๋ ค๋ฉด ๋จผ์ ๋ฒกํฐ๋ฅผ ๋ค์ด๋ก๋(์: civitAI์์)ํ ๋ค์ ๋ฒกํฐ๋ฅผ ๋ก๋ํด์ผ ํฉ๋๋ค.
๋ก์ปฌ์์:
from diffusers import StableDiffusionPipeline
import torch
model_id = "stable-diffusion-v1-5/stable-diffusion-v1-5"
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to("cuda")
pipe.load_textual_inversion("./charturnerv2.pt", token="charturnerv2")
prompt = "charturnerv2, multiple views of the same character in the same outfit, a character turnaround of a woman wearing a black jacket and red shirt, best quality, intricate details."
image = pipe(prompt, num_inference_steps=50).images[0]
image.save("character.png")
load_lora_weights[[diffusers.StableDiffusionInpaintPipeline.load_lora_weights]]
load_lora_weights(pretrained_model_name_or_path_or_dict: str | dict[str, torch.Tensor], adapter_name: str | None = None, hotswap: bool = False, **kwargs)
๋งค๊ฐ๋ณ์:
pretrained_model_name_or_path_or_dict (str ๋๋ os.PathLike ๋๋ dict) : lora_state_dict()์(๋ฅผ) ์ฐธ์กฐํ์ธ์.
adapter_name (str, ์ ํ ์ฌํญ) : ๋ก๋๋ ์ด๋ํฐ ๋ชจ๋ธ์ ์ฐธ์กฐํ ๋ ์ฌ์ฉํ ์ด๋ํฐ ์ด๋ฆ. ์ง์ ํ์ง ์์ผ๋ฉด ๋ก๋ ์ค์ธ ์ด๋ํฐ์ ์ด ๊ฐ์๋ฅผ i๋ผ๊ณ ํ ๋ default_{i}๊ฐ ์ฌ์ฉ๋ฉ๋๋ค.
low_cpu_mem_usage (bool, ์ ํ ์ฌํญ) : ๋ฌด์์ ๊ฐ์ค์น๋ฅผ ์ด๊ธฐํํ์ง ์๊ณ ์ฌ์ ํ์ต๋ LoRA ๊ฐ์ค์น๋ง ๋ก๋ํ์ฌ ๋ชจ๋ธ ๋ก๋ฉ ์๋๋ฅผ ๋์
๋๋ค.
hotswap (bool, ์ ํ ์ฌํญ) : ๊ธฐ๋ณธ๊ฐ์ False์
๋๋ค. ๊ธฐ์กด (LoRA) ์ด๋ํฐ๋ฅผ ์๋ก ๋ก๋ํ ์ด๋ํฐ๋ก ์ ์๋ฆฌ์์ ๊ต์ฒดํ ์ง ์ฌ๋ถ์
๋๋ค. ์ฆ, ์ถ๊ฐ ์ด๋ํฐ๋ฅผ ๋ก๋ํ๋ ๋์ ๊ธฐ์กด ์ด๋ํฐ์ ๊ฐ์ค์น๋ฅผ ์ ์ด๋ํฐ์ ๊ฐ์ค์น๋ก ๋์ฒดํฉ๋๋ค. ์ด๋ ๋ ๋น ๋ฅด๊ณ ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ ์ผ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ ํซ์ค์์ ์ฃผ์ ์ด์ ์ ๋ชจ๋ธ์ด torch.compile๋ก ์ปดํ์ผ๋ ๊ฒฝ์ฐ, ์ ์ด๋ํฐ๋ฅผ ๋ก๋ํ ๋ ๋ชจ๋ธ์ ์ฌ์ปดํ์ผ์ด ํ์ํ์ง ์๋ค๋ ์ ์
๋๋ค. ํซ์ค์์ ์ฌ์ฉํ ๋ ์ ๋ฌํ๋ adapter_name์ ์ด๋ฏธ ๋ก๋๋ ์ด๋ํฐ์ ์ด๋ฆ์ด์ด์ผ ํฉ๋๋ค. ์ ์ด๋ํฐ์ ๊ธฐ์กด ์ด๋ํฐ์ ๋ญํฌ์/๋๋ LoRA ์ํ(์ฆ, ์ค์ผ์ผ๋ง)๊ฐ ๋ค๋ฅธ ๊ฒฝ์ฐ, ์ด๋ํฐ๋ฅผ ๋ก๋ํ๊ธฐ ์ ์ ์ถ๊ฐ ๋ฉ์๋๋ฅผ ํธ์ถํด์ผ ํฉ๋๋ค: py pipeline = ... # load diffusers pipeline max_rank = ... # the highest rank among all LoRAs that you want to load # call *before* compiling and loading the LoRA adapter pipeline.enable_lora_hotswap(target_rank=max_rank) pipeline.load_lora_weights(file_name) # optionally compile the model now ํ
์คํธ ์ธ์ฝ๋์ ์ด๋ํฐ ํซ์ค์์ ์์ง ์ง์๋์ง ์์ต๋๋ค. ์ด ๊ธฐ๋ฒ์๋ ๋ช ๊ฐ์ง ์ถ๊ฐ ์ ํ ์ฌํญ์ด ์์ผ๋ฉฐ, ์ฌ๊ธฐ์์ ๋ฌธ์ํ๋์ด ์์ต๋๋ค: https://huggingface.co/docs/peft/main/en/package_reference/hotswap
kwargs (dict, ์ ํ ์ฌํญ) : lora_state_dict()๋ฅผ ์ฐธ์กฐํ์ธ์.
pretrained_model_name_or_path_or_dict์ ์ง์ ๋ LoRA ๊ฐ์ค์น๋ฅผ self.unet์ self.text_encoder์ ๋ก๋ํฉ๋๋ค.
๋ชจ๋ kwargs๋ self.lora_state_dict๋ก ์ ๋ฌ๋ฉ๋๋ค.
state dict๊ฐ ๋ก๋๋๋ ๋ฐฉ๋ฒ์ ๋ํ ์์ธํ ๋ด์ฉ์ lora_state_dict()์ ์ฐธ์กฐํ์ธ์.
self.unet์ ์ํ ์ฌ์ ์ด ๋ก๋๋๋ ๋ฐฉ๋ฒ์ ๋ํ ์์ธํ ๋ด์ฉ์ load_lora_into_unet()์ ์ฐธ์กฐํ์ญ์์ค.
self.text_encoder์ state dict๊ฐ ๋ก๋๋๋ ๋ฐฉ๋ฒ์ ๋ํ ์์ธํ ๋ด์ฉ์ load_lora_into_text_encoder()๋ฅผ ์ฐธ์กฐํ์ธ์.
save_lora_weights[[diffusers.StableDiffusionInpaintPipeline.save_lora_weights]]
save_lora_weights(save_directory: str | os.PathLike, unet_lora_layers: dict[str, typing.Union[torch.nn.Module, torch.Tensor]] = None, text_encoder_lora_layers: dict[str, torch.nn.Module] = None, is_main_process: bool = True, weight_name: str = None, save_function: typing.Callable = None, safe_serialization: bool = True, unet_lora_adapter_metadata = None, text_encoder_lora_adapter_metadata = None)
๋งค๊ฐ๋ณ์:
save_directory (str ๋๋ os.PathLike) : LoRA ํ๋ผ๋ฏธํฐ๋ฅผ ์ ์ฅํ ๋๋ ํฐ๋ฆฌ์
๋๋ค. ์กด์ฌํ์ง ์์ผ๋ฉด ์์ฑ๋ฉ๋๋ค.
unet_lora_layers (dict[str, torch.nn.Module] ๋๋ dict[str, torch.Tensor]) : unet์ ํด๋นํ๋ LoRA ๋ ์ด์ด์ state dict์
๋๋ค.
text_encoder_lora_layers (dict[str, torch.nn.Module] ๋๋ dict[str, torch.Tensor]) : text_encoder์ ํด๋นํ๋ LoRA ๋ ์ด์ด์ state dict์
๋๋ค. ๐ค Transformers์์ ๊ฐ์ ธ์ค๋ฏ๋ก text encoder LoRA state dict๋ฅผ ๋ช
์์ ์ผ๋ก ์ ๋ฌํด์ผ ํฉ๋๋ค.
is_main_process (bool, ์ ํ ์ฌํญ, ๊ธฐ๋ณธ๊ฐ True) : ์ด ํจ์๋ฅผ ํธ์ถํ๋ ํ๋ก์ธ์ค๊ฐ ๋ฉ์ธ ํ๋ก์ธ์ค์ธ์ง ์ฌ๋ถ์
๋๋ค. ๋ถ์ฐ ํ์ต ์ค์ ๋ชจ๋ ํ๋ก์ธ์ค์์ ์ด ํจ์๋ฅผ ํธ์ถํด์ผ ํ๋ ๊ฒฝ์ฐ ์ ์ฉํฉ๋๋ค. ์ด ๊ฒฝ์ฐ ๋ ์ด์ค ์กฐ๊ฑด์ ๋ฐฉ์งํ๊ธฐ ์ํด ๋ฉ์ธ ํ๋ก์ธ์ค์์๋ง is_main_process=True๋ก ์ค์ ํ์ธ์.
save_function (Callable) : ์ํ ์ฌ์ (state dictionary)์ ์ ์ฅํ๋ ๋ฐ ์ฌ์ฉํ ํจ์์
๋๋ค. ๋ถ์ฐ ํ์ต ์ค์ torch.save๋ฅผ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ผ๋ก ๋์ฒดํด์ผ ํ๋ ๊ฒฝ์ฐ ์ ์ฉํฉ๋๋ค. ํ๊ฒฝ ๋ณ์ DIFFUSERS_SAVE_MODE๋ก ์ค์ ํ ์ ์์ต๋๋ค.
safe_serialization (bool, ์ ํ ์ฌํญ, ๊ธฐ๋ณธ๊ฐ True) : safetensors ๋๋ pickle๋ฅผ ์ฌ์ฉํ ์ ํต์ ์ธ PyTorch ๋ฐฉ์์ผ๋ก ๋ชจ๋ธ์ ์ ์ฅํ ์ง ์ฌ๋ถ.
unet_lora_adapter_metadata : state dict์ ํจ๊ป ์ง๋ ฌํ๋ unet์ ์ฐ๊ฒฐ๋ LoRA ์ด๋ํฐ ๋ฉํ๋ฐ์ดํฐ.
text_encoder_lora_adapter_metadata : state dict์ ํจ๊ป ์ง๋ ฌํ๋ ํ ์คํธ ์ธ์ฝ๋์ ๊ด๋ จ๋ LoRA ์ด๋ํฐ ๋ฉํ๋ฐ์ดํฐ.
UNet๊ณผ ํ ์คํธ ์ธ์ฝ๋์ ํด๋นํ๋ LoRA ๋งค๊ฐ๋ณ์๋ฅผ ์ ์ฅํฉ๋๋ค.
encode_prompt[[diffusers.StableDiffusionInpaintPipeline.encode_prompt]]
encode_prompt(prompt, device, num_images_per_prompt, do_classifier_free_guidance, negative_prompt = None, prompt_embeds: typing.Optional[torch.Tensor] = None, negative_prompt_embeds: typing.Optional[torch.Tensor] = None, lora_scale: float | None = None, clip_skip: int | None = None)
๋งค๊ฐ๋ณ์:
prompt (str ๋๋ list[str], ์ ํ ์ฌํญ) : ์ธ์ฝ๋ฉํ ํ๋กฌํํธ
device : (torch.device): torch ๋๋ฐ์ด์ค
num_images_per_prompt (int) : ํ๋กฌํํธ๋น ์์ฑํ ์ด๋ฏธ์ง ์
do_classifier_free_guidance (bool) : classifier free guidance๋ฅผ ์ฌ์ฉํ ์ง ์ฌ๋ถ
negative_prompt (str ๋๋ list[str], ์ ํ ์ฌํญ) : ์ด๋ฏธ์ง ์์ฑ์ ์๋ดํ์ง ์์ ํ๋กฌํํธ์
๋๋ค. ์ ์๋์ง ์์ ๊ฒฝ์ฐ ๋์ negative_prompt_embeds๋ฅผ ์ ๋ฌํด์ผ ํฉ๋๋ค. ๊ฐ์ด๋์ค๋ฅผ ์ฌ์ฉํ์ง ์๋ ๊ฒฝ์ฐ ๋ฌด์๋ฉ๋๋ค(์ฆ, guidance_scale์ด 1๋ณด๋ค ์์ ๊ฒฝ์ฐ ๋ฌด์๋ฉ๋๋ค).
prompt_embeds (torch.Tensor, ์ ํ ์ฌํญ) : ๋ฏธ๋ฆฌ ์์ฑ๋ ํ
์คํธ ์๋ฒ ๋ฉ์
๋๋ค. ์๋ฅผ ๋ค์ด ํ๋กฌํํธ ๊ฐ์ค์น์ ๊ฐ์ ํ
์คํธ ์
๋ ฅ์ ์ฝ๊ฒ ์กฐ์ ํ๋ ๋ฐ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์ ๊ณต๋์ง ์์ผ๋ฉด prompt ์
๋ ฅ ์ธ์์์ ํ
์คํธ ์๋ฒ ๋ฉ์ด ์์ฑ๋ฉ๋๋ค.
negative_prompt_embeds (torch.Tensor, ์ ํ ์ฌํญ) : ๋ฏธ๋ฆฌ ์์ฑ๋ ๋ถ์ ํ
์คํธ ์๋ฒ ๋ฉ์
๋๋ค. ํ
์คํธ ์
๋ ฅ์ ์ฝ๊ฒ ์กฐ์ ํ๋ ๋ฐ ์ฌ์ฉํ ์ ์์ผ๋ฉฐ, ์๋ฅผ ๋ค์ด ํ๋กฌํํธ ๊ฐ์ค์น ์ค์ ์ ํ์ฉํ ์ ์์ต๋๋ค. ์ ๊ณต๋์ง ์์ผ๋ฉด negative_prompt ์
๋ ฅ ์ธ์์์ negative_prompt_embeds๊ฐ ์์ฑ๋ฉ๋๋ค.
lora_scale (float, ์ ํ ์ฌํญ) : LoRA ๋ ์ด์ด๊ฐ ๋ก๋๋ ๊ฒฝ์ฐ ํ
์คํธ ์ธ์ฝ๋์ ๋ชจ๋ LoRA ๋ ์ด์ด์ ์ ์ฉ๋ LoRA ์ค์ผ์ผ์
๋๋ค.
clip_skip (int, ์ ํ ์ฌํญ) : ํ๋กฌํํธ ์๋ฒ ๋ฉ์ ๊ณ์ฐํ ๋ CLIP์์ ๊ฑด๋๋ธ ๋ ์ด์ด ์. ๊ฐ์ด 1์ด๋ฉด ์ต์ข
๋ ์ด์ด ๋ฐ๋ก ์ด์ ๋ ์ด์ด์ ์ถ๋ ฅ์ ํ๋กฌํํธ ์๋ฒ ๋ฉ ๊ณ์ฐ์ ์ฌ์ฉํฉ๋๋ค.
ํ๋กฌํํธ๋ฅผ ํ ์คํธ ์ธ์ฝ๋์ ํ๋ ์ํ๋ก ์ธ์ฝ๋ฉํฉ๋๋ค.
get_guidance_scale_embedding[[diffusers.StableDiffusionInpaintPipeline.get_guidance_scale_embedding]]
get_guidance_scale_embedding(w: Tensor, embedding_dim: int = 512, dtype: dtype = torch.float32)
๋งค๊ฐ๋ณ์:
w (torch.Tensor) : ์ง์ ๋ guidance scale์ ์ฌ์ฉํ์ฌ ์๋ฒ ๋ฉ ๋ฒกํฐ๋ฅผ ์์ฑํ๊ณ , ์ด๋ฅผ ํตํด timestep ์๋ฒ ๋ฉ์ ํ๋ถํ๊ฒ ๋ง๋ญ๋๋ค.
embedding_dim (int, ์ ํ ์ฌํญ, ๊ธฐ๋ณธ๊ฐ 512) : ์์ฑํ ์๋ฒ ๋ฉ์ ์ฐจ์.
dtype (torch.dtype, ์ ํ ์ฌํญ, ๊ธฐ๋ณธ๊ฐ torch.float32) : ์์ฑ๋ ์๋ฒ ๋ฉ์ ๋ฐ์ดํฐ ์ ํ์
๋๋ค.
๋ฐํ๊ฐ: torch.Tensor
(len(w), embedding_dim) ํํ์ ์๋ฒ ๋ฉ ๋ฒกํฐ.
https://github.com/google-research/vdm/blob/dc27b98a554f65cdc654b800da5aa1846545d41b/model_vdm.py#L298 ๋ฅผ ์ฐธ์กฐํ์ธ์.
StableDiffusionPipelineOutput[[diffusers.pipelines.stable_diffusion.StableDiffusionPipelineOutput]]
diffusers.pipelines.stable_diffusion.StableDiffusionPipelineOutput[[diffusers.pipelines.stable_diffusion.StableDiffusionPipelineOutput]]
diffusers.pipelines.stable_diffusion.StableDiffusionPipelineOutput(images: list[PIL.Image.Image] | numpy.ndarray, nsfw_content_detected: list[bool] | None)
๋งค๊ฐ๋ณ์:
images (list[PIL.Image.Image] ๋๋ np.ndarray) : batch_size ๊ธธ์ด์ ๋
ธ์ด์ฆ ์ ๊ฑฐ๋ PIL ์ด๋ฏธ์ง ๋ชฉ๋ก ๋๋ (batch_size, height, width, num_channels) ๋ชจ์์ NumPy ๋ฐฐ์ด.
nsfw_content_detected (list[bool]) : ํด๋น ์์ฑ๋ ์ด๋ฏธ์ง์ "not-safe-for-work"(nsfw) ์ฝํ
์ธ ๊ฐ ํฌํจ๋์ด ์๋์ง ์ฌ๋ถ๋ฅผ ๋ํ๋ด๋ ๋ฆฌ์คํธ. ์์ ๊ฒ์ฌ๋ฅผ ์ํํ ์ ์๋ ๊ฒฝ์ฐ None.
Stable Diffusion ํ์ดํ๋ผ์ธ์ ์ถ๋ ฅ ํด๋์ค์ ๋๋ค.
Xet Storage Details
- Size:
- 38.1 kB
- Xet hash:
- bb988a8ffe2829561de243352bb493be9cf2fe6453728620759e7695cad58662
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.