Skip to content

Commit 2c79271

Browse files
committed
update for 5-3-1
1 parent 67f6f2a commit 2c79271

File tree

2 files changed

+238
-42
lines changed

2 files changed

+238
-42
lines changed

notebooks/5-3-1_prompt-to-prompt.ipynb

Lines changed: 54 additions & 42 deletions
Large diffs are not rendered by default.

scripts/5-3-1_prompt-to-prompt.py

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
# ---
2+
# jupyter:
3+
# jupytext:
4+
# text_representation:
5+
# extension: .py
6+
# format_name: percent
7+
# format_version: '1.3'
8+
# jupytext_version: 1.16.7
9+
# kernelspec:
10+
# display_name: Python 3 (ipykernel)
11+
# language: python
12+
# name: python3
13+
# ---
14+
15+
# %% [markdown]
16+
# # Prompt-to-Prompt の実装
17+
18+
# %% [markdown]
19+
# [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/py-img-gen/python-image-generation/blob/main/notebooks/5-3-1_prompt-to-prompt.ipynb)
20+
21+
# %% [markdown]
22+
# 参考: https://github.com/huggingface/diffusers/tree/main/examples/community#prompt2prompt-pipeline
23+
24+
# %% [markdown]
25+
# ## 準備
26+
27+
# %%
28+
# !pip install -qq py-img-gen
29+
30+
# %%
31+
import warnings
32+
33+
import torch
34+
35+
device = torch.device(
36+
"cuda" if torch.cuda.is_available() else "cpu"
37+
)
38+
dtype = torch.float16
39+
variant = "fp16"
40+
seed = 0
41+
42+
warnings.simplefilter("ignore", FutureWarning)
43+
44+
# %%
45+
from diffusers import DiffusionPipeline
46+
47+
model_id = "stable-diffusion-v1-5/stable-diffusion-v1-5"
48+
49+
prompt2prompt = DiffusionPipeline.from_pretrained(
50+
model_id,
51+
custom_pipeline="pipeline_prompt2prompt",
52+
)
53+
54+
55+
# %%
56+
from diffusers_modules.git.pipeline_prompt2prompt import (
57+
Prompt2PromptPipeline,
58+
)
59+
60+
assert isinstance(prompt2prompt, Prompt2PromptPipeline)
61+
prompt2prompt = prompt2prompt.to(device=device)
62+
63+
# %% [markdown]
64+
# ## 簡易パイプライン実行関数の定義
65+
66+
# %%
67+
from typing import Any, Dict, List
68+
69+
from diffusers.utils import make_image_grid
70+
from PIL.Image import Image as PilImage
71+
72+
73+
def prompt_to_prompt(
74+
prompt_before: str,
75+
prompt_after: str,
76+
cross_attention_kwargs: Dict[str, Any],
77+
width: int = 512,
78+
height: int = 512,
79+
num_inference_steps: int = 50,
80+
) -> List[PilImage]:
81+
outputs = prompt2prompt(
82+
prompt=[prompt_before, prompt_after],
83+
width=width,
84+
height=height,
85+
num_inference_steps=num_inference_steps,
86+
cross_attention_kwargs=cross_attention_kwargs,
87+
generator=torch.manual_seed(seed),
88+
)
89+
return outputs.images
90+
91+
92+
# %% [markdown]
93+
# ## Relace Edit
94+
95+
# %%
96+
prompt_before = "A painting of a squirrel eating a burger"
97+
prompt_after = "A painting of a cat eating a burger"
98+
99+
images = prompt_to_prompt(
100+
prompt_before=prompt_before,
101+
prompt_after=prompt_after,
102+
cross_attention_kwargs={
103+
"edit_type": "replace",
104+
"cross_replace_steps": 0.4,
105+
"self_replace_steps": 0.4,
106+
},
107+
)
108+
make_image_grid(images, rows=1, cols=len(images))
109+
110+
111+
# %% [markdown]
112+
# ## Replace Edit with Loacl Blend
113+
114+
# %%
115+
prompt_before = "A painting of a squirrel eating a burger"
116+
prompt_after = "A painting of a cat eating a burger"
117+
118+
images = prompt_to_prompt(
119+
prompt_before=prompt_before,
120+
prompt_after=prompt_after,
121+
cross_attention_kwargs={
122+
"edit_type": "replace",
123+
"cross_replace_steps": 0.4,
124+
"self_replace_steps": 0.4,
125+
"local_blend_words": ["squirrel", "cat"],
126+
},
127+
)
128+
make_image_grid(images, rows=1, cols=len(images))
129+
130+
# %% [markdown]
131+
# ## Refine Edit
132+
133+
# %%
134+
prompt_before = "A painting of a squirrel eating a burger"
135+
prompt_after = "A real photo of a cat eating a burger"
136+
137+
images = prompt_to_prompt(
138+
prompt_before=prompt_before,
139+
prompt_after=prompt_after,
140+
cross_attention_kwargs={
141+
"edit_type": "refine",
142+
"cross_replace_steps": 0.9,
143+
"self_replace_steps": 0.2,
144+
},
145+
)
146+
make_image_grid(images, rows=1, cols=len(images))
147+
148+
# %% [markdown]
149+
# ## Refine Edit with Local Blend
150+
151+
# %%
152+
prompt_before = "A painting of a squirrel eating a burger"
153+
prompt_after = "A real photo of a cat eating a burger"
154+
155+
images = prompt_to_prompt(
156+
prompt_before=prompt_before,
157+
prompt_after=prompt_after,
158+
cross_attention_kwargs={
159+
"edit_type": "refine",
160+
"cross_replace_steps": 0.9,
161+
"self_replace_steps": 0.2,
162+
"local_blend_words": ["squirrel", "cat"],
163+
},
164+
)
165+
make_image_grid(images, rows=1, cols=len(images))
166+
167+
# %% [markdown]
168+
# ## Rewiehgt Edit
169+
170+
# %%
171+
prompt = "a photo of smiling pink bunny doll"
172+
173+
images = prompt_to_prompt(
174+
prompt_before=prompt,
175+
prompt_after=prompt,
176+
cross_attention_kwargs={
177+
"edit_type": "reweight",
178+
"cross_replace_steps": 0.8,
179+
"self_replace_steps": 0.8,
180+
"equalizer_words": ["smiling"],
181+
"equalizer_strengths": [20],
182+
},
183+
)
184+
make_image_grid(images, rows=1, cols=len(images))

0 commit comments

Comments
 (0)