Spaces:
Runtime error
Runtime error
visual features handling
Browse files
app.py
CHANGED
|
@@ -11,7 +11,7 @@ urls = [
|
|
| 11 |
os.makedirs("data", exist_ok=True)
|
| 12 |
|
| 13 |
# =========================
|
| 14 |
-
# 2. LOAD LABELS
|
| 15 |
# =========================
|
| 16 |
import pandas as pd
|
| 17 |
|
|
@@ -32,21 +32,23 @@ train_labels = load_split("train_split_Depression_AVEC2017.csv")
|
|
| 32 |
dev_labels = load_split("dev_split_Depression_AVEC2017.csv")
|
| 33 |
|
| 34 |
ALL_REQUIRED_IDS = set(list(train_labels.keys()) + list(dev_labels.keys()))
|
| 35 |
-
|
| 36 |
print("Total required participants:", len(ALL_REQUIRED_IDS))
|
| 37 |
|
| 38 |
# =========================
|
| 39 |
-
# 3. SELECTIVE EXTRACTION
|
| 40 |
# =========================
|
| 41 |
def extract_needed(zip_path):
|
| 42 |
with zipfile.ZipFile(zip_path, "r") as zip_ref:
|
| 43 |
for file in zip_ref.namelist():
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
# =========================
|
| 52 |
# 4. DOWNLOAD + EXTRACT
|
|
@@ -60,10 +62,10 @@ for i, url in enumerate(urls):
|
|
| 60 |
for chunk in r.iter_content(8192):
|
| 61 |
f.write(chunk)
|
| 62 |
|
| 63 |
-
print(f"Extracting
|
| 64 |
extract_needed(zip_path)
|
| 65 |
|
| 66 |
-
os.remove(zip_path)
|
| 67 |
|
| 68 |
# =========================
|
| 69 |
# 5. GET PARTICIPANTS
|
|
@@ -77,10 +79,10 @@ def get_all_paths():
|
|
| 77 |
return paths
|
| 78 |
|
| 79 |
ALL_PATHS = get_all_paths()
|
| 80 |
-
print("Extracted
|
| 81 |
|
| 82 |
# =========================
|
| 83 |
-
# 6.
|
| 84 |
# =========================
|
| 85 |
import numpy as np
|
| 86 |
import librosa
|
|
@@ -92,11 +94,11 @@ from transformers import AutoTokenizer, AutoModel
|
|
| 92 |
from sklearn.metrics import accuracy_score, f1_score
|
| 93 |
from sklearn.preprocessing import StandardScaler
|
| 94 |
|
|
|
|
|
|
|
| 95 |
# =========================
|
| 96 |
# 7. TEXT MODEL
|
| 97 |
# =========================
|
| 98 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 99 |
-
|
| 100 |
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
|
| 101 |
bert = AutoModel.from_pretrained("distilbert-base-uncased").to(device)
|
| 102 |
bert.eval()
|
|
@@ -131,19 +133,35 @@ def get_audio_features(folder):
|
|
| 131 |
except:
|
| 132 |
return np.zeros(40)
|
| 133 |
|
|
|
|
| 134 |
def get_visual_features(folder):
|
| 135 |
feats = []
|
| 136 |
|
| 137 |
for key in ["AUs", "pose", "gaze"]:
|
| 138 |
try:
|
| 139 |
file = [f for f in os.listdir(folder) if key in f][0]
|
| 140 |
-
|
| 141 |
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
-
feat = np.concatenate([df.mean().values, df.std().values])
|
| 146 |
feats.append(feat)
|
|
|
|
| 147 |
except:
|
| 148 |
feats.append(np.zeros(20))
|
| 149 |
|
|
@@ -173,6 +191,7 @@ Xt_test, Xa_test, Xv_test, y_test = build(dev_labels)
|
|
| 173 |
|
| 174 |
print("Train size:", len(y_train))
|
| 175 |
print("Test size:", len(y_test))
|
|
|
|
| 176 |
|
| 177 |
# =========================
|
| 178 |
# 10. NORMALIZE
|
|
@@ -196,6 +215,7 @@ class Model(nn.Module):
|
|
| 196 |
self.t = nn.Sequential(nn.Linear(768,128), nn.ReLU())
|
| 197 |
self.a = nn.Sequential(nn.Linear(40,32), nn.ReLU())
|
| 198 |
self.v = nn.Sequential(nn.Linear(vdim,64), nn.ReLU())
|
|
|
|
| 199 |
self.f = nn.Sequential(
|
| 200 |
nn.Linear(224,64),
|
| 201 |
nn.ReLU(),
|
|
|
|
| 11 |
os.makedirs("data", exist_ok=True)
|
| 12 |
|
| 13 |
# =========================
|
| 14 |
+
# 2. LOAD LABELS
|
| 15 |
# =========================
|
| 16 |
import pandas as pd
|
| 17 |
|
|
|
|
| 32 |
dev_labels = load_split("dev_split_Depression_AVEC2017.csv")
|
| 33 |
|
| 34 |
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 (FIXED)
|
| 39 |
# =========================
|
| 40 |
def extract_needed(zip_path):
|
| 41 |
with zipfile.ZipFile(zip_path, "r") as zip_ref:
|
| 42 |
for file in zip_ref.namelist():
|
| 43 |
|
| 44 |
+
parts = file.split("/")
|
| 45 |
+
if len(parts) < 2:
|
| 46 |
+
continue
|
| 47 |
+
|
| 48 |
+
folder = parts[1] if parts[0].startswith("DAIC") else parts[0]
|
| 49 |
+
|
| 50 |
+
if any(folder.startswith(pid + "_") for pid in ALL_REQUIRED_IDS):
|
| 51 |
+
zip_ref.extract(file, "data")
|
| 52 |
|
| 53 |
# =========================
|
| 54 |
# 4. DOWNLOAD + EXTRACT
|
|
|
|
| 62 |
for chunk in r.iter_content(8192):
|
| 63 |
f.write(chunk)
|
| 64 |
|
| 65 |
+
print(f"Extracting dataset {i+1}...")
|
| 66 |
extract_needed(zip_path)
|
| 67 |
|
| 68 |
+
os.remove(zip_path)
|
| 69 |
|
| 70 |
# =========================
|
| 71 |
# 5. GET PARTICIPANTS
|
|
|
|
| 79 |
return paths
|
| 80 |
|
| 81 |
ALL_PATHS = get_all_paths()
|
| 82 |
+
print("Extracted participants:", len(ALL_PATHS))
|
| 83 |
|
| 84 |
# =========================
|
| 85 |
+
# 6. LIBRARIES
|
| 86 |
# =========================
|
| 87 |
import numpy as np
|
| 88 |
import librosa
|
|
|
|
| 94 |
from sklearn.metrics import accuracy_score, f1_score
|
| 95 |
from sklearn.preprocessing import StandardScaler
|
| 96 |
|
| 97 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 98 |
+
|
| 99 |
# =========================
|
| 100 |
# 7. TEXT MODEL
|
| 101 |
# =========================
|
|
|
|
|
|
|
| 102 |
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
|
| 103 |
bert = AutoModel.from_pretrained("distilbert-base-uncased").to(device)
|
| 104 |
bert.eval()
|
|
|
|
| 133 |
except:
|
| 134 |
return np.zeros(40)
|
| 135 |
|
| 136 |
+
# 🔥 FIXED VISUAL FUNCTION
|
| 137 |
def get_visual_features(folder):
|
| 138 |
feats = []
|
| 139 |
|
| 140 |
for key in ["AUs", "pose", "gaze"]:
|
| 141 |
try:
|
| 142 |
file = [f for f in os.listdir(folder) if key in f][0]
|
| 143 |
+
path = os.path.join(folder, file)
|
| 144 |
|
| 145 |
+
df = pd.read_csv(path)
|
| 146 |
+
|
| 147 |
+
# keep only numeric columns
|
| 148 |
+
df = df.select_dtypes(include=[np.number])
|
| 149 |
+
|
| 150 |
+
# replace invalid values
|
| 151 |
+
df.replace(-100, np.nan, inplace=True)
|
| 152 |
+
df.fillna(0, inplace=True)
|
| 153 |
+
|
| 154 |
+
if df.shape[1] == 0:
|
| 155 |
+
feats.append(np.zeros(20))
|
| 156 |
+
continue
|
| 157 |
+
|
| 158 |
+
feat = np.concatenate([
|
| 159 |
+
df.mean().values,
|
| 160 |
+
df.std().values
|
| 161 |
+
])
|
| 162 |
|
|
|
|
| 163 |
feats.append(feat)
|
| 164 |
+
|
| 165 |
except:
|
| 166 |
feats.append(np.zeros(20))
|
| 167 |
|
|
|
|
| 191 |
|
| 192 |
print("Train size:", len(y_train))
|
| 193 |
print("Test size:", len(y_test))
|
| 194 |
+
print("Visual shape:", Xv.shape)
|
| 195 |
|
| 196 |
# =========================
|
| 197 |
# 10. NORMALIZE
|
|
|
|
| 215 |
self.t = nn.Sequential(nn.Linear(768,128), nn.ReLU())
|
| 216 |
self.a = nn.Sequential(nn.Linear(40,32), nn.ReLU())
|
| 217 |
self.v = nn.Sequential(nn.Linear(vdim,64), nn.ReLU())
|
| 218 |
+
|
| 219 |
self.f = nn.Sequential(
|
| 220 |
nn.Linear(224,64),
|
| 221 |
nn.ReLU(),
|