readme: ---
  language: zh
  datasets:
  - common_voice
  metrics:
  - cer
  tags:
  - audio
  - automatic-speech-recognition
  - speech
  - xlsr-fine-tuning-week
  license: apache-2.0
  model-index:
  - name: XLSR Wav2Vec2 Large 53 - Chinese (zh-CN), by Yih-Dar SHIEH
    results:
    - task: 
        name: Speech Recognition
        type: automatic-speech-recognition
      dataset:
        name: Common Voice zh-CN
        type: common_voice
        args: zh-CN
      metrics:
         - name: Test CER
           type: cer
           value: 20.90
  ---
  
  # Wav2Vec2-Large-XLSR-53-Chinese-zh-cn-gpt
  
  Fine-tuned [facebook/wav2vec2-large-xlsr-53](https://huggingface.co/facebook/wav2vec2-large-xlsr-53) on Chinese (zh-CN) using the [Common Voice](https://huggingface.co/datasets/common_voice), included [Common Voice](https://huggingface.co/datasets/common_voice) Chinese (zh-TW) dataset (converting the label text to simplified Chinese). 
  When using this model, make sure that your speech input is sampled at 16kHz.
  
  ## Usage
  
  The model can be used directly (without a language model) as follows:
  
  ```python
  import torch
  import torchaudio
  from datasets import load_dataset
  from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
  
  test_dataset = load_dataset("common_voice", "zh-CN", split="test")
  
  processor = Wav2Vec2Processor.from_pretrained("ydshieh/wav2vec2-large-xlsr-53-chinese-zh-cn-gpt")
  model = Wav2Vec2ForCTC.from_pretrained("ydshieh/wav2vec2-large-xlsr-53-chinese-zh-cn-gpt")
  
  resampler = torchaudio.transforms.Resample(48_000, 16_000)
  
  # Preprocessing the datasets.
  # We need to read the aduio files as arrays
  def speech_file_to_array_fn(batch):
      speech_array, sampling_rate = torchaudio.load(batch["path"])
      batch["speech"] = resampler(speech_array).squeeze().numpy()
      return batch
  
  test_dataset = test_dataset.map(speech_file_to_array_fn)
  inputs = processor(test_dataset[:2]["speech"], sampling_rate=16_000, return_tensors="pt", padding=True)
  
  with torch.no_grad():
      logits = model(inputs.input_values, attention_mask=inputs.attention_mask).logits
  
  predicted_ids = torch.argmax(logits, dim=-1)
  
  print("Prediction:", processor.batch_decode(predicted_ids))
  print("Reference:", test_dataset[:2]["sentence"])
  ```
  
  
  ## Evaluation
  
  The model can be evaluated as follows on the zh-CN test data of Common Voice.
  Original CER calculation refer to https://huggingface.co/ctl/wav2vec2-large-xlsr-cantonese
  
  ```python
  #!pip install datasets==1.4.1
  #!pip install transformers==4.4.0
  #!pip install torchaudio
  #!pip install jiwer
  
  import torch
  import torchaudio
  from datasets import load_dataset, load_metric
  from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
  import re
  import jiwer
  
  def chunked_cer(targets, predictions, chunk_size=None):
  
      _predictions = [char for seq in predictions for char in list(seq)]
      _targets = [char for seq in targets for char in list(seq)]
      
      if chunk_size is None: return jiwer.wer(_targets, _predictions)
      
      start = 0
      end = chunk_size
      H, S, D, I = 0, 0, 0, 0
      
      while start < len(targets):
      
          _predictions = [char for seq in predictions[start:end] for char in list(seq)]
          _targets = [char for seq in targets[start:end] for char in list(seq)]
          chunk_metrics = jiwer.compute_measures(_targets, _predictions)
          H = H + chunk_metrics["hits"]
          S = S + chunk_metrics["substitutions"]
          D = D + chunk_metrics["deletions"]
          I = I + chunk_metrics["insertions"]
          start += chunk_size
          end += chunk_size
          
      return float(S + D + I) / float(H + S + D)
      
  test_dataset = load_dataset("common_voice", "zh-CN", split="test")
  
  processor = Wav2Vec2Processor.from_pretrained("ydshieh/wav2vec2-large-xlsr-53-chinese-zh-cn-gpt")
  model = Wav2Vec2ForCTC.from_pretrained("ydshieh/wav2vec2-large-xlsr-53-chinese-zh-cn-gpt")
  model.to("cuda")
  
  chars_to_ignore_regex = '[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\,\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\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