Qwen-Image-Edit-Lowres-Fix

1.2K downloads 12 likes on ModelScope updated

Model card

Qwen-Image-Edit 低分辨率输入修复 LoRA

模型介绍

Qwen-Image-Edit 是一个强大的开源图像编辑模型。然而,当模型的输入分辨率低于图像生成的目标分辨率时,模型的图像细节保持能力较差。为此,我们做了以下两点改动:

  1. Rope 插值: 将 Qwen-Image DiT 的输入图像的位置编码改为在目标分辨率下位置编码的插值采样,这一改动可独立于改动2生效。
  2. LoRA 微调: 快速训练一个 LoRA 模型,让 DiT 增强该插值编码的泛化性。

经过以上两点改动,模型可以输入低分辨率图像的情况下,得到图像一致的编辑图像。同时,相比于高分辨率输入,模型推理时长大大降低。

效果展示

图像编辑指令: 将裙子变为粉色。

输入分辨率 A100推理时长 输入图 原模型 Rope 插值 Rope 插值 + LoRA 微调
256x256 39 s
512x512 50 s
768x768 67 s
1024x1024 98 s

局限性

  1. 以低分辨率输入,高分辨率生成,推理时长会大大降低,但可能会降低模型编辑性能。
  2. 以上分析仅针对图像的细节保持能力。

推理代码

git clone https://github.com/modelscope/DiffSynth-Studio.git  
cd DiffSynth-Studio
pip install -e .
from diffsynth.pipelines.qwen_image import QwenImagePipeline, ModelConfig
import torch
from modelscope import snapshot_download

pipe = QwenImagePipeline.from_pretrained(
    torch_dtype=torch.bfloat16,
    device="cuda",
    model_configs=[
        ModelConfig(model_id="Qwen/Qwen-Image-Edit", 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"),
    ],
    tokenizer_config=None,
    processor_config=ModelConfig(model_id="Qwen/Qwen-Image-Edit", origin_file_pattern="processor/"),
)
snapshot_download("DiffSynth-Studio/Qwen-Image-Edit-Lowres-Fix", local_dir="models/DiffSynth-Studio/Qwen-Image-Edit-Lowres-Fix", allow_file_pattern="model.safetensors")
pipe.load_lora(pipe.dit, "models/DiffSynth-Studio/Qwen-Image-Edit-Lowres-Fix/model.safetensors")

prompt = "精致肖像,水下少女,蓝裙飘逸,发丝轻扬,光影透澈,气泡环绕,面容恬静,细节精致,梦幻唯美。"
image = pipe(prompt=prompt, seed=0, num_inference_steps=40, height=1024, width=768)
image.save("image.jpg")

prompt = "将裙子变成粉色"
image = image.resize((512, 384))
image = pipe(prompt, edit_image=image, seed=1, num_inference_steps=40, height=1024, width=768, edit_rope_interpolation=True, edit_image_auto_resize=False)
image.save(f"image2.jpg")