132 lines
5.2 KiB
Python
132 lines
5.2 KiB
Python
import sys
|
|
import pprint
|
|
import argparse
|
|
import numpy as np
|
|
from pathlib import Path
|
|
import torch.nn.functional as F
|
|
|
|
import torch
|
|
import torchaudio
|
|
|
|
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
|
|
|
|
torch.autograd.set_detect_anomaly(True)
|
|
|
|
|
|
def parese_args():
|
|
pareser = argparse.ArgumentParser(description='Audio2Expression Network')
|
|
|
|
# general
|
|
pareser.add_argument("--opt", type=str, default="experiments/xxx.yaml", help="experiment configure file name")
|
|
args, _ = pareser.parse_known_args()
|
|
update_config(args.opt)
|
|
|
|
pareser.add_argument("--gpus", type=str, default="0", help="gpus")
|
|
args = pareser.parse_args()
|
|
|
|
return args
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
args = parese_args()
|
|
path_config = Path(args.opt)
|
|
postfix = opt.TRAIN.resume_suffix if opt.TRAIN.resume else None # if resume, resume to resume_suffix folder
|
|
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))
|
|
opt.OUTPUT_DIR = logger.path_output
|
|
|
|
# path_audio = Path("/yrfs/lsen/DATA/LuoXData/processing2/2021-P09/audio/audio_trim_60_120s.wav")
|
|
|
|
path_audio = Path("/yrfs/lsen/DATA/Actor2/processing/AA7A9568/audio/audio_trim_120s.wav")
|
|
|
|
# path_audio = Path("/yrfs/lsen/DATA/Actor1/processing/2-23min/audio/audio_trim_120_120s.wav")
|
|
# path_audio = Path("data/test/outputs20220319.wav")
|
|
# path_audio = Path("./data/47e6d985dc11469ab7f6f47cc0b5e625_trim.wav")
|
|
|
|
# path_audio = Path("./data/test/cut_NVP.wav")
|
|
# path_audio = Path("./data/test/cut_LSP.wav")
|
|
# path_audio = Path("/yrfs/cf/data/render/obama_train.wav")
|
|
# path_audio = Path("/yrfs/lsen/DATA/Obama/processing/Bringing-Our-Workplace-Policies-into-the-21st-Century/audio/audio_trim_120s.wav")
|
|
# path_audio = Path("/yrfs/lsen/DATA/Obama/processing/Celebrating-Independence-Day/audio/audio_trim_60_120s.wav")
|
|
|
|
# wav2vec 2.0
|
|
|
|
# path_audio = Path("/yrfs/lsen/DATA/LuoXData/processing2/2021-P09/audio/audio_trim_120s.wav")
|
|
# path_audio = Path("/yrfs/lsen/DATA/LuoXData/processing2/2021-P22/audio/audio_trim.wav")
|
|
|
|
# path_audio = Path("/ssdata1/lsen/CODE/Audio2Motion/data/test/test_pangzi_zh_cut15.wav")
|
|
# path_audio = Path("/ssdata1/lsen/CODE/Audio2Motion/data/test/P74_lite.mp3")
|
|
|
|
# path_audio = Path("/ssdata1/lsen/CODE/voca/data/audio/test_video_15.wav")
|
|
# path_audio = Path("/ssdata1/lsen/CODE/voca/data/audio/cut_lsz.wav")
|
|
# path_audio = Path("/ssdata1/lsen/CODE/voca/data/audio/obama.wav")
|
|
|
|
# data extract audio
|
|
# Load data_audio and extract features
|
|
|
|
# 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)}")
|
|
|
|
if len(audio_raw.shape) == 2:
|
|
audio_raw = audio_raw[0]
|
|
|
|
if sample_rate != 16000:
|
|
resampler = torchaudio.transforms.Resample(sample_rate, 16_000)
|
|
audio_raw = resampler(audio_raw).squeeze().numpy()
|
|
print(f"resampling: audio_raw shape = {audio_raw.shape}, sr = {16000}, duration = {(len(audio_raw)/16000)}")
|
|
|
|
# padding to
|
|
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)}")
|
|
|
|
# load wav2Vec model
|
|
print("load Wav2Vec2Model ...")
|
|
MODEL_ID = "jonatasgrosman/wav2vec2-large-xlsr-53-chinese-zh-cn"
|
|
# MODEL_ID_PATH = "./data/wav2vec2-large-xlsr-53-chinese-zh-cn.json"
|
|
processor = Wav2Vec2FeatureExtractor.from_pretrained(MODEL_ID, cache_dir="/home/liangsen/.cache/huggingface/transformers")
|
|
extractor = Wav2Vec2Model.from_pretrained(pretrained_model_name_or_path=MODEL_ID, cache_dir="/home/liangsen/.cache/huggingface/transformers").to(device)
|
|
|
|
# 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}")
|
|
|
|
inputs = processor(audio_raw, sampling_rate=16000, return_tensors="pt")
|
|
inputs = inputs.to(device)
|
|
|
|
with torch.no_grad():
|
|
output = extractor(**inputs)
|
|
cond = output.last_hidden_state
|
|
print(f"cond shape = {cond.shape}")
|
|
|
|
# convert to windows
|
|
cond = F.pad(cond, pad=(0, 0, 8, 7), mode="replicate")
|
|
|
|
cond = cond.unfold(dimension=1, size=16, step=1) # (B, nseqs, nc_motion, 16)
|
|
cond = cond.permute(0, 1, 3, 2) # (B, nseqs, 16, 53)
|
|
nseq = cond.size(1)
|
|
print(f"after convert window cond shape = {cond.shape}")
|
|
# self.data = self.data[:, 7:7+nseq, :]
|
|
|
|
# get model
|
|
model = create_model(opt, logger)
|
|
model.setup(opt) # load model
|
|
# model.remove_weightnorm()
|
|
|
|
model.infer(cond, path_audio, \
|
|
f"{path_audio.stem}_infer_{Path(opt.MODEL.path_checkpoint).stem}_epoch_{opt.MODEL.load_epoch:03d}")
|