File size: 12,928 Bytes
cff4f35
 
 
 
 
 
a09bc3d
cff4f35
 
 
 
 
 
 
a09bc3d
 
cff4f35
 
 
 
 
 
 
 
 
 
a09bc3d
 
cff4f35
 
 
 
 
 
 
 
a09bc3d
 
cff4f35
a09bc3d
cff4f35
 
 
 
a09bc3d
cff4f35
 
 
 
 
 
 
a09bc3d
cff4f35
 
a09bc3d
cff4f35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a09bc3d
cff4f35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a09bc3d
 
 
cff4f35
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import argparse
import datetime
import json
import os
import time
import torch
import gradio as gr
from PIL import Image
from tokenizer.sdxl_decoder_pipe import StableDiffusionXLDecoderPipeline
from torchvision import transforms
import logging
from utils.registry_utils import Config
from tokenizer.builder import build_vq_model
from dataset.multi_ratio_dataset import get_image_size, assign_ratio


def read_config(file):
    # solve config loading conflict when multi-processes
    import time
    while True:
        config = Config.fromfile(file)
        if len(config) == 0:
            time.sleep(0.1)
            continue
        break
    return config


def build_logger(name, log_file):
    logger = logging.getLogger(name)
    logger.setLevel(logging.INFO)
    handler = logging.FileHandler(log_file)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    return logger


logger = build_logger("gradio_web_server", "gradio_web_server.log")

vq_model = None
is_ema_model = False
diffusion_pipeline = None
lazy_load = False

# diffusion decoder hyperparameters.
resolution_list = [
    (1024, 1024), (768, 1024), (1024, 768),
    (512, 2048), (2048, 512), (640, 1920),
    (1920, 640), (768, 1536),
    (1536, 768), (768, 1152), (1152, 768)
]

cfg_range = (1, 10.0)
step_range = (1, 100)


def resize_to_shortest_edge(img, shortest_edge_resolution):
    width, height = img.size

    if width < height:
        new_width = shortest_edge_resolution
        new_height = int(height * (new_width / width))
    elif height < width:
        new_height = shortest_edge_resolution
        new_width = int(width * (new_height / height))
    else:
        new_width = shortest_edge_resolution
        new_height = shortest_edge_resolution

    resized_img = img.resize((new_width, new_height))
    return resized_img


from PIL import Image


def resize_to_square_with_long_edge(image: Image.Image, size: int = 512):
    """Resize image so that its *long* side equals `size`, short side scaled proportionally."""
    width, height = image.size
    if width > height:
        new_width = size
        new_height = int(size * height / width)
    else:
        new_height = size
        new_width = int(size * width / height)
    return image.resize((new_width, new_height), Image.LANCZOS)


def pad_to_square(image: Image.Image, target_size: int = 512, color=(255, 255, 255)):
    image = resize_to_square_with_long_edge(image, target_size)
    new_img = Image.new("RGB", (target_size, target_size), color)
    offset_x = (target_size - image.width) // 2
    offset_y = (target_size - image.height) // 2
    new_img.paste(image, (offset_x, offset_y))
    return new_img


def load_vqgan_model(args, model_dtype='fp16', use_ema=False, ):
    global vq_model
    vq_model = build_vq_model(args.vq_model)

    if model_dtype == 'fp16':
        vq_model = vq_model.to(torch.float16)
        logger.info("Convert the model dtype to float16")
    elif model_dtype == 'bf16':
        vq_model = vq_model.to(torch.bfloat16)
        logger.info("Convert the model dtype to bfloat16")

    vq_model.to('cuda')
    vq_model.eval()
    checkpoint = torch.load(args.vq_ckpt, map_location="cpu")

    if "ema" in checkpoint:
        ema_state_dict = checkpoint["ema"]
    else:
        ema_state_dict = None

    if "model" in checkpoint:
        model_state_dict = checkpoint["model"]
    elif "state_dict" in checkpoint:
        model_state_dict = checkpoint["state_dict"]
    else:
        model_state_dict = checkpoint

    if use_ema:
        vq_model.load_state_dict(ema_state_dict, strict=True)
    else:
        vq_model.load_state_dict(model_state_dict, strict=True)
    return vq_model


