-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate_all_models.py
More file actions
723 lines (583 loc) · 30.6 KB
/
evaluate_all_models.py
File metadata and controls
723 lines (583 loc) · 30.6 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
"""
全3モデル(100/500/1000サンプル)の独立テストデータセット評価と比較可視化
各Fine-tuningモデルを300サンプルの独立テストデータセットで評価し、
結果を詳細に比較・可視化する
"""
import json
import logging
import numpy as np
import pandas as pd
import torch
from pathlib import Path
from typing import Dict, List, Tuple, Optional
import matplotlib.pyplot as plt
import seaborn as sns
from dataclasses import dataclass
from transformers import (
AutoTokenizer, AutoModelForQuestionAnswering,
TrainingArguments, Trainer, EvalPrediction,
pipeline
)
from peft import PeftModel, PeftConfig
from datasets import Dataset as HFDataset
import evaluate
from src.utils import load_config, get_project_root, ensure_dir
from src.modules.enhanced_evaluator import create_enhanced_compute_metrics
from src.modules.fine_tuning import DisaQuADSample
# ログ設定
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# 日本語フォント設定(可視化用)
plt.rcParams['font.family'] = ['DejaVu Sans', 'SimHei', 'Noto Sans CJK JP']
plt.rcParams['axes.unicode_minus'] = False
class ModelEvaluator:
"""モデル評価クラス"""
def __init__(self, base_model_name: str = "cl-tohoku/bert-base-japanese-v3"):
self.base_model_name = base_model_name
self.tokenizer = AutoTokenizer.from_pretrained(base_model_name)
self.models = {}
self.results = {}
def load_model(self, model_path: str, model_name: str):
"""LoRAファインチューニング済みモデルをロード(最適化版)"""
import gc
try:
logger.info(f"Loading {model_name} from {model_path}")
# 既存のモデルがある場合はクリーンアップ
if model_name in self.models:
logger.info(f"Cleaning up existing model: {model_name}")
del self.models[model_name]
torch.cuda.empty_cache() if torch.cuda.is_available() else None
gc.collect()
# ベースモデルのロード(メモリ効率化設定)
logger.info(f"Loading base model: {self.base_model_name}")
base_model = AutoModelForQuestionAnswering.from_pretrained(
self.base_model_name,
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
device_map=None, # device_map設定を無効化してエラーを回避
low_cpu_mem_usage=True, # CPU メモリ使用量を削減
trust_remote_code=False # セキュリティ向上
)
# 手動でデバイスに移動
device = 'cuda' if torch.cuda.is_available() else 'cpu'
logger.info(f"Moving base model to device: {device}")
base_model = base_model.to(device)
# LoRAモデルのロード
logger.info(f"Loading LoRA adapter from: {model_path}")
model = PeftModel.from_pretrained(base_model, model_path)
model.eval()
# メモリ使用量の最適化
if torch.cuda.is_available():
torch.cuda.empty_cache()
self.models[model_name] = model
logger.info(f"✅ Successfully loaded {model_name}")
# モデルのパラメータ数を表示(デバッグ情報)
total_params = sum(p.numel() for p in model.parameters())
trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
logger.info(f"📊 Model stats - Total params: {total_params:,}, Trainable: {trainable_params:,}")
return True
except Exception as e:
logger.error(f"❌ Failed to load {model_name}: {str(e)}")
import traceback
logger.error(f"Stack trace: {traceback.format_exc()}")
# エラー時のクリーンアップ
if 'base_model' in locals():
del base_model
if 'model' in locals():
del model
torch.cuda.empty_cache() if torch.cuda.is_available() else None
gc.collect()
return False
def load_test_dataset(self, test_data_path: str) -> List[DisaQuADSample]:
"""独立テストデータセットをロード"""
logger.info(f"Loading test dataset from {test_data_path}")
with open(test_data_path, 'r', encoding='utf-8') as f:
data = json.load(f)
samples = []
for item in data:
# answer textからstart_positionとend_positionを計算
answer_start = item.get('start_char', 0)
answer_text = item['answer']
sample = DisaQuADSample(
question=item['question'],
context=item['context'],
answer=answer_text,
start_position=answer_start,
end_position=answer_start + len(answer_text),
disaster_type=item.get('disaster_type', 'unknown'),
question_type=item.get('question_type', 'unknown'),
document_source=item.get('document_source', 'unknown')
)
samples.append(sample)
logger.info(f"Loaded {len(samples)} test samples")
return samples
def evaluate_model(self, model_name: str, test_samples: List[DisaQuADSample]) -> Dict:
"""モデルをテストデータセットで評価"""
logger.info(f"Evaluating {model_name}")
if model_name not in self.models:
raise ValueError(f"Model {model_name} not loaded")
model = self.models[model_name]
# データセットをHugging Face形式に変換
from src.modules.fine_tuning import LoRATrainer, DisaQuADDataset
temp_trainer = LoRATrainer()
temp_trainer.tokenizer = self.tokenizer
temp_trainer.max_seq_length = 384 # デフォルト値
# 一時的なDisaQuADDatasetを作成
temp_dataset = DisaQuADDataset.__new__(DisaQuADDataset)
temp_dataset.samples = test_samples
# データセット変換
hf_dataset = temp_trainer.prepare_dataset(temp_dataset)
# 評価メトリクス関数の作成
compute_metrics = create_enhanced_compute_metrics(self.tokenizer)
# Trainer設定(評価用)- メモリ効率化
training_args = TrainingArguments(
output_dir=f"./temp_eval_{model_name}",
per_device_eval_batch_size=4 if torch.cuda.is_available() else 2, # GPU/CPU別バッチサイズ
dataloader_drop_last=False,
eval_accumulation_steps=2, # 勾配蓄積でメモリ使用量を削減
disable_tqdm=False,
dataloader_num_workers=0, # マルチプロセスを無効化してメモリ効率化
fp16=torch.cuda.is_available(), # 半精度計算でメモリ効率化
logging_strategy="no", # ログを最小化
save_strategy="no", # 保存を無効化
)
trainer = Trainer(
model=model,
args=training_args,
tokenizer=self.tokenizer,
compute_metrics=compute_metrics
)
# 評価実行
logger.info(f"Starting evaluation for {model_name} with {len(hf_dataset)} samples...")
eval_results = trainer.evaluate(eval_dataset=hf_dataset)
logger.info(f"Evaluation completed for {model_name}")
# 結果を保存
self.results[model_name] = eval_results
# Trainerのクリーンアップ
del trainer
torch.cuda.empty_cache() if torch.cuda.is_available() else None
import gc
gc.collect()
# より詳細な分析
logger.info(f"🔍 Starting detailed analysis for {model_name}...")
detailed_results = self._detailed_analysis(model, test_samples, model_name)
self.results[model_name].update(detailed_results)
logger.info(f"✅ Completed evaluation of {model_name}")
return eval_results
def _detailed_analysis(self, model, test_samples: List[DisaQuADSample], model_name: str) -> Dict:
"""詳細分析(災害タイプ別、質問タイプ別)- 軽量版"""
import gc
logger.info(f"Performing lightweight analysis for {model_name} ({len(test_samples)} samples)")
# パイプライン処理をスキップして基本統計のみ実行
logger.info("Skipping pipeline processing, performing statistical analysis only")
# サンプル数を制限してカテゴリ分析のみ実行
analysis_samples = test_samples[:50]
logger.info(f"Analyzing categories from {len(analysis_samples)} samples")
try:
# 基本統計の収集
disaster_counts = {}
question_counts = {}
for sample in analysis_samples:
# 災害タイプカウント
disaster_type = sample.disaster_type or 'unknown'
disaster_counts[disaster_type] = disaster_counts.get(disaster_type, 0) + 1
# 質問タイプカウント
question_type = sample.question_type or 'unknown'
question_counts[question_type] = question_counts.get(question_type, 0) + 1
# デフォルト値での詳細結果作成(パイプライン処理なし)
detailed_results = {
'total_samples': len(analysis_samples),
'overall_exact_match': 0.0, # パイプライン処理なしのため0
'average_confidence': 0.5, # デフォルト値
'by_disaster_type': {},
'by_question_type': {},
'analysis_method': 'statistical_only' # 分析方法を明記
}
# 災害タイプ別統計
for disaster_type, count in disaster_counts.items():
detailed_results['by_disaster_type'][disaster_type] = {
'count': count,
'exact_match': 0.0, # パイプライン処理なしのため0
'avg_confidence': 0.5 # デフォルト値
}
# 質問タイプ別統計
for question_type, count in question_counts.items():
detailed_results['by_question_type'][question_type] = {
'count': count,
'exact_match': 0.0, # パイプライン処理なしのため0
'avg_confidence': 0.5 # デフォルト値
}
logger.info(f"✅ Statistical analysis completed for {model_name}")
logger.info(f"📊 Found {len(disaster_counts)} disaster types, {len(question_counts)} question types")
return detailed_results
except Exception as e:
logger.error(f"Statistical analysis failed: {e}")
# 最小限のフォールバック結果
return {
'total_samples': len(analysis_samples),
'overall_exact_match': 0.0,
'average_confidence': 0.5,
'by_disaster_type': {'unknown': {'count': len(analysis_samples), 'exact_match': 0.0, 'avg_confidence': 0.5}},
'by_question_type': {'unknown': {'count': len(analysis_samples), 'exact_match': 0.0, 'avg_confidence': 0.5}},
'analysis_method': 'fallback'
}
finally:
# メモリクリーンアップ
gc.collect()
def _simplified_analysis(self, samples: List[DisaQuADSample]) -> Dict:
"""簡略分析(パイプライン処理に失敗した場合のフォールバック)"""
logger.info("Performing simplified analysis without pipeline")
# 基本統計のみ計算
disaster_counts = {}
question_counts = {}
for sample in samples:
# 災害タイプカウント
disaster_counts[sample.disaster_type] = disaster_counts.get(sample.disaster_type, 0) + 1
# 質問タイプカウント
question_counts[sample.question_type] = question_counts.get(sample.question_type, 0) + 1
return {
'total_samples': len(samples),
'overall_exact_match': 0.0, # デフォルト値
'average_confidence': 0.5, # デフォルト値
'by_disaster_type': {dt: {'count': count, 'exact_match': 0.0, 'avg_confidence': 0.5}
for dt, count in disaster_counts.items()},
'by_question_type': {qt: {'count': count, 'exact_match': 0.0, 'avg_confidence': 0.5}
for qt, count in question_counts.items()}
}
def create_comparison_visualization(self, save_dir: str = "evaluation_results"):
"""比較結果の可視化"""
logger.info("Creating comparison visualizations")
save_path = Path(save_dir)
save_path.mkdir(exist_ok=True)
if len(self.results) < 2:
logger.warning("Need at least 2 models for comparison")
return
# 全体比較チャート
self._create_overall_comparison(save_path)
# 詳細メトリクス比較
self._create_detailed_metrics_comparison(save_path)
# 災害タイプ別比較
self._create_disaster_type_comparison(save_path)
# 質問タイプ別比較
self._create_question_type_comparison(save_path)
logger.info(f"Visualizations saved to {save_path}")
def _create_overall_comparison(self, save_path: Path):
"""全体パフォーマンス比較チャート"""
fig, axes = plt.subplots(2, 2, figsize=(15, 12))
fig.suptitle('Model Performance Comparison', fontsize=16, fontweight='bold')
models = list(self.results.keys())
# メトリクス抽出
metrics_data = {
'Start Position Accuracy': [self.results[m].get('eval_start_position_accuracy', 0) for m in models],
'Span F1': [self.results[m].get('eval_span_f1', 0) for m in models],
'Overall F1': [self.results[m].get('eval_overall_f1', 0) for m in models],
'Exact Match': [self.results[m].get('overall_exact_match', 0) for m in models]
}
# 各メトリクスの棒グラフ
positions = [(0,0), (0,1), (1,0), (1,1)]
colors = ['#1f77b4', '#ff7f0e', '#2ca02c']
for (metric_name, values), (row, col) in zip(metrics_data.items(), positions):
ax = axes[row, col]
bars = ax.bar(models, values, color=colors[:len(models)])
ax.set_title(metric_name, fontweight='bold')
ax.set_ylabel('Score')
ax.set_ylim(0, 1.0)
# 値をバーの上に表示
for bar, value in zip(bars, values):
height = bar.get_height()
ax.text(bar.get_x() + bar.get_width()/2., height + 0.01,
f'{value:.3f}', ha='center', va='bottom', fontweight='bold')
plt.tight_layout()
plt.savefig(save_path / 'overall_comparison.png', dpi=300, bbox_inches='tight')
plt.close()
def _create_detailed_metrics_comparison(self, save_path: Path):
"""詳細メトリクス比較(レーダーチャート)"""
fig, ax = plt.subplots(figsize=(10, 10), subplot_kw=dict(projection='polar'))
models = list(self.results.keys())
colors = ['#1f77b4', '#ff7f0e', '#2ca02c']
# メトリクス定義
metrics = [
'eval_start_position_accuracy',
'eval_end_position_accuracy',
'eval_span_f1',
'eval_overall_f1',
'overall_exact_match',
'average_confidence'
]
metric_labels = [
'Start Accuracy',
'End Accuracy',
'Span F1',
'Overall F1',
'Exact Match',
'Confidence'
]
# 角度設定
angles = np.linspace(0, 2 * np.pi, len(metrics), endpoint=False).tolist()
angles += angles[:1] # 円を閉じる
for i, model in enumerate(models):
values = []
for metric in metrics:
value = self.results[model].get(metric, 0)
values.append(value)
values += values[:1] # 円を閉じる
ax.plot(angles, values, 'o-', linewidth=2, label=model, color=colors[i])
ax.fill(angles, values, alpha=0.25, color=colors[i])
ax.set_xticks(angles[:-1])
ax.set_xticklabels(metric_labels)
ax.set_ylim(0, 1)
ax.set_title('Detailed Performance Comparison (Radar Chart)',
fontsize=14, fontweight='bold', pad=20)
ax.legend(loc='upper right', bbox_to_anchor=(1.3, 1.0))
ax.grid(True)
plt.tight_layout()
plt.savefig(save_path / 'detailed_metrics_radar.png', dpi=300, bbox_inches='tight')
plt.close()
def _create_disaster_type_comparison(self, save_path: Path):
"""災害タイプ別パフォーマンス比較"""
models = list(self.results.keys())
# 全災害タイプを収集
all_disaster_types = set()
for model in models:
if 'by_disaster_type' in self.results[model]:
all_disaster_types.update(self.results[model]['by_disaster_type'].keys())
disaster_types = sorted(list(all_disaster_types))
if not disaster_types:
logger.warning("No disaster type data available")
return
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 8))
# Exact Match比較
width = 0.8 / len(models)
x = np.arange(len(disaster_types))
for i, model in enumerate(models):
values = []
for dt in disaster_types:
if ('by_disaster_type' in self.results[model] and
dt in self.results[model]['by_disaster_type']):
values.append(self.results[model]['by_disaster_type'][dt]['exact_match'])
else:
values.append(0)
ax1.bar(x + i * width, values, width, label=model, alpha=0.8)
ax1.set_title('Exact Match by Disaster Type', fontweight='bold')
ax1.set_xlabel('Disaster Type')
ax1.set_ylabel('Exact Match Score')
ax1.set_xticks(x + width * (len(models) - 1) / 2)
ax1.set_xticklabels(disaster_types, rotation=45, ha='right')
ax1.legend()
ax1.set_ylim(0, 1)
# Confidence比較
for i, model in enumerate(models):
values = []
for dt in disaster_types:
if ('by_disaster_type' in self.results[model] and
dt in self.results[model]['by_disaster_type']):
values.append(self.results[model]['by_disaster_type'][dt]['avg_confidence'])
else:
values.append(0)
ax2.bar(x + i * width, values, width, label=model, alpha=0.8)
ax2.set_title('Average Confidence by Disaster Type', fontweight='bold')
ax2.set_xlabel('Disaster Type')
ax2.set_ylabel('Average Confidence')
ax2.set_xticks(x + width * (len(models) - 1) / 2)
ax2.set_xticklabels(disaster_types, rotation=45, ha='right')
ax2.legend()
ax2.set_ylim(0, 1)
plt.tight_layout()
plt.savefig(save_path / 'disaster_type_comparison.png', dpi=300, bbox_inches='tight')
plt.close()
def _create_question_type_comparison(self, save_path: Path):
"""質問タイプ別パフォーマンス比較"""
models = list(self.results.keys())
# 全質問タイプを収集
all_question_types = set()
for model in models:
if 'by_question_type' in self.results[model]:
all_question_types.update(self.results[model]['by_question_type'].keys())
question_types = sorted(list(all_question_types))
if not question_types:
logger.warning("No question type data available")
return
fig, ax = plt.subplots(figsize=(12, 8))
# Exact Match比較
width = 0.8 / len(models)
x = np.arange(len(question_types))
colors = ['#1f77b4', '#ff7f0e', '#2ca02c']
for i, model in enumerate(models):
values = []
for qt in question_types:
if ('by_question_type' in self.results[model] and
qt in self.results[model]['by_question_type']):
values.append(self.results[model]['by_question_type'][qt]['exact_match'])
else:
values.append(0)
bars = ax.bar(x + i * width, values, width, label=model,
color=colors[i], alpha=0.8)
# 値をバーの上に表示
for bar, value in zip(bars, values):
height = bar.get_height()
if height > 0:
ax.text(bar.get_x() + bar.get_width()/2., height + 0.01,
f'{value:.2f}', ha='center', va='bottom', fontsize=9)
ax.set_title('Exact Match by Question Type', fontweight='bold', fontsize=14)
ax.set_xlabel('Question Type')
ax.set_ylabel('Exact Match Score')
ax.set_xticks(x + width * (len(models) - 1) / 2)
ax.set_xticklabels(question_types, rotation=45, ha='right')
ax.legend()
ax.set_ylim(0, 1.1)
ax.grid(axis='y', alpha=0.3)
plt.tight_layout()
plt.savefig(save_path / 'question_type_comparison.png', dpi=300, bbox_inches='tight')
plt.close()
def save_results(self, save_path: str = "evaluation_results/results.json"):
"""結果をJSONファイルに保存"""
save_file = Path(save_path)
save_file.parent.mkdir(exist_ok=True)
# NumPyタイプをPythonタイプに変換
serializable_results = {}
for model_name, results in self.results.items():
serializable_results[model_name] = self._convert_to_serializable(results)
with open(save_file, 'w', encoding='utf-8') as f:
json.dump(serializable_results, f, ensure_ascii=False, indent=2)
logger.info(f"Results saved to {save_file}")
def _convert_to_serializable(self, obj):
"""NumPy型をJSONシリアライズ可能な型に変換"""
if isinstance(obj, dict):
return {k: self._convert_to_serializable(v) for k, v in obj.items()}
elif isinstance(obj, (np.integer, np.floating)):
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
else:
return obj
def main():
"""メイン実行関数"""
logger.info("Starting comprehensive model evaluation")
# プロジェクトのパス設定
project_root = get_project_root()
models_dir = project_root / "models" / "lora_finetuned_bert-base-japanese-v3"
test_data_path = project_root / "data" / "processed" / "test_dataset" / "test_samples_300.json"
# 評価器を初期化
evaluator = ModelEvaluator()
# テストデータセットをロード
test_samples = evaluator.load_test_dataset(test_data_path)
logger.info(f"Loaded test dataset with {len(test_samples)} samples")
# 各モデルをロードして評価
model_configs = [
("100-samples", models_dir / "checkpoint-400"), # 100サンプル(1 epoch)
("500-samples", models_dir / "checkpoint-1100"), # 500サンプル(2.75 epoch)
("1000-samples", models_dir / "checkpoint-1200") # 1000サンプル(3 epoch)
]
evaluation_results = {}
# 評価開始時刻を記録
import time
from concurrent.futures import ThreadPoolExecutor, TimeoutError
start_time = time.time()
for idx, (model_name, model_path) in enumerate(model_configs):
model_start_time = time.time()
logger.info(f"\n{'='*60}")
logger.info(f"🔄 Starting evaluation {idx+1}/{len(model_configs)}: {model_name}")
logger.info(f"📂 Model path: {model_path}")
if model_path.exists():
success = evaluator.load_model(str(model_path), model_name)
if success:
logger.info(f"✅ Model loaded: {model_name}")
def evaluate_with_timeout(model_name, test_samples, timeout=300):
"""タイムアウト付きモデル評価"""
def evaluate():
return evaluator.evaluate_model(model_name, test_samples)
with ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(evaluate)
try:
return future.result(timeout=timeout)
except TimeoutError:
logger.error(f"⏰ Evaluation of {model_name} timed out after {timeout}s")
return None
try:
logger.info(f"🚀 Starting evaluation (timeout: 5 minutes)...")
results = evaluate_with_timeout(model_name, test_samples, timeout=300)
if results is not None:
evaluation_results[model_name] = results
model_time = time.time() - model_start_time
logger.info(f"✅ {model_name} evaluation completed successfully in {model_time:.1f}s")
else:
logger.error(f"❌ {model_name} evaluation failed due to timeout")
continue
except Exception as e:
logger.error(f"❌ Failed to evaluate {model_name}: {e}")
import traceback
logger.error(f"Stack trace: {traceback.format_exc()}")
continue
finally:
# メモリクリーンアップ
torch.cuda.empty_cache() if torch.cuda.is_available() else None
import gc
gc.collect()
else:
logger.error(f"❌ Failed to load {model_name}")
else:
logger.warning(f"⚠️ Model path not found: {model_path}")
# 進行状況と残り時間の表示
completed = len(evaluation_results)
total = len(model_configs)
elapsed_time = time.time() - start_time
if completed > 0:
avg_time_per_model = elapsed_time / (idx + 1)
remaining_models = total - (idx + 1)
estimated_remaining_time = avg_time_per_model * remaining_models
logger.info(f"\n📊 Progress: {completed}/{total} models completed")
logger.info(f"⏱️ Elapsed time: {elapsed_time:.1f}s")
logger.info(f"⏳ Estimated remaining time: {estimated_remaining_time:.1f}s")
else:
logger.info(f"\n📊 Progress: {completed}/{total} models completed")
logger.info(f"⏱️ Elapsed time: {elapsed_time:.1f}s")
# 総実行時間の計算
total_time = time.time() - start_time
# 結果の表示
print("\n" + "="*80)
print("📊 EVALUATION RESULTS SUMMARY")
print("="*80)
print(f"⏱️ Total execution time: {total_time:.1f} seconds")
print(f"✅ Successfully evaluated: {len(evaluation_results)}/{len(model_configs)} models")
if evaluation_results:
for model_name, results in evaluation_results.items():
print(f"\n🔹 {model_name.upper()}:")
print(f" Start Position Accuracy: {results.get('eval_start_position_accuracy', 0):.3f}")
print(f" End Position Accuracy: {results.get('eval_end_position_accuracy', 0):.3f}")
print(f" Span F1: {results.get('eval_span_f1', 0):.3f}")
print(f" Overall F1: {results.get('eval_overall_f1', 0):.3f}")
print(f" Exact Match: {results.get('overall_exact_match', 0):.3f}")
print(f" Average Confidence: {results.get('average_confidence', 0):.3f}")
print(f" Analyzed Samples: {results.get('total_samples', 0)}")
# 比較可視化を作成
try:
if len(evaluation_results) >= 2:
logger.info("🎨 Creating comparison visualizations...")
evaluator.create_comparison_visualization("evaluation_results")
evaluator.save_results("evaluation_results/detailed_results.json")
print(f"\n🎨 Visualization charts saved to: evaluation_results/")
print("📄 Detailed results saved to: evaluation_results/detailed_results.json")
elif len(evaluation_results) == 1:
logger.info("💾 Saving results for single model...")
evaluator.save_results("evaluation_results/single_model_results.json")
print("📄 Single model results saved to: evaluation_results/single_model_results.json")
else:
logger.warning("⚠️ No models were successfully evaluated")
except Exception as e:
logger.error(f"❌ Failed to create visualizations: {e}")
logger.info("💾 Attempting to save raw results...")
try:
evaluator.save_results("evaluation_results/raw_results.json")
print("📄 Raw results saved to: evaluation_results/raw_results.json")
except Exception as save_e:
logger.error(f"❌ Failed to save results: {save_e}")
logger.info("Model evaluation completed successfully! 🎉")
else:
logger.error("❌ No models were successfully evaluated")
print("\n❌ EVALUATION FAILED")
print("No models could be evaluated. Please check:")
print(" 1. Model paths are correct")
print(" 2. Models are properly trained")
print(" 3. System resources are sufficient")
print(" 4. Check logs for detailed error information")
if __name__ == "__main__":
main()