glenn-jocher commited on
Commit
a03adb5
2 Parent(s): 1fca7a7 5323ad2

Merge remote-tracking branch 'origin/master'

Browse files
Files changed (2) hide show
  1. detect.py +3 -3
  2. models/export.py +12 -12
detect.py CHANGED
@@ -158,7 +158,7 @@ if __name__ == '__main__':
158
  with torch.no_grad():
159
  detect()
160
 
161
- # Update all models
162
  # for opt.weights in ['yolov5s.pt', 'yolov5m.pt', 'yolov5l.pt', 'yolov5x.pt', 'yolov3-spp.pt']:
163
- # detect()
164
- # create_pretrained(opt.weights, opt.weights)
 
158
  with torch.no_grad():
159
  detect()
160
 
161
+ # # Update all models
162
  # for opt.weights in ['yolov5s.pt', 'yolov5m.pt', 'yolov5l.pt', 'yolov5x.pt', 'yolov3-spp.pt']:
163
+ # detect()
164
+ # create_pretrained(opt.weights, opt.weights)
models/export.py CHANGED
@@ -1,4 +1,4 @@
1
- """Exports a YOLOv5 *.pt model to *.onnx and *.torchscript formats
2
 
3
  Usage:
4
  $ export PYTHONPATH="$PWD" && python models/export.py --weights ./weights/yolov5s.pt --img 640 --batch 1
@@ -6,8 +6,6 @@ Usage:
6
 
7
  import argparse
8
 
9
- import onnx
10
-
11
  from models.common import *
12
  from utils import google_utils
13
 
@@ -21,7 +19,7 @@ if __name__ == '__main__':
21
  print(opt)
22
 
23
  # Input
24
- img = torch.zeros((opt.batch_size, 3, *opt.img_size)) # image size, (1, 3, 320, 192) iDetection
25
 
26
  # Load PyTorch model
27
  google_utils.attempt_download(opt.weights)
@@ -30,20 +28,22 @@ if __name__ == '__main__':
30
  model.model[-1].export = True # set Detect() layer export=True
31
  _ = model(img) # dry run
32
 
33
- # Export to torchscript
34
  try:
35
  f = opt.weights.replace('.pt', '.torchscript') # filename
36
  ts = torch.jit.trace(model, img)
37
  ts.save(f)
38
- print('Torchscript export success, saved as %s' % f)
39
- except:
40
- print('Torchscript export failed.')
41
 
42
- # Export to ONNX
43
  try:
 
 
44
  f = opt.weights.replace('.pt', '.onnx') # filename
45
  model.fuse() # only for ONNX
46
- torch.onnx.export(model, img, f, verbose=False, opset_version=11, input_names=['images'],
47
  output_names=['output']) # output_names=['classes', 'boxes']
48
 
49
  # Checks
@@ -51,5 +51,5 @@ if __name__ == '__main__':
51
  onnx.checker.check_model(onnx_model) # check onnx model
52
  print(onnx.helper.printable_graph(onnx_model.graph)) # print a human readable representation of the graph
53
  print('ONNX export success, saved as %s\nView with https://github.com/lutzroeder/netron' % f)
54
- except:
55
- print('ONNX export failed.')
 
1
+ """Exports a YOLOv5 *.pt model to ONNX and TorchScript formats
2
 
3
  Usage:
4
  $ export PYTHONPATH="$PWD" && python models/export.py --weights ./weights/yolov5s.pt --img 640 --batch 1
 
6
 
7
  import argparse
8
 
 
 
9
  from models.common import *
10
  from utils import google_utils
11
 
 
19
  print(opt)
20
 
21
  # Input
22
+ img = torch.zeros((opt.batch_size, 3, *opt.img_size)) # image size(1,3,320,192) iDetection
23
 
24
  # Load PyTorch model
25
  google_utils.attempt_download(opt.weights)
 
28
  model.model[-1].export = True # set Detect() layer export=True
29
  _ = model(img) # dry run
30
 
31
+ # TorchScript export
32
  try:
33
  f = opt.weights.replace('.pt', '.torchscript') # filename
34
  ts = torch.jit.trace(model, img)
35
  ts.save(f)
36
+ print('TorchScript export success, saved as %s' % f)
37
+ except Exception as e:
38
+ print('TorchScript export failed: %s' % e)
39
 
40
+ # ONNX export
41
  try:
42
+ import onnx
43
+
44
  f = opt.weights.replace('.pt', '.onnx') # filename
45
  model.fuse() # only for ONNX
46
+ torch.onnx.export(model, img, f, verbose=False, opset_version=12, input_names=['images'],
47
  output_names=['output']) # output_names=['classes', 'boxes']
48
 
49
  # Checks
 
51
  onnx.checker.check_model(onnx_model) # check onnx model
52
  print(onnx.helper.printable_graph(onnx_model.graph)) # print a human readable representation of the graph
53
  print('ONNX export success, saved as %s\nView with https://github.com/lutzroeder/netron' % f)
54
+ except Exception as e:
55
+ print('ONNX export failed: %s' % e)