结构化字幕生成
更新时间:2026-07-29
简介
StructuredCaptionPipeline 基于 Qwen2.5-VL-7B-Instruct,对图像或视频编排「5 象限扫描 + 时序动态 pass」并合并为结构化 JSON caption,输出序列化的 JSON 字符串。
功能描述
• 支持图像与视频共 6 种输入源:video_url / video_base64 / video_binary / image_url / image_base64 / image_binary。
• 通过多轮 prompt engineering 生成主体、光照、5 象限扫描、时序动态与音频推测等字段,合并成结构化 JSON。
• 双后端推理:vLLM(默认)与 transformers 兜底,backend="auto" 时在 sm_120 新卡自动回退 transformers;lora_path 非空时加载结构化 caption LoRA。
• 必须运行在 CUDA GPU 上,权重需预置于 {model_path}/{model_name};do_temporal_pass 仅对视频生效,图像自动跳过。
算子参数
输入
| 输入 | 含义 |
|---|---|
| contents | 图像或视频输入列,取值形式由 content_src_type 决定(URL / base64 / 二进制) |
输出
| 输出 | 含义 |
|---|---|
| result | 结构化 caption,类型 string(序列化 JSON 字符串,含 subjects / lighting / quadrant_scan / temporal / audio);失败且 fail_on_error=False 时为空字符串 |
参数
| 参数名称 | 类型 | 默认值 | 描述 | |
|---|---|---|---|---|
| content_src_type | str | "video_url" | 输入类型,视频/图像各三种:video_url / video_base64 / video_binary / image_url / image_base64 / image_binary |
|
| model_path | str | "/opt/aihc/models" | 权重根目录,实际模型目录为 {model_path}/{model_name} |
|
| model_name | str | "Qwen/Qwen2.5-VL-7B-Instruct" | QwenVL 模型名称/子目录 | |
| lora_path | str |None | None | 结构化 caption LoRA 权重路径;None 表示无 LoRA,走原生 Qwen2.5-VL 冷启动 | |
| batch_size | int | 2 | 单批推理量(视频显存开销大) | |
| dtype | str | "auto" | 计算精度,可选 "auto" / "bfloat16" / "float16" / "float32" |
|
| max_model_len | int | 32000 | 最大上下文长度 | |
| max_pixels | int |None | 1280000 | 视觉输入像素上限 | |
| min_pixels | int |None | None | 视觉输入像素下限 | |
| fps | float |None | 2.0 | 视频抽帧帧率 | |
| tensor_parallel_size | int | 1 | 张量并行度,仅 vLLM 后端生效 | |
| gpu_memory_utilization | float | 0.85 | GPU 显存利用率,仅 vLLM 后端生效 | |
| enforce_eager | bool | False | 是否强制 eager 模式,仅 vLLM 后端生效 | |
| temperature | float | 0.1 | 采样温度 | |
| top_p | float | 0.9 | nucleus 采样 top-p | |
| max_tokens | int | 2048 | 单次生成最大 token 数 | |
| seed | int | 42 | 采样随机种子 | |
| do_quadrant_scan | bool | True | 是否做 5 象限扫描 | |
| do_temporal_pass | bool | True | 是否做时序动态单独 pass(视频生效,图像自动跳过) | |
| backend | str | "auto" | 推理后端,可选 "auto" / "vllm" / "transformers";sm_120 自动走 transformers |
|
| fail_on_error | bool | False | 出错时是否抛异常;False 时对应行返回空字符串 |
调用示例
Python
1from __future__ import annotations
2
3import os
4
5import daft
6from daft import col
7
8from daft.aihc.common.udf import aihc_udf
9from daft.aihc.functions.multimodal.structured_caption_pipeline import StructuredCaptionPipeline
10
11os.environ.setdefault("BOS_ENDPOINT", "http://bj.bcebos.com")
12os.environ.setdefault("BOS_REGION", "bj")
13
14MODEL_PATH = "/opt/aihc/models" # MOCK
15
16if __name__ == "__main__":
17 if os.getenv("DAFT_RUNNER", "native") == "ray":
18 import ray
19
20 ray.init(ignore_reinit_error=True)
21 daft.set_runner_ray()
22 daft.set_execution_config(actor_udf_ready_timeout=6000, min_cpu_per_task=0)
23
24 samples = {
25 "video": ["bos://your-bucket/sample.mp4"], # MOCK
26 }
27 ds = daft.from_pydict(samples)
28 ds = ds.with_column(
29 "caption",
30 aihc_udf(
31 StructuredCaptionPipeline,
32 construct_args={
33 "content_src_type": "video_url",
34 "model_path": MODEL_PATH,
35 "model_name": "Qwen/Qwen2.5-VL-7B-Instruct",
36 "fps": 2.0,
37 "do_quadrant_scan": True,
38 "do_temporal_pass": True,
39 "backend": "auto",
40 },
41 num_cpus=1,
42 num_gpus=1,
43 concurrency=1,
44 batch_size=1,
45 )(col("video")),
46 )
47 ds.show()
评价此篇文章
