-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_fixed_evaluation.py
More file actions
102 lines (75 loc) · 3.19 KB
/
Copy pathtest_fixed_evaluation.py
File metadata and controls
102 lines (75 loc) · 3.19 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Create trainer
# Load trained model
# Prepare data and model
# Success criteria
# Test different thresholds with fixed evaluation
# Add src to path
# Configure logging
#!/usr/bin/env python3
from src.models.emotion_detection.bert_classifier import evaluate_emotion_classifier
from src.models.emotion_detection.training_pipeline import EmotionDetectionTrainer
from pathlib import Path
import logging
import sys
import torch
"""Test Fixed Evaluation Function.
This script tests the fixed evaluation function to see if we get
realistic F1 scores now that the fallback bug is fixed.
"""
sys.path.insert(0, str(Path(__file__).parent.parent.parent / "src"))
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def main():
"""Test the fixed evaluation function."""
logger.info("🧪 Testing Fixed Evaluation Function")
try:
trainer = EmotionDetectionTrainer(
model_name="bert-base-uncased",
cache_dir="./data/cache",
output_dir="./test_checkpoints_dev",
batch_size=32,
device="cpu",
)
trainer.prepare_data(dev_mode=True)
trainer.initialize_model(class_weights=trainer.data_loader.class_weights)
model_path = Path("./test_checkpoints_dev/best_model.pt")
if not model_path.exists():
logger.error("❌ No trained model found. Run training first.")
return 1
checkpoint = torch.load(model_path, map_location="cpu", weights_only=False)
trainer.model.load_state_dict(checkpoint["model_state_dict"])
logger.info("✅ Model loaded successfully")
thresholds = [0.1, 0.15, 0.2, 0.25, 0.3]
logger.info("🎯 Testing thresholds with FIXED evaluation function:")
logger.info("=" * 60)
best_f1 = 0.0
for threshold in thresholds:
logger.info("🔍 Threshold: {threshold}")
metrics = evaluate_emotion_classifier(
trainer.model, trainer.val_dataloader, trainer.device, threshold=threshold
)
macro_f1 = metrics["macro_f1"]
metrics["micro_f1"]
logger.info(" 📊 Macro F1: {macro_f1:.4f} | Micro F1: {micro_f1:.4f}")
best_f1 = max(best_f1, macro_f1)
logger.info("=" * 60)
logger.info("🏆 BEST RESULTS:")
logger.info(" 🎯 Best Threshold: {best_threshold}")
logger.info(" 📈 Best Macro F1: {best_f1:.4f}")
if best_f1 > 0.15: # 15% is reasonable for emotion detection
logger.info("🎉 SUCCESS: Model is working well with fixed evaluation!")
logger.info("🚀 Ready to proceed with full training or deployment!")
return 0
elif best_f1 > 0.10: # 10% is acceptable for initial training
logger.info("✅ GOOD: Model shows promise, could benefit from more training")
return 0
else:
logger.warning(
"⚠️ Model still needs improvement, but evaluation is now working correctly"
)
return 1
except Exception:
logger.error("❌ Test failed: {e}")
return 1
if __name__ == "__main__":
sys.exit(main())