Spaces:
Sleeping

File size: 2,904 Bytes
7df4497
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-
"""
๊ณตํ†ต ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜ ๋ฐ ํ‰๊ฐ€ ์ง€ํ‘œ ๊ณ„์‚ฐ
"""
import numpy as np 
import random
import torch
from sklearn.metrics import accuracy_score, f1_score, mean_squared_error, mean_absolute_error
# ํ”ผ์–ด์Šจ ์ƒ๊ด€๊ณ„์ˆ˜๋ฅผ ์œ„ํ•ด scipy ์ž„ํฌํŠธ
try:
    from scipy.stats import pearsonr 
except ImportError:
    print("Warning: scipy not installed. Pearson correlation metrics will be skipped.")
    pearsonr = None

# --- ์‹œ๋“œ ๊ณ ์ • ---
def seed_everything(seed: int):
    """๋ชจ๋“  ๋‚œ์ˆ˜ ์ƒ์„ฑ์„ ๊ณ ์ •ํ•˜์—ฌ ์žฌํ˜„์„ฑ ํ™•๋ณด"""
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False

# --- ๋ถ„๋ฅ˜ ๋ชจ๋ธ (Classification) ํ‰๊ฐ€ ์ง€ํ‘œ ---
def compute_cls_metrics(p):
    """
    ๋ถ„๋ฅ˜ ๋ชจ๋ธ ํ‰๊ฐ€ ์ง€ํ‘œ ๊ณ„์‚ฐ (Accuracy, F1-Macro)
    p: (predictions, labels) ํŠœํ”Œ
    """
    predictions, labels = p
    predictions = np.argmax(predictions, axis=1)
    
    accuracy = accuracy_score(labels, predictions)
    f1_macro = f1_score(labels, predictions, average="macro", zero_division=0)
    
    return {
        "accuracy": accuracy,
        "f1_macro": f1_macro,
    }

# --- ํšŒ๊ท€ ๋ชจ๋ธ (Regression) ํ‰๊ฐ€ ์ง€ํ‘œ ---
def compute_reg_metrics(p):
    """
    ํšŒ๊ท€ ๋ชจ๋ธ ํ‰๊ฐ€ ์ง€ํ‘œ ๊ณ„์‚ฐ (RMSE, Pearson R)
    p: (predictions, labels) ํŠœํ”Œ. predictions/labels๋Š” (N, 2) ํ˜•ํƒœ (Valence, Arousal)
    """
    preds, labels = p
    
    # 1. MSE ๊ณ„์‚ฐ (squared=False ์ธ์ˆ˜๋ฅผ ์ œ๊ฑฐํ•˜์—ฌ ์ตœ์‹  scikit-learn ํ˜ธํ™˜์„ฑ ๋ฌธ์ œ ํ•ด๊ฒฐ)
    mse_v = mean_squared_error(labels[:, 0], preds[:, 0]) 
    mse_a = mean_squared_error(labels[:, 1], preds[:, 1]) 

    # 2. RMSE ๊ณ„์‚ฐ (๋ฃจํŠธ ์”Œ์šฐ๊ธฐ)
    rmse_v = np.sqrt(mse_v)
    rmse_a = np.sqrt(mse_a)
    
    # 3. ์ „์ฒด ํ‰๊ท  RMSE
    rmse_mean = (rmse_v + rmse_a) / 2
    
    metrics = {
        "rmse_mean": rmse_mean,
        "rmse_valence": rmse_v,
        "rmse_arousal": rmse_a,
        "mae_valence": mean_absolute_error(labels[:, 0], preds[:, 0]),
        "mae_arousal": mean_absolute_error(labels[:, 1], preds[:, 1]),
    }
    
    # 4. Pearson ์ƒ๊ด€ ๊ณ„์ˆ˜ ๊ณ„์‚ฐ (ํ™œ์„ฑํ™” ๋ฐ Scipy ์„ค์น˜ ํ™•์ธ)
    if pearsonr:
        try:
            v_corr, _ = pearsonr(labels[:, 0], preds[:, 0])
            a_corr, _ = pearsonr(labels[:, 1], preds[:, 1])
            pearson_mean = (v_corr + a_corr) / 2
            
            metrics["pearson_mean"] = pearson_mean
            metrics["pearson_valence"] = v_corr
            metrics["pearson_arousal"] = a_corr
        except ValueError as e:
            # ๋ผ๋ฒจ์ด๋‚˜ ์˜ˆ์ธก๊ฐ’์ด ์ƒ์ˆ˜์ผ ๊ฒฝ์šฐ (e.g. [0, 0, 0]) pearsonr ๊ณ„์‚ฐ ์‹œ ์˜ค๋ฅ˜ ๋ฐœ์ƒ
            print(f"Warning: Could not compute Pearson correlation. {e}")
            metrics["pearson_mean"] = 0.0
    
    return metrics