File size: 7,567 Bytes
943eccd
aee40b4
 
943eccd
aee40b4
 
 
 
 
 
 
 
 
 
 
 
 
943eccd
aee40b4
ebcd6f6
aee40b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fe68c2
 
 
aee40b4
2fe68c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aee40b4
 
ebcd6f6
aee40b4
ebcd6f6
aee40b4
 
 
 
 
 
 
 
 
 
 
ebcd6f6
aee40b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebcd6f6
aee40b4
 
 
 
 
 
 
 
 
 
 
 
ebcd6f6
aee40b4
 
 
ebcd6f6
 
 
 
 
 
aee40b4
 
 
 
 
 
 
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
---
language:
- en
license: apache-2.0
tags:
- audio
- audio-classification
- emotion-recognition
- speech-emotion-recognition
- f1
- formula1
- wavlm
base_model: microsoft/wavlm-base-plus
pipeline_tag: audio-classification
metrics:
- accuracy
- f1
---

# F1 Driver Tone & Emotion Detector

An advanced Speech Emotion Recognition (SER) deep learning model designed to detect the emotional tone of Formula 1 driver team radio communications in real-time.

The model processes raw audio input from driver-to-pit-wall communications, extracts context-rich speech representations using a pretrained **WavLM** encoder (`microsoft/wavlm-base-plus`), and passes them through a downstream **BiLSTM with Temporal Attention mechanism** to classify driver emotions.

---

## Model Overview

