Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CLI Fire for detection script #4981

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
6b1ec67
`FROM nvcr.io/nvidia/pytorch:21.07-py3` (#4262)
glenn-jocher Aug 1, 2021
642ba66
Merge branch 'ultralytics:master' into master
Borda Sep 28, 2021
334db40
Merge branch 'ultralytics:master' into master
Borda Oct 1, 2021
2a6c5c6
Merge remote-tracking branch 'upstream/master'
Borda Oct 13, 2021
b778267
Merge branch 'ultralytics:master' into master
Borda Oct 22, 2021
d9a5582
Merge remote-tracking branch 'upstream/master'
Borda Oct 28, 2021
6ca313f
Merge remote-tracking branch 'upstream/master'
Borda Nov 3, 2021
e662f6b
Merge remote-tracking branch 'upstream/master'
Borda Nov 3, 2021
05c6492
req
Borda Sep 28, 2021
a144034
detect typing
Borda Sep 28, 2021
337b26d
docs
Borda Sep 28, 2021
39b6516
imgsz
Borda Sep 29, 2021
c83de63
prune comments
Borda Sep 30, 2021
78ae0e4
formatting
Borda Nov 3, 2021
159c81f
Merge branch 'master' into cli/fire
Borda Nov 9, 2021
6f2a48f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 9, 2021
9fa3a12
Merge branch 'master' into cli/fire
Borda Nov 9, 2021
ecff1b0
Merge branch 'master' into cli/fire
Borda Dec 17, 2021
60eb132
Merge branch 'master' into cli/fire
Borda Jan 2, 2022
23f60e1
Merge branch 'master' into cli/fire
glenn-jocher Feb 22, 2022
dad99de
Merge branch 'master' into cli/fire
glenn-jocher Mar 14, 2022
5d5bac4
Merge branch 'master' into cli/fire
glenn-jocher Mar 14, 2022
e491186
Fix check_img_size() tuple bug
glenn-jocher Mar 14, 2022
91266ce
Update print_args()
glenn-jocher Mar 14, 2022
40b1ee1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 14, 2022
489a8b0
simplify colorstr
glenn-jocher Mar 14, 2022
2cc13a8
Merge remote-tracking branch 'origin/cli/fire' into cli/fire
glenn-jocher Mar 14, 2022
88f52e8
Merge branch 'master' into cli/fire
glenn-jocher Mar 31, 2022
f0ecfaf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 31, 2022
3955c3d
Merge branch 'master' into cli/fire
glenn-jocher Mar 31, 2022
c851451
Merge branch 'master' into cli/fire
glenn-jocher Apr 11, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 63 additions & 68 deletions detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
yolov5s_edgetpu.tflite # TensorFlow Edge TPU
"""

import argparse
import os
import sys
from pathlib import Path
from typing import Optional, Sequence, Union

import torch
import torch.backends.cudnn as cudnn
from fire import Fire

FILE = Path(__file__).resolve()
ROOT = FILE.parents[0] # YOLOv5 root directory
Expand All @@ -48,34 +49,67 @@

@torch.no_grad()
def run(
weights=ROOT / 'yolov5s.pt', # model.pt path(s)
source=ROOT / 'data/images', # file/dir/URL/glob, 0 for webcam
data=ROOT / 'data/coco128.yaml', # dataset.yaml path
imgsz=(640, 640), # inference size (height, width)
conf_thres=0.25, # confidence threshold
iou_thres=0.45, # NMS IOU threshold
max_det=1000, # maximum detections per image
device='', # cuda device, i.e. 0 or 0,1,2,3 or cpu
view_img=False, # show results
save_txt=False, # save results to *.txt
save_conf=False, # save confidences in --save-txt labels
save_crop=False, # save cropped prediction boxes
nosave=False, # do not save images/videos
classes=None, # filter by class: --class 0, or --class 0 2 3
agnostic_nms=False, # class-agnostic NMS
augment=False, # augmented inference
visualize=False, # visualize features
update=False, # update all models
project=ROOT / 'runs/detect', # save results to project/name
name='exp', # save results to project/name
exist_ok=False, # existing project/name ok, do not increment
line_thickness=3, # bounding box thickness (pixels)
hide_labels=False, # hide labels
hide_conf=False, # hide confidences
half=False, # use FP16 half-precision inference
dnn=False, # use OpenCV DNN for ONNX inference
weights: Union[str, Path] = ROOT / 'yolov5s.pt',
source: Union[str, Path] = ROOT / 'data/images',
data=ROOT / 'data/coco128.yaml',
imgsz: Sequence[int] = (640, 640),
conf_thres: float = 0.25,
iou_thres: float = 0.45,
max_det: int = 1000,
device: str = '',
view_img: bool = False,
save_txt: bool = False,
save_conf: bool = False,
save_crop: bool = False,
nosave: bool = False,
classes: Optional[Sequence] = None,
agnostic_nms: bool = False,
augment: bool = False,
visualize: bool = False,
update: bool = False,
project: Union[str, Path] = ROOT / 'runs/detect',
name: str = 'exp',
exist_ok: bool = False,
line_thickness: int = 3,
hide_labels: bool = False,
hide_conf: bool = False,
half: bool = False,
dnn: bool = False,
):
"""
Args:
weights: model path(s)
source: file/dir/URL/glob, 0 for webcam
data: dataset.yaml path
imgsz: inference size (height, width)
conf_thres: confidence threshold
iou_thres: NMS IoU threshold
max_det: maximum detections per image
device: cuda device, i.e. 0 or 0,1,2,3 or cpu
view_img: show results
save_txt: save results to *.txt
save_conf: save confidences in `save_txt` labels
save_crop: save cropped prediction boxes
nosave: do not save images/videos
classes: filter by class: `classes=0`, or `classes 0 2 3`
agnostic_nms: class-agnostic NMS
augment: augmented inference
visualize: visualize features
update: update all models
project: save results to project/name
name: save results to project/name
exist_ok: existing project/name ok, do not increment
line_thickness: bounding box thickness (pixels)
hide_labels: hide labels
hide_conf: hide confidences
half: use FP16 half-precision inference
dnn: use OpenCV DNN for ONNX inference
"""
print_args()
source = str(source)
if not isinstance(imgsz, Sequence):
imgsz = [imgsz]
imgsz *= 2 if len(imgsz) == 1 else 1 # expand
save_img = not nosave and not source.endswith('.txt') # save inference images
is_file = Path(source).suffix[1:] in (IMG_FORMATS + VID_FORMATS)
is_url = source.lower().startswith(('rtsp://', 'rtmp://', 'http://', 'https://'))
Expand Down Expand Up @@ -208,45 +242,6 @@ def run(
strip_optimizer(weights) # update model (to fix SourceChangeWarning)


def parse_opt():
parser = argparse.ArgumentParser()
parser.add_argument('--weights', nargs='+', type=str, default=ROOT / 'yolov5s.pt', help='model path(s)')
parser.add_argument('--source', type=str, default=ROOT / 'data/images', help='file/dir/URL/glob, 0 for webcam')
parser.add_argument('--data', type=str, default=ROOT / 'data/coco128.yaml', help='(optional) dataset.yaml path')
parser.add_argument('--imgsz', '--img', '--img-size', nargs='+', type=int, default=[640], help='inference size h,w')
parser.add_argument('--conf-thres', type=float, default=0.25, help='confidence threshold')
parser.add_argument('--iou-thres', type=float, default=0.45, help='NMS IoU threshold')
parser.add_argument('--max-det', type=int, default=1000, help='maximum detections per image')
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
parser.add_argument('--view-img', action='store_true', help='show results')
parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')
parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels')
parser.add_argument('--save-crop', action='store_true', help='save cropped prediction boxes')
parser.add_argument('--nosave', action='store_true', help='do not save images/videos')
parser.add_argument('--classes', nargs='+', type=int, help='filter by class: --classes 0, or --classes 0 2 3')
parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS')
parser.add_argument('--augment', action='store_true', help='augmented inference')
parser.add_argument('--visualize', action='store_true', help='visualize features')
parser.add_argument('--update', action='store_true', help='update all models')
parser.add_argument('--project', default=ROOT / 'runs/detect', help='save results to project/name')
parser.add_argument('--name', default='exp', help='save results to project/name')
parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
parser.add_argument('--line-thickness', default=3, type=int, help='bounding box thickness (pixels)')
parser.add_argument('--hide-labels', default=False, action='store_true', help='hide labels')
parser.add_argument('--hide-conf', default=False, action='store_true', help='hide confidences')
parser.add_argument('--half', action='store_true', help='use FP16 half-precision inference')
parser.add_argument('--dnn', action='store_true', help='use OpenCV DNN for ONNX inference')
opt = parser.parse_args()
opt.imgsz *= 2 if len(opt.imgsz) == 1 else 1 # expand
print_args(vars(opt))
return opt


def main(opt):
check_requirements(exclude=('tensorboard', 'thop'))
run(**vars(opt))


if __name__ == "__main__":
opt = parse_opt()
main(opt)
check_requirements(exclude=('tensorboard', 'thop'))
Fire(run)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ scipy>=1.4.1
torch>=1.7.0
torchvision>=0.8.1
tqdm>=4.41.0
fire

# Logging -------------------------------------
tensorboard>=2.4.1
Expand Down