Qwen3-VL 与 LLaMA-Factory 实现 Grounding 任务 LoRA 微调
基于 Qwen3-VL 多模态大模型和 LLaMA-Factory 框架进行 Grounding(定位)任务 LoRA 微调的完整流程。内容包括环境配置、模型下载、推理测试、数据集准备(YOLO 格式转换)、可视化界面微调设置及模型导出测试。重点讲解了如何将 YOLO 坐标转换为 Qwen3-VL 所需的相对坐标格式,以及 LLaMA-Factory 中的关键参数配置。

基于 Qwen3-VL 多模态大模型和 LLaMA-Factory 框架进行 Grounding(定位)任务 LoRA 微调的完整流程。内容包括环境配置、模型下载、推理测试、数据集准备(YOLO 格式转换)、可视化界面微调设置及模型导出测试。重点讲解了如何将 YOLO 坐标转换为 Qwen3-VL 所需的相对坐标格式,以及 LLaMA-Factory 中的关键参数配置。

Qwen3-VL 是由阿里云 Qwen 团队开发的多模态大语言模型系列。其空间感知能力大幅提升,2D grounding 从绝对坐标变为相对坐标,支持判断物体方位、视角变化、遮挡关系,能实现 3D grounding。OCR 支持更多语言及复杂场景,覆盖范围扩展至 32 种语言。
主要技术改进包括:
conda create -n Qwen3-vl python=3.10
conda activate Qwen3-vl
pip install accelerate
pip install qwen-vl-utils==0.0.14
uv pip install -U vllm>=0.11.0
git clone https://github.com/QwenLM/Qwen3-VL
pip install modelscope
modelscope download --model Qwen/Qwen3-VL-2B-Instruct
修改模型路径和图片地址后运行以下脚本:
from transformers import Qwen3VLForConditionalGeneration, AutoProcessor
import torch
from PIL import Image
def load_qwen3_vl_4b_model():
model = Qwen3VLForConditionalGeneration.from_pretrained(
"Qwen/Qwen3-VL-4B-Instruct",
torch_dtype=torch.bfloat16,
device_map="auto",
attn_implementation="flash_attention_2"
)
processor = AutoProcessor.from_pretrained("Qwen/Qwen3-VL-4B-Instruct")
return model, processor
def process_multimodal_query(model, processor, image_path, text_query):
image = Image.open(image_path).convert('RGB')
messages = [
{
"role": "user",
"content": [
{"type": "image", "image": image},
{"type": "text", "text": text_query}
]
}
]
inputs = processor.apply_chat_template(
messages, tokenize=True, add_generation_prompt=True, return_dict=True, return_tensors="pt"
)
generated_ids = model.generate(**inputs, max_new_tokens=128, do_sample=True, temperature=0.7, top_p=0.8)
generated_ids_trimmed = [out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)]
output_text = processor.batch_decode(generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False)
return output_text[0] if output_text else ""
if __name__ == "__main__":
model, processor = load_qwen3_vl_4b_model()
image_path = "example.jpg"
query = "描述这张图片中的场景和主要对象"
result = process_multimodal_query(model, processor, image_path, query)
print("模型回复:", result)
git clone https://github.com/hiyouga/LLaMA-Factory
conda create -n llama-factory python=3.12
conda activate llama-factory
pip install -e ".[torch,metrics]" --no-build-isolation
pip uninstall torch torchvision
pip install torch==2.8.0 torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
LLaMA-Factory 微调需要特定的数据格式。注意 Qwen3-VL 使用 0-1000 的相对坐标,需进行归一化。准备好数据集后,需修改 LLaMA-Factory/data/dataset_info.json 文件增加所需文件夹配置。
转换代码如下:
import os
import json
from tqdm import tqdm
IMAGE_DIR = "images"
LABEL_DIR = "labels"
OUTPUT_JSON = "qwen3_vl_grounding_mllm.json"
CLASS_ID2NAME = {0: "house"}
USER_PROMPT = (
"<image>\n"
"Locate all objects in this image and output the bbox coordinates "
"in JSON format using relative coordinates in the range [0, 1000]."
)
def yolo_to_xyxy_relative(xc, yc, w, h):
x_min = xc - w / 2
y_min = yc - h / 2
x_max = xc + w / 2
y_max = yc + h / 2
x_min = max(0.0, min(1.0, x_min))
y_min = max(0.0, min(1.0, y_min))
x_max = max(0.0, min(1.0, x_max))
y_max = max(0.0, min(1.0, y_max))
return [x_min, y_min, x_max, y_max]
def scale_to_qwen_coords(xyxy_rel, scale=1000):
x_min, y_min, x_max, y_max = xyxy_rel
return [
int(round(x_min * scale)),
int(round(y_min * scale)),
int(round(x_max * scale)),
int((y_max * scale)),
]
():
exts = {, , , , }
files = []
fname os.listdir(image_dir):
os.path.splitext(fname)[].lower() exts:
files.append(fname)
(files)
():
image_files = collect_image_files(IMAGE_DIR)
image_files:
()
dataset = []
img_name tqdm(image_files, desc=):
img_path = os.path.join(IMAGE_DIR, img_name)
img_rel_or_abs = os.path.abspath(img_path)
base, _ = os.path.splitext(img_name)
label_path = os.path.join(LABEL_DIR, base + )
os.path.exists(label_path):
bboxes_qwen = []
cls_ids = []
(label_path, , encoding=) f:
line f:
line = line.strip()
line:
parts = line.split()
(parts) < :
cls_id = (parts[])
xc = (parts[])
yc = (parts[])
w = (parts[])
h = (parts[])
xyxy_rel = yolo_to_xyxy_relative(xc, yc, w, h)
xyxy_qwen = scale_to_qwen_coords(xyxy_rel, scale=)
bboxes_qwen.append(xyxy_qwen)
cls_ids.append(cls_id)
bboxes_qwen:
objects = []
cid, box (cls_ids, bboxes_qwen):
obj = {: cid, : box}
cid CLASS_ID2NAME:
obj[] = CLASS_ID2NAME[cid]
objects.append(obj)
answer_obj = {: objects}
answer_str = json.dumps(answer_obj, ensure_ascii=)
sample = {
: [
{: , : USER_PROMPT},
{: , : answer_str}
],
: [img_rel_or_abs]
}
dataset.append(sample)
(OUTPUT_JSON, , encoding=) f:
json.dump(dataset, f, ensure_ascii=, indent=)
()
__name__ == :
main()
将生成的 json 文件放在 LLaMA-Factory/data 路径下面。
cd LLaMA-Factory
llamafactory-cli webui
关键参数包括语言、模型、模型路径、模型下载源、计算类型等。其中计算类型为 Pure_bf16 更省显存。
修改 dataset_info.json,在文件最前面添加如下配置:
"qwen3_vl_grounding_mllm": {
"file_name": "qwen3_vl_grounding_mllm.json",
"formatting": "sharegpt",
"columns": {
"messages": "conversations",
"images": "images"
}
}
保存训练参数、载入训练参数、开始训练。
点击 chat、选择训练好模型路径、点击加载模型、进行问答。输入图片和提示词进行测试。

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online
基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online
解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online