ultralytics/yolov5 · error · NotImplementedError
--task {opt.task} not in ("train", "val", "test", "speed", "
Error message
--task {opt.task} not in ("train", "val", "test", "speed", "study") What it means
segment/val.py's main() raises NotImplementedError when opt.task is not one of the five accepted strings ('train', 'val', 'test', 'speed', 'study'). The task value selects which branch of the validation/study driver runs; an unrecognized value falls through to the else. Because parse_opt does not restrict --task to choices, any string is accepted at argparse level and only rejected here at runtime.
Source
Thrown at segment/val.py:461
# python val.py --task speed --data coco.yaml --batch 1 --weights yolov5n.pt yolov5s.pt...
opt.conf_thres, opt.iou_thres, opt.save_json = 0.25, 0.45, False
for opt.weights in weights:
run(**vars(opt), plots=False)
elif opt.task == "study": # speed vs mAP benchmarks
# python val.py --task study --data coco.yaml --iou 0.7 --weights yolov5n.pt yolov5s.pt...
for opt.weights in weights:
f = f"study_{Path(opt.data).stem}_{Path(opt.weights).stem}.txt" # filename to save to
x, y = list(range(256, 1536 + 128, 128)), [] # x axis (image sizes), y axis
for opt.imgsz in x: # img-size
LOGGER.info(f"\nRunning {f} --imgsz {opt.imgsz}...")
r, _, t = run(**vars(opt), plots=False)
y.append(r + t) # results and times
np.savetxt(f, y, fmt="%10.4g") # save
subprocess.run(["zip", "-r", "study.zip", *glob.glob("study_*.txt")], check=False)
plot_val_study(x=x) # plot
else:
raise NotImplementedError(f'--task {opt.task} not in ("train", "val", "test", "speed", "study")')
if __name__ == "__main__":
opt = parse_opt()
main(opt)
View on GitHub (pinned to 20d1d78a08)
Solutions
- Use exactly one of: --task train, --task val, --task test, --task speed, --task study.
- Check the value in your wrapper script before invoking val.py.
- Note that plain validation is 'val', not 'valid'.
Example fix
# before python segment/val.py --task valid --weights yolov5s-seg.pt --data coco.yaml # after python segment/val.py --task val --weights yolov5s-seg.pt --data coco.yaml
Defensive patterns
Strategy: validation
Validate before calling
VALID_TASKS = ('train', 'val', 'test', 'speed', 'study')
def task_valid(task: str) -> bool:
return task in VALID_TASKS Type guard
VALID_TASKS = ("train", "val", "test", "speed", "study")
def is_valid_task(task: str) -> bool:
"""True if segment/val.py accepts this --task value."""
return task in VALID_TASKS Prevention
- Constrain the value in wrappers: argparse choices=VALID_TASKS fails at parse time with a clearer message.
- Validate sweep configs' task fields before launch.
When it happens
Trigger: Running segment/val.py --task valid or --task validation (common synonyms); a typo like --task tes; passing an empty string via a wrapper script or wandb sweep that leaves the field blank.
Common situations: Sweep/hyperparameter frameworks injecting arbitrary strings; scripts copied from detect/val.py where tasks differ; non-native-English synonyms for 'validation'.
Related errors
- --model {opt.model} not found. Available models are: \n
- Source path '{source}' does not exist
- "len of masks shape" should be 2 or 3, but got {len(masks.sh
- --task {opt.task} not in ("train", "val", "test", "speed", "
AI-assisted analysis of ultralytics/yolov5@20d1d78a08 (2026-08-15).
Data as JSON: /api/errors/ba4d1ec31c8c4471.
Report an issue: GitHub.