File size: 2,865 Bytes
70a9047
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a3c974
70a9047
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Run the UniTele Qwen SR adapter on one telephoto image."""

from __future__ import annotations

import argparse
from pathlib import Path

import torch
from huggingface_hub import hf_hub_download
from PIL import Image

from diffsynth.pipelines.qwen_image import ModelConfig, QwenImagePipeline


PHONE_NAMES = {
    "huawei": "Huawei",
    "iphone": "iPhone 17 Pro Max",
    "vivo": "vivo X300 Ultra",
}


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--input", type=Path, required=True)
    parser.add_argument("--output", type=Path, required=True)
    parser.add_argument("--phone", choices=PHONE_NAMES, required=True)
    parser.add_argument("--lora-path", type=Path)
    parser.add_argument("--device", default="cuda")
    parser.add_argument("--steps", type=int, default=40)
    parser.add_argument("--cfg-scale", type=float, default=4.0)
    parser.add_argument("--seed", type=int, default=123)
    args = parser.parse_args()

    lora_path = args.lora_path or Path(
        hf_hub_download("AustinZYC/UniTele", "unitele.safetensors")
    )
    with Image.open(args.input) as source:
        edit_image = source.convert("RGB")
    width, height = (768, 1024) if edit_image.height > edit_image.width else (1024, 768)

    pipe = QwenImagePipeline.from_pretrained(
        torch_dtype=torch.bfloat16,
        device=args.device,
        model_configs=[
            ModelConfig(
                model_id="Qwen/Qwen-Image-Edit-2511",
                origin_file_pattern="transformer/diffusion_pytorch_model*.safetensors",
            ),
            ModelConfig(
                model_id="Qwen/Qwen-Image",
                origin_file_pattern="text_encoder/model*.safetensors",
            ),
            ModelConfig(
                model_id="Qwen/Qwen-Image",
                origin_file_pattern="vae/diffusion_pytorch_model.safetensors",
            ),
        ],
        processor_config=ModelConfig(
            model_id="Qwen/Qwen-Image-Edit", origin_file_pattern="processor/"
        ),
    )
    pipe.load_lora(pipe.dit, str(lora_path))
    prompt = (
        f"Restore this low quality real {PHONE_NAMES[args.phone]} telephoto image "
        "into the aligned high quality reference image. Preserve the scene "
        "layout and recover natural sharp details."
    )
    with torch.inference_mode():
        result = pipe(
            prompt,
            edit_image=edit_image,
            seed=args.seed,
            num_inference_steps=args.steps,
            height=height,
            width=width,
            cfg_scale=args.cfg_scale,
            edit_image_auto_resize=True,
            zero_cond_t=True,
        )
    args.output.parent.mkdir(parents=True, exist_ok=True)
    result.save(args.output)
    print(args.output)


if __name__ == "__main__":
    main()