Spaces:
Runtime error
Runtime error
Commit
Β·
f343ea1
1
Parent(s):
123f4ac
stylestudio
Browse files- .gitattributes +1 -0
- app.py +44 -29
- app_exp.py +244 -0
- assets/style3.jpg +0 -0
- ip_adapter/__pycache__/__init__.cpython-39.pyc +0 -0
- ip_adapter/__pycache__/attention_processor.cpython-39.pyc +0 -0
- ip_adapter/__pycache__/ip_adapter.cpython-39.pyc +0 -0
- ip_adapter/__pycache__/resampler.cpython-39.pyc +0 -0
- ip_adapter/__pycache__/utils.cpython-39.pyc +0 -0
- ip_adapter/attention_processor.py +2 -0
- ip_adapter/ip_adapter.py +2 -1
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
app_exp.py
|
app.py
CHANGED
|
@@ -59,23 +59,37 @@ def get_example():
|
|
| 59 |
case = [
|
| 60 |
[
|
| 61 |
'./assets/style1.jpg',
|
| 62 |
-
"Text-Driven Style Synthesis",
|
| 63 |
"A red apple",
|
| 64 |
7.0,
|
| 65 |
42,
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
],
|
| 68 |
]
|
| 69 |
return case
|
| 70 |
|
| 71 |
-
def run_for_examples(style_image_pil,
|
| 72 |
|
| 73 |
return create_image(
|
| 74 |
style_image_pil=style_image_pil,
|
| 75 |
prompt=prompt,
|
| 76 |
-
|
|
|
|
| 77 |
num_inference_steps=50,
|
| 78 |
-
seed=
|
| 79 |
end_fusion=end_fusion,
|
| 80 |
use_SAttn=True,
|
| 81 |
crossModalAdaIN=True,
|
|
@@ -86,21 +100,20 @@ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
|
| 86 |
seed = random.randint(0, MAX_SEED)
|
| 87 |
return seed
|
| 88 |
|
| 89 |
-
|
| 90 |
-
def create_image(
|
| 91 |
-
style_image_pil,
|
| 92 |
prompt,
|
| 93 |
-
guidance_scale,
|
| 94 |
-
num_inference_steps,
|
| 95 |
-
end_fusion,
|
| 96 |
-
crossModalAdaIN,
|
| 97 |
-
use_SAttn,
|
| 98 |
-
seed,
|
| 99 |
neg_prompt="text, watermark, lowres, low quality, worst quality, deformed, glitch, low contrast, noisy, saturation, blurry",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
):
|
| 101 |
|
| 102 |
style_image = style_image_pil
|
| 103 |
|
|
|
|
| 104 |
generator = torch.Generator(device).manual_seed(seed)
|
| 105 |
init_latents = torch.randn((1, 4, 128, 128), generator=generator, device="cuda", dtype=torch.float16)
|
| 106 |
num_sample=1
|
|
@@ -122,6 +135,7 @@ def create_image(
|
|
| 122 |
use_SAttn=use_SAttn,
|
| 123 |
|
| 124 |
generator=generator,
|
|
|
|
| 125 |
)
|
| 126 |
|
| 127 |
if use_SAttn:
|
|
@@ -135,23 +149,28 @@ title = r"""
|
|
| 135 |
"""
|
| 136 |
|
| 137 |
description = r"""
|
| 138 |
-
<b>Official π€ Gradio demo</b> for <a href='https://github.com/
|
| 139 |
How to use:<br>
|
| 140 |
1. Upload a style image.
|
| 141 |
-
2. <b>Enter your desired prompt
|
| 142 |
3. Click the <b>Submit</b> button to begin customization.
|
| 143 |
4. Share your stylized photo with your friends and enjoy! π
|
| 144 |
|
| 145 |
Advanced usage:<br>
|
| 146 |
1. Click advanced options.
|
| 147 |
2. Choose different guidance and steps.
|
| 148 |
-
3. Set the timing for the Teacher Model's participation
|
|
|
|
| 149 |
"""
|
| 150 |
|
| 151 |
article = r"""
|
| 152 |
---
|
| 153 |
π **Tips**
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
---
|
| 156 |
π **Citation**
|
| 157 |
<br>
|
|
@@ -176,10 +195,6 @@ with block:
|
|
| 176 |
with gr.Column():
|
| 177 |
style_image_pil = gr.Image(label="Style Image", type='pil')
|
| 178 |
|
| 179 |
-
target = gr.Radio(["Text-Driven Style Synthesis"],
|
| 180 |
-
value="Text-Driven Style Synthesis",
|
| 181 |
-
label="task")
|
| 182 |
-
|
| 183 |
prompt = gr.Textbox(label="Prompt",
|
| 184 |
value="A red apple")
|
| 185 |
|
|
@@ -190,14 +205,14 @@ with block:
|
|
| 190 |
|
| 191 |
guidance_scale = gr.Slider(minimum=1, maximum=15.0, step=0.01, value=7.0, label="guidance scale")
|
| 192 |
|
| 193 |
-
num_inference_steps = gr.Slider(minimum=5, maximum=
|
| 194 |
label="num inference steps")
|
| 195 |
|
| 196 |
-
end_fusion = gr.Slider(minimum=0, maximum=
|
| 197 |
|
| 198 |
-
seed = gr.Slider(minimum=-1000000, maximum=1000000, value=
|
| 199 |
|
| 200 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=
|
| 201 |
|
| 202 |
crossModalAdaIN = gr.Checkbox(label="Cross Modal AdaIN", value=True)
|
| 203 |
use_SAttn = gr.Checkbox(label="Teacher Model", value=True)
|
|
@@ -218,18 +233,18 @@ with block:
|
|
| 218 |
inputs=[
|
| 219 |
style_image_pil,
|
| 220 |
prompt,
|
|
|
|
| 221 |
guidance_scale,
|
| 222 |
num_inference_steps,
|
| 223 |
end_fusion,
|
| 224 |
crossModalAdaIN,
|
| 225 |
use_SAttn,
|
| 226 |
-
seed,
|
| 227 |
-
neg_prompt,],
|
| 228 |
outputs=[generated_image])
|
| 229 |
|
| 230 |
gr.Examples(
|
| 231 |
examples=get_example(),
|
| 232 |
-
inputs=[style_image_pil,
|
| 233 |
fn=run_for_examples,
|
| 234 |
outputs=[generated_image],
|
| 235 |
cache_examples=False,
|
|
|
|
| 59 |
case = [
|
| 60 |
[
|
| 61 |
'./assets/style1.jpg',
|
|
|
|
| 62 |
"A red apple",
|
| 63 |
7.0,
|
| 64 |
42,
|
| 65 |
+
10,
|
| 66 |
+
],
|
| 67 |
+
[
|
| 68 |
+
'./assets/style2.jpg',
|
| 69 |
+
"A black car",
|
| 70 |
+
7.0,
|
| 71 |
+
42,
|
| 72 |
+
10,
|
| 73 |
+
],
|
| 74 |
+
[
|
| 75 |
+
'./assets/style3.jpg',
|
| 76 |
+
"A orange bus",
|
| 77 |
+
7.0,
|
| 78 |
+
42,
|
| 79 |
+
10,
|
| 80 |
],
|
| 81 |
]
|
| 82 |
return case
|
| 83 |
|
| 84 |
+
def run_for_examples(style_image_pil, prompt, guidance_scale, seed, end_fusion):
|
| 85 |
|
| 86 |
return create_image(
|
| 87 |
style_image_pil=style_image_pil,
|
| 88 |
prompt=prompt,
|
| 89 |
+
neg_prompt="text, watermark, lowres, low quality, worst quality, deformed, glitch, low contrast, noisy, saturation, blurry",
|
| 90 |
+
guidance_scale=guidance_scale,
|
| 91 |
num_inference_steps=50,
|
| 92 |
+
seed=seed,
|
| 93 |
end_fusion=end_fusion,
|
| 94 |
use_SAttn=True,
|
| 95 |
crossModalAdaIN=True,
|
|
|
|
| 100 |
seed = random.randint(0, MAX_SEED)
|
| 101 |
return seed
|
| 102 |
|
| 103 |
+
def create_image(style_image_pil,
|
|
|
|
|
|
|
| 104 |
prompt,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
neg_prompt="text, watermark, lowres, low quality, worst quality, deformed, glitch, low contrast, noisy, saturation, blurry",
|
| 106 |
+
guidance_scale=7,
|
| 107 |
+
num_inference_steps=50,
|
| 108 |
+
end_fusion=20,
|
| 109 |
+
crossModalAdaIN=True,
|
| 110 |
+
use_SAttn=True,
|
| 111 |
+
seed=42,
|
| 112 |
):
|
| 113 |
|
| 114 |
style_image = style_image_pil
|
| 115 |
|
| 116 |
+
print(seed)
|
| 117 |
generator = torch.Generator(device).manual_seed(seed)
|
| 118 |
init_latents = torch.randn((1, 4, 128, 128), generator=generator, device="cuda", dtype=torch.float16)
|
| 119 |
num_sample=1
|
|
|
|
| 135 |
use_SAttn=use_SAttn,
|
| 136 |
|
| 137 |
generator=generator,
|
| 138 |
+
latents=init_latents,
|
| 139 |
)
|
| 140 |
|
| 141 |
if use_SAttn:
|
|
|
|
| 149 |
"""
|
| 150 |
|
| 151 |
description = r"""
|
| 152 |
+
<b>Official π€ Gradio demo</b> for <a href='https://github.com/Westlake-AGI-Lab/StyleStudio' target='_blank'><b>StyleStudio: Text-Driven Style Transfer with Selective Control of Style Elements</b></a>.<br>
|
| 153 |
How to use:<br>
|
| 154 |
1. Upload a style image.
|
| 155 |
+
2. <b>Enter your desired prompt</b>.
|
| 156 |
3. Click the <b>Submit</b> button to begin customization.
|
| 157 |
4. Share your stylized photo with your friends and enjoy! π
|
| 158 |
|
| 159 |
Advanced usage:<br>
|
| 160 |
1. Click advanced options.
|
| 161 |
2. Choose different guidance and steps.
|
| 162 |
+
3. Set the timing for the Teacher Model's participation.
|
| 163 |
+
4. Feel free to discontinue using the Cross-Modal AdaIN and the Teacher Model for result comparison.
|
| 164 |
"""
|
| 165 |
|
| 166 |
article = r"""
|
| 167 |
---
|
| 168 |
π **Tips**
|
| 169 |
+
<br>
|
| 170 |
+
1. As the value of end_fusion <b>increases</b>, the style gradually diminishes.
|
| 171 |
+
Therefore, it is suggested to set end_fusion to be between <b>1/5 and 1/3</b> of the number of inference steps (num inference steps).
|
| 172 |
+
2. If you want to experience style-based CFG, see the details on the <a href="https://github.com/Westlake-AGI-Lab/StyleStudio">GitHub repo</a>.
|
| 173 |
+
|
| 174 |
---
|
| 175 |
π **Citation**
|
| 176 |
<br>
|
|
|
|
| 195 |
with gr.Column():
|
| 196 |
style_image_pil = gr.Image(label="Style Image", type='pil')
|
| 197 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
prompt = gr.Textbox(label="Prompt",
|
| 199 |
value="A red apple")
|
| 200 |
|
|
|
|
| 205 |
|
| 206 |
guidance_scale = gr.Slider(minimum=1, maximum=15.0, step=0.01, value=7.0, label="guidance scale")
|
| 207 |
|
| 208 |
+
num_inference_steps = gr.Slider(minimum=5, maximum=200.0, step=1.0, value=50,
|
| 209 |
label="num inference steps")
|
| 210 |
|
| 211 |
+
end_fusion = gr.Slider(minimum=0, maximum=200, step=1.0, value=20.0, label="end fusion")
|
| 212 |
|
| 213 |
+
seed = gr.Slider(minimum=-1000000, maximum=1000000, value=42, step=1, label="Seed Value")
|
| 214 |
|
| 215 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=False)
|
| 216 |
|
| 217 |
crossModalAdaIN = gr.Checkbox(label="Cross Modal AdaIN", value=True)
|
| 218 |
use_SAttn = gr.Checkbox(label="Teacher Model", value=True)
|
|
|
|
| 233 |
inputs=[
|
| 234 |
style_image_pil,
|
| 235 |
prompt,
|
| 236 |
+
neg_prompt,
|
| 237 |
guidance_scale,
|
| 238 |
num_inference_steps,
|
| 239 |
end_fusion,
|
| 240 |
crossModalAdaIN,
|
| 241 |
use_SAttn,
|
| 242 |
+
seed,],
|
|
|
|
| 243 |
outputs=[generated_image])
|
| 244 |
|
| 245 |
gr.Examples(
|
| 246 |
examples=get_example(),
|
| 247 |
+
inputs=[style_image_pil, prompt, guidance_scale, seed, end_fusion],
|
| 248 |
fn=run_for_examples,
|
| 249 |
outputs=[generated_image],
|
| 250 |
cache_examples=False,
|
app_exp.py
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
sys.path.append("./")
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import spaces
|
| 5 |
+
import torch
|
| 6 |
+
from ip_adapter.utils import BLOCKS as BLOCKS
|
| 7 |
+
import numpy as np
|
| 8 |
+
import random
|
| 9 |
+
from diffusers import (
|
| 10 |
+
AutoencoderKL,
|
| 11 |
+
StableDiffusionXLPipeline,
|
| 12 |
+
)
|
| 13 |
+
from ip_adapter import StyleStudio_Adapter
|
| 14 |
+
|
| 15 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 16 |
+
dtype = torch.float16 if str(device).__contains__("cuda") else torch.float32
|
| 17 |
+
base_model_path = "/mnt/agilab/models/sdxl"
|
| 18 |
+
image_encoder_path = "/mnt/agilab/models/ipadapter_sdxl/image_encoder"
|
| 19 |
+
csgo_ckpt = "/mnt/agilab/models/CSGO/csgo_4_32.bin"
|
| 20 |
+
pretrained_vae_name_or_path = '/mnt/agilab/models/madebyollin_sdxl-vae-fp16-fix'
|
| 21 |
+
weight_dtype = torch.float16
|
| 22 |
+
|
| 23 |
+
vae = AutoencoderKL.from_pretrained(pretrained_vae_name_or_path,torch_dtype=torch.float16)
|
| 24 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 25 |
+
base_model_path,
|
| 26 |
+
torch_dtype=torch.float16,
|
| 27 |
+
add_watermarker=False,
|
| 28 |
+
vae=vae
|
| 29 |
+
)
|
| 30 |
+
pipe.enable_vae_tiling()
|
| 31 |
+
|
| 32 |
+
target_style_blocks = BLOCKS['style']
|
| 33 |
+
|
| 34 |
+
csgo = StyleStudio_Adapter(
|
| 35 |
+
pipe, image_encoder_path, csgo_ckpt, device, num_style_tokens=32,
|
| 36 |
+
target_style_blocks=target_style_blocks,
|
| 37 |
+
controlnet_adapter=False,
|
| 38 |
+
style_model_resampler=True,
|
| 39 |
+
|
| 40 |
+
fuSAttn=True,
|
| 41 |
+
end_fusion=20,
|
| 42 |
+
adainIP=True,
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def get_example():
|
| 49 |
+
case = [
|
| 50 |
+
[
|
| 51 |
+
'./assets/style1.jpg',
|
| 52 |
+
"A red apple",
|
| 53 |
+
7.0,
|
| 54 |
+
42,
|
| 55 |
+
10,
|
| 56 |
+
],
|
| 57 |
+
[
|
| 58 |
+
'./assets/style2.jpg',
|
| 59 |
+
"A black car",
|
| 60 |
+
7.0,
|
| 61 |
+
42,
|
| 62 |
+
10,
|
| 63 |
+
],
|
| 64 |
+
[
|
| 65 |
+
'./assets/style3.jpg',
|
| 66 |
+
"A orange bus",
|
| 67 |
+
7.0,
|
| 68 |
+
42,
|
| 69 |
+
10,
|
| 70 |
+
],
|
| 71 |
+
]
|
| 72 |
+
return case
|
| 73 |
+
|
| 74 |
+
def run_for_examples(style_image_pil, prompt, guidance_scale, seed, end_fusion):
|
| 75 |
+
|
| 76 |
+
return create_image(
|
| 77 |
+
style_image_pil=style_image_pil,
|
| 78 |
+
prompt=prompt,
|
| 79 |
+
neg_prompt="text, watermark, lowres, low quality, worst quality, deformed, glitch, low contrast, noisy, saturation, blurry",
|
| 80 |
+
guidance_scale=guidance_scale,
|
| 81 |
+
num_inference_steps=50,
|
| 82 |
+
seed=seed,
|
| 83 |
+
end_fusion=end_fusion,
|
| 84 |
+
use_SAttn=True,
|
| 85 |
+
crossModalAdaIN=True,
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
| 89 |
+
if randomize_seed:
|
| 90 |
+
seed = random.randint(0, MAX_SEED)
|
| 91 |
+
return seed
|
| 92 |
+
|
| 93 |
+
def create_image(style_image_pil,
|
| 94 |
+
prompt,
|
| 95 |
+
neg_prompt="text, watermark, lowres, low quality, worst quality, deformed, glitch, low contrast, noisy, saturation, blurry",
|
| 96 |
+
guidance_scale=7,
|
| 97 |
+
num_inference_steps=50,
|
| 98 |
+
end_fusion=20,
|
| 99 |
+
crossModalAdaIN=True,
|
| 100 |
+
use_SAttn=True,
|
| 101 |
+
seed=42,
|
| 102 |
+
):
|
| 103 |
+
|
| 104 |
+
style_image = style_image_pil
|
| 105 |
+
|
| 106 |
+
generator = torch.Generator(device).manual_seed(seed)
|
| 107 |
+
init_latents = torch.randn((1, 4, 128, 128), generator=generator, device="cuda", dtype=torch.float16)
|
| 108 |
+
num_sample=1
|
| 109 |
+
if use_SAttn:
|
| 110 |
+
num_sample=2
|
| 111 |
+
init_latents = init_latents.repeat(num_sample, 1, 1, 1)
|
| 112 |
+
with torch.no_grad():
|
| 113 |
+
images = csgo.generate(pil_style_image=style_image,
|
| 114 |
+
prompt=prompt,
|
| 115 |
+
negative_prompt=neg_prompt,
|
| 116 |
+
height=1024,
|
| 117 |
+
width=1024,
|
| 118 |
+
guidance_scale=guidance_scale,
|
| 119 |
+
num_images_per_prompt=1,
|
| 120 |
+
num_samples=num_sample,
|
| 121 |
+
num_inference_steps=num_inference_steps,
|
| 122 |
+
end_fusion=end_fusion,
|
| 123 |
+
cross_modal_adain=crossModalAdaIN,
|
| 124 |
+
use_SAttn=use_SAttn,
|
| 125 |
+
|
| 126 |
+
generator=generator,
|
| 127 |
+
latents=init_latents,
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
if use_SAttn:
|
| 131 |
+
return [images[1]]
|
| 132 |
+
else:
|
| 133 |
+
return [images[0]]
|
| 134 |
+
|
| 135 |
+
# Description
|
| 136 |
+
title = r"""
|
| 137 |
+
<h1 align="center">StyleStudio: Text-Driven Style Transfer with Selective Control of Style Elements</h1>
|
| 138 |
+
"""
|
| 139 |
+
|
| 140 |
+
description = r"""
|
| 141 |
+
<b>Official π€ Gradio demo</b> for <a href='https://github.com/Westlake-AGI-Lab/StyleStudio' target='_blank'><b>StyleStudio: Text-Driven Style Transfer with Selective Control of Style Elements</b></a>.<br>
|
| 142 |
+
How to use:<br>
|
| 143 |
+
1. Upload a style image.
|
| 144 |
+
2. <b>Enter your desired prompt</b>.
|
| 145 |
+
3. Click the <b>Submit</b> button to begin customization.
|
| 146 |
+
4. Share your stylized photo with your friends and enjoy! π
|
| 147 |
+
|
| 148 |
+
Advanced usage:<br>
|
| 149 |
+
1. Click advanced options.
|
| 150 |
+
2. Choose different guidance and steps.
|
| 151 |
+
3. Set the timing for the Teacher Model's participation.
|
| 152 |
+
4.
|
| 153 |
+
"""
|
| 154 |
+
|
| 155 |
+
article = r"""
|
| 156 |
+
---
|
| 157 |
+
π **Tips**
|
| 158 |
+
<br>
|
| 159 |
+
1. As the value of end_fusion <b>increases</b>, the style gradually diminishes.
|
| 160 |
+
Therefore, it is suggested to set end_fusion to be between 1/5 and 1/3 of the number of inference steps (num inference steps).
|
| 161 |
+
2. If you want to experience style-based CFG, see the details on the <a href="https://github.com/Westlake-AGI-Lab/StyleStudio">GitHub repo</a>.
|
| 162 |
+
|
| 163 |
+
---
|
| 164 |
+
π **Citation**
|
| 165 |
+
<br>
|
| 166 |
+
If our work is helpful for your research or applications, please cite us via:
|
| 167 |
+
```bibtex
|
| 168 |
+
|
| 169 |
+
```
|
| 170 |
+
π§ **Contact**
|
| 171 |
+
<br>
|
| 172 |
+
If you have any questions, please feel free to open an issue or directly reach us out at <b>[email protected]</b>.
|
| 173 |
+
"""
|
| 174 |
+
|
| 175 |
+
block = gr.Blocks(css="footer {visibility: hidden}").queue(max_size=10, api_open=False)
|
| 176 |
+
with block:
|
| 177 |
+
gr.Markdown(title)
|
| 178 |
+
gr.Markdown(description)
|
| 179 |
+
|
| 180 |
+
with gr.Tabs():
|
| 181 |
+
with gr.Row():
|
| 182 |
+
with gr.Column():
|
| 183 |
+
with gr.Row():
|
| 184 |
+
with gr.Column():
|
| 185 |
+
style_image_pil = gr.Image(label="Style Image", type='pil')
|
| 186 |
+
|
| 187 |
+
prompt = gr.Textbox(label="Prompt",
|
| 188 |
+
value="A red apple")
|
| 189 |
+
|
| 190 |
+
neg_prompt = gr.Textbox(label="Negative Prompt",
|
| 191 |
+
value="text, watermark, lowres, low quality, worst quality, deformed, glitch, low contrast, noisy, saturation, blurry")
|
| 192 |
+
|
| 193 |
+
with gr.Accordion(open=True, label="Advanced Options"):
|
| 194 |
+
|
| 195 |
+
guidance_scale = gr.Slider(minimum=1, maximum=15.0, step=0.01, value=7.0, label="guidance scale")
|
| 196 |
+
|
| 197 |
+
num_inference_steps = gr.Slider(minimum=5, maximum=200.0, step=1.0, value=50,
|
| 198 |
+
label="num inference steps")
|
| 199 |
+
|
| 200 |
+
end_fusion = gr.Slider(minimum=0, maximum=200, step=1.0, value=20.0, label="end fusion")
|
| 201 |
+
|
| 202 |
+
seed = gr.Slider(minimum=-1000000, maximum=1000000, value=42, step=1, label="Seed Value")
|
| 203 |
+
|
| 204 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=False)
|
| 205 |
+
|
| 206 |
+
crossModalAdaIN = gr.Checkbox(label="Cross Modal AdaIN", value=True)
|
| 207 |
+
use_SAttn = gr.Checkbox(label="Teacher Model", value=True)
|
| 208 |
+
|
| 209 |
+
generate_button = gr.Button("Generate Image")
|
| 210 |
+
|
| 211 |
+
with gr.Column():
|
| 212 |
+
generated_image = gr.Gallery(label="Generated Image")
|
| 213 |
+
|
| 214 |
+
generate_button.click(
|
| 215 |
+
fn=randomize_seed_fn,
|
| 216 |
+
inputs=[seed, randomize_seed],
|
| 217 |
+
outputs=seed,
|
| 218 |
+
queue=False,
|
| 219 |
+
api_name=False,
|
| 220 |
+
).then(
|
| 221 |
+
fn=create_image,
|
| 222 |
+
inputs=[
|
| 223 |
+
style_image_pil,
|
| 224 |
+
prompt,
|
| 225 |
+
neg_prompt,
|
| 226 |
+
guidance_scale,
|
| 227 |
+
num_inference_steps,
|
| 228 |
+
end_fusion,
|
| 229 |
+
crossModalAdaIN,
|
| 230 |
+
use_SAttn,
|
| 231 |
+
seed,],
|
| 232 |
+
outputs=[generated_image])
|
| 233 |
+
|
| 234 |
+
gr.Examples(
|
| 235 |
+
examples=get_example(),
|
| 236 |
+
inputs=[style_image_pil, prompt, guidance_scale, seed, end_fusion],
|
| 237 |
+
fn=run_for_examples,
|
| 238 |
+
outputs=[generated_image],
|
| 239 |
+
cache_examples=False,
|
| 240 |
+
)
|
| 241 |
+
|
| 242 |
+
gr.Markdown(article)
|
| 243 |
+
|
| 244 |
+
block.launch(server_name="0.0.0.0", server_port=1234)
|
assets/style3.jpg
ADDED
|
ip_adapter/__pycache__/__init__.cpython-39.pyc
CHANGED
|
Binary files a/ip_adapter/__pycache__/__init__.cpython-39.pyc and b/ip_adapter/__pycache__/__init__.cpython-39.pyc differ
|
|
|
ip_adapter/__pycache__/attention_processor.cpython-39.pyc
CHANGED
|
Binary files a/ip_adapter/__pycache__/attention_processor.cpython-39.pyc and b/ip_adapter/__pycache__/attention_processor.cpython-39.pyc differ
|
|
|
ip_adapter/__pycache__/ip_adapter.cpython-39.pyc
CHANGED
|
Binary files a/ip_adapter/__pycache__/ip_adapter.cpython-39.pyc and b/ip_adapter/__pycache__/ip_adapter.cpython-39.pyc differ
|
|
|
ip_adapter/__pycache__/resampler.cpython-39.pyc
CHANGED
|
Binary files a/ip_adapter/__pycache__/resampler.cpython-39.pyc and b/ip_adapter/__pycache__/resampler.cpython-39.pyc differ
|
|
|
ip_adapter/__pycache__/utils.cpython-39.pyc
CHANGED
|
Binary files a/ip_adapter/__pycache__/utils.cpython-39.pyc and b/ip_adapter/__pycache__/utils.cpython-39.pyc differ
|
|
|
ip_adapter/attention_processor.py
CHANGED
|
@@ -838,6 +838,8 @@ class AttnProcessor2_0_hijack(torch.nn.Module):
|
|
| 838 |
# the output of sdp = (batch, num_heads, seq_len, head_dim)
|
| 839 |
# TODO: add support for attn.scale when we move to Torch 2.1
|
| 840 |
if self.fuSAttn and self.denoise_step <= self.end_fusion:
|
|
|
|
|
|
|
| 841 |
assert query.shape[0] == 4
|
| 842 |
scale_factor = 1 / math.sqrt(torch.tensor(head_dim, dtype=query.dtype))
|
| 843 |
attn_probs = (torch.matmul(query, key.transpose(-2, -1)) * scale_factor).softmax(dim=-1)
|
|
|
|
| 838 |
# the output of sdp = (batch, num_heads, seq_len, head_dim)
|
| 839 |
# TODO: add support for attn.scale when we move to Torch 2.1
|
| 840 |
if self.fuSAttn and self.denoise_step <= self.end_fusion:
|
| 841 |
+
if self.end_fusion == 0:
|
| 842 |
+
print("yes")
|
| 843 |
assert query.shape[0] == 4
|
| 844 |
scale_factor = 1 / math.sqrt(torch.tensor(head_dim, dtype=query.dtype))
|
| 845 |
attn_probs = (torch.matmul(query, key.transpose(-2, -1)) * scale_factor).softmax(dim=-1)
|
ip_adapter/ip_adapter.py
CHANGED
|
@@ -1121,6 +1121,7 @@ class StyleStudio_Adapter(CSGO):
|
|
| 1121 |
for attn_processor in self.pipe.unet.attn_processors.values():
|
| 1122 |
if isinstance(attn_processor, AttnProcessor_hijack) or isinstance(attn_processor, IPAttnProcessor_cross_modal):
|
| 1123 |
attn_processor.num_inference_step = num_T
|
|
|
|
| 1124 |
|
| 1125 |
def set_adain(self, use_CMA):
|
| 1126 |
for attn_processor in self.pipe.unet.attn_processors.values():
|
|
@@ -1143,7 +1144,7 @@ class StyleStudio_Adapter(CSGO):
|
|
| 1143 |
use_SAttn=True,
|
| 1144 |
**kwargs,
|
| 1145 |
):
|
| 1146 |
-
|
| 1147 |
self.set_endFusion(end_T = end_fusion)
|
| 1148 |
self.set_adain(use_CMA=cross_modal_adain)
|
| 1149 |
self.set_SAttn(use_SAttn=use_SAttn)
|
|
|
|
| 1121 |
for attn_processor in self.pipe.unet.attn_processors.values():
|
| 1122 |
if isinstance(attn_processor, AttnProcessor_hijack) or isinstance(attn_processor, IPAttnProcessor_cross_modal):
|
| 1123 |
attn_processor.num_inference_step = num_T
|
| 1124 |
+
attn_processor.denoise_step = 0
|
| 1125 |
|
| 1126 |
def set_adain(self, use_CMA):
|
| 1127 |
for attn_processor in self.pipe.unet.attn_processors.values():
|
|
|
|
| 1144 |
use_SAttn=True,
|
| 1145 |
**kwargs,
|
| 1146 |
):
|
| 1147 |
+
print(end_fusion)
|
| 1148 |
self.set_endFusion(end_T = end_fusion)
|
| 1149 |
self.set_adain(use_CMA=cross_modal_adain)
|
| 1150 |
self.set_SAttn(use_SAttn=use_SAttn)
|