|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import argparse |
|
|
import logging |
|
|
import torch |
|
|
from tqdm import tqdm |
|
|
import numpy as np |
|
|
import json |
|
|
|
|
|
import torch.distributed as distr |
|
|
import pathlib |
|
|
from distributed import init_distributed_context |
|
|
|
|
|
import logging |
|
|
logger = logging.getLogger(__name__) |
|
|
import os |
|
|
import sys |
|
|
import re |
|
|
import glob |
|
|
|
|
|
sys.path.insert(0,'/apdcephfs_nj7/share_303172353/ggyzhang/projects/textlesslib') |
|
|
|
|
|
import torchaudio |
|
|
from textless.data.speech_encoder import SpeechEncoder |
|
|
import soundfile |
|
|
import torchaudio.transforms as T |
|
|
|
|
|
def resample_wav(wav_data,sr,target_sr): |
|
|
if sr!=target_sr: |
|
|
resampler = T.Resample(orig_freq=sr, new_freq=target_sr) |
|
|
wav_data = resampler(wav_data) |
|
|
return wav_data |
|
|
|
|
|
|
|
|
def single_job(encoder, wav_fp, save_fp, device,sample_rate=16000): |
|
|
item_name = os.path.basename(wav_fp).split('.')[0] |
|
|
data, sr = torchaudio.load(wav_fp) |
|
|
data = resample_wav(data,sr,target_sr=sample_rate) |
|
|
encoded = encoder(data.to(device)) |
|
|
units = encoded["units"].detach().cpu().numpy() |
|
|
np.save(save_fp,units) |
|
|
|
|
|
|
|
|
def extract_speech_token(args, rank, world_size): |
|
|
all_data = [] |
|
|
test_fp = '/apdcephfs_nj7/share_303172353/ggyzhang/projects/v2s/data/lrs3/test_data.json' |
|
|
train_fp = '/apdcephfs_nj7/share_303172353/ggyzhang/projects/v2s/data/lrs3/train_data.json' |
|
|
valid_fp = '/apdcephfs_nj7/share_303172353/ggyzhang/projects/v2s/data/lrs3/valid_data_ori.json' |
|
|
with open(train_fp,'r') as fp: |
|
|
cur_data = json.load(fp) |
|
|
all_data.extend(cur_data) |
|
|
with open(valid_fp,'r') as fp: |
|
|
cur_data = json.load(fp) |
|
|
all_data.extend(cur_data) |
|
|
with open(test_fp,'r') as fp: |
|
|
cur_data = json.load(fp) |
|
|
all_data.extend(cur_data) |
|
|
wavs = [item['wav_fn'] for item in all_data] |
|
|
|
|
|
|
|
|
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu") |
|
|
dense_model_name = "hubert-base-ls960-layer-9" |
|
|
quantizer_name, vocab_size = "kmeans", 500 |
|
|
|
|
|
encoder = SpeechEncoder.by_name( |
|
|
dense_model_name=dense_model_name, |
|
|
quantizer_model_name=quantizer_name, |
|
|
vocab_size=vocab_size, |
|
|
deduplicate=False, |
|
|
dense_model_fp = '/apdcephfs_nj7/share_303172353/ggyzhang/projects/textlesslib/ckpts/hubert_base_ls960.pt', |
|
|
quantizer_model_fp='/apdcephfs_nj7/share_303172353/ggyzhang/projects/textlesslib/ckpts/hubert_base_ls960_L9_km500.bin' |
|
|
).to(device) |
|
|
|
|
|
print(len(wavs)) |
|
|
for i in tqdm(range(rank, len(wavs), world_size)): |
|
|
wav_fp = wavs[i] |
|
|
item_name = os.path.basename(wav_fp).split('.')[0] |
|
|
new_fp = os.path.dirname(wav_fp).replace('LRS3','LRS3_hubert_token') |
|
|
os.makedirs(new_fp,exist_ok=True) |
|
|
save_path = f'{new_fp}/{item_name}.npy' |
|
|
if os.path.exists(save_path): |
|
|
continue |
|
|
try: |
|
|
single_job(encoder,wav_fp,f'{new_fp}/{item_name}.npy',device) |
|
|
except: |
|
|
print('error!!!!!!!!',wav_fp) |
|
|
|
|
|
def main(args): |
|
|
context = init_distributed_context(args.distributed_port) |
|
|
logger.info(f"Distributed context {context}") |
|
|
|
|
|
n_gpus = torch.cuda.device_count() |
|
|
with torch.cuda.device(context.local_rank % n_gpus): |
|
|
extract_speech_token(args, context.rank, context.world_size) |
|
|
|
|
|
if context.world_size > 1: |
|
|
distr.barrier() |
|
|
|
|
|
if __name__ == "__main__": |
|
|
parser = argparse.ArgumentParser() |
|
|
parser.add_argument("--distributed_port", type=int, default=58564) |
|
|
args = parser.parse_args() |
|
|
|
|
|
main(args) |