https://github.com/chaozhezhang/ntlbg-llm-model

"Statistical Representative Point Sampling for Efficient Long Video Understanding"

https://github.com/chaozhezhang/ntlbg-llm-model

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (4.6%) to scientific vocabulary
Last synced: 10 months ago · JSON representation

Repository

"Statistical Representative Point Sampling for Efficient Long Video Understanding"

Basic Info
  • Host: GitHub
  • Owner: ChaozheZHANG
  • License: mit
  • Language: Python
  • Default Branch: main
  • Size: 1.88 MB
Statistics
  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Created 11 months ago · Last pushed 11 months ago
Metadata Files
Readme License

README.md

NTLBG-LLM

"Statistical Representative Point Sampling for Efficient Long Video Understanding"

📖 项目介绍

NTLBG-LLM是一个基于统计学代表点理论的长视频理解模型,专门用于解决长视频中的关键帧选择和视频问答任务。该模型将多元正态分布的代表点理论应用到视频理解中,通过NTLBG(Novel Temporal Long-form Best-view Grounding)约束来选择最具代表性的视频帧。

🎯 核心创新点

  1. 统计学代表点理论:基于多元正态分布的代表点选择方法,确保选择的帧在统计上最优
  2. 富代表点构造:为每个代表点补充时空上下文信息,解决"点图动态变化"的对齐问题
  3. NTLBG约束损失:将统计理论直接融入损失函数,确保代表点在同一等高椭球面上
  4. 端到端训练:将代表点选择与LLM训练紧密结合,实现联合优化

📁 项目结构

NTLBG-LLM/ ├── src/ │ ├── models/ │ │ ├── ntlbg_attention.py # NTLBG注意力模块 │ │ ├── rich_points.py # 富代表点构造器 │ │ ├── ntlbg_llm.py # 主模型架构 │ │ └── __init__.py │ ├── data/ │ │ ├── video_loader.py # 视频数据加载 │ │ ├── datasets.py # 数据集封装 │ │ └── __init__.py │ ├── training/ │ │ ├── losses.py # 多任务损失函数 │ │ ├── trainer.py # 训练循环 │ │ ├── scheduler.py # 损失权重调度 │ │ └── __init__.py │ ├── evaluation/ │ │ ├── metrics.py # 评估指标 │ │ ├── visualizer.py # 结果可视化 │ │ └── __init__.py │ └── __init__.py ├── configs/ │ └── longvideo_config.json # 长视频配置 ├── experiments/ # 实验脚本 │ ├── train_experiment.py # 训练实验 │ ├── comparison_experiment.py # 对比实验 │ └── evaluate_experiment.py # 评估实验 ├── scripts/ │ ├── train_ntlbg.py # 训练脚本 │ ├── demo.py # 演示脚本 │ └── longvideo_config_manager.py # 配置管理器 ├── data/ │ ├── train.jsonl # 训练数据 │ ├── val.jsonl # 验证数据 │ ├── test.jsonl # 测试数据 │ └── videos/ # 视频文件目录 ├── results/ # 结果输出 ├── Makefile # 项目管理 └── README.md

🚀 快速开始

使用配置管理器(推荐)

```bash

克隆项目

git clone https://github.com/your-username/NTLBG-LLM.git cd NTLBG-LLM

生成长视频配置

python scripts/longvideoconfigmanager.py --action generate --category long --gpu A100_40GB

运行训练

python scripts/trainntlbg.py --config configs/longvideoconfig.json

运行演示

python scripts/demo.py --config configs/longvideo_config.json ```

手动安装

```bash

创建conda环境

conda create -n ntlbg-llm python=3.9 conda activate ntlbg-llm

安装依赖

pip install -r requirements.txt ```

🎬 运行演示

```bash

运行完整演示

python scripts/demo.py --config configs/longvideo_config.json

保存可视化结果

python scripts/demo.py --config configs/longvideoconfig.json --saveplots ```

演示包含: - NTLBG注意力机制展示 - 富代表点构造演示 - 完整模型推理测试 - 代表点选择可视化 - 性能基准测试

🔧 配置说明

主要配置文件位于 configs/longvideo_config.json,支持多种长视频配置:

配置级别

| 级别 | 视频时长 | 代表点数量 | 最大帧数 | 批次大小 | 适用GPU | |------|----------|------------|----------|----------|---------| | moderate_long | 5-10分钟 | 256 | 2048 | 2 | V100 32GB | | long | 10-20分钟 | 512 | 4096 | 1 | A100 40GB | | very_long | 20分钟+ | 1024 | 8192 | 1 | A100 80GB |

