Views
No views yet
1! pip install datasets
2
3# Load test dataset
4from datasets import load_dataset, Audio
5
6dataset = load_dataset("perrynelson/waxal-wolof", trust_remote_code=True)
7dataset
8
9# Display the first audio using Ipython
10from IPython.display import Audio, display
11
12Audio(dataset['train'][322]['audio']['array'], rate=16000)
13
14from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
15import torch
16
17model_id = "bilalfaye/wav2vec2-large-mms-1b-wolof"
18
19device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
20
21# Load the model on CPU first
22model = Wav2Vec2ForCTC.from_pretrained(model_id,
23 target_lang="wol",
24 torch_dtype=torch.float16 # Use half-precision
25 ).to(device)
26
27
28processor = Wav2Vec2Processor.from_pretrained(model_id)
29processor.tokenizer.set_target_lang("wol")
30
31
32# Process the audio
33input_dict = processor(
34 dataset['train'][322]["audio"]["array"],
35 sampling_rate=16_000,
36 return_tensors="pt",
37 padding=True
38)
39
40# Move inputs to the appropriate device for the first processing layer
41input_values = input_dict.input_values.to(device, dtype=torch.float16)
42
43# Perform inference
44logits = model(input_values).logits
45
46# Decode predictions
47pred_ids = torch.argmax(logits, dim=-1)[0]
48
49print("Prediction:")
50print(processor.decode(pred_ids))
51
52print("\nReference:")
53print(dataset['train'][322]['transcription'].lower())1from transformers import pipeline
2import torch
3
4# Model ID
5model_id = "bilalfaye/wav2vec2-large-mms-1b-wolof"
6
7# Determine device (use GPU if available, otherwise fallback to CPU)
8device = 0 if torch.cuda.is_available() else -1
9
10# Use half precision (float16) for inference if GPU is available
11torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
12
13# Set up the pipeline for automatic speech recognition
14pipe = pipeline(
15 task="automatic-speech-recognition",
16 model=model_id,
17 processor=model_id,
18 device=device, # Specify the device (GPU if available, otherwise CPU)
19 torch_dtype=torch_dtype, # Set the precision (float16 for half precision, float32 otherwise)
20 framework="pt" # Use PyTorch as the framework
21)
22
23# Input audio processing
24audio_array = dataset['train'][322]["audio"]["array"] # Fetching an audio sample
25
26# Run inference
27result = pipe(audio_array)
28
29# Prediction
30print("Prediction:")
31print(result['text'])
32
33# Reference (for comparison)
34print("\nReference:")
35print(dataset['train'][322]['transcription'].lower())1import gc
2import torch
3import psutil
4
5# Free up unused memory in CUDA (GPU) - only needed if you use a GPU
6if torch.cuda.is_available():
7 torch.cuda.empty_cache() # Clears GPU memory cache
8 torch.cuda.reset_peak_memory_stats() # Resets memory stats
9
10# Collect any unused memory in Python (CPU)
11gc.collect() # Collect unused memory in Python's garbage collector
12
13# Optionally, check memory status after clearing
14if torch.cuda.is_available():
15 print(f"GPU Memory Allocated: {torch.cuda.memory_allocated()} bytes")
16 print(f"GPU Memory Cached: {torch.cuda.memory_reserved()} bytes")
17else:
18 print(f"CPU Memory Usage: {psutil.virtual_memory().percent}%")| Training Loss | Epoch | Step | Validation Loss | Wer |
|---|---|---|---|---|
| 0.3793 | 14.0 | 12250 | 0.1517 | 0.1888 |
| 0.3709 | 15.0 | 13125 | 0.1512 | 0.1882 |
| 0.3702 | 16.0 | 14000 | 0.1499 | 0.1858 |
| 0.367 | 17.0 | 14875 | 0.1492 | 0.1848 |
| 0.3656 | 18.0 | 15750 | 0.1493 | 0.1842 |