短语定位召回率
更新时间:2026-09-11
简介
从 caption 里抽名词短语,用 Owl-ViT 做开集检测,统计有多少名词短语能在图上定位到,得出召回率。算子只产出召回率分数,不做阈值过滤,过滤交给 pipeline。
功能描述
- 名词短语抽取:
word_tokenize分词(先小写)→ nltkpos_tag→RegexpParser按语法NP: {<DT>?<JJ.*>*<NN.*>+}取 NP 子树 → 去标点 → 去重 - 分词兜底:
word_tokenize在 nltk>=3.8.2 需要punkt_tab,只预置了punkt时会LookupError,此时自动回退到不需要数据文件的TreebankWordTokenizer - 检测:所有名词短语作为一组 text query 一次送
OwlViTProcessor+OwlViTForObjectDetection,post_process_object_detection的阈值取conf_thr - 召回率统计:预测按置信度降序,只看前
短语数个预测;label越界的跳过;同一短语只记一次命中;框面积 / 图面积超过large_area_ratio_thr的(几乎覆盖整图)跳过;与已命中框 IoU 超过iou_thr的(重复框)跳过。召回率 = 命中短语数 / 短语总数,保留 6 位小数 - caption 抽不到名词短语时该行记
1.0(不是 0) - 图像为
None、文本非字符串或去空白后为空、以及该行推理抛异常,都输出None并记exception日志,不中断整列 - 算子内不做网络请求与安装:nltk 数据要求离线预置在
nltk_data_path(缺失即构造期抛FileNotFoundError),并把该目录插到nltk.data.path首位 - 逐行推理,不做批合并;图像统一
convert("RGB"),target_sizes用原图尺寸,框坐标是原图像素的[xmin, ymin, xmax, ymax] - Owl-ViT 权重按默认精度(float32)加载,算子不提供 dtype 参数;设备为
cuda:(rank % 可见卡数)(use_gpu 且 CUDA 可用)否则 cpu - 构造期校验
image_src_type取值;两列长度不一致时抛ValueError
算子参数
输入
| 输入 | 含义 |
|---|---|
| images | 图像输入列,内容形式由 image_src_type 决定(URL/本地或 BOS 路径 / Base64 字符串 / 二进制) |
| texts | caption 文本列,名词短语从这里抽;空串或空白串该行输出 None |
输出
| 输出 | 含义 |
|---|---|
| grounding_recall | float64 —— 名词短语定位召回率,取值 [0, 1];抽不到名词短语记 1.0;图像/文本为空或该行失败时为 None |
参数
| 参数名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| image_src_type | str | "image_url" | 图像输入类型:image_url(本地/BOS 路径)、image_base64、image_binary |
| model_path | str | "/opt/aihc/model" | 权重根目录 |
| model_name | str | "google/owlvit-base-patch32" | 相对 model_path 的 Owl-ViT 权重子目录 |
| nltk_data_path | str | "/opt/aihc/model/nltk_data" | 离线预置的 nltk 数据目录,需含 taggers/averaged_perceptron_tagger* |
| iou_thr | float | 0.5 | 命中框之间的 IoU 抑制阈值,超过视为重复框 |
| large_area_ratio_thr | float | 0.95 | 框面积 / 图面积超过该比例视为覆盖整图,丢弃 |
| conf_thr | float | 0.0 | 检测置信度阈值,透传 post_process_object_detection;默认 0 表示不按置信度过滤 |
| rank | int | 0 | 多卡场景 worker 序号,设备取 cuda:(rank % 可见卡数) |
注意事项
- 抽不到名词短语的样本返回
1.0(表示「无短语可定位,不惩罚」),下游按「低于阈值即丢弃」过滤时不会误杀这类样本,但也意味着1.0不能一律当作「定位很好」。 - nltk 数据缺失时构造期直接抛
FileNotFoundError,不会联网下载;POS 标注用averaged_perceptron_tagger,若punkt/punkt_tab不可用,分词会退化到TreebankWordTokenizer兜底并打 warning 日志。 - NP 语法基于英文 POS,中文 caption 抽不到名词短语,结果恒为
1.0。 - 短语数越多,一次前向的 text query 越多,显存与耗时随之上升;同时「只看前
短语数个预测」的口径意味着短语很多时低分预测会被裁掉。
调用示例
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.phrase_grounding_recall import PhraseGroundingRecall
10
11MODEL_PATH = "/path/to/models"
12
13if __name__ == "__main__":
14 if os.getenv("DAFT_RUNNER", "native") == "ray":
15 import ray
16 ray.init(ignore_reinit_error=True)
17 daft.set_runner_ray()
18 daft.set_execution_config(actor_udf_ready_timeout=6000, min_cpu_per_task=0)
19
20image = "bos://your-bucket/sample.jpg"
21 ds = daft.from_pydict({"image": [image, image], "text": ["a photo of a dog and a red car", "quickly"]})
22 ds = ds.with_column(
23 "grounding_recall",
24 aihc_udf(
25 PhraseGroundingRecall,
26 construct_args={
27 "image_src_type": "image_url",
28 "model_path": MODEL_PATH,
29 "model_name": "google/owlvit-base-patch32",
30 "nltk_data_path": f"{MODEL_PATH}/nltk_data",
31 },
32 num_cpus=1,
33 num_gpus=1,
34 concurrency=1,
35 batch_size=2,
36 )(col("image"), col("text")),
37 )
38 ds.show()
评价此篇文章
