使用LLaMA-Factory微调Qwen2.5-VL-3B 的目标检测任务-数据集格式转换(voc 转 ShareGPT)

一、LLaMA-Factory + Qwen2.5-VL + ShareGPT 格式要求

ShareGPT 格式就是多轮对话的 list,每条数据如下:

复制代码
[
  {
    "conversations": [
      {"from": "user", "value": "<image>\n请标注图片中的所有目标及其类别和位置。"},
      {"from": "assistant", "value": "[{\"category\": \"person\", \"bbox\": [50, 100, 200, 300]}]"}
    ],
    "image": "相对路径/xxx.jpg"
  },
  ...
]

注意:

  • 字段是 "from" 而不是 "role"

  • 图片路径通常为相对路径,实际训练时配合 --image_folder 参数;

  • 你可以有任意多轮(这里只做单轮QA,适合目标检测)。

二、VOC批量转ShareGPT格式完整脚本

因为我的标注信息.xml和原始图像在同一个目录下,还有嵌套的文件夹;

下面脚本会递归遍历你的VOC根目录,自动配对xml和图片,生成ShareGPT格式的JSON。

复制代码
import os
import json
import xml.etree.ElementTree as ET

def find_files_recursive(root_dir, exts):
    file_list = []
    for root, dirs, files in os.walk(root_dir):
        for file in files:
            if file.lower().endswith(exts):
                abs_path = os.path.join(root, file)
                rel_path = os.path.relpath(abs_path, root_dir)
                file_list.append(rel_path)
    return file_list

def get_img_path(xml_path, all_img_paths):
    xml_base = os.path.splitext(xml_path)[0]
    for ext in ['.jpg', '.jpeg', '.png', '.bmp']:
        img_path = xml_base + ext
        if img_path in all_img_paths:
            return img_path
    return None

def voc_to_sharegpt(voc_data_dir, output_json_path):
    xml_files = find_files_recursive(voc_data_dir, ('.xml',))
    img_files = find_files_recursive(voc_data_dir, ('.jpg', '.jpeg', '.png', '.bmp'))
    img_files_set = set(img_files)

    dataset = []
    for xml_rel in xml_files:
        xml_abs = os.path.join(voc_data_dir, xml_rel)
        img_rel = get_img_path(xml_rel, img_files_set)
        if img_rel is None:
            continue

        tree = ET.parse(xml_abs)
        root = tree.getroot()
        objs = []
        for obj in root.findall('object'):
            name = obj.find('name').text
            bbox = obj.find('bndbox')
            xmin = int(float(bbox.find('xmin').text))
            ymin = int(float(bbox.find('ymin').text))
            xmax = int(float(bbox.find('xmax').text))
            ymax = int(float(bbox.find('ymax').text))
            objs.append({'category': name, 'bbox': [xmin, ymin, xmax, ymax]})
        if not objs:
            continue

        # ShareGPT 格式(提示词根据自己的需求修改)
        entry = {
            "conversations": [
                {"from": "human", "value": "<image>\n请标注图片中的所有目标及其类别和位置。"},
                {"from": "gpt", "value": json.dumps(objs, ensure_ascii=False)}
            ],
            "images": data_dir +"/"+img_rel.replace("\\", "/")
        }
        dataset.append(entry)

    with open(output_json_path, 'w', encoding='utf-8') as f:
        json.dump(dataset, f, ensure_ascii=False, indent=2)
    print(f"转换完成!共 {len(dataset)} 条,输出至 {output_json_path}")

# 示例用法:假设VOC数据在 ./myvocdata
if __name__ == '__main__':
    voc_data_dir="./myvocdata"
    output_json_path="sharegpt_qwen25vl.json"
    voc_to_sharegpt(voc_data_dir,output_json_path)

然后运行该python脚本会生成 sharegpt_qwen25vl.json文件,截取部分如下所示:

相关推荐
知乎的哥廷根数学学派10 分钟前
面向可信机械故障诊断的自适应置信度惩罚深度校准算法(Pytorch)
人工智能·pytorch·python·深度学习·算法·机器学习·矩阵
且去填词20 分钟前
DeepSeek :基于 Schema 推理与自愈机制的智能 ETL
数据仓库·人工智能·python·语言模型·etl·schema·deepseek
待续30123 分钟前
订阅了 Qoder 之后,我想通过这篇文章分享一些个人使用心得和感受。
人工智能
weixin_3975780223 分钟前
人工智能发展历史
人工智能
强盛小灵通专卖员1 小时前
基于深度学习的山体滑坡检测科研辅导:从论文实验到系统落地的完整思路
人工智能·深度学习·sci·小论文·山体滑坡
OidEncoder1 小时前
从 “粗放清扫” 到 “毫米级作业”,编码器重塑环卫机器人新能力
人工智能·自动化·智慧城市
Hcoco_me1 小时前
大模型面试题61:Flash Attention中online softmax(在线softmax)的实现方式
人工智能·深度学习·自然语言处理·transformer·vllm
阿部多瑞 ABU1 小时前
`chenmo` —— 可编程元叙事引擎 V2.3+
linux·人工智能·python·ai写作
极海拾贝2 小时前
GeoScene解决方案中心正式上线!
大数据·人工智能·深度学习·arcgis·信息可视化·语言模型·解决方案
知乎的哥廷根数学学派2 小时前
基于生成对抗U-Net混合架构的隧道衬砌缺陷地质雷达数据智能反演与成像方法(以模拟信号为例,Pytorch)
开发语言·人工智能·pytorch·python·深度学习·机器学习