File size: 10,108 Bytes
f9a6349
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# VINE: Video Understanding with Natural Language

[![HuggingFace](https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-video--fm%2Fvine-blue)](https://huggingface.co/video-fm/vine)
[![GitHub](https://img.shields.io/badge/GitHub-LASER-green)](https://github.com/kevinxuez/LASER)

VINE is a video understanding model that processes videos along with categorical, unary, and binary keywords to return probability distributions over those keywords for detected objects and their relationships.

## Quick Start

```python
from transformers import AutoModel
from vine_hf import VineConfig, VineModel, VinePipeline

# Load VINE model from HuggingFace
model = AutoModel.from_pretrained('video-fm/vine', trust_remote_code=True)

# Create pipeline with your checkpoint paths
vine_pipeline = VinePipeline(
    model=model,
    tokenizer=None,
    sam_config_path="/path/to/sam2_config.yaml",
    sam_checkpoint_path="/path/to/sam2_checkpoint.pt",
    gd_config_path="/path/to/grounding_dino_config.py",
    gd_checkpoint_path="/path/to/grounding_dino_checkpoint.pth",
    device="cuda",
    trust_remote_code=True
)

# Process a video
results = vine_pipeline(
    'path/to/video.mp4',
    categorical_keywords=['human', 'dog', 'frisbee'],
    unary_keywords=['running', 'jumping'],
    binary_keywords=['chasing', 'behind'],
    return_top_k=3
)
```

## Installation

### Option 1: Automated Setup (Recommended)

```bash
# Download the setup script
wget https://raw.githubusercontent.com/kevinxuez/vine_hf/main/setup_vine_demo.sh

# Run the setup
bash setup_vine_demo.sh

# Activate environment
conda activate vine_demo
```

### Option 2: Manual Installation

```bash
# 1. Create conda environment
conda create -n vine_demo python=3.10 -y
conda activate vine_demo

# 2. Install PyTorch with CUDA support
pip install torch==2.7.1 torchvision==0.22.1 --index-url https://download.pytorch.org/whl/cu126

# 3. Install core dependencies
pip install transformers huggingface-hub safetensors

# 4. Clone and install required repositories
git clone https://github.com/video-fm/video-sam2.git
git clone https://github.com/video-fm/GroundingDINO.git
git clone https://github.com/kevinxuez/LASER.git
git clone https://github.com/kevinxuez/vine_hf.git

# Install in editable mode
pip install -e ./video-sam2
pip install -e ./GroundingDINO
pip install -e ./LASER
pip install -e ./vine_hf

# Build GroundingDINO extensions
cd GroundingDINO && python setup.py build_ext --force --inplace && cd ..
```

## Required Checkpoints

VINE requires SAM2 and GroundingDINO checkpoints for segmentation. Download these separately:

### SAM2 Checkpoint
```bash
wget https://dl.fbaipublicfiles.com/segment_anything_2/072824/sam2_hiera_tiny.pt
wget https://raw.githubusercontent.com/facebookresearch/sam2/main/sam2/configs/sam2.1/sam2.1_hiera_t.yaml
```

### GroundingDINO Checkpoint
```bash
wget https://github.com/IDEA-Research/GroundingDINO/releases/download/v0.1.0-alpha/groundingdino_swint_ogc.pth
wget https://raw.githubusercontent.com/IDEA-Research/GroundingDINO/main/groundingdino/config/GroundingDINO_SwinT_OGC.py
```

## Architecture

```
video-fm/vine (HuggingFace Hub)
β”œβ”€β”€ VINE Model Weights (~1.8GB)
β”‚   β”œβ”€β”€ Categorical CLIP model (fine-tuned)
β”‚   β”œβ”€β”€ Unary CLIP model (fine-tuned)
β”‚   └── Binary CLIP model (fine-tuned)
└── Architecture Files
    β”œβ”€β”€ vine_config.py
    β”œβ”€β”€ vine_model.py
    β”œβ”€β”€ vine_pipeline.py
    └── utilities

User Provides:
β”œβ”€β”€ Dependencies (via pip/conda)
β”‚   β”œβ”€β”€ laser (video processing utilities)
β”‚   β”œβ”€β”€ sam2 (segmentation)
β”‚   └── groundingdino (object detection)
└── Checkpoints (downloaded separately)
    β”œβ”€β”€ SAM2 model files
    └── GroundingDINO model files
```

## Why This Architecture?

This separation of concerns provides several benefits:

1. **Lightweight Distribution**: Only VINE-specific weights (~1.8GB) are on HuggingFace
2. **Version Control**: Users can choose their preferred SAM2/GroundingDINO versions
3. **Licensing**: Keeps different model licenses separate
4. **Flexibility**: Easy to swap segmentation backends
5. **Standard Practice**: Similar to models like LLaVA, BLIP-2, etc.

## Full Usage Example

```python
import os
from pathlib import Path
from transformers import AutoModel
from vine_hf import VinePipeline

# Set up paths
checkpoint_dir = Path("/path/to/checkpoints")
sam_config = checkpoint_dir / "sam2_hiera_t.yaml"
sam_checkpoint = checkpoint_dir / "sam2_hiera_tiny.pt"
gd_config = checkpoint_dir / "GroundingDINO_SwinT_OGC.py"
gd_checkpoint = checkpoint_dir / "groundingdino_swint_ogc.pth"

# Load VINE from HuggingFace
model = AutoModel.from_pretrained('video-fm/vine', trust_remote_code=True)

# Create pipeline
vine_pipeline = VinePipeline(
    model=model,
    tokenizer=None,
    sam_config_path=str(sam_config),
    sam_checkpoint_path=str(sam_checkpoint),
    gd_config_path=str(gd_config),
    gd_checkpoint_path=str(gd_checkpoint),
    device="cuda:0",
    trust_remote_code=True
)

# Process video
results = vine_pipeline(
    "path/to/video.mp4",
    categorical_keywords=['person', 'dog', 'ball'],
    unary_keywords=['running', 'jumping', 'sitting'],
    binary_keywords=['chasing', 'next to', 'holding'],
    object_pairs=[(0, 1), (0, 2)],  # person-dog, person-ball
    return_top_k=5,
    include_visualizations=True
)

# Access results
print(f"Detected {results['summary']['num_objects_detected']} objects")
print(f"Top categories: {results['summary']['top_categories']}")
print(f"Top actions: {results['summary']['top_actions']}")
print(f"Top relations: {results['summary']['top_relations']}")

# Access detailed predictions
for obj_id, predictions in results['categorical_predictions'].items():
    print(f"\nObject {obj_id}:")
    for prob, category in predictions:
        print(f"  {category}: {prob:.3f}")
```

## Output Format

```python
{
    "categorical_predictions": {
        object_id: [(probability, category), ...]
    },
    "unary_predictions": {
        (frame_id, object_id): [(probability, action), ...]
    },
    "binary_predictions": {
        (frame_id, (obj1_id, obj2_id)): [(probability, relation), ...]
    },
    "confidence_scores": {
        "categorical": float,
        "unary": float,
        "binary": float
    },
    "summary": {
        "num_objects_detected": int,
        "top_categories": [(category, probability), ...],
        "top_actions": [(action, probability), ...],
        "top_relations": [(relation, probability), ...]
    },
    "visualizations": {  # if include_visualizations=True
        "vine": {
            "all": {"frames": [...], "video_path": "..."},
            ...
        }
    }
}
```

## Configuration Options

```python
from vine_hf import VineConfig

config = VineConfig(
    model_name="openai/clip-vit-base-patch32",  # CLIP backbone
    segmentation_method="grounding_dino_sam2",   # or "sam2"
    box_threshold=0.35,                          # GroundingDINO threshold
    text_threshold=0.25,                         # GroundingDINO threshold
    target_fps=5,                                # Video sampling rate
    visualize=True,                              # Enable visualizations
    visualization_dir="outputs/",                # Output directory
    debug_visualizations=False,                  # Debug mode
    device="cuda:0"                              # Device
)
```

## Deployment Examples

### Local Script
```python
# test_vine.py
from transformers import AutoModel
from vine_hf import VinePipeline

model = AutoModel.from_pretrained('video-fm/vine', trust_remote_code=True)
pipeline = VinePipeline(model=model, ...)
results = pipeline("video.mp4", ...)
```

### HuggingFace Spaces
```python
# app.py for Gradio Space
import gradio as gr
from transformers import AutoModel
from vine_hf import VinePipeline

model = AutoModel.from_pretrained('video-fm/vine', trust_remote_code=True)
# ... set up pipeline and Gradio interface
```

### API Server
```python
# FastAPI server
from fastapi import FastAPI
from transformers import AutoModel
from vine_hf import VinePipeline

app = FastAPI()
model = AutoModel.from_pretrained('video-fm/vine', trust_remote_code=True)
pipeline = VinePipeline(model=model, ...)

@app.post("/process")
async def process_video(video_path: str):
    return pipeline(video_path, ...)
```

## Troubleshooting

### Import Errors
```bash
# Make sure all dependencies are installed
pip list | grep -E "laser|sam2|groundingdino"

# Reinstall if needed
pip install -e ./LASER
pip install -e ./video-sam2
pip install -e ./GroundingDINO
```

### CUDA Errors
```python
# Check CUDA availability
import torch
print(torch.cuda.is_available())
print(torch.version.cuda)

# Use CPU if needed
pipeline = VinePipeline(model=model, device="cpu", ...)
```

### Checkpoint Not Found
```bash
# Verify checkpoint paths
ls -lh /path/to/sam2_hiera_tiny.pt
ls -lh /path/to/groundingdino_swint_ogc.pth
```

## System Requirements

- **Python**: 3.10+
- **CUDA**: 11.8+ (for GPU)
- **GPU**: 8GB+ VRAM recommended (T4, V100, A100, etc.)
- **RAM**: 16GB+ recommended
- **Storage**: ~3GB for checkpoints

## Citation

```bibtex
@article{laser2024,
  title={LASER: Language-guided Object Grounding and Relation Understanding in Videos},
  author={Your Authors},
  journal={Your Conference/Journal},
  year={2024}
}
```

## License

This model and code are released under the MIT License. Note that SAM2 and GroundingDINO have their own respective licenses.

## Links

- **Model**: https://huggingface.co/video-fm/vine
- **Code**: https://github.com/kevinxuez/LASER
- **vine_hf Package**: https://github.com/kevinxuez/vine_hf
- **SAM2**: https://github.com/facebookresearch/sam2
- **GroundingDINO**: https://github.com/IDEA-Research/GroundingDINO

## Support

For issues or questions:
- **Model/Architecture**: [HuggingFace Discussions](https://huggingface.co/video-fm/vine/discussions)
- **LASER Framework**: [GitHub Issues](https://github.com/kevinxuez/LASER/issues)
- **vine_hf Package**: [GitHub Issues](https://github.com/kevinxuez/vine_hf/issues)