Pytorch模型转Tensorflow

1.将pytorch pt模型转onnx

def pttoonnx():
    model = torch.jit.load("./weights/crowdhuman_yolov5m.pt")
    img = torch.rand((1,3,640,640))
    torch.onnx.export(model, img, "./weights/yolov5.onnx")

2.将onnx模型转Tensorflow格式pb

def onnxtopb():
    onnx_model = onnx.load("./weights/yolov5.onnx")
    tf_rep = prepare(onnx_model, device="CPU")
    tf_rep.export_graph("./weights/yolov5")

yolov5模型onnx转pb的时候,出现一个错误,原因是转换算子版本太高,onnx_tf版本不支持,需要降低本版,torch.onnx.export(model, img, "./weights/yolov5.onnx", opset_version=12),将opset_version设置为12

BackendIsNotSupposedToImplementIt: Unsqueeze version 13 is not implemented.

Pytorch模型转Tensorflow_第1张图片

导出onnx模型时,降低版本

def pttoonnx():
    model = torch.jit.load("./weights/crowdhuman_yolov5m.pt")
    img = torch.rand((1,3,640,640))
    torch.onnx.export(model, img, "./weights/yolov5.onnx", opset_version=12)

 

 

你可能感兴趣的:(tensorflow,tfserving,Python,pytorch,tensorflow,深度学习)