import torch
import argparse
import torch.onnx
import torchvision.models as models
from collections import OrderedDict
def proc_node_module(checkpoint, attr_name):
new_state_dict = OrderedDict()
for k, v in checkpoint[attr_name].items():
if(k[0:7] == "module."):
name = k[7:]
else:
name = k[0:]
new_state_dict[name] = v
return new_state_dict
def convert(bs, ckpt_path="./model_lincls_best.pth.tar"):
checkpoint = torch.load(ckpt_path, map_location='cpu')
checkpoint['state_dict'] = proc_node_module(checkpoint, 'state_dict')
model = models.__dict__['resnet50']()
model.load_state_dict(checkpoint['state_dict'])
model.eval()
input_names = ["actual_input_1"]
output_names = ["output1"]
dummy_input = torch.randn(bs, 3, 224, 224)
torch.onnx.export(model, dummy_input, "moco-v2-bs" + str(bs) + ".onnx", input_names=input_names, output_names=output_names, opset_version=11)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--bs', type=int, default=1)
parser.add_argument('--weight', type=str, default="./model_lincls_best.pth.tar")
args = parser.parse_args()
convert(args.bs, args.weight)