File size: 7,199 Bytes
5554ef1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Evaluation script for Whisper German ASR model
Computes WER, CER, and other metrics on test data
"""

import torch
from transformers import WhisperForConditionalGeneration, WhisperProcessor
from datasets import load_from_disk
import jiwer
import librosa
import numpy as np
from pathlib import Path
import json
from tqdm import tqdm
import argparse


def normalize_text(text):
    """Normalize text for consistent evaluation"""
    import re
    text = text.lower()
    text = re.sub(r'[^\w\s]', '', text)  # Remove punctuation
    text = ' '.join(text.split())  # Normalize whitespace
    return text


def load_model(model_path):
    """Load fine-tuned Whisper model"""
    print(f"\n๐Ÿ“ฆ Loading model from: {model_path}")
    
    model_path = Path(model_path)
    
    # Check for checkpoint directories
    if model_path.is_dir():
        checkpoints = list(model_path.glob('checkpoint-*'))
        if checkpoints:
            # Use the latest checkpoint
            latest = max(checkpoints, key=lambda p: int(p.name.split('-')[1]))
            model_path = latest
            print(f"   Using checkpoint: {latest.name}")
    
    model = WhisperForConditionalGeneration.from_pretrained(model_path)
    processor = WhisperProcessor.from_pretrained("openai/whisper-small")
    
    # Set language conditioning
    model.config.forced_decoder_ids = processor.get_decoder_prompt_ids(
        language="german", 
        task="transcribe"
    )
    
    device = "cuda" if torch.cuda.is_available() else "cpu"
    model = model.to(device)
    model.eval()
    
    print(f"โœ“ Model loaded on {device}")
    print(f"โœ“ Parameters: {sum(p.numel() for p in model.parameters()) / 1e6:.0f}M")
    
    return model, processor, device


def transcribe_audio(audio_array, sample_rate, model, processor, device):
    """Transcribe a single audio sample"""
    # Resample if needed
    if sample_rate != 16000:
        audio_array = librosa.resample(
            audio_array, 
            orig_sr=sample_rate, 
            target_sr=16000
        )
    
    # Process audio
    input_features = processor(
        audio_array,
        sampling_rate=16000,
        return_tensors="pt"
    ).input_features.to(device)
    
    # Generate transcription
    with torch.no_grad():
        predicted_ids = model.generate(
            input_features,
            max_length=448,
            num_beams=5,
            early_stopping=True
        )
    
    transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
    return transcription


def evaluate_dataset(model, processor, device, dataset_path, split='test', max_samples=None):
    """Evaluate model on dataset"""
    print(f"\n๐Ÿ“Š Evaluating on dataset: {dataset_path}")
    
    # Load dataset
    dataset = load_from_disk(dataset_path)
    
    # Handle different dataset formats
    if isinstance(dataset, dict):
        if split in dataset:
            dataset = dataset[split]
        elif 'test' in dataset:
            dataset = dataset['test']
        elif 'validation' in dataset:
            dataset = dataset['validation']
        else:
            # Use a portion of train as test
            dataset = dataset['train'].train_test_split(test_size=0.1, seed=42)['test']
    
    if max_samples:
        dataset = dataset.select(range(min(max_samples, len(dataset))))
    
    print(f"   Evaluating on {len(dataset)} samples...")
    
    predictions = []
    references = []
    
    for sample in tqdm(dataset, desc="Transcribing"):
        # Get audio
        audio = sample['audio']['array']
        sr = sample['audio']['sampling_rate']
        
        # Transcribe
        pred = transcribe_audio(audio, sr, model, processor, device)
        ref = sample['transcription']
        
        predictions.append(normalize_text(pred))
        references.append(normalize_text(ref))
    
    # Compute metrics
    wer = jiwer.wer(references, predictions)
    cer = jiwer.cer(references, predictions)
    
    # Word-level metrics
    wer_transform = jiwer.Compose([
        jiwer.ToLowerCase(),
        jiwer.RemovePunctuation(),
        jiwer.RemoveMultipleSpaces(),
        jiwer.Strip(),
    ])
    
    measures = jiwer.compute_measures(
        references, 
        predictions,
        truth_transform=wer_transform,
        hypothesis_transform=wer_transform
    )
    
    results = {
        'wer': wer,
        'cer': cer,
        'num_samples': len(dataset),
        'substitutions': measures['substitutions'],
        'deletions': measures['deletions'],
        'insertions': measures['insertions'],
        'hits': measures['hits'],
    }
    
    return results, predictions, references


def print_results(results):
    """Print evaluation results"""
    print("\n" + "=" * 60)
    print("EVALUATION RESULTS")
    print("=" * 60)
    print(f"\n๐Ÿ“Š Metrics:")
    print(f"   Word Error Rate (WER):     {results['wer']:.4f} ({results['wer']*100:.2f}%)")
    print(f"   Character Error Rate (CER): {results['cer']:.4f} ({results['cer']*100:.2f}%)")
    print(f"\n๐Ÿ“ˆ Word-level Statistics:")
    print(f"   Correct (Hits):      {results['hits']}")
    print(f"   Substitutions:       {results['substitutions']}")
    print(f"   Deletions:           {results['deletions']}")
    print(f"   Insertions:          {results['insertions']}")
    print(f"   Total samples:       {results['num_samples']}")
    print("=" * 60)


def save_results(results, predictions, references, output_file):
    """Save evaluation results to file"""
    output = {
        'metrics': results,
        'samples': [
            {'prediction': p, 'reference': r}
            for p, r in zip(predictions, references)
        ]
    }
    
    with open(output_file, 'w', encoding='utf-8') as f:
        json.dump(output, f, indent=2, ensure_ascii=False)
    
    print(f"\n๐Ÿ’พ Results saved to: {output_file}")


def main():
    parser = argparse.ArgumentParser(description="Evaluate Whisper German ASR model")
    parser.add_argument('--model', type=str, default='./whisper_test_tuned',
                        help='Path to fine-tuned model')
    parser.add_argument('--dataset', type=str, default='./data/minds14_medium',
                        help='Path to dataset')
    parser.add_argument('--split', type=str, default='test',
                        help='Dataset split to evaluate (test/validation)')
    parser.add_argument('--max-samples', type=int, default=None,
                        help='Maximum number of samples to evaluate')
    parser.add_argument('--output', type=str, default='./evaluation_results.json',
                        help='Output file for results')
    
    args = parser.parse_args()
    
    # Load model
    model, processor, device = load_model(args.model)
    
    # Evaluate
    results, predictions, references = evaluate_dataset(
        model, processor, device,
        args.dataset,
        split=args.split,
        max_samples=args.max_samples
    )
    
    # Print results
    print_results(results)
    
    # Save results
    save_results(results, predictions, references, args.output)
    
    print("\nโœ… Evaluation complete!\n")


if __name__ == "__main__":
    main()