File size: 10,773 Bytes
3284d90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from io import BytesIO
from urllib.request import urlopen
import soundfile
import torch
from datasets import load_dataset, Audio
import numpy as np
from transformers import AutoModel, AutoProcessor, BatchFeature
from tqdm import tqdm
import json
import os
import time
from datetime import datetime
from whisper_normalizer.english import EnglishTextNormalizer
from whisper_normalizer.basic import BasicTextNormalizer
import sacrebleu
from jiwer import cer, wer
from torch.utils.data import Dataset, DataLoader
import soundfile as sf
import re
from pathlib import Path
import opencc
from ASRDataset import *
converter = opencc.OpenCC('s2tw.json')
normalizer = {
    "en_us" : EnglishTextNormalizer(),
    "other" : BasicTextNormalizer()
}

model_id = "/mnt/jeff/InCar/LlamaNemotronOmni/test_nemotron_omni"
revision = "main" #"v1.0"

model = AutoModel.from_pretrained(
    model_id, device_map="cuda", revision = revision, trust_remote_code=True
).eval()

processor = AutoProcessor.from_pretrained(
    model_id, revision = revision, trust_remote_code=True
)
if 'LlamaNemotronOmni' in model_id:
    processor.tokenizer.pad_token_id = processor.tokenizer.eos_token_id

results_dir = f"evaluation_results_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
os.makedirs(results_dir, exist_ok=True)


INSTRUCTION = {
    "ast": "Translate the audio to {0}.",
    "asr": "Transcribe the audio clip into text.",
}

def covost_collate_fn_test(batch):
    input_ids_list = []
    input_audio_embeds_list = []
    audio_embed_sizes_list = []
    audio_attention_mask_list = []
    input_modes_list = []
    answer_list = []
    for inputs in batch:
        input_ids_list.append(inputs['input_ids'][0])
        input_audio_embeds_list.append(inputs['input_audio_embeds'])
        audio_embed_sizes_list.append(inputs['audio_embed_sizes'])
        audio_attention_mask_list.append(
            inputs['input_audio_embeds'].new_full((inputs['input_audio_embeds'].size(1),), True, dtype=torch.bool)
        )
        input_modes_list.append(inputs['input_modes'])
        answer_list.append(inputs['answer'])

    try:
        input_ids = pad_sequence(input_ids_list, padding_side='left', padding_value=0)
        audio_attention_mask = (
            pad_sequence(audio_attention_mask_list, padding_side='right', padding_value=False)
            if len(audio_attention_mask_list) > 1
            else None
        )
    except Exception as e:
        print(e)
        print(input_ids_list)
        print(audio_attention_mask)
        raise
    attention_mask = (input_ids != 0).long()
    input_audio_embeds = cat_with_pad(input_audio_embeds_list, dim=0)
    audio_embed_sizes = torch.cat(audio_embed_sizes_list)
    input_modes = torch.cat(input_modes_list)

    return BatchFeature(
        {
            'input_ids': input_ids,
            'attention_mask': attention_mask,
            'input_audio_embeds': input_audio_embeds,
            'audio_embed_sizes': audio_embed_sizes,
            'audio_attention_mask': audio_attention_mask,
            'input_modes': input_modes,
            'answer': answer_list,
        }
    )

