Llama-Factory微调Qwen2.5-VL从数据集制作到部署记录

Llama-Factory微调Qwen2.5-VL从数据集制作到部署记录

电脑环境配置:
1.ubuntu24
2.3090(24G)
3.Cuda==12.9

一、数据集制作

我的数据集主要是对图像内容进行描述
1.Label-studio制作数据集
这是最原始的从零开始制作数据集的方法,不建议这样做!
安装完label-studio后,输入指令启动

label-studio start 

进入浏览器界面

在这里插入图片描述


创建项目:Create Project,引入图片后,选择图像描述数据集制作(Image Captioning)

在这里插入图片描述


2.利用Qwen2.5-VL半自动制作数据集
既然qwen本身具有较好的图像描述能力,那我们可以先使用qwen进行图像描述,在此基础上进行复核修改,这样做可以减少人力成本。
我这编写的脚本如下:

import torch from modelscope import Qwen2_5_VLForConditionalGeneration, AutoTokenizer, AutoProcessor from qwen_vl_utils import process_vision_info import time import os from pathlib import Path import json defprocess_single_image(model, processor, image_path, prompt): messages =[{"role":"user","content":[{"type":"image","image": image_path,},{"type":"text","text": prompt},],}]# Preparation for inference text = processor.apply_chat_template( messages, tokenize=False, add_generation_prompt=True) image_inputs, video_inputs = process_vision_info(messages) inputs = processor( text=[text], images=image_inputs, videos=video_inputs, padding=True, return_tensors="pt",) inputs = inputs.to("cuda") time_start = time.time()# Inference: Generation of the output generated_ids = model.generate(**inputs, max_new_tokens=256, do_sample=False) time_end = time.time()print(f"Inference time for {Path(image_path).name}: {time_end - time_start:.2f}s") generated_ids_trimmed =[ out_ids[len(in_ids):]for in_ids, out_ids inzip(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]defprocess_images_in_folder(model, processor, image_folder, prompt, output_file=None):# 支持的图像格式 image_extensions ={'.jpg','.jpeg','.png','.bmp','.tiff','.tif'}# 获取文件夹中所有图像文件 image_files =[]forfilein Path(image_folder).iterdir():iffile.suffix.lower()in image_extensions: image_files.append(file) image_files.sort()ifnot image_files:print(f"No image files found in {image_folder}")returnprint(f"Found {len(image_files)} image files")# 存储结果 results =[]# 遍历处理每张图像for image_file in image_files:print(f"\nProcessing: {image_file.name}")try: result = process_single_image(model, processor,str(image_file), prompt)print(f"Result: {result}")# 保存结果 results.append({'image': image_file.name,'path':str(image_file),'result': result })except Exception as e:print(f"Error processing {image_file.name}: {e}") results.append({'image': image_file.name,'path':str(image_file),'result':f"Error: {e}",'error':True})# 如果指定了输出文件,则保存为JSONL格式if output_file:withopen(output_file,'w', encoding='utf-8')as f:for item in results:# 构造JSONL格式的字典 json_line ={"image": item['path'],"text": item['result']}# 写入一行JSON f.write(json.dumps(json_line, ensure_ascii=False)+'\n')print(f"\nResults saved to {output_file}")return results if __name__ =='__main__':# default: Load the model on the available device(s) model = Qwen2_5_VLForConditionalGeneration.from_pretrained("/home/ct/work/BigModel/Qwen2.5-VL/models/Qwen2.5-VL-7B-Instruct", torch_dtype="auto", device_map="auto")# The default range for the number of visual tokens per image in the model is 4-16384.# You can set min_pixels and max_pixels according to your needs, such as a token range of 256-1280, to balance performance and cost. min_pixels =256*28*28 max_pixels =1280*28*28 processor = AutoProcessor.from_pretrained("/home/ct/work/BigModel/Qwen2.5-VL/models/Qwen2.5-VL-7B-Instruct", min_pixels=min_pixels, max_pixels=max_pixels )# 设置图像文件夹路径和提示词 image_folder ="/home/ct/work/Label_tools/PICS/Flame/" prompt ="查看图像中红色矩形框中是否存在烟火,判定存在烟火需要看到明显的烟雾和火焰,注意区分灯光、太阳光和一些其他的影响。" output_file ="inference_results.jsonl"# 结果输出文件# 处理文件夹中的所有图像 results = process_images_in_folder(model, processor, image_folder, prompt, output_file)# 打印汇总信息print(f"\nProcessing completed. Total images processed: {len(results)}")

配置运行后,将会生成推理结果的JSONL文件。主要包含图像路径和对应描述。其他任务主要修改以下提示词就可以。
接下来就是对这图像查看与qwen2.5-vl描述的是否一致就行。

二、LLama-Factory微调

1.配置LLama-Factory环境
因为我是一边测试一边记录,为了安全起见,建议使用anaconda建立LLama-Factory虚拟环境。
(1)克隆LLama-Factory项目

git clone https://github.com/hiyouga/LLaMA-Factory.git cd LLaMA-Factory 

