Spaces:
Running on Zero
Running on Zero
File size: 13,565 Bytes
90fa9aa | 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 | import os
import sys
import pickle
import numpy as np
from pathlib import Path
sys.path.append(str(Path(__file__).resolve().parent.parent.parent))
from src.config import AUDIO_FEATURES_PATH, MODELS_DIR, MENTAL_HEALTH_CATEGORIES, TOTAL_AUDIO_FEATURES
try:
import pandas as pd
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score, classification_report
HAS_TORCH = True
except ImportError:
HAS_TORCH = False
pd = None
AUDIO_MODEL_PATH = os.path.join(MODELS_DIR, "audio_dnn_transformer.pkl")
class AudioFeatureTransformer(nn.Module):
"""
Optimized Deep Neural Network for Acoustic Features.
Uses deep dense layers with BatchNorm.
"""
def __init__(self, input_dim=195, num_classes=8):
super(AudioFeatureTransformer, self).__init__()
self.net = nn.Sequential(
nn.Linear(input_dim, 512),
nn.BatchNorm1d(512),
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(512, 256),
nn.BatchNorm1d(256),
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(256, 128),
nn.BatchNorm1d(128),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(128, num_classes)
)
def forward(self, x):
return self.net(x)
class AudioEnsemblePipeline:
"""
Deep Learning Audio Pipeline.
Replaces the traditional sklearn ensemble with a PyTorch Attention DNN.
"""
def __init__(self):
self.classes_ = MENTAL_HEALTH_CATEGORIES
self.num_classes = len(self.classes_)
self.is_fitted = False
if HAS_TORCH:
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.model = AudioFeatureTransformer(input_dim=TOTAL_AUDIO_FEATURES, num_classes=self.num_classes).to(self.device)
self.scaler = StandardScaler()
else:
self.device = "cpu"
self.model = None
self.scaler = None
self.feature_names_in = [f"feature_{i+1}" for i in range(TOTAL_AUDIO_FEATURES)]
def train_and_evaluate(self, data_path=AUDIO_FEATURES_PATH):
if not HAS_TORCH:
print("[Audio Pipeline] PyTorch not available. Skipping Deep Learning training.")
return 0.0
if not os.path.exists(data_path):
raise FileNotFoundError(f"Audio features dataset not found at {data_path}.")
print(f"[Audio Pipeline] Loading dataset from {data_path}...")
df = pd.read_csv(data_path)
# Drop duplicates to prevent data leakage and memorization
initial_len = len(df)
df = df.drop_duplicates(subset=self.feature_names_in)
print(f"[Audio Pipeline] Dropped {initial_len - len(df)} duplicate rows to prevent data leakage.")
X = df[self.feature_names_in].values
raw_y = df["emotion"].values
# Map raw emotion labels to Unified Mental Health Categories
def map_audio_label(label):
label = str(label).strip()
if label in self.classes_: return label
if label in ["Angry"]: return "Stress"
if label in ["Sad"]: return "Depression"
if label in ["Fearful"]: return "Anxiety"
if label in ["Disgust", "Surprised"]: return "Emotional Distress"
return "Normal" # Calm, Happy, Neutral
y = np.array([map_audio_label(l) for l in raw_y])
label_map = {cat: i for i, cat in enumerate(self.classes_)}
y_encoded = np.array([label_map[label] for label in y])
X_train, X_test, y_train, y_test = train_test_split(
X, y_encoded, test_size=0.2, random_state=42, stratify=y_encoded
)
print("[Audio Pipeline] Scaling features...")
X_train_scaled = self.scaler.fit_transform(X_train)
X_test_scaled = self.scaler.transform(X_test)
train_dataset = TensorDataset(torch.tensor(X_train_scaled, dtype=torch.float32), torch.tensor(y_train, dtype=torch.long))
test_dataset = TensorDataset(torch.tensor(X_test_scaled, dtype=torch.float32), torch.tensor(y_test, dtype=torch.long))
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
criterion = nn.CrossEntropyLoss()
optimizer = optim.AdamW(self.model.parameters(), lr=0.001, weight_decay=0.01)
print(f"[Audio Pipeline] Training Attention DNN model on {self.device.type.upper()}...")
checkpoint_path = os.path.join(MODELS_DIR, "audio_checkpoint.pt")
start_epoch = 0
epochs = 60 # Increased for higher accuracy
if os.path.exists(checkpoint_path):
print(f"[Audio Pipeline] Resuming from checkpoint: {checkpoint_path}")
checkpoint = torch.load(checkpoint_path, map_location=self.device)
self.model.load_state_dict(checkpoint['model_state'])
optimizer.load_state_dict(checkpoint['optimizer_state'])
start_epoch = checkpoint['epoch'] + 1
print(f"[Audio Pipeline] Resumed at epoch {start_epoch}")
self.model.train()
for epoch in range(start_epoch, epochs):
total_loss = 0
for batch_x, batch_y in train_loader:
batch_x, batch_y = batch_x.to(self.device), batch_y.to(self.device)
optimizer.zero_grad()
outputs = self.model(batch_x)
loss = criterion(outputs, batch_y)
loss.backward()
optimizer.step()
total_loss += loss.item()
# Save checkpoint after each epoch
torch.save({
'epoch': epoch,
'model_state': self.model.state_dict(),
'optimizer_state': optimizer.state_dict(),
}, checkpoint_path)
print(f"[Audio Pipeline] Epoch {epoch+1}/{epochs}, Loss: {total_loss:.4f} (Saved checkpoint)")
self.is_fitted = True
print("[Audio Pipeline] Evaluating model...")
self.model.eval()
with torch.no_grad():
x_test_tensor = torch.tensor(X_test_scaled, dtype=torch.float32).to(self.device)
outputs = self.model(x_test_tensor)
_, y_pred = torch.max(outputs, 1)
y_pred = y_pred.cpu().numpy()
acc = accuracy_score(y_test, y_pred)
print(f"\n[Audio Pipeline] Test Accuracy: {acc*100:.2f}%")
inv_map = {i: cat for cat, i in label_map.items()}
y_test_names = [inv_map[i] for i in y_test]
y_pred_names = [inv_map[i] for i in y_pred]
print(classification_report(y_test_names, y_pred_names))
self.save_model()
return acc
def predict(self, feature_vector_195):
if not self.is_fitted:
try:
self.load_model()
except Exception:
pass
if not self.is_fitted or self.model is None:
return self._heuristic_predict(feature_vector_195)
x = np.array(feature_vector_195, dtype=np.float32).reshape(1, -1)
if x.shape[1] != TOTAL_AUDIO_FEATURES:
raise ValueError(f"Expected {TOTAL_AUDIO_FEATURES} features, got {x.shape[1]}")
x_scaled = self.scaler.transform(x)
# Check if model is PyTorch or Scikit-Learn
if hasattr(self.model, "predict_proba"):
# Scikit-Learn Random Forest
probs = self.model.predict_proba(x_scaled)[0]
else:
# PyTorch Model
self.model.eval()
with torch.no_grad():
x_tensor = torch.tensor(x_scaled, dtype=torch.float32).to(self.device)
logits = self.model(x_tensor)
probs = torch.nn.functional.softmax(logits, dim=1).cpu().numpy()[0]
pred_idx = np.argmax(probs)
pred_emotion = str(self.classes_[pred_idx])
prob_dict = {str(self.classes_[i]): round(float(probs[i]), 4) for i in range(len(self.classes_))}
high_stress_emotions = ["Stress", "Anxiety", "Depression", "Emotional Distress"]
# Safe prob sum calculation checking if classes exist
stress_prob_sum = 0.0
for e in high_stress_emotions:
if e in self.classes_:
stress_prob_sum += probs[self.classes_.index(e)]
rms_val = float(feature_vector_195[-1])
# Reduce the impact of RMS volume so normal speech doesn't get flagged as Stress
# Default stress relies much more on the predicted probabilities
base_intensity = stress_prob_sum * 100.0
volume_penalty = min(20.0, rms_val * 20.0) # Cap volume contribution
stress_intensity = base_intensity + volume_penalty
# Boost if the primary predicted emotion is actually a stress state
# This prevents the issue where poorly trained models with spread probabilities
# fail to reach the threshold for Severe Stress
if pred_emotion in high_stress_emotions:
stress_intensity = max(stress_intensity, 75.0 + (probs[pred_idx] * 20.0))
stress_intensity = round(float(min(100.0, max(5.0, stress_intensity))), 2)
return {
"predicted_emotion": pred_emotion,
"probabilities": prob_dict,
"acoustic_stress_score": stress_intensity,
"confidence": round(float(np.max(probs)), 4)
}
def save_model(self, path=AUDIO_MODEL_PATH):
os.makedirs(os.path.dirname(path), exist_ok=True)
checkpoint = {
"scaler": self.scaler,
"classes_": self.classes_
}
if hasattr(self.model, "predict_proba"):
# Sklearn Model
checkpoint["sklearn_model"] = self.model
else:
# PyTorch Model
self.model.cpu()
checkpoint["model_state"] = self.model.state_dict()
self.model.to(self.device)
with open(path, "wb") as f:
pickle.dump(checkpoint, f)
print(f"[Audio Pipeline] Model saved successfully to {path}")
def load_model(self, path=AUDIO_MODEL_PATH):
if not os.path.exists(path):
for alt in [Path("/var/task/models_bin/audio_dnn_transformer.pkl"), Path("models_bin/audio_dnn_transformer.pkl"), Path(__file__).resolve().parent.parent.parent / "models_bin" / "audio_dnn_transformer.pkl"]:
if alt.exists():
path = str(alt)
break
if not os.path.exists(path):
raise FileNotFoundError(f"Trained audio model not found at {path}")
with open(path, "rb") as f:
checkpoint = pickle.load(f)
self.scaler = checkpoint["scaler"]
self.classes_ = checkpoint["classes_"]
if "sklearn_model" in checkpoint:
self.model = checkpoint["sklearn_model"]
else:
if not HAS_TORCH:
raise ImportError("PyTorch not available to load this model.")
self.model.load_state_dict(checkpoint["model_state"])
self.model.to(self.device)
self.is_fitted = True
print(f"[Audio Pipeline] Model loaded successfully from {path}")
def _heuristic_predict(self, feature_vector_195):
vec = np.array(feature_vector_195, dtype=np.float32)
mean_val = float(np.mean(np.abs(vec)))
std_val = float(np.std(vec))
rms_val = float(vec[-1]) if len(vec) > 0 else 0.5
stress_intensity = round(min(95.0, max(8.0, (mean_val * 45.0) + (std_val * 60.0) + (rms_val * 50.0))), 2)
if stress_intensity > 60.0:
pred_emotion = "Angry" if std_val > 0.4 else "Fearful"
prob_dict = {"Angry": 0.42, "Fearful": 0.38, "Sad": 0.12, "Neutral": 0.04, "Happy": 0.02, "Disgust": 0.01, "Surprise": 0.01}
elif stress_intensity > 40.0:
pred_emotion = "Sad"
prob_dict = {"Sad": 0.52, "Fearful": 0.22, "Neutral": 0.16, "Angry": 0.06, "Happy": 0.02, "Disgust": 0.01, "Surprise": 0.01}
else:
pred_emotion = "Neutral"
prob_dict = {"Neutral": 0.74, "Happy": 0.14, "Sad": 0.06, "Surprise": 0.04, "Fearful": 0.01, "Angry": 0.01, "Disgust": 0.00}
return {
"predicted_emotion": pred_emotion,
"probabilities": prob_dict,
"acoustic_stress_score": stress_intensity,
"confidence": round(float(prob_dict[pred_emotion]), 4)
}
if __name__ == "__main__":
pipeline = AudioEnsemblePipeline()
pipeline.train_and_evaluate()
|