patrickvonplaten commited on
Commit
6f0b794
1 Parent(s): 87f7f02

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +9 -9
README.md CHANGED
@@ -31,13 +31,13 @@ The original model can be found under https://github.com/pytorch/fairseq/tree/ma
31
  To transcribe audio files the model can be used as a standalone acoustic model as follows:
32
 
33
  ```python
34
- from transformers import Wav2Vec2Tokenizer, Wav2Vec2ForCTC
35
  from datasets import load_dataset
36
  import soundfile as sf
37
  import torch
38
 
39
- # load model and tokenizer
40
- tokenizer = Wav2Vec2Tokenizer.from_pretrained("facebook/wav2vec2-large-960h-lv60-self")
41
  model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-large-960h-lv60-self")
42
 
43
  # define function to read in sound file
@@ -51,14 +51,14 @@ To transcribe audio files the model can be used as a standalone acoustic model a
51
  ds = ds.map(map_to_array)
52
 
53
  # tokenize
54
- input_values = tokenizer(ds["speech"][:2], return_tensors="pt", padding="longest").input_values # Batch size 1
55
 
56
  # retrieve logits
57
  logits = model(input_values).logits
58
 
59
  # take argmax and decode
60
  predicted_ids = torch.argmax(logits, dim=-1)
61
- transcription = tokenizer.batch_decode(predicted_ids)
62
  ```
63
 
64
  ## Evaluation
@@ -67,7 +67,7 @@ To transcribe audio files the model can be used as a standalone acoustic model a
67
 
68
  ```python
69
  from datasets import load_dataset
70
- from transformers import Wav2Vec2ForCTC, Wav2Vec2Tokenizer
71
  import soundfile as sf
72
  import torch
73
  from jiwer import wer
@@ -76,7 +76,7 @@ from jiwer import wer
76
  librispeech_eval = load_dataset("librispeech_asr", "clean", split="test")
77
 
78
  model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-large-960h-lv60-self").to("cuda")
79
- tokenizer = Wav2Vec2Tokenizer.from_pretrained("facebook/wav2vec2-large-960h-lv60-self")
80
 
81
  def map_to_array(batch):
82
  speech, _ = sf.read(batch["file"])
@@ -86,7 +86,7 @@ def map_to_array(batch):
86
  librispeech_eval = librispeech_eval.map(map_to_array)
87
 
88
  def map_to_pred(batch):
89
- inputs = tokenizer(batch["speech"], return_tensors="pt", padding="longest")
90
  input_values = inputs.input_values.to("cuda")
91
  attention_mask = inputs.attention_mask.to("cuda")
92
 
@@ -94,7 +94,7 @@ def map_to_pred(batch):
94
  logits = model(input_values, attention_mask=attention_mask).logits
95
 
96
  predicted_ids = torch.argmax(logits, dim=-1)
97
- transcription = tokenizer.batch_decode(predicted_ids)
98
  batch["transcription"] = transcription
99
  return batch
100
 
31
  To transcribe audio files the model can be used as a standalone acoustic model as follows:
32
 
33
  ```python
34
+ from transformers import Wav2Vec2Processor, Wav2Vec2ForCTC
35
  from datasets import load_dataset
36
  import soundfile as sf
37
  import torch
38
 
39
+ # load model and processor
40
+ processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-large-960h-lv60-self")
41
  model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-large-960h-lv60-self")
42
 
43
  # define function to read in sound file
51
  ds = ds.map(map_to_array)
52
 
53
  # tokenize
54
+ input_values = processor(ds["speech"][:2], return_tensors="pt", padding="longest").input_values # Batch size 1
55
 
56
  # retrieve logits
57
  logits = model(input_values).logits
58
 
59
  # take argmax and decode
60
  predicted_ids = torch.argmax(logits, dim=-1)
61
+ transcription = processor.batch_decode(predicted_ids)
62
  ```
63
 
64
  ## Evaluation
67
 
68
  ```python
69
  from datasets import load_dataset
70
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
71
  import soundfile as sf
72
  import torch
73
  from jiwer import wer
76
  librispeech_eval = load_dataset("librispeech_asr", "clean", split="test")
77
 
78
  model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-large-960h-lv60-self").to("cuda")
79
+ processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-large-960h-lv60-self")
80
 
81
  def map_to_array(batch):
82
  speech, _ = sf.read(batch["file"])
86
  librispeech_eval = librispeech_eval.map(map_to_array)
87
 
88
  def map_to_pred(batch):
89
+ inputs = processor(batch["speech"], return_tensors="pt", padding="longest")
90
  input_values = inputs.input_values.to("cuda")
91
  attention_mask = inputs.attention_mask.to("cuda")
92
 
94
  logits = model(input_values, attention_mask=attention_mask).logits
95
 
96
  predicted_ids = torch.argmax(logits, dim=-1)
97
+ transcription = processor.batch_decode(predicted_ids)
98
  batch["transcription"] = transcription
99
  return batch
100