(2)创建虚拟环境

# 使用 conda(推荐) conda create -n llama-factory python=3.10 conda activate llama-factory # 或使用 venv python -m venv venv source venv/bin/activate 

(3)安装依赖

pip install -r requirements.txt 
在这里插入图片描述


2.转化标签数据格式
目前我们的数据格式大概是:

{"image":"/path/to/image.jpg","text":"图像描述语句"}

而LLama-Factory对于多模态大模型的建议数据格式为:

{"images":["/home/ct/work/Label_tools/PICS/Smoke/Smoke001.png"],"conversations":[{"content":"<image>\n请分析图像中红色矩形框内是否存在吸烟行为,并说明理由。","from":"user"},{"content":"红色矩形框中的人在吸烟。","from":"assistant"}]}

转换脚本如下:

import json # 读取原始文件 input_file ="/home/ct/work/LLaMA-Factory/inference_results_Smoke.jsonl" output_file ="/home/ct/work/LLaMA-Factory/smoke_dataset.jsonl"withopen(input_file,'r', encoding='utf-8')as infile: lines = infile.readlines()# 转换格式 converted_lines =[]for line in lines: data = json.loads(line.strip())# 构建新的数据结构 new_data ={"images":[data["image"]],"conversations":[{"content":"<image>\n请分析图像中红色矩形框内是否存在吸烟行为,并说明理由。","from":"user"},{"content": data["text"],"from":"assistant"}]} converted_lines.append(json.dumps(new_data, ensure_ascii=False)+'\n')# 写入新文件withopen(output_file,'w', encoding='utf-8')as outfile: outfile.writelines(converted_lines)print(f"转换完成!已保存到 {output_file}")

3.启动微调
(1)下载模型

huggingface由于是外网,下载困难,建议去魔塔社区下载,下载后置于LLama-factory根目录下,新建models文件夹。

在这里插入图片描述


(2)构建dataset_info.json
在LLama-factory的根目录下新建该文件,并写入:

{"smoke_dataset":{"file_name":"smoke_dataset.jsonl","formatting":"sharegpt","columns":{"messages":"conversations","images":"images"},"tags":{"role_tag":"from","content_tag":"content","user_tag":"user","assistant_tag":"assistant"}}}

注意smoke_dataset和smoke_dataset.jsonl两者需要对应。
(3)启动微调

cd /home/ct/work/LLaMA-Factory python src/train.py \ --stage sft \ --do_train \ --model_name_or_path /home/ct/work/LLaMA-Factory/models/Qwen2.5-VL-7B-Instruct \ --dataset smoke_dataset \ --dataset_dir . \ --template qwen2_vl \ --finetuning_type lora \ --lora_target all \ --output_dir saves/Qwen2.5-VL-7B-Instruct-lora \ --per_device_train_batch_size 1 \ --gradient_accumulation_steps 8 \ --lr_scheduler_type cosine \ --logging_steps 10 \ --save_steps 100 \ --learning_rate 5e-5 \ --num_train_epochs 3.0 \ --plot_loss \ --fp16 
在这里插入图片描述


生成的权重文件在LLama-Factory根目录下的Saves文件夹下。
内存占用大概22G.

在这里插入图片描述

三、模型合并

在模型微调训练后,会在saves文件夹下生成一系列的微调权重文件,我使用的lora微调。大小在100~300m之间。需要与原始权重文件合并。

在这里插入图片描述

可以采用llama-factory和pytorch+transform等多种方法进行合并,我这的脚本如下:

# merge_lora_weights.pyimport os import torch from transformers import AutoModelForVision2Seq, AutoTokenizer, AutoProcessor from peft import PeftModel defmerge_lora_weights():# 配置路径 base_model_path ="models/Qwen2.5-VL-7B-Instruct"# 原始模型路径 lora_weights_path ="saves/Qwen2.5-VL-7B-Instruct-lora/checkpoint-3520"# LoRA权重路径 output_path ="./merged_qwen2.5-vl-finetuned"# 合并后模型保存路径print("Loading base model...") base_model = AutoModelForVision2Seq.from_pretrained( base_model_path, torch_dtype=torch.float16, low_cpu_mem_usage=True, trust_remote_code=True)print("Loading LoRA adapter...") lora_model = PeftModel.from_pretrained(base_model, lora_weights_path)print("Merging weights...") merged_model = lora_model.merge_and_unload()print("Saving merged model...")# 创建输出目录 os.makedirs(output_path, exist_ok=True)# 保存模型 merged_model.save_pretrained(output_path, safe_serialization=True, max_shard_size="5GB")# 保存tokenizer和processor tokenizer = AutoTokenizer.from_pretrained(base_model_path, trust_remote_code=True) tokenizer.save_pretrained(output_path)# 保存processor(对VL模型很重要) processor = AutoProcessor.from_pretrained(base_model_path, trust_remote_code=True) processor.save_pretrained(output_path)print(f"Merged model saved to {output_path}")if __name__ =="__main__": merge_lora_weights()