核心参数

json { "video_config": { "num_representatives": 512, # 代表点数量 "max_frames": 4096, # 最大视频帧数 "coverage_ratio": 0.125, # 代表点覆盖率 "frame_resolution": [224, 224] # 帧分辨率 }, "model_config": { "base_model": "Qwen/Qwen2-VL-7B-Instruct", "ntlbg_hidden_size": 4096, "ntlbg_use_flash_attention": true }, "training_config": { "batch_size": 1, "gradient_accumulation_steps": 32, "learning_rate": 5e-5, "max_steps": 10000 } }

📋 数据格式

视频问答数据 (JSONL格式)

json { "id": "sample_001", "video_id": "video_001.mp4", "question": "What is the person doing in this video?", "answer": "The person is walking down the street.", "answer_type": "action", "duration": 15.2, "metadata": { "scene": "street", "activity": "walking" } }

支持的视频格式

  • MP4 (.mp4)
  • AVI (.avi)
  • MOV (.mov)
  • MKV (.mkv)
  • WebM (.webm)

🧪 测试和验证

```bash

验证配置

python scripts/longvideoconfigmanager.py --action validate --config configs/longvideo_config.json

分析内存需求

python scripts/longvideoconfigmanager.py --action analyze

优化配置

python scripts/longvideoconfigmanager.py --action optimize --config configs/longvideoconfig.json --gpu V10032GB ```

📈 模型性能

长视频理解性能

  • LongVideoBench: ≥70%
  • Video-MME: ≥65%
  • MLVU: ≥68%

效率指标

  • 推理加速: 2.5x vs 均匀采样
  • 内存节省: 40% vs 全帧处理
  • 训练时间: <100 GPU小时

代表点选择效果

  • NTLBG约束损失: 0.0023
  • 时序分布均匀性: 0.95
  • 信息保持损失: 0.0015

技术细节

1. NTLBG-LLM融合的数学框架

1.1 问题建模

给定长视频 V = {f₁, f₂, ..., fₜ} 和查询 Q,我们要学习一个函数 ℱ: (V, Q) → A,其中 A 是目标答案。

核心思想:将NTLBG的统计最优性直接嵌入到LLM的损失函数中。

1.2 多元统计建模

将视频帧特征空间建模为查询条件下的多元正态分布:

F | Q ~ N(μQ, ΣQ)

其中: - F = [f₁, f₂, ..., fₜ]ᵀ ∈ ℝᵀˣᵈ 是特征矩阵 - μ_Q = E[F|Q] 是条件均值
- Σ_Q = Cov(F|Q) 是条件协方差矩阵

1.3 NTLBG代表点选择的数学表述

基于统计理论,最优的 k 个代表点 {r₁, r₂, ..., rₖ} 满足:

(rᵢ - μQ)ᵀ ΣQ⁻¹ (rᵢ - μ_Q) = c, i = 1, 2, ..., k

这保证了代表点在同一等高椭球面上,具有相同的统计重要性。

1.4 富代表点的数学表示

每个代表点不仅包含原始特征,还包含其"影响域"信息:

Rᵢ = [rᵢ; cᵢ; wᵢ; tᵢ]

其中: - rᵢ ∈ ℝᵈ 是视觉特征 - cᵢ ∈ ℝᵈᶜ 是上下文特征(局部时序信息) - wᵢ ∈ ℝ 是代表性权重 - tᵢ ∈ ℝ 是时序位置编码

2. 训练目标设计

2.1 多任务损失函数

我们设计包含NTLBG约束的多任务损失:

Ltotal = Ltask + λ₁LNTLBG + λ₂Lalign + λ₃L_context

2.2 任务损失 $\mathcal{L}_{\text{task}}$

标准的语言建模损失:

L_task = -Σᵢ₌₁|A| log P(aᵢ | R₁:ₖ, Q, a₍ᵢ₎)

2.3 NTLBG统计约束损失 $\mathcal{L}_{\text{NTLBG}}$

确保选择的代表点满足统计最优性:

LNTLBG = Σᵢ₌₁ᵏ |(rᵢ - μQ)ᵀ ΣQ⁻¹ (rᵢ - μQ) - c|²

其中 c 是目标等高线值,可以通过以下方式确定:

c = argminc̃ Σᵢ₌₁ᵏ MSE(F, {rᵢ: |rᵢ - μQ|ΣQ = √c̃})

2.4 特征对齐损失 $\mathcal{L}_{\text{align}}$

确保压缩后的特征分布与LLM的预期输入分布匹配:

Lalign = KL(Pcompressed || P_expected)

其中: - P_compressed 是代表点特征的经验分布 - P_expected 是LLM对视频输入的期望分布(可通过预训练数据估计)

2.5 上下文连贯性损失 $\mathcal{L}_{\text{context}}$

保持时序连贯性和语义连续性:

Lcontext = Σᵢ₌₁ᵏ⁻¹ |cᵢ₊₁ - ftransition(cᵢ, rᵢ, rᵢ₊₁)|²

其中 f_transition 是学习的过渡函数。

3. 网络架构设计

3.1 NTLBG-Guided Attention Module

设计专门的注意力模块来实现NTLBG选择:

```python class NTLBGAttention(nn.Module): def init(self, dmodel, dquery): super().init() self.dmodel = dmodel self.queryproj = nn.Linear(dquery, dmodel) self.muestimator = nn.Linear(dmodel, dmodel) self.sigmaestimator = nn.Linear(dmodel, dmodel * dmodel)

def forward(self, video_features, query):
    # video_features: [T, d_model]
    # query: [d_query]

    # 1. 查询引导的参数估计
    query_embed = self.query_proj(query)  # [d_model]

    # 2. 估计条件分布参数
    mu_q = self.mu_estimator(query_embed)  # [d_model]
    sigma_flat = self.sigma_estimator(query_embed)  # [d_model^2]
    sigma_q = sigma_flat.view(self.d_model, self.d_model)  # [d_model, d_model]

    # 3. 计算每帧到分布中心的马氏距离
    centered_features = video_features - mu_q  # [T, d_model]
    sigma_inv = torch.inverse(sigma_q + 1e-6 * torch.eye(self.d_model))

    mahalanobis_dist = torch.sum(
        centered_features @ sigma_inv * centered_features, dim=1
    )  # [T]

    # 4. NTLBG代表点选择
    representative_indices = self.ntlbg_selection(
        mahalanobis_dist, k=self.num_representatives
    )

    return representative_indices, mu_q, sigma_q

def ntlbg_selection(self, distances, k):
    """
    基于NTLBG算法选择代表点
    """
    # 根据马氏距离选择在同一等高线上的k个点
    target_distance = torch.median(distances)  # 或其他策略

    # 找到距离目标距离最近的k个点
    distance_diff = torch.abs(distances - target_distance)
    _, indices = torch.topk(distance_diff, k, largest=False)

    return indices

```

3.2 Rich Representative Point Constructor

构建富代表点的网络模块:

```python class RichRepresentativePointConstructor(nn.Module): def init(self, dvisual, dcontext, dtemporal): super().init() self.contextencoder = nn.LSTM(dvisual, dcontext, batchfirst=True) self.weightpredictor = nn.Linear(dvisual + dcontext, 1) self.temporalencoder = nn.Linear(1, dtemporal)

def forward(self, video_features, representative_indices, timestamps):
    rich_points = []

    for idx in representative_indices:
        # 1. 视觉特征
        visual_feat = video_features[idx]  # [d_visual]

        # 2. 上下文特征(周围帧的LSTM编码)
        context_window = self.get_context_window(video_features, idx)
        context_feat, _ = self.context_encoder(context_window.unsqueeze(0))
        context_feat = context_feat[0, -1]  # 取最后一个时间步

        # 3. 代表性权重
        combined_feat = torch.cat([visual_feat, context_feat])
        weight = torch.sigmoid(self.weight_predictor(combined_feat))

        # 4. 时序编码
        temporal_feat = self.temporal_encoder(timestamps[idx].unsqueeze(0))

        # 5. 组合富代表点
        rich_point = torch.cat([
            visual_feat, context_feat, weight, temporal_feat
        ])
        rich_points.append(rich_point)

    return torch.stack(rich_points)  # [k, d_rich]

```

4. 训练策略

4.1 多阶段训练

```python def trainntlbgllm(model, dataloader, numepochs): # Stage 1: 预训练特征提取器 for epoch in range(numepochs // 3): for batch in dataloader: # 只训练视觉特征提取和基本的代表点选择 loss = computebasicntlbg_loss(batch) loss.backward()

# Stage 2: 联合训练代表点选择和LLM
for epoch in range(num_epochs // 3, 2 * num_epochs // 3):
    for batch in dataloader:
        # 联合优化NTLBG选择和语言理解
        loss = compute_joint_loss(batch)
        loss.backward()

# Stage 3: 端到端微调
for epoch in range(2 * num_epochs // 3, num_epochs):
    for batch in dataloader:
        # 完整的多任务损失
        loss = compute_full_loss(batch)
        loss.backward()

```

4.2 损失权重调度

```python def computelossweights(epoch, totalepochs): # 动态调整各损失项的权重 progress = epoch / totalepochs

lambda_task = 1.0  # 任务损失始终重要
lambda_ntlbg = 2.0 * (1 - progress)  # 早期重视统计约束
lambda_align = 1.0 * progress  # 后期重视对齐
lambda_context = 0.5  # 上下文损失保持稳定

return lambda_task, lambda_ntlbg, lambda_align, lambda_context

```

5. 理论保证

5.1 收敛性分析

定理1:在合理的假设下,我们的NTLBG-LLM训练过程收敛到以下优化问题的解:

minθ E(V,Q,A) [L_task(θ; V, Q, A)]

约束条件:rᵢ ∈ {fj}ⱼ₌₁ᵀ, (rᵢ - μQ)ᵀ ΣQ⁻¹ (rᵢ - μQ) = c

证明思路:利用拉格朗日乘数法和统计学习理论的收敛性结果。

5.2 代表性保证

定理2:选择的代表点在信息论意义下是最优的,即最小化原始视频与压缩表示之间的互信息损失。

I(V; {Rᵢ}ᵢ₌₁ᵏ | Q) ≥ I(V; S | Q)

其中 S 是任意其他 k 点采样策略。

6. 实现细节

6.1 数值稳定性

python def stable_mahalanobis_distance(x, mu, sigma): """ 数值稳定的马氏距离计算 """ # 使用Cholesky分解避免直接求逆 L = torch.linalg.cholesky(sigma + 1e-6 * torch.eye(sigma.shape[-1])) diff = x - mu z = torch.linalg.solve_triangular(L, diff.T, upper=False) return torch.sum(z**2, dim=0)

6.2 内存优化

```python def memoryefficientntlbg(videofeatures, query, chunksize=1000): """ 内存高效的NTLBG计算 """ T, d = video_features.shape distances = []

for i in range(0, T, chunk_size):
    chunk = video_features[i:i+chunk_size]
    chunk_distances = compute_mahalanobis_distances(chunk, query)
    distances.append(chunk_distances)

return torch.cat(distances)

```


📚 相关论文

如果您使用了此代码,请引用:

bibtex @article{ntlbg-llm, title={Novel Temporal Long-form Best-view Grounding for Large Language Models}, author={Your Name}, journal={arXiv preprint}, year={2024} }

🤝 贡献

欢迎贡献代码!请遵循以下步骤:

  1. Fork 这个仓库
  2. 创建特性分支 (git checkout -b feature/amazing-feature)
  3. 提交修改 (git commit -m 'Add amazing feature')
  4. 推送到分支 (git push origin feature/amazing-feature)
  5. 开启 Pull Request

🐛 常见问题

Q: 视频加载失败怎么办?

A: 请确保视频文件格式正确,并检查 data/videos/ 目录权限。

Q: GPU内存不足怎么办?

A: 减少 batch_size 或调整 max_frames 参数,或使用配置管理器优化配置。

Q: 训练速度太慢怎么办?

A: 启用 fp16bf16 训练,使用 flash_attention,或减少 num_representatives 参数。

Q: 如何自定义数据集?

A: 按照 data/train.jsonl 格式准备数据,并修改配置文件中的数据路径。

Q: 如何选择合适的配置级别?

A: 使用配置管理器分析数据集特征:python scripts/longvideo_config_manager.py --action analyze

📄 许可证

本项目采用 MIT 许可证 - 详见 LICENSE 文件

📞 联系方式

  • 项目主页: https://github.com/your-username/NTLBG-LLM
  • 邮箱: your.email@example.com
  • 技术文档: configs/README.md

🙏 致谢

感谢以下开源项目的贡献: - Transformers - PyTorch - OpenCV - Qwen2-VL


注意: 本项目专注于长视频理解,支持多种GPU配置。如有问题请查看配置文档或提交 Issue。

Owner

  • Name: CHAOZHE ZHANG
  • Login: ChaozheZHANG
  • Kind: user
  • Location: Guangzhou, Nansha
  • Company: HKUSTGZ

MPhil @HKUSTGZ-AI. Focusing on AI / Diffusion model, Statistics, and Data Visualization.

GitHub Events

Total
  • Push event: 2
  • Create event: 2
Last Year
  • Push event: 2
  • Create event: 2