84 lines
3.2 KiB
Python
84 lines
3.2 KiB
Python
# Copyright (c) OpenMMLab. All rights reserved.
|
|
import argparse
|
|
from pathlib import Path
|
|
|
|
import torch
|
|
import numpy as np
|
|
from torch.utils.data import DataLoader
|
|
|
|
import onnx
|
|
import onnxruntime as rt
|
|
|
|
import os, sys
|
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../')))
|
|
|
|
from lsp.options.test_feature2face_options import TestOptions as RenderOptions
|
|
from lsp.models import create_model
|
|
from lsp.models import feature2face_model
|
|
from lsp.models.feature2face_G import Feature2Face_G
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
# create m2f model
|
|
gen_opt = RenderOptions().parse()
|
|
gen_opt.name = "ljj" #args.name
|
|
gen_opt.batch_size = 64 #args.batch_size_m2f
|
|
path_ckpt_m2f = f'./data/render/models/{gen_opt.name}/render_g.pkl'
|
|
|
|
device = torch.device("cuda:0")
|
|
|
|
# model_m2f = create_model(gen_opt)
|
|
# model_m2f.Feature2Face_G.eval()
|
|
# model_m2f.Feature2Face_G.load_state_dict(torch.load(path_ckpt_m2f), strict = True)
|
|
|
|
netG = Feature2Face_G(gen_opt).to(device)
|
|
netG = torch.nn.DataParallel(netG, [device,])
|
|
netG.load_state_dict(torch.load(path_ckpt_m2f), strict = True)
|
|
netG.eval()
|
|
|
|
path_onnx = Path(f"./data/render/models/{gen_opt.name}/render_g.onnx")
|
|
if not path_onnx.exists():
|
|
# forward test
|
|
batch_input = torch.randn(size=(1, 6, 512, 512)).float().to(device) # (B, 6, 512, 512)
|
|
batch_otput = netG(batch_input) # (B, 3, 512, 512)
|
|
print(f"batch_input shape = {batch_input.shape}, batch_otput shape = {batch_otput.shape}")
|
|
|
|
torch.onnx.export(
|
|
netG.module, # model being run
|
|
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"},
|
|
"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(size=(1, 6, 512, 512)).float().to(device) # (B, 6, 512, 512)
|
|
batch_otput = netG(batch_input).detach().cpu().numpy() # (B, 3, 512, 512)
|
|
print(f"batch_input 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('The numerical values are same between Pytorch and ONNX')
|
|
print(f"output[0, :, 100] = \n{batch_otput[0, 0, 100:120, 100]} \batch_onnx[0][0, :, 100]=\n{batch_onnx[0, 0, 100:120, 100]}")
|
|
|