Commit
·
f1d67f4
1
Parent(s):
e74ccb2
update export.py
Browse files- models/export.py +8 -8
models/export.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
"""Exports a YOLOv5 *.pt model to
|
2 |
|
3 |
Usage:
|
4 |
$ export PYTHONPATH="$PWD" && python models/export.py --weights ./weights/yolov5s.pt --img 640 --batch 1
|
@@ -30,20 +30,20 @@ if __name__ == '__main__':
|
|
30 |
model.model[-1].export = True # set Detect() layer export=True
|
31 |
_ = model(img) # dry run
|
32 |
|
33 |
-
# Export to
|
34 |
try:
|
35 |
f = opt.weights.replace('.pt', '.torchscript') # filename
|
36 |
ts = torch.jit.trace(model, img)
|
37 |
ts.save(f)
|
38 |
-
print('
|
39 |
-
except:
|
40 |
-
print('
|
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=
|
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
|
|
|
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 Exception as e:
|
40 |
+
print('TorchScript export failed: %s' % e)
|
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=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)
|