Views
No views yet
1import torchaudio
2import torch
3import soundfile as sf
4import numpy as np
5from transformers import AutoModel
6
7model_id = "HTill/flexEAT-base_epoch30_pretrain"
8model = AutoModel.from_pretrained(model_id, trust_remote_code=True).eval().cuda()
9
10source_file = "/path/to/input.wav"
11target_file = "/path/to/output.npy"
12norm_mean = -4.268
13norm_std = 4.569
14
15# Load and resample audio
16wav, sr = sf.read(source_file)
17waveform = torch.tensor(wav).float().cuda()
18if sr != 16000:
19 waveform = torchaudio.functional.resample(waveform, sr, 16000)
20
21# Normalize and convert to mel-spectrogram
22waveform = waveform - waveform.mean()
23mel = torchaudio.compliance.kaldi.fbank(
24 waveform.unsqueeze(0),
25 htk_compat=True,
26 sample_frequency=16000,
27 use_energy=False,
28 window_type='hanning',
29 num_mel_bins=128,
30 dither=0.0,
31 frame_shift=10
32).unsqueeze(0)
33
34# Normalize
35mel = (mel - norm_mean) / (norm_std * 2)
36mel = mel.unsqueeze(0).cuda() # shape: [1, 1, T, F]
37
38# Extract features
39with torch.no_grad():
40 feat = model.extract_features(mel)
41
42feat = feat.squeeze(0).cpu().numpy()
43np.save(target_file, feat)
44print(f"Feature shape: {feat.shape}")
45print(f"Saved to: {target_file}")1@article{chen2024eat,
2 title={EAT: Self-supervised pre-training with efficient audio transformer},
3 author={Chen, Wenxi and Liang, Yuzhe and Ma, Ziyang and Zheng, Zhisheng and Chen, Xie},
4 journal={arXiv preprint arXiv:2401.03497},
5 year={2024}
6}