Files
audio2motion/infer.py
T
2022-01-21 17:42:41 +08:00

114 lines
3.9 KiB
Python

import time
import sys
import pprint
import shutil
import argparse
from pathlib import Path
import torch
from torch.utils.data import DataLoader
from src.datasets import create_dataset
from src.models import create_model, calculate_model_params
from src.config import config as opt
from src.config import update_config
from common.vis.visualizer import Visualizer
from common.logger import Logger
from common.animation import save_animation_grid
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)
logger.info(pprint.pformat(opt))
opt.OUTPUT_DIR = logger.path_output
# load motion
test_dataset = create_dataset(opt, phase='test')
test_loader = DataLoader(test_dataset,
batch_size=opt.TEST.batch_size * len(opt.GPUS),
shuffle=True, # NOTE: usually set False
num_workers=opt.WORKERS,
pin_memory=False)
# logger.info(f"Test dataset size = {len(test_dataset)}, batch = {len(test_loader)}")
device = torch.device(f'cuda:{opt.GPUS[0]}') # get device name: CPU or GPU
# get model
model = create_model(opt, logger)
model.setup(opt) # load model
# model.remove_weightnorm()
# Add for WaveFlow
# model.netG.module = model.netG.module.remove_weightnorm(model.netG.module)
for i, batch_input in enumerate(test_loader):
motion = batch_input['data'].float().to(device) # (nbatch, nsteps=60, dim_motion=136)
speech = batch_input["cond"].float().to(device) # (nbatch, nsteps=173, dim_condit=80)
motion = motion.permute(0, 2, 1) # (nbatch, 136, 60)
speech = speech.permute(0, 2, 1) # (nbatch, 80, 173)
B = motion.size(0)
motion_gt = motion.permute(0,2,1)[0:1, :, :]
# generate k samples
motion_pt = []
for j in range(8):
data_pt = model.testing(batch_input, cur_epoch=opt.MODEL.load_epoch, save_animation=False) # B x nsteps x 136
data_pt = data_pt[0:1, :, :]
motion_pt.append(data_pt)
motion_pt = torch.cat(motion_pt, dim=0) # 8 x nsteps x 136
nsteps = min(motion_gt.size(1), motion_pt.size(1))
total_motion = torch.cat([motion_gt[:, :nsteps, :], motion_pt[:, :nsteps, :]], dim=0)
print(f"total motion shape = {total_motion.shape}")
path_save=Path(opt.OUTPUT_DIR).joinpath(f'sampled_{opt.MODEL.load_epoch:05d}_{i:03d}.mp4')
save_animation_grid(total_motion, path_save=path_save, ncol=3, w=256, h=256, dpi=100)
print(f"SVAE -> {opt.OUTPUT_DIR}")
# break
# # infer
# epoch_beg = 400
# epoch_end = 580
# for epoch in range(epoch_end, epoch_beg, -50):
# print(f"Testing the epoch{epoch}/{epoch_end}")
# opt.MODEL.load_epoch = epoch # set load epoch
# model.setup(opt) # load model
# for i, ibatch_data in enumerate(test_loader):
# model.testing(ibatch_data, cur_epoch=epoch, cur_batch=i)
# model.testing(ibatch_data, cur_epoch=epoch, cur_batch=(i*100+1))
# model.testing(ibatch_data, cur_epoch=epoch, cur_batch=(i*1000+1))
# break