FP16 inference update

This commit is contained in:
Glenn Jocher committed 2020-06-14 18:32:29 -07:00
1 parent 629d3701fc
commit 260b1729f0
4 files changed
+28 -23

No files matched your search

+4 -10
View File
@@ -14,6 +14,7 @@ def detect(save_img=False):
if os.path.exists(out):
shutil.rmtree(out) # delete output folder
os.makedirs(out) # make new output folder
half &= device.type != 'cpu' # half precision only supported on CUDA
# Load model
google_utils.attempt_download(weights)
@@ -21,6 +22,8 @@ def detect(save_img=False):
# torch.save(torch.load(weights, map_location=device), weights) # update model if SourceChangeWarning
# model.fuse()
model.to(device).eval()
if half:
model.half() # to FP16
# Second-stage classifier
classify = False
@@ -29,11 +32,6 @@ def detect(save_img=False):
modelc.load_state_dict(torch.load('weights/resnet101.pt', map_location=device)['model']) # load weights
modelc.to(device).eval()
# Half precision
half = half and device.type != 'cpu' # half precision only supported on CUDA
if half:
model.half()
# Set Dataloader
vid_path, vid_writer = None, None
if webcam:
@@ -51,7 +49,7 @@ def detect(save_img=False):
# Run inference
t0 = time.time()
img = torch.zeros((1, 3, imgsz, imgsz), device=device) # init img
_ = model(img.half() if half else img.float()) if device.type != 'cpu' else None # run once
_ = model(img.half() if half else img) if device.type != 'cpu' else None # run once
for path, img, im0s, vid_cap in dataset:
img = torch.from_numpy(img).to(device)
img = img.half() if half else img.float() # uint8 to fp16/32
@@ -63,10 +61,6 @@ def detect(save_img=False):
t1 = torch_utils.time_synchronized()
pred = model(img, augment=opt.augment)[0]
# to float
if half:
pred = pred.float()
# Apply NMS
pred = non_max_suppression(pred, opt.conf_thres, opt.iou_thres,
fast=True, classes=opt.classes, agnostic=opt.agnostic_nms)