Skip to content

Commit

Permalink
Add support for FP16 (half) to export.py (ultralytics#3010)
Browse files Browse the repository at this point in the history
* Added support for fp16 (half) to export.py

* minimize code additions

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
  • Loading branch information
hodovo and glenn-jocher committed May 2, 2021
1 parent 1a3629b commit d076b26
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions models/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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()
Expand All @@ -44,11 +45,14 @@
# 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
Expand Down

0 comments on commit d076b26

Please sign in to comment.