- **Base Audio Encoder:** [microsoft/wavlm-base-plus](https://huggingface.co/microsoft/wavlm-base-plus) (768-dimensional frozen embeddings)
- **Downstream Architecture:** Bidirectional LSTM (128 hidden size) + Temporal Attention Module + Linear Classifier
- **Target Emotions (6 Classes):**
  - `0`: **Anger**
  - `1`: **Disgust**
  - `2`: **Fear**
  - `3`: **Happy**
  - `4`: **Neutral**
  - `5`: **Sad**

---

## Model Architecture

```text
                     RAW DRIVER AUDIO (.wav, .mp3, .flac)
                                     |
                                     v
                        Audio Preprocessing Pipeline
                  - Convert Multi-channel to Mono
                  - Resample to 16,000 Hz (16 kHz)
                  - Peak Amplitude Normalization
                                     |
                                     v
                        Frozen WavLM Encoder
                      (microsoft/wavlm-base-plus)
                                     |
                                     v
                       Frame Embeddings [B, T, 768]
                                     |
                                     v
                              Bidirectional LSTM
                          (hidden_size = 128 x 2 = 256)
                                     |
                                     v
                           Temporal Attention
                 (Aggregates sequence into fixed context vector [B, 256])
                                     |
                                     v
                            Linear Classifier
                             (256 -> 6 Logits)
                                     |
                                     v
                  Softmax Probabilities & Emotion Output
```
---

## Model Summary

| Property | Value |
| :--- | :--- |
| **Model Name** | F1 Driver Tone & Emotion Detector |
| **Architecture Type** | WavLM + BiLSTM + Temporal Attention |
| **Base Speech Encoder** | [microsoft/wavlm-base-plus](https://huggingface.co/microsoft/wavlm-base-plus) |
| **Total Parameters** | ~95.2M |
| **Trainable Parameters** | ~462K (Downstream Classifier Head) |
| **Frozen Parameters** | ~94.7M (Frozen Base Encoder) |
| **Encoder Transformer Layers** | 12 Layers |
| **Encoder Hidden Dimension** | 768 |
| **Encoder Attention Heads** | 12 |
| **Downstream Feature Extractor** | Bidirectional LSTM (`hidden_size` = 128) |
| **BiLSTM Output Dimension** | 256 (`128 x 2`) |
| **Aggregation Mechanism** | Frame-level Temporal Attention |
| **Classifier Head** | Linear Layer (`256 -> 6` Logits) |
| **Number of Classes** | 6 (`Anger`, `Disgust`, `Fear`, `Happy`, `Neutral`, `Sad`) |
| **Target Sampling Rate** | 16,000 Hz (16 kHz Mono) |
| **Inference Chunking** | 2.5s Sliding Window (1.5s Hop / 1.0s Overlap) |
| **Modality** | Audio |
| **Output Format** | Emotion Label, Confidence %, Probability Distribution |
| **Training Loss Function** | Cross-Entropy Loss |
| **Optimizer** | AdamW (`lr` = 1e-3, `weight_decay` = 1e-4) |

---
---

## Input & Output Specifications

### Direct PyTorch Model I/O (`models.py` + `best_model.pt`)
For users directly loading the PyTorch `BiLSTMAttentionClassifier` model and `.pt` weights:

- **Model Input:** 
  - `x`: PyTorch Tensor of WavLM frame embeddings with shape **`[Batch_Size, Sequence_Length, 768]`**
  - `mask`: Timestep mask tensor with shape **`[Batch_Size, Sequence_Length]`** (Optional: 1 for valid frame, 0 for padding)
- **Model Output:**
  - `logits`: Tensor of shape **`[Batch_Size, 6]`** (Unnormalized raw class logits)
  - `attn_weights`: Attention weight tensor of shape **`[Batch_Size, Sequence_Length]`** (Frame-level temporal attention scores)

---

### Full Pipeline I/O (Using `modeling_f1tone.py`)
For users using the complete audio-to-prediction helper script:

- **Input Requirements:**
  - Raw Audio File (`.wav`, `.mp3`, `.flac`, `.ogg`, `.m4a`)
  - Automatically converted to 16 kHz Mono and peak-normalized.
- **Output Return Format:**
  Structured Python dictionary:
  ```json
  {
    "audio_file": "driver_radio_01.wav",
    "predicted_emotion": "Anger",
    "confidence": 94.25,
    "probabilities": {
      "Anger": 94.25,
      "Neutral": 3.10,
      "Disgust": 1.45,
      "Fear": 0.70,
      "Happy": 0.30,
      "Sad": 0.20
    },
    "attention_weights": [0.012, 0.045, 0.180, ...]
  }
  ```

---

## How to Run Inference using `modeling_f1tone.py`

### Directory Setup (`input/` folder)
To run predictions on audio files:
1. Create an `input/` folder in the directory where `modeling_f1tone.py` is located.
2. Place your team radio `.wav` or `.mp3` files inside `input/`.

```text
ToneDetectorF1/
β”œβ”€β”€ best_model.pt
β”œβ”€β”€ modeling_f1tone.py
β”œβ”€β”€ config.json
└── input/
    β”œβ”€β”€ driver_radio_1.wav
    └── pit_communication_2.mp3
```

### Running via Command Line

**Option A: Process all files in `input/` folder**
```bash
python modeling_f1tone.py
```

**Option B: Pass an explicit audio file path**
```bash
python modeling_f1tone.py --audio_path path/to/my_audio.wav
```

### Python Code Integration Example
```python
from modeling_f1tone import FinalPredictor

# Initialize predictor with model checkpoint
predictor = FinalPredictor(checkpoint_path="best_model.pt")

# Perform inference on an audio clip
result = predictor.predict_single("input/driver_radio_1.wav")

print(f"Predicted Emotion : {result['predicted_emotion']}")
print(f"Confidence        : {result['confidence']:.2f}%")
print("Full Probabilities:", result['probabilities'])
```

---

## Verified Model Evaluation Results

Evaluation performed on holdout test set (1,117 audio evaluation samples):

| Metric | Score |
| :--- | :--- |
| **Test Set Accuracy** | **67.86%** |
| **Test Set Macro F1-Score** | **0.6794** |
| **Validation Accuracy (Epoch 8)** | **69.89%** |
| **Validation Macro F1-Score** | **0.7006** |
| **Feature Encoder** | `microsoft/wavlm-base-plus` (768-dim, Frozen) |
| **Classifier Head** | BiLSTM (128) + Temporal Attention + Linear (6) |

### Per-Class Performance Breakdown (Test Set)

| Emotion Class | Precision | Recall | F1-Score | Evaluation Support |
| :--- | :--- | :--- | :--- | :--- |
| **Anger**  | **76.71%** | **87.96%** | **0.8195** | 191 samples |
| **Neutral**  | **77.85%** | **75.46%** | **0.7664** | 163 samples |
| **Happy** | **79.05%** | **61.26%** | **0.6903** | 191 samples |
| **Disgust** | **64.65%** | **67.02%** | **0.6581** | 191 samples |
| **Fear** | **52.05%** | **66.84%** | **0.5853** | 190 samples |
| **Sad** | **63.33%** | **49.74%** | **0.5572** | 191 samples |

---

## Citation & License

- **License:** Apache 2.0
- **Base Encoder Paper:** *WavLM: Large-Scale Self-Supervised Pre-Training for Full Stack Speech Processing* (Chen et al., 2022)