Create AniTalker/extract_audio_features.py
Browse files
AniTalker/extract_audio_features.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import os
|
| 3 |
+
import numpy as np
|
| 4 |
+
import librosa
|
| 5 |
+
import torch
|
| 6 |
+
from tqdm import tqdm
|
| 7 |
+
from transformers import Wav2Vec2FeatureExtractor, HubertModel
|
| 8 |
+
|
| 9 |
+
def main(args):
|
| 10 |
+
if not torch.cuda.is_available() and args.computed_device == 'cuda':
|
| 11 |
+
print('CUDA is not available on this device. Switching to CPU.')
|
| 12 |
+
args.computed_device = 'cpu'
|
| 13 |
+
|
| 14 |
+
device = torch.device(args.computed_device)
|
| 15 |
+
model = HubertModel.from_pretrained(args.model_path).to(device)
|
| 16 |
+
feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(args.model_path)
|
| 17 |
+
model.feature_extractor._freeze_parameters()
|
| 18 |
+
model.eval()
|
| 19 |
+
|
| 20 |
+
os.makedirs(args.audio_feature_saved_path, exist_ok=True)
|
| 21 |
+
|
| 22 |
+
for wavfile in tqdm(os.listdir(args.audio_dir_path)):
|
| 23 |
+
npy_save_path = os.path.join(args.audio_feature_saved_path, os.path.splitext(os.path.basename(wavfile))[0] + '.npy')
|
| 24 |
+
|
| 25 |
+
if os.path.exists(npy_save_path):
|
| 26 |
+
continue
|
| 27 |
+
|
| 28 |
+
audio, sr = librosa.load(os.path.join(args.audio_dir_path, wavfile), sr=16000)
|
| 29 |
+
input_values = feature_extractor(audio, sampling_rate=16000, padding=True, do_normalize=True, return_tensors="pt").input_values
|
| 30 |
+
input_values = input_values.to(device)
|
| 31 |
+
ws_feats = []
|
| 32 |
+
with torch.no_grad():
|
| 33 |
+
outputs = model(input_values, output_hidden_states=True)
|
| 34 |
+
for i in range(len(outputs.hidden_states)):
|
| 35 |
+
ws_feats.append(outputs.hidden_states[i].detach().cpu().numpy())
|
| 36 |
+
ws_feat_obj = np.array(ws_feats)
|
| 37 |
+
ws_feat_obj = np.squeeze(ws_feat_obj, 1)
|
| 38 |
+
|
| 39 |
+
if args.padding_to_align_audio:
|
| 40 |
+
ws_feat_obj = np.pad(ws_feat_obj, ((0, 0), (0, 1), (0, 0)), 'edge')
|
| 41 |
+
np.save(npy_save_path, ws_feat_obj)
|
| 42 |
+
|
| 43 |
+
if __name__ == "__main__":
|
| 44 |
+
parser = argparse.ArgumentParser(description="Extract audio features using a pre-trained HuBERT model.")
|
| 45 |
+
parser.add_argument("--model_path", type=str, default='weights/chinese-hubert-large', help="Path to the pre-trained model weights.")
|
| 46 |
+
parser.add_argument("--audio_dir_path", type=str, default='./audio_samples/raw_audios/', help="Directory containing raw audio files.")
|
| 47 |
+
parser.add_argument("--audio_feature_saved_path", type=str, default='./audio_samples/audio_features/', help="Directory where extracted audio features will be saved.")
|
| 48 |
+
parser.add_argument("--computed_device", type=str, default='cuda', choices=['cuda', 'cpu'], help="Device to compute the audio features on. Use 'cuda' for GPU or 'cpu' for CPU.")
|
| 49 |
+
parser.add_argument("--padding_to_align_audio", type=bool, default=True, help="Whether to pad the audio to align features.")
|
| 50 |
+
args = parser.parse_args()
|
| 51 |
+
main(args)
|