Spaces:
Running
on
Zero
Running
on
Zero
File size: 9,623 Bytes
f9abe33 1c1d081 cdf0274 1c1d081 12903b1 1c1d081 5ef2207 12903b1 1c1d081 5ef2207 12903b1 480ec1d 1c1d081 06c6140 1c1d081 480ec1d efd00eb 1c1d081 5ef2207 1c1d081 2583838 1c1d081 cdf0274 1c1d081 cdf0274 1c1d081 4d01ceb cdf0274 eb9c2d1 cdf0274 eb9c2d1 1c1d081 1d65d71 1c1d081 9fe1f75 1c1d081 9fe1f75 1c1d081 c044b60 9fe1f75 c044b60 1c1d081 cdf0274 1c1d081 cdf0274 1c1d081 cdf0274 1c1d081 cdf0274 1c1d081 cdf0274 1c1d081 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
import spaces
import sys
sys.path.append('./')
from diffusers import (
StableDiffusionPipeline,
UNet2DConditionModel,
DPMSolverMultistepScheduler,
LCMScheduler
)
from arc2face import CLIPTextModelWrapper, project_face_embs
import torch
from insightface.app import FaceAnalysis
from PIL import Image
import numpy as np
import random
#import os
import gradio as gr
# global variable
MAX_SEED = np.iinfo(np.int32).max
if torch.cuda.is_available():
device = "cuda"
dtype = torch.float16
else:
device = "cpu"
dtype = torch.float32
# download models
from huggingface_hub import hf_hub_download
#from modelscope import snapshot_download
#from modelscope.hub.file_download import model_file_download
hf_hub_download(repo_id="FoivosPar/Arc2Face", filename="arc2face/config.json", local_dir="./models")
hf_hub_download(repo_id="FoivosPar/Arc2Face", filename="arc2face/diffusion_pytorch_model.safetensors", local_dir="./models")
hf_hub_download(repo_id="FoivosPar/Arc2Face", filename="encoder/config.json", local_dir="./models")
hf_hub_download(repo_id="FoivosPar/Arc2Face", filename="encoder/pytorch_model.bin", local_dir="./models")
hf_hub_download(repo_id="FoivosPar/Arc2Face", filename="arcface.onnx", local_dir="./models/antelopev2")
#base_model = snapshot_download('AI-ModelScope/stable-diffusion-v1-5', cache_dir='./models')
#model_dir = model_file_download(model_id='AI-ModelScope/stable-diffusion-v1-5', file_path='model_index.json', cache_dir='./models')
#base_model = os.path.dirname(model_dir)
#_ = model_file_download(model_id='AI-ModelScope/stable-diffusion-v1-5', file_path='scheduler/scheduler_config.json', cache_dir='./models')
#_ = model_file_download(model_id='AI-ModelScope/stable-diffusion-v1-5', file_path='tokenizer/merges.txt', cache_dir='./models')
#_ = model_file_download(model_id='AI-ModelScope/stable-diffusion-v1-5', file_path='tokenizer/special_tokens_map.json', cache_dir='./models')
#_ = model_file_download(model_id='AI-ModelScope/stable-diffusion-v1-5', file_path='tokenizer/tokenizer_config.json', cache_dir='./models')
#_ = model_file_download(model_id='AI-ModelScope/stable-diffusion-v1-5', file_path='tokenizer/vocab.json', cache_dir='./models')
#_ = model_file_download(model_id='AI-ModelScope/stable-diffusion-v1-5', file_path='vae/config.json', cache_dir='./models')
#_ = model_file_download(model_id='AI-ModelScope/stable-diffusion-v1-5', file_path='vae/diffusion_pytorch_model.safetensors', cache_dir='./models')
# Load face detection and recognition package
app = FaceAnalysis(name='antelopev2', root='./', providers=['CPUExecutionProvider'])
app.prepare(ctx_id=0, det_size=(640, 640))
# Load pipeline
#base_model = 'runwayml/stable-diffusion-v1-5'
base_model = 'stable-diffusion-v1-5/stable-diffusion-v1-5'
encoder = CLIPTextModelWrapper.from_pretrained(
'models', subfolder="encoder", torch_dtype=dtype
)
unet = UNet2DConditionModel.from_pretrained(
'models', subfolder="arc2face", torch_dtype=dtype
)
pipeline = StableDiffusionPipeline.from_pretrained(
base_model,
text_encoder=encoder,
unet=unet,
torch_dtype=dtype,
safety_checker=None,
)
pipeline.scheduler = DPMSolverMultistepScheduler.from_config(pipeline.scheduler.config)
pipeline = pipeline.to(device)
# load and disable LCM
pipeline.load_lora_weights("latent-consistency/lcm-lora-sdv1-5")
pipeline.disable_lora()
def toggle_lcm_ui(value):
if value:
return (
gr.update(minimum=1, maximum=20, step=1, value=3),
gr.update(minimum=0.1, maximum=10.0, step=0.1, value=1.0),
)
else:
return (
gr.update(minimum=1, maximum=100, step=1, value=25),
gr.update(minimum=0.1, maximum=10.0, step=0.1, value=3.0),
)
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
if randomize_seed:
seed = random.randint(0, MAX_SEED)
return seed
def get_example():
case = [
[
'./assets/examples/freeman.jpg',
],
[
'./assets/examples/lily.png',
],
[
'./assets/examples/joacquin.png',
],
[
'./assets/examples/jackie.png',
],
[
'./assets/examples/freddie.png',
],
[
'./assets/examples/hepburn.png',
],
]
return case
def run_example(img_file):
return generate_image(img_file, 25, 3, 23, 2, False)
@spaces.GPU
def generate_image(image_path, num_steps, guidance_scale, seed, num_images, use_lcm, progress=gr.Progress(track_tqdm=True)):
if use_lcm:
pipeline.scheduler = LCMScheduler.from_config(pipeline.scheduler.config)
pipeline.enable_lora()
pipeline.to(device)
else:
pipeline.disable_lora()
pipeline.scheduler = DPMSolverMultistepScheduler.from_config(pipeline.scheduler.config)
pipeline.to(device)
if image_path is None:
raise gr.Error(f"Cannot find any input face image! Please upload a face image.")
img = np.array(Image.open(image_path))[:,:,::-1]
# Face detection and ID-embedding extraction
faces = app.get(img)
if len(faces) == 0:
raise gr.Error(f"Face detection failed! Please try with another image.")
faces = sorted(faces, key=lambda x:(x['bbox'][2]-x['bbox'][0])*(x['bbox'][3]-x['bbox'][1]))[-1] # select largest face (if more than one detected)
id_emb = torch.tensor(faces['embedding'], dtype=dtype)[None].to(device)
id_emb = id_emb/torch.norm(id_emb, dim=1, keepdim=True) # normalize embedding
id_emb = project_face_embs(pipeline, id_emb) # pass throught the encoder
generator = torch.Generator(device=device).manual_seed(seed)
print("Start inference...")
images = pipeline(
prompt_embeds=id_emb,
num_inference_steps=num_steps,
guidance_scale=guidance_scale,
num_images_per_prompt=num_images,
generator=generator
).images
return images
### Description
title = r"""
<h1>Arc2Face: A Foundation Model for ID-Consistent Human Faces</h1>
"""
description = r"""
<b>Official 🤗 Gradio demo</b> for <a href='https://arc2face.github.io/' target='_blank'><b>Arc2Face: A Foundation Model for ID-Consistent Human Faces</b></a>.<br>
Steps:<br>
1. Upload an image with a face. If multiple faces are detected, we use the largest one. For images with already tightly cropped faces, detection may fail, try images with a larger margin.
2. Click <b>Submit</b> to generate new images of the subject.
"""
Footer = r"""
---
📝 **Citation**
<br>
If you find Arc2Face helpful for your research, please consider citing our paper:
```bibtex
@inproceedings{paraperas2024arc2face,
title={Arc2Face: A Foundation Model for ID-Consistent Human Faces},
author={Paraperas Papantoniou, Foivos and Lattas, Alexandros and Moschoglou, Stylianos and Deng, Jiankang and Kainz, Bernhard and Zafeiriou, Stefanos},
booktitle={Proceedings of the European Conference on Computer Vision (ECCV)},
year={2024}
}
```
"""
css = '''
.gradio-container {width: 85% !important}
'''
with gr.Blocks(css=css) as demo:
# description
gr.Markdown(title)
gr.Markdown(description)
with gr.Row():
with gr.Column():
# upload face image
img_file = gr.Image(label="Upload a photo with a face", type="filepath")
submit = gr.Button("Submit", variant="primary")
use_lcm = gr.Checkbox(
label="Use LCM-LoRA to accelerate sampling", value=False,
info="Reduces sampling steps significantly, but may decrease quality.",
)
with gr.Accordion(open=False, label="Advanced Options"):
num_steps = gr.Slider(
label="Number of sample steps",
minimum=1,
maximum=100,
step=1,
value=25,
)
guidance_scale = gr.Slider(
label="Guidance scale",
minimum=0.1,
maximum=10.0,
step=0.1,
value=3.0,
)
num_images = gr.Slider(
label="Number of output images",
minimum=1,
maximum=4,
step=1,
value=2,
)
seed = gr.Slider(
label="Seed",
minimum=0,
maximum=MAX_SEED,
step=1,
value=0,
)
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
with gr.Column():
gallery = gr.Gallery(label="Generated Images")
submit.click(
fn=randomize_seed_fn,
inputs=[seed, randomize_seed],
outputs=seed,
queue=False,
api_name=False,
).then(
fn=generate_image,
inputs=[img_file, num_steps, guidance_scale, seed, num_images, use_lcm],
outputs=[gallery]
)
use_lcm.input(
fn=toggle_lcm_ui,
inputs=[use_lcm],
outputs=[num_steps, guidance_scale],
queue=False,
)
gr.Examples(
examples=get_example(),
inputs=[img_file],
run_on_click=True,
fn=run_example,
outputs=[gallery],
)
gr.Markdown(Footer)
demo.launch() |