合并后模型权重大小:

在这里插入图片描述


接下来就是测试了。

Read more

Qwen3-TTS部署教程:Qwen3-TTS与Whisper ASR构建双向语音对话系统

Qwen3-TTS部署教程:Qwen3-TTS与Whisper ASR构建双向语音对话系统 想象一下,你对着电脑说一句话,电脑不仅能听懂,还能用自然、有感情的声音回答你,整个过程流畅得就像在和朋友聊天。这听起来像是科幻电影里的场景,但现在,通过Qwen3-TTS和Whisper ASR这两个强大的开源模型,我们完全可以自己动手搭建这样一个系统。 今天,我就带你一步步实现这个目标。无论你是想做一个智能语音助手,还是想为你的应用增加语音交互功能,这篇教程都会给你一个清晰的路线图。我们会从最基础的部署开始,到最终实现一个能听会说的双向对话系统。 1. 准备工作与环境搭建 在开始之前,我们先来了解一下今天要用到的两个核心工具。 Qwen3-TTS 是一个强大的文本转语音模型。它最吸引人的地方在于,它支持10种主要语言,包括中文、英文、日文等,还能生成多种方言和语音风格。更厉害的是,它能理解你文本里的情感和意图,自动调整说话的语调、语速,让生成的声音听起来特别自然。 Whisper ASR 则是OpenAI开源的语音识别模型,它的识别准确率非常高,支持多种语言,而且对带口音、有噪声

2025终极指南:whisper.cpp跨平台语音识别部署全流程

2025终极指南:whisper.cpp跨平台语音识别部署全流程 【免费下载链接】whisper.cppOpenAI 的 Whisper 模型在 C/C++ 中的移植版本。 项目地址: https://gitcode.com/GitHub_Trending/wh/whisper.cpp 还在为语音转文字服务的网络延迟和高成本烦恼?whisper.cpp作为开源语音识别解决方案,提供了本地化部署的完美选择。本文将带你深入了解如何在不同平台上快速部署和使用这个强大的离线语音识别工具。 通过本文,你将掌握: * 多平台环境配置的一键安装方法 * 模型下载与优化的性能调优技巧 * 常见部署问题的快速解决方案 * 监控与维护的最佳实践 平台选择:找到最适合你的方案 平台类型安装难度推理速度内存占用适用场景Windows桌面⭐⭐1.2x1.1GB个人使用Linux服务器⭐⭐⭐1.5x0.9GB企业部署macOS开发⭐2.0x0.7GB移动应用Android设备⭐⭐⭐⭐0.8x0.5GB边缘计算 环境搭建:快速启动的完整步骤 基础环境准备

AIGC 新势力:探秘海螺 AI 与蓝耘 MaaS 平台的协同创新之旅

AIGC 新势力:探秘海螺 AI 与蓝耘 MaaS 平台的协同创新之旅

探秘海螺AI:多模态架构下的认知智能新引擎 在人工智能持续进阶的进程中,海螺AI作为一款前沿的多功能AI工具,正凭借其独特的多模态架构崭露头角。它由上海稀宇科技有限公司(MiniMax)精心打造,依托自研的万亿参数MoE大语言模型ABAB6.5以及MiniMax语音大模型,展现出非凡的技术实力与应用潜力。MiniMax的核心团队源自商汤科技等业内知名企业,在多模态大模型研发领域深耕细作,为海螺AI的诞生奠定了坚实基础。 在这里插入图片描述 一、核心模型架构剖析 (一)基础模型:abab - 6.5 海螺AI的基础模型abab - 6.5采用了创新的混合专家系统设计,借助动态路由机制,即Sparse Gating Network,可依据输入内容智能激活8 - 12个子专家模型。这些子专家模型涵盖代码专家、多语言专家、逻辑推理专家等,各司其职,协同作业。在参数规模上,abab - 6.5总参数量高达1.2万亿,同时通过巧妙的设计,将活跃参数量控制在2000亿/query,有效平衡了模型的高容量与低推理成本。在训练优化环节,

拒绝“环境劝退”:Llama-2-7b 在昇腾 NPU 上的工程化部署与深度故障排查实录

拒绝“环境劝退”:Llama-2-7b 在昇腾 NPU 上的工程化部署与深度故障排查实录

文章目录 * 一、 引言 * 二、 运行环境搭建与配置 * 资源选型与启动 * 基础环境核验 * 依赖库的安装与换源 * 三、 Llama-2-7b 模型部署实操 * 模型下载策略 * 模型加载与 NPU 迁移 * 执行推理生成 * 四、 实操过程中的问题总结与解决 (Troubleshooting) * 问题 1:Unsupported Operator 算子报错 * 问题 2:显存溢出 (OOM) * 问题 3:模型下载连接超时 (Connection Timeout) * 问题 4:推理结果乱码或无限重复 * 问题 5:下载太慢或连接超时 * 问题 6:内核崩溃:Resource temporarily unavailable * 问题 7:推理报错:Unsupported Operator