File size: 5,613 Bytes
8ef091c
 
 
 
 
 
 
f707e83
8ef091c
198ffe3
 
 
 
8ef091c
 
 
 
 
198ffe3
8ef091c
f707e83
8ef091c
 
 
 
 
 
f707e83
8ef091c
 
 
198ffe3
 
 
8ef091c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a9df89
f707e83
 
198ffe3
f707e83
198ffe3
f707e83
 
 
 
198ffe3
 
 
 
f707e83
198ffe3
 
 
 
f707e83
198ffe3
fbd5927
198ffe3
f707e83
198ffe3
fbd5927
f707e83
fbd5927
 
f707e83
 
1a9df89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3f2922
1a9df89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef96ccf
1a9df89
 
 
 
f3f2922
 
1a9df89
 
f3f2922
1a9df89
 
 
 
 
 
 
f3f2922
1a9df89
 
f3f2922
1a9df89
 
 
f707e83
8ef091c
 
 
 
 
198ffe3
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
---
license: apache-2.0
tags:
- generated_from_trainer
metrics:
- wer
model-index:
- name: whisper-large-v2-arabic-5k-steps
  results: []
datasets:
- mozilla-foundation/common_voice_11_0
language:
- ar
---

<!-- This model card has been generated automatically according to the information the Trainer had access to. You
should probably proofread and complete it, then remove this comment. -->

# whisper-large-v2-arabic-5k-steps

This model is a fine-tuned version of [openai/whisper-large-v2](https://huggingface.co/openai/whisper-large-v2) on the Arabic CommonVoice dataset (v11).
It achieves the following results on the evaluation set:
- Loss: 0.3434
- Wer: 0.4239

## Model description

This model is finetuned for 5000 steps for research purposes which means that the transcriptions might not be that satisfactory for users.

## Training and evaluation data

- Training Data: CommonVoice (v11) train split
- Validation Data: CommonVoice (v11) Validation split
- Test Data: CommonVoice (v11) Test split

## Training procedure

### Training hyperparameters

The following hyperparameters were used during training:
- learning_rate: 1e-05
- train_batch_size: 50
- eval_batch_size: 16
- seed: 42
- optimizer: Adam with betas=(0.9,0.999) and epsilon=1e-08
- lr_scheduler_type: linear
- lr_scheduler_warmup_steps: 500
- training_steps: 5000
- mixed_precision_training: Native AMP

### Training results

| Training Loss | Epoch | Step | Validation Loss | Wer    |
|:-------------:|:-----:|:----:|:---------------:|:------:|
| 0.1638        | 1.78  | 1000 | 0.2295          | 0.4410 |
| 0.0587        | 3.57  | 2000 | 0.2337          | 0.4272 |
| 0.0125        | 5.35  | 3000 | 0.2745          | 0.4208 |
| 0.004         | 7.13  | 4000 | 0.3124          | 0.4252 |
| 0.0016        | 8.91  | 5000 | 0.3434          | 0.4239 |

### Transcription:

```python
from datasets import load_dataset, Audio
import torch
from transformers import WhisperProcessor, WhisperForConditionalGeneration

# device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# load the model
processor = WhisperProcessor.from_pretrained("clu-ling/whisper-large-v2-arabic-5k-steps")
model = WhisperForConditionalGeneration.from_pretrained("clu-ling/whisper-large-v2-arabic-5k-steps").to(device)
forced_decoder_ids = processor.get_decoder_prompt_ids(language="ar", task="transcribe")

# load the dataset
commonvoice_eval = load_dataset("mozilla-foundation/common_voice_11_0", "ar", split="validation", streaming=True)
commonvoice_eval = commonvoice_eval.cast_column("audio", Audio(sampling_rate=16000))
sample = next(iter(commonvoice_eval))["audio"]

# features and generate token ids
input_features = processor(sample["array"], sampling_rate=sample["sampling_rate"], return_tensors="pt").input_features
predicted_ids = model.generate(input_features.to(device), forced_decoder_ids=forced_decoder_ids)

# decode
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]

print("Transcription:", transcription)
Transcription: عمي هو أخو أبي.
```

### Evaluation:

Evaluates this model on `mozilla-foundation/common_voice_11_0` test split.

```python
import pyarabic.araby as araby
from transformers.models.whisper.english_normalizer import BasicTextNormalizer
from datasets import load_dataset, Audio
import evaluate
import torch
import re
from transformers import WhisperProcessor, WhisperForConditionalGeneration

# device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# metric
wer_metric = evaluate.load("wer")

# model
processor = WhisperProcessor.from_pretrained("clu-ling/whisper-large-v2-arabic-5k-steps")
model = WhisperForConditionalGeneration.from_pretrained("clu-ling/whisper-large-v2-arabic-5k-steps")

# dataset
dataset = load_dataset("mozilla-foundation/common_voice_11_0", "ar", split="test", ) #cache_dir=args.cache_dir
dataset = dataset.cast_column("audio", Audio(sampling_rate=16000))

#for debuggings: it gets two examples
#dataset = dataset.shard(num_shards=10000, index=0)
#print(dataset)

def clean_text(text):
  """Normalizes TRANSCRIPT"""
  text = re.sub(r'[\,\?\.\!\-\;\:\"\“\%\٪\‘\”\�\«\»\،\.\:\؟\؛\*\>\<]', '', text) + " " # special characters
  text = re.sub(r'http\S+', '', text) + " " # links
  text = re.sub(r'[\[\]\(\)\-\/\{\}]', '', text) + " " # brackets
  text = re.sub(r'\s+', ' ', text) + " " # extra white space
  text = araby.strip_diacritics(text) # remove diacrirics
  return text.strip()
    
def normalize(batch):
  """Normalizes GOLD"""
  #batch["gold_text"] = whisper_norm(batch['sentence'])
  batch["gold_text"] = clean_text(batch['sentence'])
  return batch

def map_wer(batch):
  model.to(device)
  forced_decoder_ids = processor.get_decoder_prompt_ids(language = "ar", task = "transcribe")
  inputs = processor(batch["audio"]["array"], sampling_rate=batch["audio"]["sampling_rate"], return_tensors="pt").input_features
  with torch.no_grad():
    generated_ids = model.generate(inputs=inputs.to(device), forced_decoder_ids=forced_decoder_ids)
    transcription = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
  batch["predicted_text"] = clean_text(transcription)
  return batch

# process GOLD text
processed_dataset = dataset.map(normalize)
# get predictions
predicted = processed_dataset.map(map_wer)

# word error rate
wer = wer_metric.compute(references=predicted['gold_text'], predictions=predicted['predicted_text'])
wer = round(100 * wer, 2)
print("WER:", wer)
```

### Framework versions

- Transformers 4.26.0.dev0
- Pytorch 1.13.1
- Datasets 2.8.1.dev0
- Tokenizers 0.13.2