File size: 9,824 Bytes
f1ef7e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Baseline speech emotion classifier for the capstone sentiment module.

This script trains a classical ML baseline using acoustic features extracted
from CREMA-D audio. The baseline is useful because it gives measurable results
before fine-tuning Wav2Vec2.

Run from ml-services:

    python -m src.models.baseline_emotion_model

Optional quick test:

    python -m src.models.baseline_emotion_model --limit-per-split 200
"""

import argparse
import json
from pathlib import Path
from typing import Dict, Optional, Tuple

import joblib
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import (
    accuracy_score,
    classification_report,
    confusion_matrix,
    f1_score,
)
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

from src.data.audio_dataset import DEFAULT_METADATA_PATH, load_metadata
from src.features.audio_feature_extractor import (
    AudioFeatureConfig,
    extract_feature_dataframe,
)


PROJECT_ROOT = Path(__file__).resolve().parents[3]
ML_SERVICES_ROOT = PROJECT_ROOT / "ml-services"

DEFAULT_FEATURES_CSV = ML_SERVICES_ROOT / "data" / "processed" / "cremad_baseline_features.csv"
DEFAULT_MODEL_PATH = ML_SERVICES_ROOT / "outputs" / "models" / "baseline_emotion_model.joblib"
DEFAULT_REPORT_PATH = ML_SERVICES_ROOT / "outputs" / "reports" / "baseline_emotion_report.json"
DEFAULT_CONFUSION_MATRIX_PATH = (
    ML_SERVICES_ROOT / "outputs" / "reports" / "baseline_emotion_confusion_matrix.csv"
)


LABEL_COLUMN = "emotion_label"
NON_FEATURE_COLUMNS = {
    "filename",
    "actor_id",
    "emotion_label",
    "sentiment_label",
    "split",
}


def prepare_or_load_features(
    metadata_path: Path = DEFAULT_METADATA_PATH,
    features_csv: Path = DEFAULT_FEATURES_CSV,
    force_rebuild: bool = False,
    limit_per_split: Optional[int] = None,
) -> pd.DataFrame:
    """
    Load saved baseline features or build them from audio files.

    Feature extraction can take time, so we cache the result as CSV.
    """
    if features_csv.exists() and not force_rebuild and limit_per_split is None:
        print(f"Loading existing feature CSV: {features_csv}")
        return pd.read_csv(features_csv)

    print("Building baseline acoustic features from audio files...")
    metadata = load_metadata(metadata_path)

    if limit_per_split is not None:
        limited_parts = []

        for split_name in ["train", "validation", "test"]:
            split_df = metadata[metadata["split"] == split_name].head(limit_per_split)
            limited_parts.append(split_df)

        metadata = pd.concat(limited_parts, ignore_index=True)

        print(f"Using quick-test limit: {limit_per_split} samples per split")
        print(f"Quick-test metadata columns: {list(metadata.columns)}")
        print(f"Quick-test split distribution: {metadata['split'].value_counts().to_dict()}")

    feature_config = AudioFeatureConfig(
        sample_rate=16_000,
        n_mfcc=20,
        max_duration_seconds=6.0,
    )

    features_df = extract_feature_dataframe(
        metadata=metadata,
        ml_services_root=ML_SERVICES_ROOT,
        config=feature_config,
    )

    if limit_per_split is None:
        features_csv.parent.mkdir(parents=True, exist_ok=True)
        features_df.to_csv(features_csv, index=False)
        print(f"Saved feature CSV to: {features_csv}")
    else:
        print("Quick-test mode: feature CSV was not saved.")

    return features_df


def split_features_and_labels(
    features_df: pd.DataFrame,
) -> Tuple[pd.DataFrame, pd.Series, pd.DataFrame, pd.Series, pd.DataFrame, pd.Series]:
    """
    Split features into train, validation, and test sets.
    """
    train_df = features_df[features_df["split"] == "train"].copy()
    validation_df = features_df[features_df["split"] == "validation"].copy()
    test_df = features_df[features_df["split"] == "test"].copy()

    if train_df.empty or validation_df.empty or test_df.empty:
        raise ValueError("Train, validation, and test splits must all contain samples.")

    feature_columns = [
        column for column in features_df.columns if column not in NON_FEATURE_COLUMNS
    ]

    x_train = train_df[feature_columns]
    y_train = train_df[LABEL_COLUMN]

    x_validation = validation_df[feature_columns]
    y_validation = validation_df[LABEL_COLUMN]

    x_test = test_df[feature_columns]
    y_test = test_df[LABEL_COLUMN]

    return x_train, y_train, x_validation, y_validation, x_test, y_test


def build_baseline_pipeline() -> Pipeline:
    """
    Build the baseline model pipeline.

    Random Forest is used because it handles nonlinear relationships and works
    well as a strong classical baseline for tabular acoustic features.
    """
    return Pipeline(
        steps=[
            ("scaler", StandardScaler()),
            (
                "classifier",
                RandomForestClassifier(
                    n_estimators=400,
                    max_depth=None,
                    min_samples_split=4,
                    min_samples_leaf=2,
                    class_weight="balanced",
                    random_state=42,
                    n_jobs=-1,
                ),
            ),
        ]
    )


def evaluate_model(
    model: Pipeline,
    x: pd.DataFrame,
    y_true: pd.Series,
    split_name: str,
) -> Dict:
    """
    Evaluate a trained model on one split.
    """
    y_pred = model.predict(x)

    return {
        "split": split_name,
        "accuracy": float(accuracy_score(y_true, y_pred)),
        "macro_f1": float(f1_score(y_true, y_pred, average="macro")),
        "weighted_f1": float(f1_score(y_true, y_pred, average="weighted")),
        "classification_report": classification_report(
            y_true,
            y_pred,
            output_dict=True,
            zero_division=0,
        ),
    }


def save_confusion_matrix(
    model: Pipeline,
    x_test: pd.DataFrame,
    y_test: pd.Series,
    output_path: Path,
) -> None:
    """
    Save test confusion matrix as CSV.
    """
    labels = sorted(y_test.unique())
    y_pred = model.predict(x_test)

    matrix = confusion_matrix(y_test, y_pred, labels=labels)

    matrix_df = pd.DataFrame(
        matrix,
        index=[f"actual_{label}" for label in labels],
        columns=[f"predicted_{label}" for label in labels],
    )

    output_path.parent.mkdir(parents=True, exist_ok=True)
    matrix_df.to_csv(output_path)


def train_baseline_model(
    metadata_path: Path = DEFAULT_METADATA_PATH,
    features_csv: Path = DEFAULT_FEATURES_CSV,
    model_path: Path = DEFAULT_MODEL_PATH,
    report_path: Path = DEFAULT_REPORT_PATH,
    confusion_matrix_path: Path = DEFAULT_CONFUSION_MATRIX_PATH,
    force_rebuild_features: bool = False,
    limit_per_split: Optional[int] = None,
) -> Dict:
    """
    Train and evaluate the baseline emotion classifier.

    Returns:
        Evaluation report dictionary.
    """
    features_df = prepare_or_load_features(
        metadata_path=metadata_path,
        features_csv=features_csv,
        force_rebuild=force_rebuild_features,
        limit_per_split=limit_per_split,
    )

    (
        x_train,
        y_train,
        x_validation,
        y_validation,
        x_test,
        y_test,
    ) = split_features_and_labels(features_df)

    print("\nTraining baseline emotion model...")
    model = build_baseline_pipeline()
    model.fit(x_train, y_train)

    print("Evaluating baseline model...")
    validation_report = evaluate_model(model, x_validation, y_validation, "validation")
    test_report = evaluate_model(model, x_test, y_test, "test")

    full_report = {
        "model_name": "RandomForest acoustic baseline",
        "task": "6-class speech emotion classification",
        "label_column": LABEL_COLUMN,
        "feature_count": int(x_train.shape[1]),
        "train_samples": int(len(x_train)),
        "validation_samples": int(len(x_validation)),
        "test_samples": int(len(x_test)),
        "validation": validation_report,
        "test": test_report,
    }

    if limit_per_split is None:
        model_path.parent.mkdir(parents=True, exist_ok=True)
        report_path.parent.mkdir(parents=True, exist_ok=True)

        joblib.dump(model, model_path)

        with report_path.open("w", encoding="utf-8") as file:
            json.dump(full_report, file, indent=2)

        save_confusion_matrix(model, x_test, y_test, confusion_matrix_path)

        print(f"\nSaved model to: {model_path}")
        print(f"Saved report to: {report_path}")
        print(f"Saved confusion matrix to: {confusion_matrix_path}")
    else:
        print("\nQuick-test mode: model and reports were not saved.")

    print("\nBaseline Results")
    print("-" * 60)
    print(f"Validation accuracy: {validation_report['accuracy']:.4f}")
    print(f"Validation macro F1: {validation_report['macro_f1']:.4f}")
    print(f"Test accuracy: {test_report['accuracy']:.4f}")
    print(f"Test macro F1: {test_report['macro_f1']:.4f}")
    print("-" * 60)

    return full_report


def parse_args() -> argparse.Namespace:
    """
    Parse command-line arguments.
    """
    parser = argparse.ArgumentParser(
        description="Train baseline CREMA-D emotion classifier."
    )

    parser.add_argument(
        "--force-rebuild-features",
        action="store_true",
        help="Re-extract features even if cached CSV already exists.",
    )

    parser.add_argument(
        "--limit-per-split",
        type=int,
        default=None,
        help="Optional quick-test limit per split. Does not save outputs.",
    )

    return parser.parse_args()


if __name__ == "__main__":
    args = parse_args()

    train_baseline_model(
        force_rebuild_features=args.force_rebuild_features,
        limit_per_split=args.limit_per_split,
    )