Skip to content

Commit

Permalink
chore: remove unnecessary things from YOLOPv2 demo
Browse files Browse the repository at this point in the history
  • Loading branch information
DiegoFernandezC committed Oct 18, 2022
1 parent 438277a commit 922722c
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 491 deletions.
30 changes: 3 additions & 27 deletions demos/yolopv2/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,30 +1,6 @@
# Usage: pip install -r requirements.txt

# Base ----------------------------------------
matplotlib>=3.2.2
numpy>=1.18.5
opencv-python==4.5.5.64
Pillow>=7.1.2
PyYAML>=5.3.1
requests>=2.23.0
scipy>=1.4.1
tqdm>=4.41.0
protobuf<4.21.3

# Torch
--find-links https://download.pytorch.org/whl/cu116
torch==1.12.0+cu116
torchvision==0.13.0+cu116
torchaudio==0.12.0

# Logging -------------------------------------
tensorboard>=2.4.1

# Plotting ------------------------------------
pandas>=1.1.4
seaborn>=0.11.0

# Extras --------------------------------------
ipython # interactive notebook
psutil # system utilization
thop # FLOPs computation
torch==1.13.0
torchvision==1.13.0
numpy==1.22.4
36 changes: 5 additions & 31 deletions demos/yolopv2/src/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,13 @@

# Conclude setting / general reprocessing / plots / metrices / datasets
from utils import (
AverageMeter,
LoadImages,
driving_area_mask,
increment_path,
lane_line_mask,
non_max_suppression,
plot_one_box,
scale_coords,
select_device,
show_seg_result,
split_for_trace_model,
time_synchronized,
yolop_detections_to_norfair_detections,
)

Expand Down Expand Up @@ -93,15 +88,10 @@ def detect():
parents=True, exist_ok=True
) # make dir

inf_time = AverageMeter()
waste_time = AverageMeter()
nms_time = AverageMeter()

# Load model
stride = 32
model = torch.jit.load(weights)
device = select_device(opt.device)
half = device.type != "cpu" # half precision only supported on CUDA
device = "cuda:0" if torch.cuda.is_available() else "cpu"
half = device != "cpu" # half precision only supported on CUDA
model = model.to(device)

# Norfair Tracker init
Expand All @@ -116,10 +106,10 @@ def detect():

# Set Dataloader
vid_path, vid_writer = None, None
dataset = LoadImages(source, img_size=imgsz, stride=stride)
dataset = LoadImages(source, img_size=imgsz)

# Run inference
if device.type != "cpu":
if device != "cpu":
model(
torch.zeros(1, 3, imgsz, imgsz).to(device).type_as(next(model.parameters()))
) # run once
Expand All @@ -134,26 +124,18 @@ def detect():
img = img.unsqueeze(0)

# Inference
t1 = time_synchronized()
[pred, anchor_grid], seg, ll = model(img)
t2 = time_synchronized()

# waste time: the incompatibility of torch.jit.trace causes extra time consumption in demo version
# but this problem will not appear in offical version
tw1 = time_synchronized()
pred = split_for_trace_model(pred, anchor_grid)
tw2 = time_synchronized()

# Apply NMS
t3 = time_synchronized()
pred = non_max_suppression(
pred,
opt.conf_thres,
opt.iou_thres,
classes=opt.classes,
agnostic=opt.agnostic_nms,
)
t4 = time_synchronized()

da_seg_mask = driving_area_mask(seg)
ll_seg_mask = lane_line_mask(ll)
Expand All @@ -177,10 +159,6 @@ def detect():
save_path = str(save_dir / p.name) # img.jpg
s += "%gx%g " % img.shape[2:] # print string

# Print time (inference)
print(f"{s}Done. ({t2 - t1:.3f}s)")
show_seg_result(im0, (da_seg_mask, ll_seg_mask), is_demo=True)

# Save results (image with detections)
if save_img:
if dataset.mode == "image":
Expand All @@ -202,11 +180,7 @@ def detect():
)
vid_writer.write(im0)

inf_time.update(t2 - t1, img.size(0))
nms_time.update(t4 - t3, img.size(0))
waste_time.update(tw2 - tw1, img.size(0))
print("inf : (%.4fs/frame) nms : (%.4fs/frame)" % (inf_time.avg, nms_time.avg))
print(f"Done. ({time.time() - t0:.3f}s)")
print(f"\nDone. ({time.time() - t0:.3f}s)")


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit 922722c

Please sign in to comment.