update actor5 and infer_folder_live
This commit is contained in:
1 parent
854609dd0c
commit
dac314c9a5
34 files changed
+2810
-658
No files matched your search
+30
-24
@@ -1,10 +1,12 @@
|
||||
import sys
|
||||
import sys,os
|
||||
import pprint
|
||||
import argparse
|
||||
import numpy as np
|
||||
from pathlib import Path
|
||||
import torch.nn.functional as F
|
||||
|
||||
from scipy.io import wavfile
|
||||
|
||||
import torch
|
||||
import torchaudio
|
||||
|
||||
@@ -12,6 +14,9 @@ from src.models import create_model
|
||||
from src.config import config as opt
|
||||
from src.config import update_config
|
||||
|
||||
import warnings
|
||||
warnings.filterwarnings("ignore")
|
||||
|
||||
from common.logger import Logger
|
||||
from transformers import Wav2Vec2FeatureExtractor, Wav2Vec2Model
|
||||
|
||||
@@ -34,31 +39,30 @@ def parese_args():
|
||||
def infer_one(path_audio, model, processor, extractor, save_npy):
|
||||
|
||||
# load raw speech
|
||||
audio_raw, sample_rate = torchaudio.load(path_audio)
|
||||
print(f"audio_raw shape = {audio_raw.shape}, sr = {sample_rate}, duration = {(len(audio_raw[0])/sample_rate)}")
|
||||
raw_speech, sample_rate = torchaudio.load(path_audio)
|
||||
raw_speech = raw_speech.numpy()
|
||||
print(f"raw_speech shape = {raw_speech.shape}, sr = {sample_rate}, duration = {(len(raw_speech[0])/sample_rate)}")
|
||||
|
||||
if len(audio_raw.shape) == 2:
|
||||
audio_raw = audio_raw[0]
|
||||
if len(raw_speech.shape) == 2:
|
||||
raw_speech = raw_speech[0]
|
||||
|
||||
if sample_rate != 16000:
|
||||
resampler = torchaudio.transforms.Resample(sample_rate, 16_000)
|
||||
audio_raw = resampler(audio_raw).squeeze().numpy()
|
||||
raw_speech = resampler(raw_speech).squeeze().numpy()
|
||||
sample_rate = 16000
|
||||
print(f"resampling: audio_raw shape = {audio_raw.shape}, sr = {16000}, duration = {(len(audio_raw)/16000)}")
|
||||
print(f"resampling: raw_speech shape = {raw_speech.shape}, sr = {16000}, duration = {(len(raw_speech)/16000)}")
|
||||
|
||||
# padding to 1s
|
||||
audio_raw, sample_rate = torchaudio.load(path_audio)
|
||||
# padding to
|
||||
len_align = (raw_speech.shape[0] // (16000 // 50) ) * (16000 // 50)
|
||||
raw_speech = raw_speech[: len_align]
|
||||
print(f"after raw_speech shape = {raw_speech.shape}, sr = {sample_rate}, duration = {(len(raw_speech)/sample_rate)}")
|
||||
|
||||
len_align = (audio_raw.shape[0] // 16000 ) * 16000
|
||||
audio_raw = audio_raw[: len_align]
|
||||
print(f"after audio_raw shape = {audio_raw.shape}, sr = {sample_rate}, duration = {(len(audio_raw)/sample_rate)}")
|
||||
|
||||
# padding half frame length for bed and end
|
||||
npad = int(0.5 * 16000* ( 1 / 50) + 0.5)
|
||||
audio_raw = np.pad(audio_raw, pad_width=((npad, npad)), mode="edge")
|
||||
print(f"after padding audio_raw = {audio_raw.shape}, dtype={audio_raw.dtype}")
|
||||
pad_speech = np.pad(raw_speech, pad_width=((npad, npad)), mode="edge")
|
||||
print(f"after padding pad_speech = {pad_speech.shape}, dtype={pad_speech.dtype}")
|
||||
|
||||
inputs = processor(audio_raw, sampling_rate=16000, return_tensors="pt")
|
||||
inputs = processor(pad_speech, sampling_rate=16000, return_tensors="pt")
|
||||
inputs = inputs.to(device)
|
||||
|
||||
with torch.no_grad():
|
||||
@@ -74,9 +78,14 @@ def infer_one(path_audio, model, processor, extractor, save_npy):
|
||||
print(f"after convert window cond shape = {cond.shape}")
|
||||
# self.data = self.data[:, 7:7+nseq, :]
|
||||
|
||||
suffix =f"{path_audio.stem}_infer_{Path(opt.MODEL.path_checkpoint).stem}_epoch_{opt.MODEL.load_epoch:03d}"
|
||||
model.infer(cond, path_audio, suffix, save_npy)
|
||||
|
||||
# save trim wav
|
||||
path_audio_pad_trim = Path(opt.OUTPUT_DIR).joinpath(f'{path_audio.stem}_input_pad_trim.wav')
|
||||
wavfile.write(str(path_audio_pad_trim), rate=16000, data=raw_speech)
|
||||
|
||||
# inference
|
||||
path_save = f"{path_audio.stem}_infer_{Path(opt.MODEL.path_checkpoint).stem}_epoch_{opt.MODEL.load_epoch:03d}"
|
||||
model.infer(cond, raw_speech, path_save)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
@@ -86,7 +95,7 @@ if __name__ == "__main__":
|
||||
logger = Logger(opt.OUTPUT_DIR, path_config=path_config, postfix=postfix)
|
||||
device = torch.device(f'cuda:{opt.GPUS[0]}') # get device name: CPU or GPU
|
||||
|
||||
logger.info(pprint.pformat(opt))
|
||||
# logger.info(pprint.pformat(opt))
|
||||
|
||||
opt.OUTPUT_DIR = logger.path_output
|
||||
|
||||
@@ -96,7 +105,6 @@ if __name__ == "__main__":
|
||||
model.setup(opt) # load model
|
||||
# model.remove_weightnorm()
|
||||
|
||||
|
||||
# load wav2Vec model
|
||||
print("load Wav2Vec2Model ...")
|
||||
MODEL_ID = "jonatasgrosman/wav2vec2-large-xlsr-53-chinese-zh-cn"
|
||||
@@ -105,10 +113,8 @@ if __name__ == "__main__":
|
||||
|
||||
# path_folder = Path("/mfs/dylan/tts/wav")
|
||||
# path_folder = Path("/mfs/dylan/tts/wav_sen")
|
||||
|
||||
path_folder = Path("./data/microsoft-tts")
|
||||
|
||||
opt.OUTPUT_DIR = path_folder.joinpath("inference_video_actor1_with_npy")
|
||||
path_folder = Path("./data/test/tts-scenes")
|
||||
opt.OUTPUT_DIR = path_folder.joinpath("inference_video_actor5_with_npy")
|
||||
opt.OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
|
||||
save_npy = True
|
||||
|
||||
|
||||
Reference in new issue
Block a user