7B参数!Qwen开源Qwen-Image-2.1统一文生图与图像编辑

AI技术动态 2026-09-20
近期,Qwen团队开源了全新的Qwen-Image-2.1模型,这款仅7B参数的视觉生成工具首次实现了文生图与图像编辑的统一架构,不仅原生支持透明背景图像生成,还可处理最多10张参考图的多模态编辑任务,在保证生成质量的同时大幅优化了推理效率。
轻量高效的模型架构与性能表现
Qwen-Image-2.1的视觉生成模块参数量控制在7B级别,采用32层Single-Stream DiT架构。在官方公开的Qwen-Image-Bench评测中,模型拿到了60.28的总分,领先所有参与对比的开源模型,即便是面对多款参数未公开的闭源同类产品,也保持了极具竞争力的综合表现。
在推理性能方面,这款模型同样做了针对性优化,尤其是在多图输入的场景下优势更为明显。其核心优化点在于混合粒度注意力结构:针对文本输入包括系统前缀与编辑指令采用Token级因果掩码,而图像生成环节则使用Chunk级掩码;同时通过KV Cache复用机制,将输入图像与编辑指令作为静态上下文在首次推理时预计算并缓存,有效降低了显存占用并提升了整体推理速度。
全场景覆盖的核心功能特性
原生透明图生成与编辑
这款模型原生支持透明背景图像生成,无需额外的后处理步骤,可根据提示词自动输出普通RGB图像或带Alpha通道的RGBA格式文件,非常适合制作贴纸、商品素材、人像抠图等设计场景。不仅如此,生成的透明图像还可以作为后续编辑的输入条件,比如将现有RGB图片转换为透明图层后,继续修改其中的文字、表情或主体内容。
多图参考编辑能力
Qwen-Image-2.1最高支持10张参考图的输入编辑。在多人合影场景中,模型可以将6张独立的人像素材整合成一张自然的群像照片;在商品穿戴场景中,可同时读取模特、服饰、配饰等多张素材进行融合;在家居设计场景中,甚至可以基于10张不同的家具参考图生成完整的室内布置方案。
灵活的局部编辑控制
模型还提供了三种灵活的局部编辑控制方式:圈选模式可以在多个指定区域同时执行删除、替换与颜色调整操作;涂抹模式适合快速指定新增内容的位置;掩码模式则通过输入额外的区域蒙版,在不覆盖原图信息的前提下实现更精细的局部修改。此外,局部编辑还支持连续执行,可生成展示内容连续变化的短动画。
高保真的编辑效果
针对人像和商品编辑场景,模型重点优化了内容保真能力:在人像编辑时会尽量保留编辑前后的身份特征一致性,避免出现人脸变形或特征丢失;在商品编辑时则会尽可能保留包装文字、材质纹理与外形轮廓,降低主体被重绘或变形的概率。
拓展场景支持
除了基础的文生图与图像编辑,Qwen-Image-2.1还支持全景图生成、复杂信息图制作与分镜图创作。同时,模型对文字排版、光影效果与人物细节都做了增强优化,可满足海报、封面、品牌视觉与人像肖像等多种场景的创作需求。
便捷的体验与部署方式
开发者可以通过国内主流AI开源模型社区的创空间Demo直接体验Qwen-Image-2.1的图像生成与编辑能力,快速验证不同提示词和参考图的实际效果。同时,社区的AIGC专区也提供了在线推理与模型训练的能力,无需配置本地环境,就能测试生成效果、调整参数并开展模型微调工作。
基于DiffSynth套件的本地部署与训练
基础推理环境搭建
首先创建独立的Python环境,并安装相关依赖:
conda create -n diffsynth python=3.10 -y
conda activate diffsynth
git clone https://github.com/modelscope/DiffSynth-Studio.git
cd DiffSynth-Studio
pip install -e .
直接运行以下代码,可下载Qwen-Image-2.1并生成图像:
from diffsynth.pipelines.qwen_image_21 import QwenImage21Pipeline, ModelConfig
import torch
from PIL import Image
pipe = QwenImage21Pipeline.from_pretrained(
torch_dtype=torch.bfloat16,
device="cuda",
model_configs=[
ModelConfig(model_id="Qwen/Qwen-Image-2.1", origin_file_pattern="transformer/diffusion_pytorch_model*.safetensors"),
ModelConfig(model_id="Qwen/Qwen-Image-2.1", origin_file_pattern="text_encoder/model*.safetensors"),
ModelConfig(model_id="Qwen/Qwen-Image-2.1", origin_file_pattern="vae/diffusion_pytorch_model*.safetensors"),
],
processor_config=ModelConfig(model_id="Qwen/Qwen-Image-2.1", origin_file_pattern="processor/"),
)
Text-to-Image, the output is an RGBA image
prompt = “Flat anime-style illustration, a girl with long black hair, wearing a JK uniform.”
image = pipe(prompt, seed=0)
image.save(“image1.png”)
prompt = “Flat anime-style illustration, a sunny and cheerful high school girl.”
image_2 = pipe(prompt=prompt, seed=0)
image_2.save(“image2.png”)
Image Editing, the generated RGBA image is fed back as the condition
prompt = “Generate a group photo of these two characters.”
edit_image = [Image.open(“image1.png”), Image.open(“image2.png”)]
image_3 = pipe(prompt, edit_image=edit_image, seed=1)
image_3.save(“image3.png”)
通过DiffSynth-WebUI推理
基于DiffSynth-Studio的零代码LoRA训练平台DiffSynth-WebUI也支持了这个模型,通过以下指令配置环境并启动WebUI:
git clone --recurse-submodules https://github.com/modelscope/DiffSynth-WebUI.git
cd DiffSynth-WebUI
pip install -e DiffSynth-Studio/
pip install -e .
bash training_ui/launch.sh
在WebUI中导入训练数据集 -> 点击New Task -> 选择Qwen-Image-2.1,即可开始训练。
LoRA模型训练
先下载示例训练数据:
modelscope download \
--dataset DiffSynth-Studio/diffsynth_example_dataset \
--include "qwen_image_21/Qwen-Image-2.1/*" \
--local_dir ./data/diffsynth_example_dataset
随后通过Accelerate启动Qwen-Image-2.1 LoRA训练:
accelerate launch examples/qwen_image_21/model_training/train.py \
--dataset_base_path data/diffsynth_example_dataset/qwen_image_21/Qwen-Image-2.1 \
--dataset_metadata_path data/diffsynth_example_dataset/qwen_image_21/Qwen-Image-2.1/metadata.csv \
--max_pixels 1048576 \
--dataset_repeat 50 \
--model_id_with_origin_paths "Qwen/Qwen-Image-2.1:transformer/diffusion_pytorch_model*.safetensors,Qwen/Qwen-Image-2.1:text_encoder/model*.safetensors,Qwen/Qwen-Image-2.1:vae/diffusion_pytorch_model*.safetensors" \
--learning_rate 1e-4 \
--num_epochs 5 \
--remove_prefix_in_ckpt "pipe.dit." \
--output_path "./models/train/Qwen-Image-2.1_lora" \
--lora_base_model "dit" \
--lora_target_modules "" \
--lora_rank 32 \
--use_gradient_checkpointing \
--find_unused_parameters
图像编辑LoRA可沿用同一训练入口,通过data_file_keys和extra_inputs加入编辑图像条件。
多平台本地推理与部署方案
使用ComfyUI推理
准备ComfyUI,并创建独立的Python环境,完成环境配置:
git clone https://github.com/comfyanonymous/ComfyUI.git
cd ComfyUI
pip install -r requirements.txt
pip install git+https://github.com/modelscope/DiffSynth-Studio.git
在ComfyUI中使用DiffSynth-Studio的自定义节点包:
cd ComfyUI/custom_nodes
git clone https://github.com/modelscope/DiffSynth-ComfyUI.git
cd ..
python main.py
在「模板」中可以找到提供的Qwen-Image-2.1工作流。此外,ComfyUI官方也Day0支持了模型推理,官方工作流如下:
- 生图工作流地址:https://github.com/Comfy-Org/workflow_templates/blob/main/templates/image_qwen_image_2_1_t2i.json
- 编辑工作流地址:https://github.com/Comfy-Org/workflow_templates/blob/main/templates/image_qwen_image_2_1_image_edit.json
使用Diffusers推理
环境安装:
pip install torch>=2.4.0
pip install transformers>=5.17
pip install git+https://github.com/huggingface/diffusers
pip install accelerate pillow
文生图脚本:
import torch
from diffusers import QwenImage21Pipeline
pipe = QwenImage21Pipeline.from_pretrained(
"Qwen/Qwen-Image-2.1", torch_dtype=torch.bfloat16
).to("cuda")
image = pipe(
prompt="A neon shop sign that reads \"QWEN IMAGE 2.1\", rainy night, reflections on wet pavement",
width=2048, height=2048,
num_inference_steps=40,
generator=torch.Generator("cuda").manual_seed(42),
).images[0]
image.save("t2i_example.png")
图生图脚本:
import torch
from PIL import Image
from diffusers import QwenImage21Pipeline
pipe = QwenImage21Pipeline.from_pretrained(
"Qwen/Qwen-Image-2.1", torch_dtype=torch.bfloat16
).to("cuda")
input_image = Image.open("input.png")
image = pipe(
prompt="Change the background to a sunset beach",
image=input_image,
num_inference_steps=40,
generator=torch.Generator("cuda").manual_seed(42),
).images[0]
image.save("edit_example.png")
通过vLLM部署
服务启动命令:
vllm serve Qwen/Qwen-Image-2.1 --omni --port 8091
接口调用示例:
curl http://localhost:8091/v1/images/generations \
-H "Content-Type: application/json" \
-d '{
"model": "Qwen/Qwen-Image-2.1",
"prompt": "A ceramic teapot on a wooden table",
"size": "1024x1024",
"num_inference_steps": 50,
"true_cfg_scale": 1.0,
"seed": 42
}'
详情参考:https://recipes.vllm.ai/Qwen/Qwen-Image-2.1
通过SGLang部署
服务启动命令:
sglang serve \
--model-path "/models/qwen-image-2.1" \
--model-id Qwen-Image-2.1 \
--num-gpus 1 \
--ulysses-degree 1 \
--host 0.0.0.0 \
--port 30010 \
--performance-mode speed \
--attention-backend fa \
--encoder-parallel auto \
--batching-max-size 1
接口调用:
curl -sS --fail-with-body http://localhost:30010/v1/images/generations \
-H 'Content-Type: application/json' \
-d '{
"model": "Qwen-Image-2.1",
"prompt": "A capybara reading a book by candlelight",
"n": 1,
"size": "1024x1024",
"num_inference_steps": 40,
"guidance_scale": 1,
"seed": 42,
"generator_device": "cpu",
"output_format": "png",
"response_format": "b64_json",
"background": "auto",
"enable_cache_dit": false
}'
详情参考:https://docs.sglang.io/cookbook/diffusion/Qwen-Image/Qwen-Image-2.1
在AMD平台上推理
Qwen-Image-2.1可通过ROCm 7.2.4 PyTorch容器在AMD Radeon GPU上运行。以下示例使用Diffusers加载模型权重并完成文生图推理。
拉取ROCm PyTorch镜像:
docker pull rocm/pytorch:rocm7.2.4_ubuntu24.04_py3.12_pytorch_release_2.10.0
启动容器:
docker run -d --name qwen-image \
--device=/dev/kfd --device=/dev/dri \
--ipc=host --shm-size 32G \
--security-opt seccomp=unconfined \
-v "$PWD":/workspace \
-w /workspace \
rocm/pytorch:rocm7.2.4_ubuntu24.04_py3.12_pytorch_release_2.10.0 sleep infinity
进入容器:
docker exec -it qwen-image bash
后续命令均在容器内执行。
安装Diffusers及相关依赖:
pip install "transformers>=5.0" accelerate safetensors sentencepiece modelscope
pip install --no-deps "git+https://github.com/huggingface/diffusers"
下载模型权重:
modelscope login --token <YOUR_TOKEN>
modelscope download Qwen/Qwen-Image-2.1 --local_dir /root/.cache/modelscope/Qwen-Image-2.1
运行文生图推理:
import torch
from diffusers import QwenImage21Pipeline
model_path = "/root/.cache/modelscope/Qwen-Image-2.1"
pipe = QwenImage21Pipeline.from_pretrained(model_path, dtype=torch.bfloat16)
pipe.enable_model_cpu_offload()
prompt = "A capybara wearing a wizard hat, oil painting"
image = pipe(prompt, generator=torch.Generator("cuda").manual_seed(42)).images[0]
image.save("t2i.png")
通过FlagOS在多种AI芯片上推理
Qwen-Image-2.1已通过FlagOS支持NVIDIA、沐曦、海光、昇腾、摩尔线程、平头哥真武、燧原和Arm共8种芯片平台。开发者只需从对应平台的预构建镜像与模型权重,即可沿用Diffusers的推理方式运行模型,无需修改代码;各平台的推理精度已与官方实现对齐。
更多相关信息可前往官方开源平台查询。