def save_results(results, dataset_name, task, source_lang, target_lang=None, sample_idx=None):
    filename = f"{task}_{dataset_name}_{source_lang}"
    if target_lang:
        filename += f"_to_{target_lang}"
    if sample_idx is not None:
        filename += f"_sample_{sample_idx}"
    
    filepath = os.path.join(results_dir, f"{filename}.json")
    
    results["timestamp"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    
    with open(filepath, 'w', encoding='utf-8') as f:
        json.dump(results, f, ensure_ascii=False, indent=2)
    
    return filepath

def evaluate_task(dataset, source_lang, target_lang, num_samples=-1, batch_size = 4, is_asr=True):
    task_type = "asr" if is_asr else "translation"
    eval_lang = source_lang if is_asr else target_lang
    if eval_lang in normalizer:
        eval_normalizer = normalizer[eval_lang]
    else:
        eval_normalizer = normalizer['other']
    sample_results = []
    
    if num_samples > 0 and num_samples < len(dataset):
        indices = np.random.choice(len(dataset), num_samples, replace=False)
        dataset = dataset.select(indices)
    
    dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=False, collate_fn=covost_collate_fn_test)
        
    evaluated_samples = {} 
    
    for batch_idx, batch in enumerate(tqdm(dataloader)):
        batch_references = batch.pop("answer")

        if torch.cuda.is_available():
            try:
                batch = {k: v.to("cuda") for k, v in batch.items()}
            except:
                print('error')
                break
        
        with torch.inference_mode():
            generate_ids = model.generate(**batch, 
            max_new_tokens=256,
            #temperature = 1.0, top_p = 0.95, top_k = 64, do_sample=True
            )
            
            input_lengths = batch['input_ids'].shape[1]
            generate_ids = generate_ids[:, input_lengths:]
        
            batch_predictions = processor.batch_decode(
                generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
            )
        
        for i, (reference, prediction) in enumerate(zip(batch_references, batch_predictions)):
            idx = batch_idx * batch_size + i
            sample_result = {
                "id": idx,
                "reference": reference,
                "prediction": converter.convert(prediction)
            }
            sample_results.append(sample_result)

        if (batch_idx + 1) % 10 == 0:
            temp_results = []
            
            for item in sample_results:
                sample_id = item["id"]
                
                if sample_id in evaluated_samples:
                    temp_item = item.copy()
                    temp_item.update(evaluated_samples[sample_id])
                    temp_results.append(temp_item)
                else:
                    temp_item = item.copy()
                    try:
                        ref = eval_normalizer(item["reference"])
                        pred = eval_normalizer(item["prediction"])

                        # BLEU, WER/CER
                        utt_bleu = sacrebleu.sentence_bleu(pred, [ref]).score
                        utt_cer = round(cer(re.sub(r"\s+", "", ref), re.sub(r"\s+", "", pred)) * 100, 2)
                        utt_wer = round(wer(ref, pred) * 100, 2)

                        metrics = {
                            "bleu": utt_bleu,
                            "cer": min(100,utt_cer),
                            "wer": utt_wer
                        }
                        
                        evaluated_samples[sample_id] = metrics
                        temp_item.update(metrics)
                    except Exception as e:
                        print(f"Error evaluating sample {sample_id}: {e}")
                        metrics = {
                            "bleu": 0,
                            "cer": 100,
                            "wer": 100,
                            "error": str(e)
                        }
                        evaluated_samples[sample_id] = metrics
                        temp_item.update(metrics)
                
                    temp_results.append(temp_item)

            partial_results = {
                "task": task_type,
                "source_lang": source_lang,
                "target_lang": target_lang,
                "num_samples": len(temp_results),
                "sample_results": temp_results
            }
            save_results(partial_results, dataset.name, task_type, source_lang, target_lang)

    for item in sample_results:
        ref = eval_normalizer(item["reference"])
        pred = eval_normalizer(item["prediction"])

        utt_bleu = sacrebleu.sentence_bleu(pred, [ref]).score
        utt_cer = round(cer(re.sub(r"\s+", "", ref), re.sub(r"\s+", "", pred)) * 100, 2)
        utt_wer = round(wer(ref, pred) * 100, 2)

        item.update({
            "bleu": utt_bleu,
            "cer": min(100,utt_cer),
            "wer": utt_wer
        })

    avg_bleu = sum(item["bleu"] for item in sample_results) / len(sample_results)
    avg_cer = sum(item["cer"] for item in sample_results) / len(sample_results)
    avg_wer = sum(item["wer"] for item in sample_results) / len(sample_results)
    
    results = {
        "dataset": dataset.name,
        "task": task_type,
        "source_lang": source_lang,
        "target_lang": target_lang,
        "num_samples": len(sample_results),
        "metrics": {
            "bleu": avg_bleu,
            "cer": avg_cer,
            "wer": avg_wer
        },
        "sample_results": sample_results
    }
    
    save_results(results, dataset.name, task_type, source_lang, target_lang)
    return results


if __name__ == "__main__":

    source_languages = [
        ("en_us", "English"),
    ]
    
    target_languages = [
        ("zh-TW", "zh-TW"),
    ]

    num_samples = -1
    batch_size = 32

    for source_lang, target_lang in zip(source_languages, target_languages):
        print(f"\n===== {source_lang[0]} ASR =====")
        
        split = "test"

        datasets = []

        
        
        commonvoice_speech_tw = CommonVoiceDataset(
            processor=processor,
            source_lang="zh-TW",
            split=split
        )
        datasets.append(commonvoice_speech_tw)
        fleurs = FleursDataset(
            processor=processor,
            split=split,
            source_lang="en_us",  # English
            mode="asr"
        )
        datasets.append(fleurs)

        # Libri Speech Clean ASR mode (English -> English text)
        # libri_speech_clean = LibriSpeechDataset(
        #     processor=processor,
        #     subset="clean",
        #     split=split
        # )
        # datasets.append(libri_speech_clean)

        # # Libri Speech Other ASR mode (English -> English text)
        # libri_speech_other = LibriSpeechDataset(
        #     processor=processor,
        #     subset="other",
        #     split=split
        # )
        # datasets.append(libri_speech_other)

        # Fleurs ASR mode (English -> English text)
        

        for dataset in datasets:
            # ASR
            asr_results = evaluate_task(dataset, source_lang[0], target_lang[0], num_samples, batch_size=batch_size, is_asr = True)

            print(f"\n=== {asr_results.get('dataset', 'Dataset')} | {source_lang[0]} ASR===")
            print(f"BLEU: {asr_results.get('metrics', {}).get('bleu', 'N/A')}")
            print(f"WER: {asr_results.get('metrics', {}).get('wer', 'N/A')}")
            print(f"CER: {asr_results.get('metrics', {}).get('cer', 'N/A')}")