def load_diffusion_decoder(args):
    global diffusion_pipeline
    diffusion_pipeline = StableDiffusionXLDecoderPipeline.from_pretrained(
        args.sdxl_decoder_path,
        add_watermarker=False,
        vq_config=args,
        vq_model=vq_model,
    )
    diffusion_pipeline.to(vq_model.device)


def vqgan_diffusion_decoder_reconstruct(input_image, diffusion_upsample, cfg_values, steps):
    transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
    ])
    input_tensor = transform(input_image).unsqueeze(0).to(vq_model.device)

    org_width, org_height = input_image.size
    if diffusion_upsample:
        width, height = org_width * 2, org_height * 2
    else:
        width, height = org_width, org_height

    print(diffusion_upsample, org_width, org_height, width, height)
    group_index = assign_ratio(height, width, resolution_list)
    select_h, select_w = resolution_list[group_index]

    diffusion_outputs = diffusion_pipeline(
        images=input_tensor,
        height=select_h,
        width=select_w,
        guidance_scale=cfg_values,
        num_inference_steps=steps
    )
    sample = diffusion_outputs.images[0]
    sample.resize((width, height))
    return sample, f"�� **Output Resolution**: {width}x{height}"


@torch.no_grad()
def vqgan_reconstruct(input_image):
    transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
    ])

    org_width, org_height = input_image.size

    width = org_width // 16 * 16
    height = org_height // 16 * 16

    input_image = input_image.resize((width, height))
    input_tensor = transform(input_image).unsqueeze(0).to(vq_model.device)

    with torch.no_grad():
        inputs = vq_model.get_input(dict(image=input_tensor))
        (quant_semantic, _, _, _), \
        (quant_detail, _, _) = vq_model.encode(**inputs)
        reconstructed_image = vq_model.decode(quant_semantic, quant_detail)

    reconstructed_image = torch.clamp(127.5 * reconstructed_image + 128.0, 0, 255)
    reconstructed_image = reconstructed_image.squeeze(0).permute(1, 2, 0).cpu().numpy().astype('uint8')

    output_image = Image.fromarray(reconstructed_image)
    output_image.resize((org_width, org_height))
    return output_image, f"�� **Output Resolution**: {org_width}x{org_height}"


title_markdown = '''# DualViTok Demo
The DualViTok is a dual-branch vision tokenizer designed to capture both deep semantics and fine-grained textures. Implementation details can be found in ILLUME+[[ArXiv](https://arxiv.org/abs/2504.01934)].  
'''

usage_markdown = """
<details>
<summary><strong>�� Usage Instructions (click to expand)</strong></summary>

1. Upload an image and click the <strong>Reconstruct</strong> button.  
2. Set <code>Max Shortest Side</code> to limit the image resolution.  
3. Click <code>Force Upscale to Max Shortest Side to enable <strong>Force Upscale</strong> to resize the shortest side of the image to the <code>Max Shortest Side</code>.  
4. <em>(Optional)</em> Check <code>Use EMA model</code> to use the EMA checkpoint for reconstruction.  
5. <em>(Optional)</em> Click <code>Load Diffusion Decoder</code> to enable Diffusion Model decoding.  
   You can also enable <code>2x Upsample</code> to apply super-resolution to the uploaded image.

</details>
"""


