File size: 6,951 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
# VINE HuggingFace Interface - Complete Overview

This directory contains a complete HuggingFace-compatible interface for the VINE (Video Understanding with Natural Language) model. The interface allows you to easily use, share, and deploy your VINE model through the HuggingFace ecosystem.

## πŸ“ Directory Structure

```
vine_hf/
β”œβ”€β”€ __init__.py                 # Package initialization and exports
β”œβ”€β”€ vine_config.py              # VineConfig class (PretrainedConfig)
β”œβ”€β”€ vine_model.py               # VineModel class (PreTrainedModel)  
β”œβ”€β”€ vine_pipeline.py            # VinePipeline class (Pipeline)
β”œβ”€β”€ example_usage.py            # Comprehensive usage examples
β”œβ”€β”€ convert_inference.py        # Migration guide from inference.py
β”œβ”€β”€ push_to_hub.py             # Script to push model to HF Hub
β”œβ”€β”€ setup.py                   # Package setup configuration
β”œβ”€β”€ README.md                  # Detailed documentation
└── OVERVIEW.md                # This file
```

## πŸ—οΈ Architecture Components

### 1. VineConfig (`vine_config.py`)
- Inherits from `PretrainedConfig`
- Configures model parameters, segmentation methods, and processing options
- Compatible with HuggingFace configuration system

### 2. VineModel (`vine_model.py`)  
- Inherits from `PreTrainedModel`
- Implements the core VINE model with three CLIP backbones
- Supports categorical, unary, and binary predictions
- Provides both `forward()` and `predict()` methods

### 3. VinePipeline (`vine_pipeline.py`)
- Inherits from `Pipeline`
- Handles end-to-end video processing workflow
- Integrates segmentation (SAM2, Grounding DINO + SAM2)
- Provides user-friendly interface for video understanding

## πŸš€ Key Features

βœ… **Full HuggingFace Compatibility**
- Compatible with `transformers` library
- Supports `AutoModel` and `pipeline` interfaces
- Can be pushed to and loaded from HuggingFace Hub

βœ… **Flexible Segmentation**
- Support for SAM2 automatic segmentation
- Support for Grounding DINO + SAM2 text-guided segmentation
- Configurable thresholds and parameters

βœ… **Multi-Modal Understanding**
- Categorical classification (object types)
- Unary predicates (single object actions)
- Binary relations (object-object relationships)

βœ… **Easy Integration**
- Simple pipeline interface for end users
- Direct model access for researchers
- Comprehensive configuration options

## πŸ“– Usage Examples

### Quick Start with Pipeline
```python
from transformers import pipeline
from vine_hf import VineModel, VinePipeline

# Create pipeline
vine_pipeline = pipeline(
    "vine-video-understanding",
    model="your-username/vine-model",
    trust_remote_code=True
)

# Process video
results = vine_pipeline(
    "video.mp4",
    categorical_keywords=['human', 'dog', 'frisbee'],
    unary_keywords=['running', 'jumping'],
    binary_keywords=['chasing', 'behind']
)
```

### Direct Model Usage
```python
from vine_hf import VineConfig, VineModel

config = VineConfig(segmentation_method="grounding_dino_sam2")
model = VineModel(config)

results = model.predict(
    video_frames=video_tensor,
    masks=masks_dict,
    bboxes=bboxes_dict,
    categorical_keywords=['human', 'dog'],
    unary_keywords=['running', 'sitting'],
    binary_keywords=['chasing', 'near']
)
```

## πŸ”§ Migration from Original Code

The `convert_inference.py` script shows how to migrate from the original `inference.py` workflow:

**Original Approach:**
- Manual model loading and configuration
- Direct handling of segmentation pipeline  
- Custom result processing
- Complex setup requirements

**New HuggingFace Interface:**
- Standardized model configuration
- Automatic preprocessing/postprocessing
- Simple pipeline interface
- Easy sharing via HuggingFace Hub

## πŸ“€ Sharing Your Model

Use the `push_to_hub.py` script to share your trained model:

```bash
python vine_hf/push_to_hub.py \
    --weights path/to/your/model.pth \
    --repo your-username/vine-model \
    --login
```

## πŸ› οΈ Installation & Setup

1. **Install Dependencies:**
```bash
pip install transformers torch torchvision opencv-python pillow numpy
```

2. **Install Segmentation Models (Optional):**
   - SAM2: https://github.com/facebookresearch/sam2
   - Grounding DINO: https://github.com/IDEA-Research/GroundingDINO

3. **Install VINE HF Interface:**
```bash
cd vine_hf
pip install -e .
```

## 🎯 Configuration Options

The `VineConfig` class supports extensive configuration:

- **Model Settings:** CLIP backbone, hidden dimensions
- **Segmentation:** Method, thresholds, target FPS  
- **Processing:** Alpha values, top-k results, video length limits
- **Performance:** Multi-class mode, output format options

## πŸ“Š Output Format

The interface returns structured predictions:

```python
{
    "categorical_predictions": {obj_id: [(prob, category), ...]},
    "unary_predictions": {(frame, obj): [(prob, action), ...]},
    "binary_predictions": {(frame, pair): [(prob, relation), ...]},
    "confidence_scores": {"categorical": float, "unary": float, "binary": float},
    "summary": {
        "num_objects_detected": int,
        "top_categories": [(category, prob), ...],
        "top_actions": [(action, prob), ...],
        "top_relations": [(relation, prob), ...]
    }
}
```

## πŸ” Testing & Validation

Run the example scripts to test your setup:

```bash
# Test basic functionality
python vine_hf/example_usage.py

# Test migration from original code  
python vine_hf/convert_inference.py
```

## 🀝 Contributing

To contribute or customize:

1. **Modify Configuration:** Edit `vine_config.py` for new parameters
2. **Extend Model:** Add functionality to `vine_model.py`
3. **Enhance Pipeline:** Improve preprocessing/postprocessing in `vine_pipeline.py`
4. **Add Features:** Create additional utility scripts

## πŸ“ Next Steps

1. **Load Your Weights:** Use your trained VINE model weights
2. **Test Segmentation:** Set up Grounding DINO and SAM2 models
3. **Validate Results:** Compare with original inference.py output
4. **Share Model:** Push to HuggingFace Hub for community use
5. **Deploy:** Use in applications, demos, or research projects

## πŸ› Troubleshooting

**Common Issues:**
- **Import Errors:** Check PYTHONPATH and package installation
- **Segmentation Failures:** Verify Grounding DINO/SAM2 setup
- **Weight Loading:** Adjust weight loading logic in `convert_inference.py`
- **CUDA Issues:** Check GPU availability and PyTorch installation

**Support:**
- Check the README.md for detailed documentation
- Review example_usage.py for working code examples
- Examine convert_inference.py for migration guidance

---

This HuggingFace interface makes VINE accessible to the broader ML community while maintaining all the powerful video understanding capabilities of the original model. The standardized interface enables easy sharing, deployment, and integration with existing HuggingFace workflows.