Views
No views yet
pip install git+https://github.com/topel/audioset-convnext-inf@pip-install1import os
2import numpy as np
3import torch
4from torch.nn import functional as TF
5import torchaudio
6import torchaudio.functional as TAF
7
8from audioset_convnext_inf.pytorch.convnext import ConvNeXt
9from audioset_convnext_inf.utils.utilities import read_audioset_label_tags
10
11model = ConvNeXt.from_pretrained("topel/ConvNeXt-Tiny-AT", map_location='cpu')
12
13print(
14 "# params:",
15 sum(param.numel() for param in model.parameters() if param.requires_grad),
16)
17if torch.cuda.is_available():
18 device = torch.device("cuda")
19else:
20 device = torch.device("cpu")
21
22if "cuda" in str(device):
23 model = model.to(device)# params: 28222767254906__tpellegrini__cavaco1.wav and class_labels_indices.csv from this repository.1sample_rate = 32000
2audio_target_length = 10 * sample_rate # 10 s
3
4# AUDIO_FNAME = "f62-S-v2swA_200000_210000.wav"
5AUDIO_FNAME = "254906__tpellegrini__cavaco1.wav"
6
7current_dir=os.getcwd()
8AUDIO_FPATH = os.path.join(current_dir, AUDIO_FNAME)
9
10waveform, sample_rate_ = torchaudio.load(AUDIO_FPATH)
11if sample_rate_ != sample_rate:
12 print("Resampling from %d to 32000 Hz"%sample_rate_)
13 waveform = TAF.resample(
14 waveform,
15 sample_rate_,
16 sample_rate,
17 )
18
19if waveform.shape[-1] < audio_target_length:
20 print("Padding waveform")
21 missing = max(audio_target_length - waveform.shape[-1], 0)
22 waveform = TF.pad(waveform, (0,missing), mode="constant", value=0.0)
23elif waveform.shape[-1] > audio_target_length:
24 print("Cropping waveform")
25 waveform = waveform[:, :audio_target_length]
26
27waveform = waveform.contiguous()
28waveform = waveform.to(device)
29
30print("\nInference on " + AUDIO_FNAME + "\n")
31
32with torch.no_grad():
33 model.eval()
34 output = model(waveform)
35
36logits = output["clipwise_logits"]
37print("logits size:", logits.size())
38
39probs = output["clipwise_output"]
40# Equivalent: probs = torch.sigmoid(logits)
41print("probs size:", probs.size())
42
43lb_to_ix, ix_to_lb, id_to_ix, ix_to_id = read_audioset_label_tags(os.path.join(current_dir, "class_labels_indices.csv"))
44
45threshold = 0.25
46sample_labels = np.where(probs[0].clone().detach().cpu() > threshold)[0]
47print("\nPredicted labels using activity threshold 0.25:\n")
48# print(sample_labels)
49for l in sample_labels:
50 print("%s: %.3f"%(ix_to_lb[l], probs[0,l]))Inference on 254906__tpellegrini__cavaco1.wav
Resampling rate from 44100 to 32000 Hz
Padding waveform
logits size: torch.Size([1, 527])
probs size: torch.Size([1, 527])
Predicted labels using activity threshold 0.25:
[137 138 139 140 149 151]
Music: 0.896
Musical instrument: 0.686
Plucked string instrument: 0.608
Guitar: 0.369
Mandolin: 0.710
Ukulele: 0.2681with torch.no_grad():
2 model.eval()
3 output = model.forward_scene_embeddings(waveform)
4
5print("\nScene embedding, shape:", output.size())Scene embedding, shape: torch.Size([1, 768])1with torch.no_grad():
2 model.eval()
3 output = model.forward_frame_embeddings(waveform)
4
5print("\nFrame-level embeddings, shape:", output.size())Frame-level embeddings, shape: torch.Size([1, 768, 31, 7])1@inproceedings{pellegrini23_interspeech,
2 author={Thomas Pellegrini and Ismail Khalfaoui-Hassani and Etienne Labb\'e and Timoth\'ee Masquelier},
3 title={{Adapting a ConvNeXt Model to Audio Classification on AudioSet}},
4 year=2023,
5 booktitle={Proc. INTERSPEECH 2023},
6 pages={4169--4173},
7 doi={10.21437/Interspeech.2023-1564}
8}