ARTICLE DETAIL

资讯详情

深耕编程入门与网站建设的一线实战洞察。

InternVL Chat多模态对话教程:图片对话、多轮交互与视频理解快速上手指南

InternVL Chat多模态对话教程:图片对话、多轮交互与视频理解快速上手指南 InternVL Chat多模态对话教程图片对话、多轮交互与视频理解快速上手指南【免费下载链接】InternVL[CVPR 2024 Oral] InternVL Family: A Pioneering Open-Source Alternative to GPT-4o. 接近GPT-4o表现的开源多模态对话模型项目地址: https://gitcode.com/GitHub_Trending/in/InternVLInternVL Chat 是一个接近 GPT-4o 表现的开源多模态对话模型CVPR 2024 Oral支持图片对话、多轮交互和视频理解。本教程面向新手带你从零搭建环境到完成单图问答、多图对比、视频理解等实战场景。 一键安装三步完成 InternVL 环境配置按照 INSTALLATION.md 的官方指引只需 3 个命令git clone https://gitcode.com/GitHub_Trending/in/InternVL conda create -n internvl python3.9 -y conda activate internvl pip install -r requirements.txt 提示依赖清单见 requirements/internvl_chat.txtGPU 用户建议再安装flash-attn加速推理推荐显存InternVL2_5-8B 全精度约需 16GB 显存小显存可换用 1B/2B 小模型或启用 BNB 8-bit 量化加载。 图片对话实战让小熊猫开口描述自己InternVL Chat 使用 Hugging Face Transformers 标准接口加载模型。下面的示例图正是官方示例 examples/image1.jpg核心代码只有 4 步加载模型 → 预处理图片 → 组织问题 → 调用 chatimport torch from transformers import AutoModel, AutoTokenizer # 1. 加载模型transformers4.37.2 path OpenGVLab/InternVL2_5-8B model AutoModel.from_pretrained(path, torch_dtypetorch.bfloat16, low_cpu_mem_usageTrue, use_flash_attnTrue, trust_remote_codeTrue).eval().cuda() tokenizer AutoTokenizer.from_pretrained(path, trust_remote_codeTrue, use_fastFalse) # 2. 动态高分辨率预处理官方提供 load_image 工具函数 pixel_values load_image(./examples/image1.jpg, max_num12).to(torch.bfloat16).cuda() # 3. 发起图片对话在问题中用 image 占位 question image\n请用一句话描述这张图片。 response model.chat(tokenizer, pixel_values, question, dict(max_new_tokens512, do_sampleTrue)) print(response)⭐ 关键设计动态分辨率切图。InternVL 会把大图切成 448×448 的图块tilemax_num控制最多切几块——图块越多文字和细节识别越精准但显存消耗也越高。 多轮交互实战让对话记住上下文InternVL Chat 通过history参数实现上下文记忆只需return_historyTrue即可# 第一轮带图提问 question image\n详细描述这张图片。 response, history model.chat(tokenizer, pixel_values, question, generation_config, historyNone, return_historyTrue) # 第二轮无需再传图片模型记得上一轮的小熊猫 question 给它写一首藏头诗诗题是小熊猫。 response, history model.chat(tokenizer, pixel_values, question, generation_config, historyhistory, return_historyTrue)多轮技巧场景做法纯文本闲聊不传pixel_values直接model.chat(tokenizer, None, question, ...)单图多轮每轮沿用同一份history想重新开始将history置回None流式输出用TextIteratorStreamer 子线程逐字打印详见 internvl_chat/README.md Streaming Output 一节️ 多图对比一次看懂两张图InternVL 2.0 之后原生支持多图输入官方示例图 examples/image2.jpg 也来自示例目录# 方式一拼接多图两图合成一个视觉序列 pixel_values torch.cat((pixel_values1, pixel_values2), dim0) question image\n请描述这两张图并说说它们的异同。 # 方式二独立编号推荐多轮对比时指代更清晰 num_patches_list [pixel_values1.size(0), pixel_values2.size(0)] question Image-1: image\nImage-2: image\n请对比这两张图。 response, history model.chat(tokenizer, pixel_values, question, generation_config, num_patches_listnum_patches_list, historyNone, return_historyTrue)需要批量处理多张图片时可直接调用model.batch_chat(...)一次推理返回所有结果适合做图片批量打标。 视频理解实战抽帧 多轮问答InternVL 将视频理解转化为图像序列理解均匀抽取 N 帧num_segments控制帧数每帧作为带Frame-i编号的图片喂给模型。完整load_video实现见 internvl_chat/README.md核心用法如下from decord import VideoReader, cpu # pip install decord pixel_values, num_patches_list load_video(./examples/red-panda.mp4, num_segments8, max_num1) video_prefix .join([fFrame{i1}: image\n for i in range(len(num_patches_list))]) question video_prefix 视频里的动物在做什么 response, history model.chat(tokenizer, pixel_values, question, generation_config, num_patches_listnum_patches_list, historyNone, return_historyTrue) # 追问描述整个视频 response, history model.chat(tokenizer, pixel_values, 详细描述这段视频。, generation_config, num_patches_listnum_patches_list, historyhistory, return_historyTrue) 调参建议短视频用 8 帧足够长视频或动作细节多的场景提高到 16~32 帧每帧max_num1可大幅节省显存。️ 不写代码也能玩Streamlit 本地 Demo仓库内置了可视化聊天界面支持上传图片与多轮对话主入口为 streamlit_demo/app.py。界面中可自由选择 InternVL 系列模型进行图片对话效果示例此外internvl_chat_llava/ 目录还基于 LLaVA 架构提供了一套命令行对话serve/cli.py适合习惯终端交互的用户。 进阶部署与微调从个人电脑到生产服务LMDeploy 部署官方推荐的推理加速方案lmdeploy serve api_server一条命令即可把模型包装成兼容 OpenAI 接口的 REST 服务多轮对话直接用pipe.chat会话接口LoRA 微调internvl_chat/shell/ 目录提供了 InternVL 1.2 / 1.5 / 2.0 / 2.5 / 3.0 全系列的预训练、SFT、LoRA、MPO 偏好优化脚本例如 internvl_chat_v1_5_internlm2_1_8b_dynamic_res_finetune.sh权重转换工具internvl_chat/tools/ 提供 HuggingFace ↔ 自定义格式互转、LoRA 合并、INT8 量化等实用工具模型选型InternVL2_5 家族覆盖 1B~78B 七种规格1B 适合笔记本级显存78B 对标顶级闭源模型对照表见 internvl_chat/README.md。✨ 总结新手使用 InternVL Chat 的 5 个要点环境Python 3.9 pip install -r requirements.txttransformers ≥ 4.37.2单图对话问题中加image占位符配load_image切图即可多轮记忆return_historyTrue 每轮回传history视频理解抽帧 Frame-i编号复用 chat 多轮接口生产化LMDeploy 一条命令部署为 OpenAI 兼容 API。InternVL Chat 以完全开源的方式提供了接近 GPT-4o 的多模态对话能力无论是图片问答、文档解析还是视频分析都值得一试【免费下载链接】InternVL[CVPR 2024 Oral] InternVL Family: A Pioneering Open-Source Alternative to GPT-4o. 接近GPT-4o表现的开源多模态对话模型项目地址: https://gitcode.com/GitHub_Trending/in/InternVL创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表