add infer support for audio2motion
This commit is contained in:
1 parent
0b46d9f3eb
commit
23fdd42edd
23 files changed
+824
-74
No files matched your search
@@ -9,11 +9,11 @@ import numpy as np
|
||||
|
||||
import onnx
|
||||
import onnxruntime as rt
|
||||
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
||||
|
||||
from src.config import config as opt
|
||||
from src.config import update_config
|
||||
from src.models import create_model
|
||||
|
||||
from common.logger import Logger
|
||||
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
# Copyright (c) OpenMMLab. All rights reserved.
|
||||
import os
|
||||
import sys
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
|
||||
import torch
|
||||
import numpy as np
|
||||
|
||||
import onnx
|
||||
import onnxruntime as rt
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
||||
|
||||
from src.config import config as opt
|
||||
from src.config import update_config
|
||||
from src.models import create_model
|
||||
|
||||
from common.logger import Logger
|
||||
|
||||
def parse_args():
|
||||
parser = argparse.ArgumentParser(description='Convert DigitalHuman models to ONNX')
|
||||
|
||||
parser.add_argument("--opt", type=str, default="experiments/infer_audio2mesh_CN_20220312.yaml", help="experiment configure file name")
|
||||
args, _ = parser.parse_known_args()
|
||||
update_config(args.opt)
|
||||
|
||||
# parser.add_argument('checkpoint', help='checkpoint file')
|
||||
# parser.add_argument('--show', action='store_true', help='show onnx graph')
|
||||
# parser.add_argument('--output-file', type=str, default='tmp.onnx')
|
||||
# parser.add_argument('--opset-version', type=int, default=11)
|
||||
# parser.add_argument('--verify', action='store_true', help='verify the onnx model output against pytorch output')
|
||||
# parser.add_argument('--is-localizer', action='store_true', help='whether it is a localizer')
|
||||
# parser.add_argument('--shape', type=int, nargs='+', default=[1, 3, 8, 224, 224], help='input video size')
|
||||
# parser.add_argument('--softmax', action='store_true', help='wheter to add softmax layer at the end of recognizers')
|
||||
args = parser.parse_args()
|
||||
return args
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
args = parse_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)
|
||||
|
||||
# build the model
|
||||
model = create_model(opt, logger)
|
||||
model.setup(opt) # load checkpoints
|
||||
|
||||
# export model
|
||||
netG = model.netG
|
||||
netG.eval() # set inference model
|
||||
|
||||
path_ckpt = Path(opt.MODEL.path_checkpoint)
|
||||
path_onnx = Path(f"checkpoints/audio2motion-{path_ckpt.stem}-{opt.MODEL.load_epoch}.onnx")
|
||||
|
||||
if not path_onnx.exists():
|
||||
# forward
|
||||
batch_input = torch.randn((1, 35, 16, 1024)).float().to(model.device) # (B, nseq, nseqlen, nc_condit)
|
||||
batch_otput = model.netG.forward(batch_input).detach().cpu().numpy() # (B, nseq, 5023, 3)
|
||||
print(f"cond shape = {batch_input.shape}, batch_otput shape = {batch_otput.shape}")
|
||||
# cond shape = torch.Size([20, 35, 16, 1024]), batch_otput shape = (20, 35, 5023, 3)
|
||||
|
||||
torch.onnx.export(
|
||||
netG, # model being run, netG.module,
|
||||
batch_input,
|
||||
str(path_onnx),
|
||||
export_params=True,
|
||||
verbose=True,
|
||||
opset_version=11,
|
||||
input_names=["input"],
|
||||
output_names=["output"],
|
||||
dynamic_axes = {
|
||||
"input": {0: "batch_size", 1: "nseqlen"},
|
||||
"output": {0: "batch_size"}
|
||||
}
|
||||
)
|
||||
|
||||
print(f'Successfully exported ONNX model: {path_onnx}')
|
||||
else:
|
||||
print(f'Have existed exported ONNX model: {path_onnx}')
|
||||
|
||||
############################################ valify
|
||||
onnx_model = onnx.load(path_onnx)
|
||||
onnx.checker.check_model(onnx_model)
|
||||
|
||||
# check the numerical value
|
||||
batch_input = torch.randn((1, 35, 16, 1024)).float().to(model.device) # (B, nseq, nseqlen, nc_condit)
|
||||
batch_otput = model.netG.forward(batch_input).detach().cpu().numpy() # (B, nseq, 53)
|
||||
print(f"cond shape = {batch_input.shape}, batch_otput shape = {batch_otput.shape}")
|
||||
|
||||
# get onnx output
|
||||
sess = rt.InferenceSession(str(path_onnx), providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
|
||||
batch_onnx = sess.run(["output"], {"input": batch_input.cpu().numpy()})[0]
|
||||
|
||||
print(f"batch_onnx = {batch_onnx.shape}, dtype={batch_onnx.dtype}, ")
|
||||
|
||||
assert np.allclose(batch_otput, batch_onnx, atol=1e-4), 'The outputs are different between Pytorch and ONNX'
|
||||
print(f"output[0,0,100:120,0] = \n{batch_otput[0,0,::5]} \
|
||||
batch_onnx[0][0,0,100:120,0]=\n{batch_onnx[0,0,::5]}")
|
||||
print('The numerical values are same between Pytorch and ONNX')
|
||||
|
||||
Reference in new issue
Block a user