File size: 5,158 Bytes
74a242e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import TypedDict

import diffusers.image_processor
import gradio as gr
import pillow_heif  # pyright: ignore[reportMissingTypeStubs]
import spaces  # pyright: ignore[reportMissingTypeStubs]
import torch
from PIL import Image

from pipeline import TryOffAnyone

pillow_heif.register_heif_opener()  # pyright: ignore[reportUnknownMemberType]
pillow_heif.register_avif_opener()  # pyright: ignore[reportUnknownMemberType]

torch.set_float32_matmul_precision("high")
torch.backends.cuda.matmul.allow_tf32 = True

TITLE = """
# Try Off Anyone

## ⚠️ Important

1. Choose an example image or upload your own
2. Use the Pen tool to draw a mask over the clothing area you want to extract

[![arxiv badge](https://img.shields.io/badge/arXiv-Paper-b31b1b.svg?style=for-the-badge)](https://arxiv.org/abs/2412.08573)
"""

DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
DTYPE = torch.bfloat16 if torch.cuda.is_bf16_supported() else torch.float32

pipeline_tryoff = TryOffAnyone(
    device=DEVICE,
    dtype=DTYPE,
)
mask_processor = diffusers.image_processor.VaeImageProcessor(
    vae_scale_factor=8,
    do_normalize=False,
    do_binarize=True,
    do_convert_grayscale=True,
)
vae_processor = diffusers.image_processor.VaeImageProcessor(
    vae_scale_factor=8,
)


class ImageData(TypedDict):
    background: Image.Image
    composite: Image.Image
    layers: list[Image.Image]


@spaces.GPU
def process(
    image_data: ImageData,
    image_width: int,
    image_height: int,
    num_inference_steps: int,
    condition_scale: float,
    seed: int,
) -> Image.Image:
    assert image_width > 0
    assert image_height > 0
    assert num_inference_steps > 0
    assert condition_scale > 0
    assert seed >= 0

    # extract image and mask from image_data
    image = image_data["background"]
    mask = image_data["layers"][0]

    # preprocess image
    image = image.convert("RGB").resize((image_width, image_height))
    image_preprocessed = vae_processor.preprocess(  # pyright: ignore[reportUnknownMemberType,reportAssignmentType]
        image=image,
        width=image_width,
        height=image_height,
    )[0]

    # preprocess mask
    mask = mask.getchannel("A").resize((image_width, image_height))
    mask_preprocessed = mask_processor.preprocess(  # pyright: ignore[reportUnknownMemberType]
        image=mask,
        width=image_width,
        height=image_height,
    )[0]

    # generate the TryOff image
    gen = torch.Generator(device=DEVICE).manual_seed(seed)
    tryoff_image = pipeline_tryoff(
        image_preprocessed,
        mask_preprocessed,
        inference_steps=num_inference_steps,
        scale=condition_scale,
        generator=gen,
    )[0]

    return tryoff_image


with gr.Blocks() as demo:
    gr.Markdown(TITLE)

    with gr.Row():
        with gr.Column():
            input_image = gr.ImageMask(
                label="Input Image",
                height=1024,  # https://github.com/gradio-app/gradio/issues/10236
                type="pil",
                interactive=True,
            )
            run_button = gr.Button(
                value="Extract Clothing",
            )
            gr.Examples(
                examples=[
                    ["examples/model_1.jpg"],
                    ["examples/model_2.jpg"],
                    ["examples/model_3.jpg"],
                    ["examples/model_4.jpg"],
                    ["examples/model_5.jpg"],
                    ["examples/model_6.jpg"],
                    ["examples/model_7.jpg"],
                    ["examples/model_8.jpg"],
                    ["examples/model_9.jpg"],
                ],
                inputs=[input_image],
            )
        with gr.Column():
            output_image = gr.Image(
                label="TryOff result",
                height=1024,
                image_mode="RGB",
                type="pil",
            )

    with gr.Accordion("Advanced Settings", open=True):
        seed = gr.Slider(
            label="Seed",
            minimum=0,
            maximum=100_000,
            value=69_420,
            step=1,
        )
        scale = gr.Slider(
            label="Scale",
            minimum=0.5,
            maximum=5,
            value=2.5,
            step=0.05,
        )
        num_inference_steps = gr.Slider(
            label="Number of inference steps",
            minimum=1,
            maximum=50,
            value=25,
            step=1,
        )
        with gr.Row():
            image_width = gr.Slider(
                label="Image Width",
                minimum=64,
                maximum=1024,
                value=384,
                step=8,
            )
            image_height = gr.Slider(
                label="Image Height",
                minimum=64,
                maximum=1024,
                value=512,
                step=8,
            )

    run_button.click(
        fn=process,
        inputs=[
            input_image,
            image_width,
            image_height,
            num_inference_steps,
            scale,
            seed,
        ],
        outputs=output_image,
    )

demo.launch()