s
stella4528/peek-npu
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

PEEK(peek-npu)昇腾 NPU 适配

PEEK: Picking Essential frames via Efficient Knowledge distillation

PEEK 是一个无查询(query-free)的视频关键帧选择器:推理时仅接收视频帧,按预算 k 帧 预测每帧相关性分数,并按时间顺序返回选中的帧,可直接送入下游视频语言模型(Video-LLM)。

  • 原模型仓库:https://huggingface.co/momentslab/peek
  • 论文:PEEK: Picking Essential frames via Efficient Knowledge distillation(arXiv:2605.31029,BMVC 2026)
  • 权重:peek_base.safetensors(约 6.8 MB,2 层 Transformer,约 1.7M 参数,CC-BY-NC-SA-4.0)
  • 本仓库:昇腾 NPU(Ascend 910)推理适配版,权重已随仓库内置

模型架构

  1. 帧编码器:MobileCLIP2-S0(冻结,open_clip 预训练),每帧输出 512 维特征; 预处理为 resize-shorter-side 256 + 中心裁剪 256 + 归一化到 [0,1]。
  2. 打分器 PeekScorer:2 层 Transformer(hidden 256,4 头,FFN 1024,LayerNorm-first), 输入 (B, T, 512) 帧特征,输出 (B, T) 每帧相关性分数(identity 激活,logits)。
  3. 选取策略:stratified_argmax —— 将视频按时间等分为 k 个桶,每桶取最高分帧(论文策略)。
视频 → ffmpeg 按 fps 抽帧 → MobileCLIP2-S0 编码 (T, 512) → PeekScorer 打分 (T,)
     → stratified_argmax 选取 k 帧 → 关键帧索引 / 时间戳 / 分数

环境要求

  • 昇腾 NPU(Ascend 910 系列),CANN + torch_npu
  • Python >= 3.10
  • 依赖见 requirements.txt(torch、torch_npu、open_clip_torch、transformers、imageio-ffmpeg、safetensors 等)

快速开始

# 1. 安装依赖(昇腾环境需先安装 torch_npu,见 requirements.txt 注释)
pip install -r requirements.txt -i https://repo.huaweicloud.com/repository/pypi/simple

# 2. 运行默认用例(仓库内置 sample_video.mp4,6 秒 640x360 测试视频)
python3 inference.py --k 4

# 3. 指定视频与参数
python3 inference.py --video your_video.mp4 --k 4 --fps 2.0 --output-json result.json

参数说明

参数默认值说明
--videosample_video.mp4输入视频路径
--k4关键帧预算(选取帧数)
--fps2.0抽帧帧率
--selection-modestratified_argmax选取策略:stratified_argmax / topk / uniform
--device自动(npu → cuda → cpu)推理设备
--output-json无推理报告输出路径

说明:MobileCLIP2-S0 编码器权重首次运行会从 Hugging Face 自动下载(约 90 MB); 网络受限时可设置 HF_ENDPOINT=https://hf-mirror.com 走镜像。

NPU 实测结果(Ascend 910)

默认用例 sample_video.mp4(6 秒 / 2 fps → 12 帧,k=4):

项目结果
设备npu:0
帧数12
选中帧索引[2, 5, 7, 9]
选中帧时间戳(s)[1.0, 2.5, 3.5, 4.5]
抽帧耗时0.07 s
MobileCLIP2 编码耗时0.27 s
PeekScorer 打分耗时0.02 s
总计(含模型加载)10.4 s

输出示例:

{
  "model": "momentslab/peek",
  "device": "npu:0",
  "k": 4,
  "frame_count": 12,
  "selected_frame_indices": [2, 5, 7, 9],
  "selected_timestamps_sec": [1.0, 2.5, 3.5, 4.5],
  "selected_scores": [-0.5273, -0.1409, -0.3872, -0.1724],
  "timings_sec": {
    "frame_extract_sec": 0.073,
    "mobileclip2_encode_sec": 0.268,
    "peek_score_sec": 0.017,
    "total_sec": 10.429
  }
}

仓库内容

文件说明
inference.pyNPU 推理脚本(设备自动选择 npu/cuda/cpu)
peek_base.safetensorsPEEK 打分器权重(CC-BY-NC-SA-4.0)
config.json模型架构配置
sample_video.mp4内置演示视频
peek/PEEK 官方代码(Apache-2.0,vendored):model / encoder / frames / selection
requirements.txt环境依赖清单
LICENSECC-BY-NC-SA-4.0(权重许可证)

许可证

  • 权重:CC-BY-NC-SA-4.0(peek_base.safetensors)
  • 代码:Apache-2.0(vendored 官方代码 peek/ 与推理脚本)