ZImage-i2L-v2

963 downloads 14 likes on ModelScope updated

Model card

模型介绍

i2L (Image to LoRA) 模型是我们设计的特殊模型结构。模型的输入为若干张图片,输出为这些图片训练出的 LoRA 模型。

本模型专门增强了风格提取能力,模型能力与上一版本相比有了较大的提升,支持输入任意多张图,能够快速从这些图像中抽取风格信息,并用于生成新的图像。

效果展示

提示词:A cat is sitting on a stone.

输入 输出 输入 输出

模型支持任意多张输入图:

输入图1 输入图2 输入图3 输入图4 输入图5 输入图6 输入图7 输入图8
输入1张图产生的输出图 输入2张图产生的输出图 输入3张图产生的输出图 输入4张图产生的输出图
输入5张图产生的输出图 输入6张图产生的输出图 输入7张图产生的输出图 输入8张图产生的输出图

推理代码

安装 DiffSynth-Studio:

git clone https://github.com/modelscope/DiffSynth-Studio.git  
cd DiffSynth-Studio
pip install -e .

模型推理(以 Diffusion Templates 推理,开启 CFG 增强以提升效果):

from diffsynth.diffusion.template import TemplatePipeline
from diffsynth.pipelines.z_image import ZImagePipeline, ModelConfig
from modelscope import snapshot_download
from PIL import Image
import numpy as np
import torch

pipe = ZImagePipeline.from_pretrained(
    torch_dtype=torch.bfloat16,
    device="cuda",
    model_configs=[
        ModelConfig(model_id="Tongyi-MAI/Z-Image", origin_file_pattern="transformer/*.safetensors"),
        ModelConfig(model_id="Tongyi-MAI/Z-Image-Turbo", origin_file_pattern="text_encoder/*.safetensors"),
        ModelConfig(model_id="Tongyi-MAI/Z-Image-Turbo", origin_file_pattern="vae/diffusion_pytorch_model.safetensors"),
    ],
    tokenizer_config=ModelConfig(model_id="Tongyi-MAI/Z-Image-Turbo", origin_file_pattern="tokenizer/"),
)
pipe.enable_lora_hot_loading(pipe.dit)
template = TemplatePipeline.from_pretrained(
    torch_dtype=torch.bfloat16,
    device="cuda",
    model_configs=[ModelConfig(model_id="DiffSynth-Studio/ZImage-i2L-v2")],
)
snapshot_download("DiffSynth-Studio/ZImage-i2L-v2", allow_file_pattern="assets/*", local_dir="data")
images = [Image.open(f"data/assets/multi_input_{i}.jpg") for i in range(4)]
image = template(
    pipe,
    prompt="A cat is sitting on a stone",
    seed=0, cfg_scale=4, num_inference_steps=50,
    template_inputs = [{"image": images}],
    negative_template_inputs = [{"image": [Image.fromarray(np.zeros_like(np.array(i)) + 128) for i in images]}],
)
image.save("image_output.jpg")

模型推理(导出 LoRA 模型文件,可上传到魔搭社区并作为预加载 LoRA 继续训练):

from diffsynth.diffusion.template import TemplatePipeline, ModelConfig
from modelscope import snapshot_download
from safetensors.torch import save_file
from PIL import Image
import torch

template = TemplatePipeline.from_pretrained(
    torch_dtype=torch.bfloat16,
    device="cuda",
    model_configs=[ModelConfig(model_id="DiffSynth-Studio/ZImage-i2L-v2")],
)
snapshot_download("DiffSynth-Studio/ZImage-i2L-v2", allow_file_pattern="assets/*", local_dir="data")
images = [Image.open(f"data/assets/multi_input_{i}.jpg") for i in range(4)]
lora = template.call_single_side(inputs=[{"image": images}])["lora"]
save_file(lora, "lora.safetensors")