Add support for FP16 (half) to export.py (#3010)

* Added support for fp16 (half) to export.py

* minimize code additions

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
This commit is contained in:
HodovoandGlenn Jocher authored and GitHub committed 2021-05-02 22:42:33 +02:00
1 parent 31ee54c28c
commit e2a80c6c0f
1 file changed
+4
+4
View File
@@ -28,6 +28,7 @@ if __name__ == '__main__':
parser.add_argument('--batch-size', type=int, default=1, help='batch size')
parser.add_argument('--grid', action='store_true', help='export Detect() layer grid')
parser.add_argument('--device', default='cpu', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
parser.add_argument('--half', action='store_true', help='FP16 half-precision export')
parser.add_argument('--dynamic', action='store_true', help='dynamic ONNX axes') # ONNX-only
parser.add_argument('--simplify', action='store_true', help='simplify ONNX model') # ONNX-only
opt = parser.parse_args()
@@ -44,11 +45,14 @@ if __name__ == '__main__':
# Checks
gs = int(max(model.stride)) # grid size (max stride)
opt.img_size = [check_img_size(x, gs) for x in opt.img_size] # verify img_size are gs-multiples
assert not (opt.device.lower() == "cpu" and opt.half), '--half only compatible with GPU export, i.e. use --device 0'
# Input
img = torch.zeros(opt.batch_size, 3, *opt.img_size).to(device) # image size(1,3,320,192) iDetection
# Update model
if opt.half:
img, model = img.half(), model.half() # to FP16
for k, m in model.named_modules():
m._non_persistent_buffers_set = set() # pytorch 1.6.0 compatibility
if isinstance(m, models.common.Conv): # assign export-friendly activations