Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

42 linhas
1.6KB

  1. """Exports a pytorch *.pt model to *.onnx format
  2. Usage:
  3. $ export PYTHONPATH="$PWD" && python models/onnx_export.py --weights ./weights/yolov5s.pt --img 640 --batch 1
  4. """
  5. import argparse
  6. import onnx
  7. from models.common import *
  8. if __name__ == '__main__':
  9. parser = argparse.ArgumentParser()
  10. parser.add_argument('--weights', type=str, default='./yolov5s.pt', help='weights path')
  11. parser.add_argument('--img-size', nargs='+', type=int, default=[640, 640], help='image size')
  12. parser.add_argument('--batch-size', type=int, default=1, help='batch size')
  13. opt = parser.parse_args()
  14. print(opt)
  15. # Parameters
  16. f = opt.weights.replace('.pt', '.onnx') # onnx filename
  17. img = torch.zeros((opt.batch_size, 3, *opt.img_size)) # image size, (1, 3, 320, 192) iDetection
  18. # Load pytorch model
  19. google_utils.attempt_download(opt.weights)
  20. model = torch.load(opt.weights, map_location=torch.device('cpu'))['model'].float()
  21. model.eval()
  22. model.fuse()
  23. # Export to onnx
  24. model.model[-1].export = True # set Detect() layer export=True
  25. _ = model(img) # dry run
  26. torch.onnx.export(model, img, f, verbose=False, opset_version=11, input_names=['images'],
  27. output_names=['output']) # output_names=['classes', 'boxes']
  28. # Check onnx model
  29. model = onnx.load(f) # load onnx model
  30. onnx.checker.check_model(model) # check onnx model
  31. print(onnx.helper.printable_graph(model.graph)) # print a human readable representation of the graph
  32. print('Export complete. ONNX model saved to %s\nView with https://github.com/lutzroeder/netron' % f)