mirror of
https://github.com/deepinsight/insightface.git
synced 2026-05-19 07:27:52 +00:00
Merge pull request #1522 from 007gzs/patch-1
added main to convert pytorch model to onnx
This commit is contained in:
@@ -19,3 +19,36 @@ def convert_onnx(net, path_module, output, opset=11):
|
||||
graph = model.graph
|
||||
graph.input[0].type.tensor_type.shape.dim[0].dim_param = 'None'
|
||||
onnx.save(model, output)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import os
|
||||
import argparse
|
||||
from backbones import get_model
|
||||
|
||||
parser = argparse.ArgumentParser(description='ArcFace PyTorch to onnx')
|
||||
parser.add_argument('input', type=str, help='input backbone.pth file or path')
|
||||
parser.add_argument('--output', type=str, default=None, help='output onnx path')
|
||||
parser.add_argument('--network', type=str, default=None, help='backbone network')
|
||||
args = parser.parse_args()
|
||||
input_file = args.input
|
||||
if os.path.isdir(input_file):
|
||||
input_file = os.path.join(input_file, "backbone.pth")
|
||||
assert os.path.exists(input_file)
|
||||
model_name = os.path.basename(os.path.dirname(input_file)).lower()
|
||||
params = model_name.split("_")
|
||||
if len(params) >= 3 and params[1] in ('arcface', 'cosface'):
|
||||
if args.network is None:
|
||||
args.network = params[2]
|
||||
assert args.network is not None
|
||||
print(args)
|
||||
backbone_onnx = get_model(args.network, dropout=0)
|
||||
|
||||
output_path = args.output
|
||||
if output_path is None:
|
||||
output_path = os.path.join(os.path.dirname(__file__), 'onnx')
|
||||
if not os.path.exists(output_path):
|
||||
os.makedirs(output_path)
|
||||
assert os.path.isdir(output_path)
|
||||
output_file = os.path.join(output_path, "%s.onnx" % model_name)
|
||||
convert_onnx(backbone_onnx, input_file, output_file)
|
||||
|
||||
Reference in New Issue
Block a user