Views
No views yet
1import torch
2import librosa
3from transformers import AutoFeatureExtractor, AutoModelForAudioClassification
4
5model_id = "airasoul/wav2vec2-base-drum-kit" # e.g. username/wav2vec2-base-drum-kit
6feature_extractor = AutoFeatureExtractor.from_pretrained(model_id)
7model = AutoModelForAudioClassification.from_pretrained(model_id)
8model.eval()
9
10# Load a WAV (16 kHz mono)
11audio, sr = librosa.load("path/to/audio.wav", sr=16000, mono=True)
12inputs = feature_extractor(
13 audio, sampling_rate=16000, max_length=48000, # 3 s at 16 kHz
14 truncation=True, return_tensors="pt", padding=True
15)
16with torch.no_grad():
17 logits = model(**inputs).logits
18pred_id = logits.argmax(dim=-1).item()
19label = model.config.id2label.get(pred_id) or model.config.id2label.get(str(pred_id))
20print(label) # e.g. "kick"| Class | Precision | Recall | F1-score |
|---|---|---|---|
| clap | 1.00 | 1.00 | 1.00 |
| conga | 0.96 | 0.93 | 0.95 |
| crash | 0.97 | 0.97 | 0.97 |
| cymbal | 1.00 | 0.91 | 0.95 |
| hat | 1.00 | 0.97 | 0.98 |
| kick | 1.00 | 0.94 | 0.97 |
| ride | 0.94 | 1.00 | 0.97 |
| rim | 1.00 | 1.00 | 1.00 |
| snare | 0.89 | 0.96 | 0.93 |
| tom | 0.92 | 1.00 | 0.96 |