-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest2.py
More file actions
85 lines (71 loc) · 2.89 KB
/
test2.py
File metadata and controls
85 lines (71 loc) · 2.89 KB
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
import pickle
import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from sklearn.metrics.pairwise import cosine_similarity
# L2 정규화
def l2norm(x): return x / np.linalg.norm(x, axis=1, keepdims=True)
# 데이터셋 클래스
class FaceDataset(Dataset):
def __init__(self, X, y):
self.X = torch.tensor(X, dtype=torch.float32)
self.y = torch.tensor(y, dtype=torch.long)
def __len__(self): return len(self.y)
def __getitem__(self, idx): return self.X[idx], self.y[idx]
# 분류기 정의
class SimpleClassifier(nn.Module):
def __init__(self, embedding_dim=512, num_classes=400):
super().__init__()
self.fc = nn.Linear(embedding_dim, num_classes)
def forward(self, x): return self.fc(x)
# 데이터 로드
with open('train_embeddings.pkl', 'rb') as f: train_emb = pickle.load(f)
with open('val_embeddings.pkl', 'rb') as f: val_emb = pickle.load(f)
with open('label_to_id.pkl', 'rb') as f: label2id = pickle.load(f)
def build_dataset(emb_dict):
X, y = [], []
for pid, embs in emb_dict.items():
for e in embs:
X.append(e)
y.append(label2id[pid])
return np.array(X), np.array(y)
X_train, y_train = build_dataset(train_emb)
X_val, y_val = build_dataset(val_emb)
X_train = l2norm(X_train)
X_val = l2norm(X_val)
# ✅ 유사도 기반 예측
def predict_similarity(X_train, y_train, X_val):
y_pred = []
for v in X_val:
sims = cosine_similarity([v], X_train)[0]
pred = y_train[np.argmax(sims)]
y_pred.append(pred)
return np.array(y_pred)
y_pred_sim = predict_similarity(X_train, y_train, X_val)
# ✅ 딥러닝 모델 기반 예측
device = 'cuda' if torch.cuda.is_available() else 'cpu'
model = SimpleClassifier(512, len(label2id)).to(device)
ckpt = torch.load('classifier.pth', map_location=device)
model.load_state_dict(ckpt['model'])
model.eval()
val_ds = FaceDataset(X_val, y_val)
val_loader = DataLoader(val_ds, batch_size=64)
y_pred_clf, y_true_clf = [], []
with torch.no_grad():
for X, y in val_loader:
X = X.to(device)
preds = torch.argmax(model(X), dim=1).cpu().numpy()
y_pred_clf.extend(preds)
y_true_clf.extend(y.numpy())
# 평가 함수
def evaluate(y_true, y_pred, label="모델"):
print(f"\n📊 {label} 성능 평가:")
print(f"✅ Accuracy : {accuracy_score(y_true, y_pred):.4f}")
print(f"✅ Precision (macro): {precision_score(y_true, y_pred, average='macro', zero_division=0):.4f}")
print(f"✅ Recall (macro) : {recall_score(y_true, y_pred, average='macro', zero_division=0):.4f}")
print(f"✅ F1 Score (macro) : {f1_score(y_true, y_pred, average='macro', zero_division=0):.4f}")
# 결과 출력
evaluate(y_val, y_pred_sim, "유사도 기반")
evaluate(y_true_clf, y_pred_clf, "딥러닝 모델")