|
<!--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. |
|
--> |
|
|
|
# ํ
์คํธ ๊ธฐ๋ฐ image-to-image ์์ฑ |
|
|
|
[[open-in-colab]] |
|
|
|
[`StableDiffusionImg2ImgPipeline`]์ ์ฌ์ฉํ๋ฉด ํ
์คํธ ํ๋กฌํํธ์ ์์ ์ด๋ฏธ์ง๋ฅผ ์ ๋ฌํ์ฌ ์ ์ด๋ฏธ์ง ์์ฑ์ ์กฐ๊ฑด์ ์ง์ ํ ์ ์์ต๋๋ค. |
|
|
|
์์ํ๊ธฐ ์ ์ ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ๋ชจ๋ ์ค์น๋์ด ์๋์ง ํ์ธํ์ธ์: |
|
|
|
```bash |
|
!pip install diffusers transformers ftfy accelerate |
|
``` |
|
|
|
[`nitrosocke/Ghibli-Diffusion`](https://huggingface.co/nitrosocke/Ghibli-Diffusion)๊ณผ ๊ฐ์ ์ฌ์ ํ์ต๋ stable diffusion ๋ชจ๋ธ๋ก [`StableDiffusionImg2ImgPipeline`]์ ์์ฑํ์ฌ ์์ํ์ธ์. |
|
|
|
|
|
```python |
|
import torch |
|
import requests |
|
from PIL import Image |
|
from io import BytesIO |
|
from diffusers import StableDiffusionImg2ImgPipeline |
|
|
|
device = "cuda" |
|
pipe = StableDiffusionImg2ImgPipeline.from_pretrained("nitrosocke/Ghibli-Diffusion", torch_dtype=torch.float16).to( |
|
device |
|
) |
|
``` |
|
|
|
์ด๊ธฐ ์ด๋ฏธ์ง๋ฅผ ๋ค์ด๋ก๋ํ๊ณ ์ฌ์ ์ฒ๋ฆฌํ์ฌ ํ์ดํ๋ผ์ธ์ ์ ๋ฌํ ์ ์์ต๋๋ค: |
|
|
|
```python |
|
url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg" |
|
|
|
response = requests.get(url) |
|
init_image = Image.open(BytesIO(response.content)).convert("RGB") |
|
init_image.thumbnail((768, 768)) |
|
init_image |
|
``` |
|
|
|
<div class="flex justify-center"> |
|
<img src="https://huggingface.co/datasets/YiYiXu/test-doc-assets/resolve/main/image_2_image_using_diffusers_cell_8_output_0.jpeg"/> |
|
</div> |
|
|
|
<Tip> |
|
|
|
๐ก `strength`๋ ์
๋ ฅ ์ด๋ฏธ์ง์ ์ถ๊ฐ๋๋ ๋
ธ์ด์ฆ์ ์์ ์ ์ดํ๋ 0.0์์ 1.0 ์ฌ์ด์ ๊ฐ์
๋๋ค. 1.0์ ๊ฐ๊น์ด ๊ฐ์ ๋ค์ํ ๋ณํ์ ํ์ฉํ์ง๋ง ์
๋ ฅ ์ด๋ฏธ์ง์ ์๋ฏธ์ ์ผ๋ก ์ผ์นํ์ง ์๋ ์ด๋ฏธ์ง๋ฅผ ์์ฑํฉ๋๋ค. |
|
|
|
</Tip> |
|
|
|
ํ๋กฌํํธ๋ฅผ ์ ์ํ๊ณ (์ง๋ธ๋ฆฌ ์คํ์ผ(Ghibli-style)์ ๋ง๊ฒ ์กฐ์ ๋ ์ด ์ฒดํฌํฌ์ธํธ์ ๊ฒฝ์ฐ ํ๋กฌํํธ ์์ `ghibli style` ํ ํฐ์ ๋ถ์ฌ์ผ ํฉ๋๋ค) ํ์ดํ๋ผ์ธ์ ์คํํฉ๋๋ค: |
|
|
|
```python |
|
prompt = "ghibli style, a fantasy landscape with castles" |
|
generator = torch.Generator(device=device).manual_seed(1024) |
|
image = pipe(prompt=prompt, image=init_image, strength=0.75, guidance_scale=7.5, generator=generator).images[0] |
|
image |
|
``` |
|
|
|
<div class="flex justify-center"> |
|
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/ghibli-castles.png"/> |
|
</div> |
|
|
|
๋ค๋ฅธ ์ค์ผ์ค๋ฌ๋ก ์คํํ์ฌ ์ถ๋ ฅ์ ์ด๋ค ์ํฅ์ ๋ฏธ์น๋์ง ํ์ธํ ์๋ ์์ต๋๋ค: |
|
|
|
```python |
|
from diffusers import LMSDiscreteScheduler |
|
|
|
lms = LMSDiscreteScheduler.from_config(pipe.scheduler.config) |
|
pipe.scheduler = lms |
|
generator = torch.Generator(device=device).manual_seed(1024) |
|
image = pipe(prompt=prompt, image=init_image, strength=0.75, guidance_scale=7.5, generator=generator).images[0] |
|
image |
|
``` |
|
|
|
<div class="flex justify-center"> |
|
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/lms-ghibli.png"/> |
|
</div> |
|
|
|
์๋ ๊ณต๋ฐฑ์ ํ์ธํ๊ณ `strength` ๊ฐ์ ๋ค๋ฅด๊ฒ ์ค์ ํ์ฌ ์ด๋ฏธ์ง๋ฅผ ์์ฑํด ๋ณด์ธ์. `strength`๋ฅผ ๋ฎ๊ฒ ์ค์ ํ๋ฉด ์๋ณธ ์ด๋ฏธ์ง์ ๋ ์ ์ฌํ ์ด๋ฏธ์ง๊ฐ ์์ฑ๋๋ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค. |
|
|
|
์์ ๋กญ๊ฒ ์ค์ผ์ค๋ฌ๋ฅผ [`LMSDiscreteScheduler`]๋ก ์ ํํ์ฌ ์ถ๋ ฅ์ ์ด๋ค ์ํฅ์ ๋ฏธ์น๋์ง ํ์ธํด ๋ณด์ธ์. |
|
|
|
<iframe |
|
src="https://stevhliu-ghibli-img2img.hf.space" |
|
frameborder="0" |
|
width="850" |
|
height="500" |
|
></iframe> |