Spaces:
Runtime error
Runtime error
fix added
Browse files
app.py
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# =========================
|
| 2 |
# 1. DOWNLOAD + SELECTIVE EXTRACT
|
| 3 |
# =========================
|
|
@@ -35,7 +44,7 @@ ALL_REQUIRED_IDS = set(list(train_labels.keys()) + list(dev_labels.keys()))
|
|
| 35 |
print("Total required participants:", len(ALL_REQUIRED_IDS))
|
| 36 |
|
| 37 |
# =========================
|
| 38 |
-
# 3. SELECTIVE EXTRACTION
|
| 39 |
# =========================
|
| 40 |
def extract_needed(zip_path):
|
| 41 |
with zipfile.ZipFile(zip_path, "r") as zip_ref:
|
|
@@ -84,15 +93,8 @@ print("Extracted participants:", len(ALL_PATHS))
|
|
| 84 |
# =========================
|
| 85 |
# 6. LIBRARIES
|
| 86 |
# =========================
|
| 87 |
-
import numpy as np
|
| 88 |
import librosa
|
| 89 |
-
import torch
|
| 90 |
import torch.nn as nn
|
| 91 |
-
import random
|
| 92 |
-
|
| 93 |
-
torch.manual_seed(42)
|
| 94 |
-
np.random.seed(42)
|
| 95 |
-
random.seed(42)
|
| 96 |
|
| 97 |
from tqdm import tqdm
|
| 98 |
from transformers import AutoTokenizer, AutoModel
|
|
@@ -156,11 +158,7 @@ def get_visual_features(folder):
|
|
| 156 |
feats.append(np.zeros(20))
|
| 157 |
continue
|
| 158 |
|
| 159 |
-
feat = np.concatenate([
|
| 160 |
-
df.mean().values,
|
| 161 |
-
df.std().values
|
| 162 |
-
])
|
| 163 |
-
|
| 164 |
feats.append(feat)
|
| 165 |
|
| 166 |
except:
|
|
@@ -192,7 +190,6 @@ Xt_test, Xa_test, Xv_test, y_test = build(dev_labels)
|
|
| 192 |
|
| 193 |
print("Train size:", len(y_train))
|
| 194 |
print("Test size:", len(y_test))
|
| 195 |
-
print("Visual shape:", Xv.shape)
|
| 196 |
|
| 197 |
# =========================
|
| 198 |
# 10. NORMALIZE
|
|
@@ -208,7 +205,7 @@ Xa_test = sc_a.transform(Xa_test)
|
|
| 208 |
Xv_test = sc_v.transform(Xv_test)
|
| 209 |
|
| 210 |
# =========================
|
| 211 |
-
# 11.
|
| 212 |
# =========================
|
| 213 |
class Model(nn.Module):
|
| 214 |
def __init__(self, vdim):
|
|
@@ -216,59 +213,37 @@ class Model(nn.Module):
|
|
| 216 |
self.t = nn.Sequential(nn.Linear(768,128), nn.ReLU())
|
| 217 |
self.a = nn.Sequential(nn.Linear(40,32), nn.ReLU())
|
| 218 |
self.v = nn.Sequential(nn.Linear(vdim,64), nn.ReLU())
|
| 219 |
-
|
| 220 |
self.f = nn.Sequential(
|
| 221 |
nn.Linear(224,64),
|
| 222 |
nn.ReLU(),
|
| 223 |
nn.Dropout(0.3),
|
| 224 |
nn.Linear(64,1)
|
| 225 |
)
|
| 226 |
-
|
| 227 |
def forward(self, t,a,v):
|
| 228 |
return self.f(torch.cat([self.t(t), self.a(a), self.v(v)],1))
|
| 229 |
|
| 230 |
-
# =========================
|
| 231 |
-
# 12. ATTENTION MODEL (NEW)
|
| 232 |
-
# =========================
|
| 233 |
class AttentionFusionModel(nn.Module):
|
| 234 |
def __init__(self, vdim):
|
| 235 |
super().__init__()
|
| 236 |
-
|
| 237 |
-
self.
|
| 238 |
-
self.
|
| 239 |
-
self.
|
| 240 |
-
|
| 241 |
-
self.attention = nn.Sequential(
|
| 242 |
-
nn.Linear(224,64),
|
| 243 |
-
nn.Tanh(),
|
| 244 |
-
nn.Linear(64,3)
|
| 245 |
-
)
|
| 246 |
-
|
| 247 |
-
self.classifier = nn.Sequential(
|
| 248 |
nn.Linear(224,64),
|
| 249 |
nn.ReLU(),
|
| 250 |
nn.Dropout(0.3),
|
| 251 |
nn.Linear(64,1)
|
| 252 |
)
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
combined = torch.cat([t_feat, a_feat, v_feat], dim=1)
|
| 260 |
-
attn = torch.softmax(self.attention(combined), dim=1)
|
| 261 |
-
|
| 262 |
-
fused = torch.cat([
|
| 263 |
-
attn[:,0:1]*t_feat,
|
| 264 |
-
attn[:,1:2]*a_feat,
|
| 265 |
-
attn[:,2:3]*v_feat
|
| 266 |
-
], dim=1)
|
| 267 |
-
|
| 268 |
-
return self.classifier(fused)
|
| 269 |
|
| 270 |
# =========================
|
| 271 |
-
#
|
| 272 |
# =========================
|
| 273 |
Xt = torch.tensor(Xt, dtype=torch.float32).to(device)
|
| 274 |
Xa = torch.tensor(Xa, dtype=torch.float32).to(device)
|
|
@@ -279,7 +254,7 @@ Xt_test = torch.tensor(Xt_test, dtype=torch.float32).to(device)
|
|
| 279 |
Xa_test = torch.tensor(Xa_test, dtype=torch.float32).to(device)
|
| 280 |
Xv_test = torch.tensor(Xv_test, dtype=torch.float32).to(device)
|
| 281 |
|
| 282 |
-
#
|
| 283 |
baseline_model = Model(Xv.shape[1]).to(device)
|
| 284 |
opt1 = torch.optim.Adam(baseline_model.parameters(), lr=1e-4)
|
| 285 |
loss_fn = nn.BCEWithLogitsLoss()
|
|
@@ -291,14 +266,12 @@ for e in range(10):
|
|
| 291 |
loss.backward()
|
| 292 |
opt1.step()
|
| 293 |
|
| 294 |
-
#
|
| 295 |
attention_model = AttentionFusionModel(Xv.shape[1]).to(device)
|
| 296 |
-
opt2 = torch.optim.AdamW(attention_model.parameters(), lr=
|
| 297 |
-
|
| 298 |
-
pos_weight = torch.tensor([2.0]).to(device)
|
| 299 |
-
loss_fn_attn = nn.BCEWithLogitsLoss(pos_weight=pos_weight)
|
| 300 |
|
| 301 |
-
for e in range(
|
| 302 |
attention_model.train()
|
| 303 |
opt2.zero_grad()
|
| 304 |
loss = loss_fn_attn(attention_model(Xt,Xa,Xv).squeeze(), yt)
|
|
@@ -306,7 +279,7 @@ for e in range(20):
|
|
| 306 |
opt2.step()
|
| 307 |
|
| 308 |
# =========================
|
| 309 |
-
#
|
| 310 |
# =========================
|
| 311 |
baseline_model.eval()
|
| 312 |
attention_model.eval()
|
|
@@ -316,11 +289,10 @@ with torch.no_grad():
|
|
| 316 |
pred2 = (torch.sigmoid(attention_model(Xt_test,Xa_test,Xv_test).squeeze())>0.5).int().cpu().numpy()
|
| 317 |
|
| 318 |
print("\n===== MODEL COMPARISON =====")
|
| 319 |
-
|
| 320 |
print("\nBaseline Model:")
|
| 321 |
print("Accuracy:", accuracy_score(y_test, pred1))
|
| 322 |
print("F1 Score:", f1_score(y_test, pred1))
|
| 323 |
|
| 324 |
-
print("\nAttention
|
| 325 |
print("Accuracy:", accuracy_score(y_test, pred2))
|
| 326 |
print("F1 Score:", f1_score(y_test, pred2))
|
|
|
|
| 1 |
+
# =========================
|
| 2 |
+
# 0. REPRODUCIBILITY (NEW FIX)
|
| 3 |
+
# =========================
|
| 4 |
+
import torch, numpy as np, random
|
| 5 |
+
|
| 6 |
+
torch.manual_seed(42)
|
| 7 |
+
np.random.seed(42)
|
| 8 |
+
random.seed(42)
|
| 9 |
+
|
| 10 |
# =========================
|
| 11 |
# 1. DOWNLOAD + SELECTIVE EXTRACT
|
| 12 |
# =========================
|
|
|
|
| 44 |
print("Total required participants:", len(ALL_REQUIRED_IDS))
|
| 45 |
|
| 46 |
# =========================
|
| 47 |
+
# 3. SELECTIVE EXTRACTION
|
| 48 |
# =========================
|
| 49 |
def extract_needed(zip_path):
|
| 50 |
with zipfile.ZipFile(zip_path, "r") as zip_ref:
|
|
|
|
| 93 |
# =========================
|
| 94 |
# 6. LIBRARIES
|
| 95 |
# =========================
|
|
|
|
| 96 |
import librosa
|
|
|
|
| 97 |
import torch.nn as nn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
from tqdm import tqdm
|
| 100 |
from transformers import AutoTokenizer, AutoModel
|
|
|
|
| 158 |
feats.append(np.zeros(20))
|
| 159 |
continue
|
| 160 |
|
| 161 |
+
feat = np.concatenate([df.mean().values, df.std().values])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
feats.append(feat)
|
| 163 |
|
| 164 |
except:
|
|
|
|
| 190 |
|
| 191 |
print("Train size:", len(y_train))
|
| 192 |
print("Test size:", len(y_test))
|
|
|
|
| 193 |
|
| 194 |
# =========================
|
| 195 |
# 10. NORMALIZE
|
|
|
|
| 205 |
Xv_test = sc_v.transform(Xv_test)
|
| 206 |
|
| 207 |
# =========================
|
| 208 |
+
# 11. MODELS
|
| 209 |
# =========================
|
| 210 |
class Model(nn.Module):
|
| 211 |
def __init__(self, vdim):
|
|
|
|
| 213 |
self.t = nn.Sequential(nn.Linear(768,128), nn.ReLU())
|
| 214 |
self.a = nn.Sequential(nn.Linear(40,32), nn.ReLU())
|
| 215 |
self.v = nn.Sequential(nn.Linear(vdim,64), nn.ReLU())
|
|
|
|
| 216 |
self.f = nn.Sequential(
|
| 217 |
nn.Linear(224,64),
|
| 218 |
nn.ReLU(),
|
| 219 |
nn.Dropout(0.3),
|
| 220 |
nn.Linear(64,1)
|
| 221 |
)
|
|
|
|
| 222 |
def forward(self, t,a,v):
|
| 223 |
return self.f(torch.cat([self.t(t), self.a(a), self.v(v)],1))
|
| 224 |
|
|
|
|
|
|
|
|
|
|
| 225 |
class AttentionFusionModel(nn.Module):
|
| 226 |
def __init__(self, vdim):
|
| 227 |
super().__init__()
|
| 228 |
+
self.t = nn.Sequential(nn.Linear(768,128), nn.ReLU())
|
| 229 |
+
self.a = nn.Sequential(nn.Linear(40,32), nn.ReLU())
|
| 230 |
+
self.v = nn.Sequential(nn.Linear(vdim,64), nn.ReLU())
|
| 231 |
+
self.attn = nn.Sequential(nn.Linear(224,64), nn.Tanh(), nn.Linear(64,3))
|
| 232 |
+
self.f = nn.Sequential(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
nn.Linear(224,64),
|
| 234 |
nn.ReLU(),
|
| 235 |
nn.Dropout(0.3),
|
| 236 |
nn.Linear(64,1)
|
| 237 |
)
|
| 238 |
+
def forward(self,t,a,v):
|
| 239 |
+
t,a,v = self.t(t), self.a(a), self.v(v)
|
| 240 |
+
comb = torch.cat([t,a,v],1)
|
| 241 |
+
w = torch.softmax(self.attn(comb),1)
|
| 242 |
+
fused = torch.cat([w[:,0:1]*t, w[:,1:2]*a, w[:,2:3]*v],1)
|
| 243 |
+
return self.f(fused)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
|
| 245 |
# =========================
|
| 246 |
+
# 12. TRAIN
|
| 247 |
# =========================
|
| 248 |
Xt = torch.tensor(Xt, dtype=torch.float32).to(device)
|
| 249 |
Xa = torch.tensor(Xa, dtype=torch.float32).to(device)
|
|
|
|
| 254 |
Xa_test = torch.tensor(Xa_test, dtype=torch.float32).to(device)
|
| 255 |
Xv_test = torch.tensor(Xv_test, dtype=torch.float32).to(device)
|
| 256 |
|
| 257 |
+
# Baseline
|
| 258 |
baseline_model = Model(Xv.shape[1]).to(device)
|
| 259 |
opt1 = torch.optim.Adam(baseline_model.parameters(), lr=1e-4)
|
| 260 |
loss_fn = nn.BCEWithLogitsLoss()
|
|
|
|
| 266 |
loss.backward()
|
| 267 |
opt1.step()
|
| 268 |
|
| 269 |
+
# Attention (FIXED)
|
| 270 |
attention_model = AttentionFusionModel(Xv.shape[1]).to(device)
|
| 271 |
+
opt2 = torch.optim.AdamW(attention_model.parameters(), lr=1e-4)
|
| 272 |
+
loss_fn_attn = nn.BCEWithLogitsLoss() # ✅ FIXED
|
|
|
|
|
|
|
| 273 |
|
| 274 |
+
for e in range(10):
|
| 275 |
attention_model.train()
|
| 276 |
opt2.zero_grad()
|
| 277 |
loss = loss_fn_attn(attention_model(Xt,Xa,Xv).squeeze(), yt)
|
|
|
|
| 279 |
opt2.step()
|
| 280 |
|
| 281 |
# =========================
|
| 282 |
+
# 13. EVALUATION
|
| 283 |
# =========================
|
| 284 |
baseline_model.eval()
|
| 285 |
attention_model.eval()
|
|
|
|
| 289 |
pred2 = (torch.sigmoid(attention_model(Xt_test,Xa_test,Xv_test).squeeze())>0.5).int().cpu().numpy()
|
| 290 |
|
| 291 |
print("\n===== MODEL COMPARISON =====")
|
|
|
|
| 292 |
print("\nBaseline Model:")
|
| 293 |
print("Accuracy:", accuracy_score(y_test, pred1))
|
| 294 |
print("F1 Score:", f1_score(y_test, pred1))
|
| 295 |
|
| 296 |
+
print("\nAttention Model:")
|
| 297 |
print("Accuracy:", accuracy_score(y_test, pred2))
|
| 298 |
print("F1 Score:", f1_score(y_test, pred2))
|