Views
No views yet
1import numpy as np
2from transformers import AutoFeatureExtractor, AutoModelForAudioClassification
3
4# Some configurations
5model_id = 'yangwang825/mert-base'
6batch_size = 4
7num_classes = 10
8max_duration = 1.0
9
10# Initialise the extractor and model
11feature_extractor = AutoFeatureExtractor.from_pretrained(
12 model_id,
13 trust_remote_code=True
14)
15mert = AutoModelForAudioClassification.from_pretrained(
16 model_id,
17 num_labels=num_classes,
18 ignore_mismatched_sizes=True,
19 trust_remote_code=True
20)
21
22# Simulate a list of waveforms (e.g. four audio clips)
23audio_arrays = [
24 np.random.rand(16000, ),
25 np.random.rand(24000, ),
26 np.random.rand(22050, ),
27 np.random.rand(44100, )
28]
29inputs = feature_extractor(
30 audio_arrays, # List of waveforms in numpy array format
31 sampling_rate=feature_extractor.sampling_rate,
32 max_length=int(feature_extractor.sampling_rate * max_duration),
33 padding='max_length',
34 truncation=True,
35 return_tensors='pt'
36)
37# The shape of `input_values` is (batch_size, sample_rate * max_duration)
38input_values = inputs['input_values']
39outputs = mert(**inputs)
40# The shape of `logits` is (batch_size, num_classes)
41logits = outputs['logits']