def build_gradio_interface(args):
    if not lazy_load:
        load_vqgan_model(args, model_dtype=args.model_dtype)

    with gr.Blocks() as demo:
        gr.Markdown(title_markdown)
        gr.Markdown(usage_markdown)

        with gr.Row():
            with gr.Column():
                gr.Markdown("## ��️ Input Image")
                input_image = gr.Image(type="pil", label="Upload Image", width=384, height=384)
                input_resolution_display = gr.Markdown("")
                gr.Examples(
                    examples=[
                        ["../configs/data_configs/test_data_examples/ImageUnderstandingExample/images/1.png",],
                        ["../configs/data_configs/test_data_examples/ImageUnderstandingExample/images/2.png",],
                        ["../configs/data_configs/test_data_examples/ImageUnderstandingExample/images/3.png",],
                    ],
                    inputs=input_image,
                    label="Example Images",
                )

            with gr.Column():
                gr.Markdown("## �� Reconstructed Image")
                output_image_recon = gr.Image(type="pil", label="Reconstruction", width=384, height=384)
                output_resolution_display = gr.Markdown("")

            with gr.Column():
                gr.Markdown("## ⚙ Hyperparameters")
                # with gr.Row():
                short_resolution_dropdown = gr.Dropdown(
                    choices=[None, 256, 384, 512, 1024],
                    value=1024,
                    label="Max Shortest Side"
                )
                force_upscale_checkbox = gr.Checkbox(label="Force Upscale to Max Shortest Side", value=False)
                use_ema_checkbox = gr.Checkbox(label="Use EMA Model", value=False)

                with gr.Accordion("�� Use Diffusion Decoder", open=False):
                    use_diffusion_checkbox = gr.Checkbox(label="Load Diffusion Decoder", value=False)
                    diffusion_upsample_checkbox = gr.Checkbox(label="Enable 2x Upsample", value=False)
                    cfg_slider = gr.Slider(
                        minimum=cfg_range[0], maximum=cfg_range[1],
                        step=0.5, value=1.5,
                        label="CFG Value"
                    )
                    step_slider = gr.Slider(
                        minimum=step_range[0], maximum=step_range[1],
                        step=1, value=20,
                        label="Inference Steps"
                    )
                reconstruct_btn = gr.Button("�� Reconstruct", variant="primary")

        def handle_input_image(image):
            if image is not None:
                image = image.convert("RGB")
                w, h = image.size
                return image, f"�� **Input Resolution**: {w}x{h}"
            return None, ""

        input_image.change(
            handle_input_image,
            inputs=input_image,
            outputs=[input_image, input_resolution_display]
        )

        def reconstruct_fn(image, use_ema_flag, short_edge_resolution, force_upscale,
                           use_diffusion_flag, diffusion_upsample, cfg_value, num_steps):

            if short_edge_resolution is not None:
                if force_upscale or min(image.size) > short_edge_resolution:
                    image = resize_to_shortest_edge(image, int(short_edge_resolution))

            global vq_model
            if lazy_load and vq_model is None:
                load_vqgan_model(args, model_dtype=args.model_dtype)

            if use_ema_flag:
                if not is_ema_model:
                    load_vqgan_model(args, model_dtype=args.model_dtype, use_ema=True)
                    logger.info("Switched to EMA checkpoint")
            else:
                if is_ema_model:
                    load_vqgan_model(args, model_dtype=args.model_dtype, use_ema=False)
                    logger.info("Switched to non-EMA checkpoint")

            if use_diffusion_flag:
                if diffusion_pipeline is None:
                    load_diffusion_decoder(args)
                recon_image, resolution_str = vqgan_diffusion_decoder_reconstruct(image, diffusion_upsample, cfg_value,
                                                                                  num_steps)
            else:
                recon_image, resolution_str = vqgan_reconstruct(image)

            return pad_to_square(recon_image, target_size=384), resolution_str

        reconstruct_btn.click(
            reconstruct_fn,
            inputs=[input_image, use_ema_checkbox, short_resolution_dropdown, force_upscale_checkbox,
                    use_diffusion_checkbox, diffusion_upsample_checkbox, cfg_slider, step_slider],
            outputs=[output_image_recon, output_resolution_display])

    demo.launch(server_name='0.0.0.0')


# 主函数
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("config", type=str)
    parser.add_argument("--local_rank", type=int, default=0)
    parser.add_argument("--vq-ckpt", type=str, help="ckpt path for vq model")
    parser.add_argument("--torch-dtype", type=str, default='fp32')
    parser.add_argument("--model-dtype", type=str, default='fp32')
    parser.add_argument("--sdxl-decoder-path", type=str, default=None)
    parser.add_argument("--verbose", action='store_true')

    args = parser.parse_args()

    config = read_config(args.config)
    config.vq_ckpt = args.vq_ckpt
    config.torch_dtype = args.torch_dtype
    config.model_dtype = args.model_dtype
    config.verbose = args.verbose
    config.sdxl_decoder_path = args.sdxl_decoder_path

    build_gradio_interface(config)


if __name__ == "__main__":
    main()