Update README.md
Browse files
README.md
CHANGED
|
@@ -6,7 +6,7 @@ tags:
|
|
| 6 |
- multimodal
|
| 7 |
- qwen2.5-omni
|
| 8 |
datasets:
|
| 9 |
-
- keentomato/
|
| 10 |
---
|
| 11 |
|
| 12 |
# OmniSapiens SFT
|
|
@@ -14,4 +14,89 @@ datasets:
|
|
| 14 |
Fine-tuned [Qwen2.5-Omni-7B](https://huggingface.co/Qwen/Qwen2.5-Omni-7B) for human behavior understanding.
|
| 15 |
|
| 16 |
## Benchmark
|
| 17 |
-
Evaluated on [keentomato/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
- multimodal
|
| 7 |
- qwen2.5-omni
|
| 8 |
datasets:
|
| 9 |
+
- keentomato/human_behavior_atlas
|
| 10 |
---
|
| 11 |
|
| 12 |
# OmniSapiens SFT
|
|
|
|
| 14 |
Fine-tuned [Qwen2.5-Omni-7B](https://huggingface.co/Qwen/Qwen2.5-Omni-7B) for human behavior understanding.
|
| 15 |
|
| 16 |
## Benchmark
|
| 17 |
+
Evaluated on [keentomato/human_behavior_atlas](https://huggingface.co/datasets/keentomato/human_behavior_atlas).
|
| 18 |
+
|
| 19 |
+
## Usage
|
| 20 |
+
|
| 21 |
+
### Installation
|
| 22 |
+
```bash
|
| 23 |
+
pip install transformers torch huggingface_hub
|
| 24 |
+
```
|
| 25 |
+
|
| 26 |
+
### Classification
|
| 27 |
+
|
| 28 |
+
```python
|
| 29 |
+
import json, torch
|
| 30 |
+
from huggingface_hub import hf_hub_download
|
| 31 |
+
from transformers import Qwen2_5OmniThinkerForConditionalGeneration, AutoProcessor
|
| 32 |
+
|
| 33 |
+
MODEL_ID = "keentomato/omnisapiens_sft"
|
| 34 |
+
|
| 35 |
+
# 1. Load backbone and processor
|
| 36 |
+
model = Qwen2_5OmniThinkerForConditionalGeneration.from_pretrained(
|
| 37 |
+
MODEL_ID, torch_dtype=torch.float16, device_map="auto"
|
| 38 |
+
)
|
| 39 |
+
processor = AutoProcessor.from_pretrained(MODEL_ID)
|
| 40 |
+
|
| 41 |
+
# 2. Load classification heads and label scheme
|
| 42 |
+
heads_path = hf_hub_download(MODEL_ID, "heads.bin")
|
| 43 |
+
label_path = hf_hub_download(MODEL_ID, "label_scheme.json")
|
| 44 |
+
heads_sd = torch.load(heads_path, map_location="cpu")
|
| 45 |
+
with open(label_path) as f:
|
| 46 |
+
label_scheme = json.load(f)
|
| 47 |
+
|
| 48 |
+
# 3. Reconstruct domain heads
|
| 49 |
+
global_classes = label_scheme["meta"]["global_classes"] # {domain: [{index, label}, ...]}
|
| 50 |
+
hidden_size = model.config.hidden_size
|
| 51 |
+
domain_names = list(global_classes.keys())
|
| 52 |
+
domain_heads = torch.nn.ModuleList([
|
| 53 |
+
torch.nn.Linear(hidden_size, len(global_classes[d])) for d in domain_names
|
| 54 |
+
])
|
| 55 |
+
domain_heads.load_state_dict({k.replace("heads.", ""): v for k, v in heads_sd.items()})
|
| 56 |
+
domain_heads.eval().to(model.device).to(torch.float16)
|
| 57 |
+
domain_to_id = {d: i for i, d in enumerate(domain_names)}
|
| 58 |
+
|
| 59 |
+
# 4. Prepare multimodal inputs
|
| 60 |
+
# video_tensor: [T, C, H, W] tensor or list of PIL images
|
| 61 |
+
# audio_waveform: 1-D numpy array / tensor at 16 kHz
|
| 62 |
+
domain = "emotion" # one of: "sentiment_intensity", "emotion", "mental_health_ptsd", "mental_health_depression", "mental_health_anxiety", "sarcasm", "humour"
|
| 63 |
+
messages = [{"role": "user", "content": [
|
| 64 |
+
{"type": "video"},
|
| 65 |
+
{"type": "audio"},
|
| 66 |
+
{"type": "text", "text": "Classify the human behavior expressed."},
|
| 67 |
+
]}]
|
| 68 |
+
text = processor.apply_chat_template(messages, add_generation_prompt=False, tokenize=False)
|
| 69 |
+
inputs = processor(text=[text], videos=[video_tensor], audio=[audio_waveform], return_tensors="pt")
|
| 70 |
+
inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
| 71 |
+
|
| 72 |
+
# 5. Forward pass — pool penultimate hidden layer, route through domain head
|
| 73 |
+
with torch.no_grad():
|
| 74 |
+
out = model(**inputs, output_hidden_states=True, use_cache=False)
|
| 75 |
+
h = out.hidden_states[-2] # [B, T, H]
|
| 76 |
+
mask = inputs["attention_mask"].unsqueeze(-1).float()
|
| 77 |
+
pooled = (h * mask).sum(1) / mask.sum(1) # [B, H]
|
| 78 |
+
logits = domain_heads[domain_to_id[domain]](pooled.float()) # [B, K_d]
|
| 79 |
+
pred_idx = logits.argmax(dim=-1).item()
|
| 80 |
+
|
| 81 |
+
label_name = global_classes[domain][pred_idx]["label"]
|
| 82 |
+
print(f"Predicted {domain}: {label_name}")
|
| 83 |
+
```
|
| 84 |
+
|
| 85 |
+
### QA / Open-ended generation
|
| 86 |
+
|
| 87 |
+
```python
|
| 88 |
+
messages = [{"role": "user", "content": [
|
| 89 |
+
{"type": "video"},
|
| 90 |
+
{"type": "audio"},
|
| 91 |
+
{"type": "text", "text": "Describe the emotional state of the person in this video."},
|
| 92 |
+
]}]
|
| 93 |
+
text = processor.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
|
| 94 |
+
inputs = processor(text=[text], videos=[video_tensor], audio=[audio_waveform], return_tensors="pt")
|
| 95 |
+
inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
| 96 |
+
|
| 97 |
+
with torch.no_grad():
|
| 98 |
+
generated = model.generate(**inputs, max_new_tokens=128)
|
| 99 |
+
|
| 100 |
+
answer = processor.decode(generated[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
|
| 101 |
+
print(answer)
|
| 102 |
+
```
|