Spaces:
Running
on
Zero
Running
on
Zero
rizavelioglu
commited on
Commit
•
1ca8185
1
Parent(s):
05c2963
v1
Browse files- app.py +168 -0
- examples/00084_00.jpg +0 -0
- examples/00151_00.jpg +0 -0
- examples/00254_00.jpg +0 -0
- examples/00345_00.jpg +0 -0
- examples/00348_00.jpg +0 -0
- examples/00397_00.jpg +0 -0
- examples/00475_00.jpg +0 -0
- examples/00617_00.jpg +0 -0
- examples/01229_00.jpg +0 -0
- examples/01242_00.jpg +0 -0
- examples/01320_00.jpg +0 -0
- examples/01399_00.jpg +0 -0
- examples/01486_00.jpg +0 -0
- examples/02244_00.jpg +0 -0
- examples/02390_00.jpg +0 -0
- examples/02871_00.jpg +0 -0
- examples/03199_00.jpg +0 -0
- examples/05537_00.jpg +0 -0
- examples/07160_00.jpg +0 -0
- examples/07194_00.jpg +0 -0
- examples/07208_00.jpg +0 -0
- examples/11967_00.jpg +0 -0
- examples/13912_00.jpg +0 -0
- examples/14227_00.jpg +0 -0
- requirements.txt +10 -0
app.py
ADDED
@@ -0,0 +1,168 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import spaces
|
4 |
+
import torch
|
5 |
+
import torch.nn as nn
|
6 |
+
from diffusers import EulerDiscreteScheduler, AutoencoderKL, UNet2DConditionModel
|
7 |
+
from huggingface_hub import hf_hub_download
|
8 |
+
from transformers import SiglipImageProcessor, SiglipVisionModel
|
9 |
+
from torchvision.io import read_image
|
10 |
+
import torchvision.transforms.v2 as transforms
|
11 |
+
from torchvision.utils import make_grid
|
12 |
+
|
13 |
+
|
14 |
+
class TryOffDiff(nn.Module):
|
15 |
+
def __init__(self):
|
16 |
+
super().__init__()
|
17 |
+
self.unet = UNet2DConditionModel.from_pretrained("CompVis/stable-diffusion-v1-4", subfolder="unet")
|
18 |
+
self.transformer = torch.nn.TransformerEncoderLayer(d_model=768, nhead=8, batch_first=True)
|
19 |
+
self.proj = nn.Linear(1024, 77)
|
20 |
+
self.norm = nn.LayerNorm(768)
|
21 |
+
|
22 |
+
def adapt_embeddings(self, x):
|
23 |
+
x = self.transformer(x)
|
24 |
+
x = self.proj(x.permute(0, 2, 1)).permute(0, 2, 1)
|
25 |
+
return self.norm(x)
|
26 |
+
|
27 |
+
def forward(self, noisy_latents, t, cond_emb):
|
28 |
+
cond_emb = self.adapt_embeddings(cond_emb)
|
29 |
+
return self.unet(noisy_latents, t, encoder_hidden_states=cond_emb).sample
|
30 |
+
|
31 |
+
|
32 |
+
class PadToSquare:
|
33 |
+
def __call__(self, img):
|
34 |
+
_, h, w = img.shape # Get the original dimensions
|
35 |
+
max_side = max(h, w)
|
36 |
+
pad_h = (max_side - h) // 2
|
37 |
+
pad_w = (max_side - w) // 2
|
38 |
+
padding = (pad_w, pad_h, max_side - w - pad_w, max_side - h - pad_h)
|
39 |
+
return transforms.functional.pad(img, padding, padding_mode="edge")
|
40 |
+
|
41 |
+
|
42 |
+
# Set device
|
43 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
44 |
+
|
45 |
+
# Initialize Image Encoder
|
46 |
+
img_processor = SiglipImageProcessor.from_pretrained(
|
47 |
+
"google/siglip-base-patch16-512",
|
48 |
+
do_resize=False,
|
49 |
+
do_rescale=False,
|
50 |
+
do_normalize=False
|
51 |
+
)
|
52 |
+
img_enc = SiglipVisionModel.from_pretrained("google/siglip-base-patch16-512").eval().to(device)
|
53 |
+
img_enc_transform = transforms.Compose([
|
54 |
+
PadToSquare(), # Custom transform to pad the image to a square
|
55 |
+
transforms.Resize((512, 512)),
|
56 |
+
transforms.ToDtype(torch.float32, scale=True),
|
57 |
+
transforms.Normalize(mean=[0.5], std=[0.5]),
|
58 |
+
])
|
59 |
+
|
60 |
+
# Load TryOffDiff Model
|
61 |
+
path_model = hf_hub_download(
|
62 |
+
repo_id="rizavelioglu/tryoffdiff",
|
63 |
+
filename="tryoffdiff.pth", # or one of ["ldm-1", "ldm-2", "ldm-3", ...],
|
64 |
+
force_download=False
|
65 |
+
)
|
66 |
+
path_scheduler = hf_hub_download(
|
67 |
+
repo_id="rizavelioglu/tryoffdiff",
|
68 |
+
filename="scheduler/scheduler_config.json",
|
69 |
+
force_download=False
|
70 |
+
)
|
71 |
+
net = TryOffDiff()
|
72 |
+
net.load_state_dict(torch.load(path_model, weights_only=False))
|
73 |
+
net.eval().to(device)
|
74 |
+
|
75 |
+
# Initialize VAE (only Decoder will be used)
|
76 |
+
vae = AutoencoderKL.from_pretrained("CompVis/stable-diffusion-v1-4", subfolder="vae").eval().to(device)
|
77 |
+
torch.cuda.empty_cache()
|
78 |
+
|
79 |
+
|
80 |
+
# Define image generation function
|
81 |
+
@spaces.GPU(duration=75)
|
82 |
+
@torch.no_grad()
|
83 |
+
def generate_image(input_image, seed=42, guidance_scale=2.0, num_inference_steps=50, is_upscale=False):
|
84 |
+
# Configure scheduler
|
85 |
+
scheduler = EulerDiscreteScheduler.from_pretrained(path_scheduler)
|
86 |
+
scheduler.is_scale_input_called = True # suppress warning
|
87 |
+
scheduler.set_timesteps(num_inference_steps)
|
88 |
+
|
89 |
+
# Set random seed
|
90 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
91 |
+
x = torch.randn(1, 4, 64, 64, generator=generator, device=device)
|
92 |
+
|
93 |
+
# Process input image
|
94 |
+
cond_image = img_enc_transform(read_image(input_image))
|
95 |
+
inputs = {k: v.to(img_enc.device) for k, v in img_processor(images=cond_image, return_tensors="pt").items()}
|
96 |
+
cond_emb = img_enc(**inputs).last_hidden_state.to(device)
|
97 |
+
|
98 |
+
# Prepare unconditioned embeddings
|
99 |
+
uncond_emb = torch.zeros_like(cond_emb) if guidance_scale > 1 else None
|
100 |
+
|
101 |
+
# Denoising loop with mixed precision
|
102 |
+
with torch.autocast(device):
|
103 |
+
for t in scheduler.timesteps:
|
104 |
+
if guidance_scale > 1:
|
105 |
+
noise_pred = net(
|
106 |
+
torch.cat([x] * 2), t, torch.cat([uncond_emb, cond_emb])
|
107 |
+
).chunk(2)
|
108 |
+
noise_pred = noise_pred[0] + guidance_scale * (noise_pred[1] - noise_pred[0])
|
109 |
+
else:
|
110 |
+
noise_pred = net(x, t, cond_emb)
|
111 |
+
|
112 |
+
scheduler_output = scheduler.step(noise_pred, t, x)
|
113 |
+
x = scheduler_output.prev_sample
|
114 |
+
|
115 |
+
# Decode preds
|
116 |
+
decoded = vae.decode(1 / 0.18215 * scheduler_output.pred_original_sample).sample
|
117 |
+
images = (decoded / 2 + 0.5).cpu()
|
118 |
+
|
119 |
+
# Create grid
|
120 |
+
grid = make_grid(images, nrow=len(images), normalize=True, scale_each=True)
|
121 |
+
if is_upscale:
|
122 |
+
pass
|
123 |
+
else:
|
124 |
+
return transforms.ToPILImage()(grid)
|
125 |
+
|
126 |
+
|
127 |
+
title = "Virtual Try-Off Generator"
|
128 |
+
description = r"""
|
129 |
+
This is the demo of the paper <a href="https://arxiv.org/abs/2411.18350">TryOffDiff: Virtual-Try-Off via High-Fidelity Garment Reconstruction using Diffusion Models</a>.
|
130 |
+
<br>Upload an image of a clothed individual to generate a standardized garment image using TryOffDiff.
|
131 |
+
<br> Check out the <a href="https://rizavelioglu.github.io/tryoffdiff/">project page</a> for more information.
|
132 |
+
"""
|
133 |
+
article = r"""
|
134 |
+
Example images are sampled from the `VITON-HD-test` set, which the models did not see during training.
|
135 |
+
|
136 |
+
<br>**Citation** <br>If you find our work useful in your research, please consider giving a star ⭐ and
|
137 |
+
a citation:
|
138 |
+
```
|
139 |
+
@article{velioglu2024tryoffdiff,
|
140 |
+
title = {TryOffDiff: Virtual-Try-Off via High-Fidelity Garment Reconstruction using Diffusion Models},
|
141 |
+
author = {Velioglu, Riza and Bevandic, Petra and Chan, Robin and Hammer, Barbara},
|
142 |
+
journal = {arXiv},
|
143 |
+
year = {2024},
|
144 |
+
note = {\url{https://doi.org/nt3n}}
|
145 |
+
}
|
146 |
+
```
|
147 |
+
"""
|
148 |
+
examples = [[f"examples/{img_filename}", 42, 2.0, 20, False] for img_filename in sorted(os.listdir("examples/"))]
|
149 |
+
|
150 |
+
# Create Gradio App
|
151 |
+
demo = gr.Interface(
|
152 |
+
fn=generate_image,
|
153 |
+
inputs=[
|
154 |
+
gr.Image(type="filepath", label="Reference Image"),
|
155 |
+
gr.Slider(value=42, minimum=0, maximum=1e6, step=1, label="Seed"),
|
156 |
+
gr.Slider(value=2.0, minimum=1, maximum=5, step=0.5, label="Guidance Scale(s)", info="No guidance applied at s=1, hence faster inference."),
|
157 |
+
gr.Slider(value=20, minimum=0, maximum=1000, step=10, label="# of Inference Steps"),
|
158 |
+
gr.Checkbox(value=False, label="Upscale Output")
|
159 |
+
],
|
160 |
+
outputs=gr.Image(type="pil", label="Generated Garment", height=512, width=512),
|
161 |
+
title=title,
|
162 |
+
description=description,
|
163 |
+
article=article,
|
164 |
+
examples=examples,
|
165 |
+
examples_per_page=4,
|
166 |
+
)
|
167 |
+
|
168 |
+
demo.launch()
|
examples/00084_00.jpg
ADDED
examples/00151_00.jpg
ADDED
examples/00254_00.jpg
ADDED
examples/00345_00.jpg
ADDED
examples/00348_00.jpg
ADDED
examples/00397_00.jpg
ADDED
examples/00475_00.jpg
ADDED
examples/00617_00.jpg
ADDED
examples/01229_00.jpg
ADDED
examples/01242_00.jpg
ADDED
examples/01320_00.jpg
ADDED
examples/01399_00.jpg
ADDED
examples/01486_00.jpg
ADDED
examples/02244_00.jpg
ADDED
examples/02390_00.jpg
ADDED
examples/02871_00.jpg
ADDED
examples/03199_00.jpg
ADDED
examples/05537_00.jpg
ADDED
examples/07160_00.jpg
ADDED
examples/07194_00.jpg
ADDED
examples/07208_00.jpg
ADDED
examples/11967_00.jpg
ADDED
examples/13912_00.jpg
ADDED
examples/14227_00.jpg
ADDED
requirements.txt
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch>=2.4.0
|
2 |
+
torchvision>=0.19.0
|
3 |
+
diffusers>=0.29.2
|
4 |
+
transformers>=4.37.2
|
5 |
+
gradio>=5.7.0
|
6 |
+
spaces>=0.30.4
|
7 |
+
huggingface-hub>=0.26.2
|
8 |
+
#torchvision>=0.20.1
|
9 |
+
#diffusers>=0.31.0
|
10 |
+
#transformers>=4.46.3
|