2023-04-14 20:36:16 +08:00
|
|
|
# YOLOv5 🚀 by Ultralytics, AGPL-3.0 license
|
2021-08-15 03:17:51 +08:00
|
|
|
"""
|
2022-08-22 07:06:29 +08:00
|
|
|
Validate a trained YOLOv5 detection model on a detection dataset
|
2021-06-21 23:25:04 +08:00
|
|
|
|
|
|
|
Usage:
|
2022-08-22 07:06:29 +08:00
|
|
|
$ python val.py --weights yolov5s.pt --data coco128.yaml --img 640
|
2022-01-03 08:09:45 +08:00
|
|
|
|
|
|
|
Usage - formats:
|
2022-08-22 07:06:29 +08:00
|
|
|
$ python val.py --weights yolov5s.pt # PyTorch
|
|
|
|
yolov5s.torchscript # TorchScript
|
|
|
|
yolov5s.onnx # ONNX Runtime or OpenCV DNN with --dnn
|
2022-10-21 01:54:07 +08:00
|
|
|
yolov5s_openvino_model # OpenVINO
|
2022-08-22 07:06:29 +08:00
|
|
|
yolov5s.engine # TensorRT
|
|
|
|
yolov5s.mlmodel # CoreML (macOS-only)
|
|
|
|
yolov5s_saved_model # TensorFlow SavedModel
|
|
|
|
yolov5s.pb # TensorFlow GraphDef
|
|
|
|
yolov5s.tflite # TensorFlow Lite
|
|
|
|
yolov5s_edgetpu.tflite # TensorFlow Edge TPU
|
2022-09-10 18:25:01 +08:00
|
|
|
yolov5s_paddle_model # PaddlePaddle
|
2021-06-21 23:25:04 +08:00
|
|
|
"""
|
|
|
|
|
2020-05-30 08:04:54 +08:00
|
|
|
import argparse
|
|
|
|
import json
|
2020-08-03 06:47:36 +08:00
|
|
|
import os
|
2023-02-09 21:58:24 +08:00
|
|
|
import subprocess
|
2021-06-21 23:25:04 +08:00
|
|
|
import sys
|
2020-08-03 06:47:36 +08:00
|
|
|
from pathlib import Path
|
2020-05-30 08:04:54 +08:00
|
|
|
|
2020-08-03 06:47:36 +08:00
|
|
|
import numpy as np
|
|
|
|
import torch
|
2022-04-27 06:00:01 +08:00
|
|
|
from tqdm import tqdm
|
2020-08-03 06:47:36 +08:00
|
|
|
|
2021-09-12 04:46:33 +08:00
|
|
|
FILE = Path(__file__).resolve()
|
2021-09-18 21:02:08 +08:00
|
|
|
ROOT = FILE.parents[0] # YOLOv5 root directory
|
|
|
|
if str(ROOT) not in sys.path:
|
|
|
|
sys.path.append(str(ROOT)) # add ROOT to PATH
|
2021-10-12 00:47:24 +08:00
|
|
|
ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
|
2021-06-21 23:25:04 +08:00
|
|
|
|
2021-11-09 23:45:02 +08:00
|
|
|
from models.common import DetectMultiBackend
|
2021-11-05 00:24:25 +08:00
|
|
|
from utils.callbacks import Callbacks
|
2022-05-13 20:34:16 +08:00
|
|
|
from utils.dataloaders import create_dataloader
|
2022-11-19 04:39:13 +08:00
|
|
|
from utils.general import (LOGGER, TQDM_BAR_FORMAT, Profile, check_dataset, check_img_size, check_requirements,
|
|
|
|
check_yaml, coco80_to_coco91_class, colorstr, increment_path, non_max_suppression,
|
|
|
|
print_args, scale_boxes, xywh2xyxy, xyxy2xywh)
|
2022-04-10 19:46:07 +08:00
|
|
|
from utils.metrics import ConfusionMatrix, ap_per_class, box_iou
|
2021-09-16 23:55:58 +08:00
|
|
|
from utils.plots import output_to_target, plot_images, plot_val_study
|
2022-08-19 02:26:18 +08:00
|
|
|
from utils.torch_utils import select_device, smart_inference_mode
|
2021-07-19 16:43:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
def save_one_txt(predn, save_conf, shape, file):
|
|
|
|
# Save one txt result
|
|
|
|
gn = torch.tensor(shape)[[1, 0, 1, 0]] # normalization gain whwh
|
|
|
|
for *xyxy, conf, cls in predn.tolist():
|
|
|
|
xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
|
|
|
|
line = (cls, *xywh, conf) if save_conf else (cls, *xywh) # label format
|
|
|
|
with open(file, 'a') as f:
|
|
|
|
f.write(('%g ' * len(line)).rstrip() % line + '\n')
|
|
|
|
|
|
|
|
|
|
|
|
def save_one_json(predn, jdict, path, class_map):
|
|
|
|
# Save one JSON result {"image_id": 42, "category_id": 18, "bbox": [258.15, 41.29, 348.26, 243.78], "score": 0.236}
|
|
|
|
image_id = int(path.stem) if path.stem.isnumeric() else path.stem
|
|
|
|
box = xyxy2xywh(predn[:, :4]) # xywh
|
|
|
|
box[:, :2] -= box[:, 2:] / 2 # xy center to top-left corner
|
|
|
|
for p, b in zip(predn.tolist(), box.tolist()):
|
2022-03-31 22:52:34 +08:00
|
|
|
jdict.append({
|
|
|
|
'image_id': image_id,
|
|
|
|
'category_id': class_map[int(p[5])],
|
|
|
|
'bbox': [round(x, 3) for x in b],
|
|
|
|
'score': round(p[4], 5)})
|
2021-07-19 16:43:01 +08:00
|
|
|
|
|
|
|
|
2021-08-02 02:36:00 +08:00
|
|
|
def process_batch(detections, labels, iouv):
|
|
|
|
"""
|
2023-12-27 06:58:32 +08:00
|
|
|
Return correct prediction matrix.
|
|
|
|
|
2021-08-02 02:36:00 +08:00
|
|
|
Arguments:
|
YOLOv5 segmentation model support (#9052)
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix duplicate plots.py
* Fix check_font()
* # torch.use_deterministic_algorithms(True)
* update doc detect->predict
* Resolve precommit for segment/train and segment/val
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit for utils/segment
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit min_wh
* Resolve precommit utils/segment/plots
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit utils/segment/general
* Align NMS-seg closer to NMS
* restore deterministic init_seeds code
* remove easydict dependency
* update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* restore output_to_target mask
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update
* cleanup
* Remove unused ImageFont import
* Unified NMS
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* DetectMultiBackend compatibility
* segment/predict.py update
* update plot colors
* fix bbox shifted
* sort bbox by confidence
* enable overlap by default
* Merge detect/segment output_to_target() function
* Start segmentation CI
* fix plots
* Update ci-testing.yml
* fix training whitespace
* optimize process mask functions (can we merge both?)
* Update predict/detect
* Update plot_images
* Update plot_images_and_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* Add train to CI
* fix precommit
* fix precommit CI
* fix precommit pycocotools
* fix val float issues
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix masks float float issues
* suppress errors
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix no-predictions plotting bug
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add CSV Logger
* fix val len(plot_masks)
* speed up evaluation
* fix process_mask
* fix plots
* update segment/utils build_targets
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optimize utils/segment/general crop()
* optimize utils/segment/general crop() 2
* minor updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* torch.where revert
* downsample only if different shape
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup
* loss cleanup 2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup 3
* update project names
* Rename -seg yamls from _underscore to -dash
* prepare for yolov5n-seg.pt
* precommit space fix
* add coco128-seg.yaml
* update coco128-seg comments
* cleanup val.py
* Major val.py cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* precommit fix
* precommit fix
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optional pycocotools
* remove CI pip install pycocotools (auto-installed now)
* seg yaml fix
* optimize mask_iou() and masks_iou()
* threaded fix
* Major train.py update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Major segments/val/process_batch() update
* yolov5/val updates from segment
* process_batch numpy/tensor fix
* opt-in to pycocotools with --save-json
* threaded pycocotools ops for 2x speed increase
* Avoid permute contiguous if possible
* Add max_det=300 argument to both val.py and segment/val.py
* fix onnx_dynamic
* speed up pycocotools ops
* faster process_mask(upsample=True) for predict
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* eliminate permutations for process_mask(upsample=True)
* eliminate permute-contiguous in crop(), use native dimension order
* cleanup comment
* Add Proto() module
* fix class count
* fix anchor order
* broadcast mask_gti in loss for speed
* Cleanup seg loss
* faster indexing
* faster indexing fix
* faster indexing fix2
* revert faster indexing
* fix validation plotting
* Loss cleanup and mxyxy simplification
* Loss cleanup and mxyxy simplification 2
* revert validation plotting
* replace missing tanh
* Eliminate last permutation
* delete unneeded .float()
* Remove MaskIOULoss and crop(if HWC)
* Final v6.3 SegmentationModel architecture updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add support for TF export
* remove debugger trace
* add call
* update
* update
* Merge master
* Merge master
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update dataloaders.py
* Restore CI
* Update dataloaders.py
* Fix TF/TFLite export for segmentation model
* Merge master
* Cleanup predict.py mask plotting
* cleanup scale_masks()
* rename scale_masks to scale_image
* cleanup/optimize plot_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add Annotator.masks()
* Annotator.masks() fix
* Update plots.py
* Annotator mask optimization
* Rename crop() to crop_mask()
* Do not crop in predict.py
* crop always
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Merge master
* Add vid-stride from master PR
* Update seg model outputs
* Update seg model outputs
* Add segmentation benchmarks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add segmentation benchmarks
* Add segmentation benchmarks
* Add segmentation benchmarks
* Fix DetectMultiBackend for OpenVINO
* update Annotator.masks
* fix val plot
* revert val plot
* clean up
* revert pil
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix CI error
* fix predict log
* remove upsample
* update interpolate
* fix validation plot logging
* Annotator.masks() cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Remove segmentation_model definition
* Restore 0.99999 decimals
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: Laughing-q <1185102784@qq.com>
Co-authored-by: Jiacong Fang <zldrobit@126.com>
2022-09-16 06:12:46 +08:00
|
|
|
detections (array[N, 6]), x1, y1, x2, y2, conf, class
|
|
|
|
labels (array[M, 5]), class, x1, y1, x2, y2
|
2021-08-02 02:36:00 +08:00
|
|
|
Returns:
|
YOLOv5 segmentation model support (#9052)
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix duplicate plots.py
* Fix check_font()
* # torch.use_deterministic_algorithms(True)
* update doc detect->predict
* Resolve precommit for segment/train and segment/val
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit for utils/segment
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit min_wh
* Resolve precommit utils/segment/plots
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit utils/segment/general
* Align NMS-seg closer to NMS
* restore deterministic init_seeds code
* remove easydict dependency
* update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* restore output_to_target mask
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update
* cleanup
* Remove unused ImageFont import
* Unified NMS
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* DetectMultiBackend compatibility
* segment/predict.py update
* update plot colors
* fix bbox shifted
* sort bbox by confidence
* enable overlap by default
* Merge detect/segment output_to_target() function
* Start segmentation CI
* fix plots
* Update ci-testing.yml
* fix training whitespace
* optimize process mask functions (can we merge both?)
* Update predict/detect
* Update plot_images
* Update plot_images_and_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* Add train to CI
* fix precommit
* fix precommit CI
* fix precommit pycocotools
* fix val float issues
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix masks float float issues
* suppress errors
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix no-predictions plotting bug
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add CSV Logger
* fix val len(plot_masks)
* speed up evaluation
* fix process_mask
* fix plots
* update segment/utils build_targets
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optimize utils/segment/general crop()
* optimize utils/segment/general crop() 2
* minor updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* torch.where revert
* downsample only if different shape
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup
* loss cleanup 2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup 3
* update project names
* Rename -seg yamls from _underscore to -dash
* prepare for yolov5n-seg.pt
* precommit space fix
* add coco128-seg.yaml
* update coco128-seg comments
* cleanup val.py
* Major val.py cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* precommit fix
* precommit fix
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optional pycocotools
* remove CI pip install pycocotools (auto-installed now)
* seg yaml fix
* optimize mask_iou() and masks_iou()
* threaded fix
* Major train.py update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Major segments/val/process_batch() update
* yolov5/val updates from segment
* process_batch numpy/tensor fix
* opt-in to pycocotools with --save-json
* threaded pycocotools ops for 2x speed increase
* Avoid permute contiguous if possible
* Add max_det=300 argument to both val.py and segment/val.py
* fix onnx_dynamic
* speed up pycocotools ops
* faster process_mask(upsample=True) for predict
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* eliminate permutations for process_mask(upsample=True)
* eliminate permute-contiguous in crop(), use native dimension order
* cleanup comment
* Add Proto() module
* fix class count
* fix anchor order
* broadcast mask_gti in loss for speed
* Cleanup seg loss
* faster indexing
* faster indexing fix
* faster indexing fix2
* revert faster indexing
* fix validation plotting
* Loss cleanup and mxyxy simplification
* Loss cleanup and mxyxy simplification 2
* revert validation plotting
* replace missing tanh
* Eliminate last permutation
* delete unneeded .float()
* Remove MaskIOULoss and crop(if HWC)
* Final v6.3 SegmentationModel architecture updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add support for TF export
* remove debugger trace
* add call
* update
* update
* Merge master
* Merge master
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update dataloaders.py
* Restore CI
* Update dataloaders.py
* Fix TF/TFLite export for segmentation model
* Merge master
* Cleanup predict.py mask plotting
* cleanup scale_masks()
* rename scale_masks to scale_image
* cleanup/optimize plot_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add Annotator.masks()
* Annotator.masks() fix
* Update plots.py
* Annotator mask optimization
* Rename crop() to crop_mask()
* Do not crop in predict.py
* crop always
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Merge master
* Add vid-stride from master PR
* Update seg model outputs
* Update seg model outputs
* Add segmentation benchmarks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add segmentation benchmarks
* Add segmentation benchmarks
* Add segmentation benchmarks
* Fix DetectMultiBackend for OpenVINO
* update Annotator.masks
* fix val plot
* revert val plot
* clean up
* revert pil
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix CI error
* fix predict log
* remove upsample
* update interpolate
* fix validation plot logging
* Annotator.masks() cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Remove segmentation_model definition
* Restore 0.99999 decimals
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: Laughing-q <1185102784@qq.com>
Co-authored-by: Jiacong Fang <zldrobit@126.com>
2022-09-16 06:12:46 +08:00
|
|
|
correct (array[N, 10]), for 10 IoU levels
|
2021-08-02 02:36:00 +08:00
|
|
|
"""
|
2022-06-18 19:54:55 +08:00
|
|
|
correct = np.zeros((detections.shape[0], iouv.shape[0])).astype(bool)
|
2021-08-02 02:36:00 +08:00
|
|
|
iou = box_iou(labels[:, 1:], detections[:, :4])
|
2022-05-20 17:33:10 +08:00
|
|
|
correct_class = labels[:, 0:1] == detections[:, 5]
|
|
|
|
for i in range(len(iouv)):
|
|
|
|
x = torch.where((iou >= iouv[i]) & correct_class) # IoU > threshold and classes match
|
|
|
|
if x[0].shape[0]:
|
|
|
|
matches = torch.cat((torch.stack(x, 1), iou[x[0], x[1]][:, None]), 1).cpu().numpy() # [label, detect, iou]
|
|
|
|
if x[0].shape[0] > 1:
|
|
|
|
matches = matches[matches[:, 2].argsort()[::-1]]
|
|
|
|
matches = matches[np.unique(matches[:, 1], return_index=True)[1]]
|
|
|
|
# matches = matches[matches[:, 2].argsort()[::-1]]
|
|
|
|
matches = matches[np.unique(matches[:, 0], return_index=True)[1]]
|
|
|
|
correct[matches[:, 1].astype(int), i] = True
|
2022-06-18 19:54:55 +08:00
|
|
|
return torch.tensor(correct, dtype=torch.bool, device=iouv.device)
|
2020-05-30 08:04:54 +08:00
|
|
|
|
|
|
|
|
2022-08-14 02:38:51 +08:00
|
|
|
@smart_inference_mode()
|
2022-03-31 22:52:34 +08:00
|
|
|
def run(
|
|
|
|
data,
|
2021-06-21 23:25:04 +08:00
|
|
|
weights=None, # model.pt path(s)
|
|
|
|
batch_size=32, # batch size
|
|
|
|
imgsz=640, # inference size (pixels)
|
|
|
|
conf_thres=0.001, # confidence threshold
|
|
|
|
iou_thres=0.6, # NMS IoU threshold
|
YOLOv5 segmentation model support (#9052)
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix duplicate plots.py
* Fix check_font()
* # torch.use_deterministic_algorithms(True)
* update doc detect->predict
* Resolve precommit for segment/train and segment/val
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit for utils/segment
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit min_wh
* Resolve precommit utils/segment/plots
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit utils/segment/general
* Align NMS-seg closer to NMS
* restore deterministic init_seeds code
* remove easydict dependency
* update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* restore output_to_target mask
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update
* cleanup
* Remove unused ImageFont import
* Unified NMS
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* DetectMultiBackend compatibility
* segment/predict.py update
* update plot colors
* fix bbox shifted
* sort bbox by confidence
* enable overlap by default
* Merge detect/segment output_to_target() function
* Start segmentation CI
* fix plots
* Update ci-testing.yml
* fix training whitespace
* optimize process mask functions (can we merge both?)
* Update predict/detect
* Update plot_images
* Update plot_images_and_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* Add train to CI
* fix precommit
* fix precommit CI
* fix precommit pycocotools
* fix val float issues
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix masks float float issues
* suppress errors
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix no-predictions plotting bug
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add CSV Logger
* fix val len(plot_masks)
* speed up evaluation
* fix process_mask
* fix plots
* update segment/utils build_targets
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optimize utils/segment/general crop()
* optimize utils/segment/general crop() 2
* minor updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* torch.where revert
* downsample only if different shape
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup
* loss cleanup 2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup 3
* update project names
* Rename -seg yamls from _underscore to -dash
* prepare for yolov5n-seg.pt
* precommit space fix
* add coco128-seg.yaml
* update coco128-seg comments
* cleanup val.py
* Major val.py cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* precommit fix
* precommit fix
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optional pycocotools
* remove CI pip install pycocotools (auto-installed now)
* seg yaml fix
* optimize mask_iou() and masks_iou()
* threaded fix
* Major train.py update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Major segments/val/process_batch() update
* yolov5/val updates from segment
* process_batch numpy/tensor fix
* opt-in to pycocotools with --save-json
* threaded pycocotools ops for 2x speed increase
* Avoid permute contiguous if possible
* Add max_det=300 argument to both val.py and segment/val.py
* fix onnx_dynamic
* speed up pycocotools ops
* faster process_mask(upsample=True) for predict
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* eliminate permutations for process_mask(upsample=True)
* eliminate permute-contiguous in crop(), use native dimension order
* cleanup comment
* Add Proto() module
* fix class count
* fix anchor order
* broadcast mask_gti in loss for speed
* Cleanup seg loss
* faster indexing
* faster indexing fix
* faster indexing fix2
* revert faster indexing
* fix validation plotting
* Loss cleanup and mxyxy simplification
* Loss cleanup and mxyxy simplification 2
* revert validation plotting
* replace missing tanh
* Eliminate last permutation
* delete unneeded .float()
* Remove MaskIOULoss and crop(if HWC)
* Final v6.3 SegmentationModel architecture updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add support for TF export
* remove debugger trace
* add call
* update
* update
* Merge master
* Merge master
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update dataloaders.py
* Restore CI
* Update dataloaders.py
* Fix TF/TFLite export for segmentation model
* Merge master
* Cleanup predict.py mask plotting
* cleanup scale_masks()
* rename scale_masks to scale_image
* cleanup/optimize plot_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add Annotator.masks()
* Annotator.masks() fix
* Update plots.py
* Annotator mask optimization
* Rename crop() to crop_mask()
* Do not crop in predict.py
* crop always
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Merge master
* Add vid-stride from master PR
* Update seg model outputs
* Update seg model outputs
* Add segmentation benchmarks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add segmentation benchmarks
* Add segmentation benchmarks
* Add segmentation benchmarks
* Fix DetectMultiBackend for OpenVINO
* update Annotator.masks
* fix val plot
* revert val plot
* clean up
* revert pil
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix CI error
* fix predict log
* remove upsample
* update interpolate
* fix validation plot logging
* Annotator.masks() cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Remove segmentation_model definition
* Restore 0.99999 decimals
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: Laughing-q <1185102784@qq.com>
Co-authored-by: Jiacong Fang <zldrobit@126.com>
2022-09-16 06:12:46 +08:00
|
|
|
max_det=300, # maximum detections per image
|
2021-06-21 23:25:04 +08:00
|
|
|
task='val', # train, val, test, speed or study
|
|
|
|
device='', # cuda device, i.e. 0 or 0,1,2,3 or cpu
|
2021-12-02 23:49:50 +08:00
|
|
|
workers=8, # max dataloader workers (per RANK in DDP mode)
|
2021-06-21 23:25:04 +08:00
|
|
|
single_cls=False, # treat as single-class dataset
|
|
|
|
augment=False, # augmented inference
|
|
|
|
verbose=False, # verbose output
|
|
|
|
save_txt=False, # save results to *.txt
|
|
|
|
save_hybrid=False, # save label+prediction hybrid results to *.txt
|
|
|
|
save_conf=False, # save confidences in --save-txt labels
|
2021-07-19 16:43:01 +08:00
|
|
|
save_json=False, # save a COCO-JSON results file
|
2021-09-28 08:40:20 +08:00
|
|
|
project=ROOT / 'runs/val', # save to project/name
|
2021-06-21 23:25:04 +08:00
|
|
|
name='exp', # save to project/name
|
|
|
|
exist_ok=False, # existing project/name ok, do not increment
|
|
|
|
half=True, # use FP16 half-precision inference
|
2021-11-09 23:45:02 +08:00
|
|
|
dnn=False, # use OpenCV DNN for ONNX inference
|
2021-06-21 23:25:04 +08:00
|
|
|
model=None,
|
|
|
|
dataloader=None,
|
|
|
|
save_dir=Path(''),
|
|
|
|
plots=True,
|
2021-08-01 06:18:07 +08:00
|
|
|
callbacks=Callbacks(),
|
2021-06-21 23:25:04 +08:00
|
|
|
compute_loss=None,
|
2022-03-31 22:52:34 +08:00
|
|
|
):
|
2020-05-30 08:04:54 +08:00
|
|
|
# Initialize/load model and set device
|
2020-07-08 06:40:50 +08:00
|
|
|
training = model is not None
|
|
|
|
if training: # called by train.py
|
2021-12-02 23:06:45 +08:00
|
|
|
device, pt, jit, engine = next(model.parameters()).device, True, False, False # get model device, PyTorch model
|
2021-11-09 23:45:02 +08:00
|
|
|
half &= device.type != 'cpu' # half precision only supported on CUDA
|
|
|
|
model.half() if half else model.float()
|
2020-07-08 06:40:50 +08:00
|
|
|
else: # called directly
|
2021-06-10 03:36:10 +08:00
|
|
|
device = select_device(device, batch_size=batch_size)
|
2020-05-30 08:04:54 +08:00
|
|
|
|
2020-11-09 02:39:05 +08:00
|
|
|
# Directories
|
2021-06-10 03:36:10 +08:00
|
|
|
save_dir = increment_path(Path(project) / name, exist_ok=exist_ok) # increment run
|
2020-11-13 06:37:46 +08:00
|
|
|
(save_dir / 'labels' if save_txt else save_dir).mkdir(parents=True, exist_ok=True) # make dir
|
2020-05-30 08:04:54 +08:00
|
|
|
|
|
|
|
# Load model
|
2022-03-11 23:31:52 +08:00
|
|
|
model = DetectMultiBackend(weights, device=device, dnn=dnn, data=data, fp16=half)
|
|
|
|
stride, pt, jit, engine = model.stride, model.pt, model.jit, model.engine
|
2021-11-09 23:45:02 +08:00
|
|
|
imgsz = check_img_size(imgsz, s=stride) # check image size
|
2022-03-11 23:31:52 +08:00
|
|
|
half = model.fp16 # FP16 supported on limited backends with CUDA
|
|
|
|
if engine:
|
2021-11-22 21:58:07 +08:00
|
|
|
batch_size = model.batch_size
|
2021-11-09 23:45:02 +08:00
|
|
|
else:
|
2022-03-11 23:31:52 +08:00
|
|
|
device = model.device
|
2022-03-12 19:57:08 +08:00
|
|
|
if not (pt or jit):
|
2022-03-11 23:31:52 +08:00
|
|
|
batch_size = 1 # export.py models default to batch-size 1
|
|
|
|
LOGGER.info(f'Forcing --batch-size 1 square inference (1,3,{imgsz},{imgsz}) for non-PyTorch models')
|
2020-05-30 08:04:54 +08:00
|
|
|
|
2021-06-25 07:25:03 +08:00
|
|
|
# Data
|
2021-07-28 08:04:10 +08:00
|
|
|
data = check_dataset(data) # check
|
2021-06-25 07:25:03 +08:00
|
|
|
|
2020-06-15 10:25:39 +08:00
|
|
|
# Configure
|
|
|
|
model.eval()
|
2022-03-12 19:57:08 +08:00
|
|
|
cuda = device.type != 'cpu'
|
2022-04-20 06:40:06 +08:00
|
|
|
is_coco = isinstance(data.get('val'), str) and data['val'].endswith(f'coco{os.sep}val2017.txt') # COCO dataset
|
2020-05-30 08:04:54 +08:00
|
|
|
nc = 1 if single_cls else int(data['nc']) # number of classes
|
2022-03-12 21:00:48 +08:00
|
|
|
iouv = torch.linspace(0.5, 0.95, 10, device=device) # iou vector for mAP@0.5:0.95
|
2020-05-30 08:04:54 +08:00
|
|
|
niou = iouv.numel()
|
|
|
|
|
|
|
|
# Dataloader
|
2020-07-08 06:40:50 +08:00
|
|
|
if not training:
|
2022-04-05 20:23:15 +08:00
|
|
|
if pt and not single_cls: # check --weights are trained on --data
|
2022-04-20 12:15:04 +08:00
|
|
|
ncm = model.model.nc
|
2022-07-11 21:09:42 +08:00
|
|
|
assert ncm == nc, f'{weights} ({ncm} classes) trained on different --data than what you passed ({nc} ' \
|
2022-04-05 20:23:15 +08:00
|
|
|
f'classes). Pass correct combination of --weights and --data that are trained together.'
|
2022-03-11 23:31:52 +08:00
|
|
|
model.warmup(imgsz=(1 if pt else batch_size, 3, imgsz, imgsz)) # warmup
|
2022-10-02 19:37:54 +08:00
|
|
|
pad, rect = (0.0, False) if task == 'speed' else (0.5, pt) # square inference for benchmarks
|
2021-06-10 03:36:10 +08:00
|
|
|
task = task if task in ('train', 'val', 'test') else 'val' # path to train/val/test images
|
2022-03-31 22:52:34 +08:00
|
|
|
dataloader = create_dataloader(data[task],
|
|
|
|
imgsz,
|
|
|
|
batch_size,
|
|
|
|
stride,
|
|
|
|
single_cls,
|
|
|
|
pad=pad,
|
|
|
|
rect=rect,
|
|
|
|
workers=workers,
|
|
|
|
prefix=colorstr(f'{task}: '))[0]
|
2020-05-30 08:04:54 +08:00
|
|
|
|
|
|
|
seen = 0
|
2020-11-23 22:27:14 +08:00
|
|
|
confusion_matrix = ConfusionMatrix(nc=nc)
|
2022-08-17 23:52:53 +08:00
|
|
|
names = model.names if hasattr(model, 'names') else model.module.names # get class names
|
|
|
|
if isinstance(names, (list, tuple)): # old format
|
|
|
|
names = dict(enumerate(names))
|
2021-07-19 16:43:01 +08:00
|
|
|
class_map = coco80_to_coco91_class() if is_coco else list(range(1000))
|
YOLOv5 segmentation model support (#9052)
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix duplicate plots.py
* Fix check_font()
* # torch.use_deterministic_algorithms(True)
* update doc detect->predict
* Resolve precommit for segment/train and segment/val
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit for utils/segment
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit min_wh
* Resolve precommit utils/segment/plots
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit utils/segment/general
* Align NMS-seg closer to NMS
* restore deterministic init_seeds code
* remove easydict dependency
* update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* restore output_to_target mask
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update
* cleanup
* Remove unused ImageFont import
* Unified NMS
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* DetectMultiBackend compatibility
* segment/predict.py update
* update plot colors
* fix bbox shifted
* sort bbox by confidence
* enable overlap by default
* Merge detect/segment output_to_target() function
* Start segmentation CI
* fix plots
* Update ci-testing.yml
* fix training whitespace
* optimize process mask functions (can we merge both?)
* Update predict/detect
* Update plot_images
* Update plot_images_and_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* Add train to CI
* fix precommit
* fix precommit CI
* fix precommit pycocotools
* fix val float issues
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix masks float float issues
* suppress errors
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix no-predictions plotting bug
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add CSV Logger
* fix val len(plot_masks)
* speed up evaluation
* fix process_mask
* fix plots
* update segment/utils build_targets
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optimize utils/segment/general crop()
* optimize utils/segment/general crop() 2
* minor updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* torch.where revert
* downsample only if different shape
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup
* loss cleanup 2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup 3
* update project names
* Rename -seg yamls from _underscore to -dash
* prepare for yolov5n-seg.pt
* precommit space fix
* add coco128-seg.yaml
* update coco128-seg comments
* cleanup val.py
* Major val.py cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* precommit fix
* precommit fix
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optional pycocotools
* remove CI pip install pycocotools (auto-installed now)
* seg yaml fix
* optimize mask_iou() and masks_iou()
* threaded fix
* Major train.py update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Major segments/val/process_batch() update
* yolov5/val updates from segment
* process_batch numpy/tensor fix
* opt-in to pycocotools with --save-json
* threaded pycocotools ops for 2x speed increase
* Avoid permute contiguous if possible
* Add max_det=300 argument to both val.py and segment/val.py
* fix onnx_dynamic
* speed up pycocotools ops
* faster process_mask(upsample=True) for predict
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* eliminate permutations for process_mask(upsample=True)
* eliminate permute-contiguous in crop(), use native dimension order
* cleanup comment
* Add Proto() module
* fix class count
* fix anchor order
* broadcast mask_gti in loss for speed
* Cleanup seg loss
* faster indexing
* faster indexing fix
* faster indexing fix2
* revert faster indexing
* fix validation plotting
* Loss cleanup and mxyxy simplification
* Loss cleanup and mxyxy simplification 2
* revert validation plotting
* replace missing tanh
* Eliminate last permutation
* delete unneeded .float()
* Remove MaskIOULoss and crop(if HWC)
* Final v6.3 SegmentationModel architecture updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add support for TF export
* remove debugger trace
* add call
* update
* update
* Merge master
* Merge master
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update dataloaders.py
* Restore CI
* Update dataloaders.py
* Fix TF/TFLite export for segmentation model
* Merge master
* Cleanup predict.py mask plotting
* cleanup scale_masks()
* rename scale_masks to scale_image
* cleanup/optimize plot_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add Annotator.masks()
* Annotator.masks() fix
* Update plots.py
* Annotator mask optimization
* Rename crop() to crop_mask()
* Do not crop in predict.py
* crop always
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Merge master
* Add vid-stride from master PR
* Update seg model outputs
* Update seg model outputs
* Add segmentation benchmarks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add segmentation benchmarks
* Add segmentation benchmarks
* Add segmentation benchmarks
* Fix DetectMultiBackend for OpenVINO
* update Annotator.masks
* fix val plot
* revert val plot
* clean up
* revert pil
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix CI error
* fix predict log
* remove upsample
* update interpolate
* fix validation plot logging
* Annotator.masks() cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Remove segmentation_model definition
* Restore 0.99999 decimals
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: Laughing-q <1185102784@qq.com>
Co-authored-by: Jiacong Fang <zldrobit@126.com>
2022-09-16 06:12:46 +08:00
|
|
|
s = ('%22s' + '%11s' * 6) % ('Class', 'Images', 'Instances', 'P', 'R', 'mAP50', 'mAP50-95')
|
2022-09-16 06:21:13 +08:00
|
|
|
tp, fp, p, r, f1, mp, mr, map50, ap50, map = 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
|
|
|
|
dt = Profile(), Profile(), Profile() # profiling times
|
2020-05-30 08:04:54 +08:00
|
|
|
loss = torch.zeros(3, device=device)
|
2021-07-19 16:43:01 +08:00
|
|
|
jdict, stats, ap, ap_class = [], [], [], []
|
2022-04-06 23:23:34 +08:00
|
|
|
callbacks.run('on_val_start')
|
2022-11-19 04:39:13 +08:00
|
|
|
pbar = tqdm(dataloader, desc=s, bar_format=TQDM_BAR_FORMAT) # progress bar
|
2021-11-10 22:47:38 +08:00
|
|
|
for batch_i, (im, targets, paths, shapes) in enumerate(pbar):
|
2022-04-06 23:23:34 +08:00
|
|
|
callbacks.run('on_val_batch_start')
|
2022-08-19 01:55:38 +08:00
|
|
|
with dt[0]:
|
|
|
|
if cuda:
|
|
|
|
im = im.to(device, non_blocking=True)
|
|
|
|
targets = targets.to(device)
|
|
|
|
im = im.half() if half else im.float() # uint8 to fp16/32
|
|
|
|
im /= 255 # 0 - 255 to 0.0 - 1.0
|
|
|
|
nb, _, height, width = im.shape # batch size, channels, height, width
|
2020-05-30 08:04:54 +08:00
|
|
|
|
2021-11-09 23:45:02 +08:00
|
|
|
# Inference
|
2022-08-19 01:55:38 +08:00
|
|
|
with dt[1]:
|
YOLOv5 segmentation model support (#9052)
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix duplicate plots.py
* Fix check_font()
* # torch.use_deterministic_algorithms(True)
* update doc detect->predict
* Resolve precommit for segment/train and segment/val
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit for utils/segment
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit min_wh
* Resolve precommit utils/segment/plots
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit utils/segment/general
* Align NMS-seg closer to NMS
* restore deterministic init_seeds code
* remove easydict dependency
* update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* restore output_to_target mask
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update
* cleanup
* Remove unused ImageFont import
* Unified NMS
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* DetectMultiBackend compatibility
* segment/predict.py update
* update plot colors
* fix bbox shifted
* sort bbox by confidence
* enable overlap by default
* Merge detect/segment output_to_target() function
* Start segmentation CI
* fix plots
* Update ci-testing.yml
* fix training whitespace
* optimize process mask functions (can we merge both?)
* Update predict/detect
* Update plot_images
* Update plot_images_and_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* Add train to CI
* fix precommit
* fix precommit CI
* fix precommit pycocotools
* fix val float issues
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix masks float float issues
* suppress errors
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix no-predictions plotting bug
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add CSV Logger
* fix val len(plot_masks)
* speed up evaluation
* fix process_mask
* fix plots
* update segment/utils build_targets
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optimize utils/segment/general crop()
* optimize utils/segment/general crop() 2
* minor updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* torch.where revert
* downsample only if different shape
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup
* loss cleanup 2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup 3
* update project names
* Rename -seg yamls from _underscore to -dash
* prepare for yolov5n-seg.pt
* precommit space fix
* add coco128-seg.yaml
* update coco128-seg comments
* cleanup val.py
* Major val.py cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* precommit fix
* precommit fix
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optional pycocotools
* remove CI pip install pycocotools (auto-installed now)
* seg yaml fix
* optimize mask_iou() and masks_iou()
* threaded fix
* Major train.py update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Major segments/val/process_batch() update
* yolov5/val updates from segment
* process_batch numpy/tensor fix
* opt-in to pycocotools with --save-json
* threaded pycocotools ops for 2x speed increase
* Avoid permute contiguous if possible
* Add max_det=300 argument to both val.py and segment/val.py
* fix onnx_dynamic
* speed up pycocotools ops
* faster process_mask(upsample=True) for predict
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* eliminate permutations for process_mask(upsample=True)
* eliminate permute-contiguous in crop(), use native dimension order
* cleanup comment
* Add Proto() module
* fix class count
* fix anchor order
* broadcast mask_gti in loss for speed
* Cleanup seg loss
* faster indexing
* faster indexing fix
* faster indexing fix2
* revert faster indexing
* fix validation plotting
* Loss cleanup and mxyxy simplification
* Loss cleanup and mxyxy simplification 2
* revert validation plotting
* replace missing tanh
* Eliminate last permutation
* delete unneeded .float()
* Remove MaskIOULoss and crop(if HWC)
* Final v6.3 SegmentationModel architecture updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add support for TF export
* remove debugger trace
* add call
* update
* update
* Merge master
* Merge master
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update dataloaders.py
* Restore CI
* Update dataloaders.py
* Fix TF/TFLite export for segmentation model
* Merge master
* Cleanup predict.py mask plotting
* cleanup scale_masks()
* rename scale_masks to scale_image
* cleanup/optimize plot_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add Annotator.masks()
* Annotator.masks() fix
* Update plots.py
* Annotator mask optimization
* Rename crop() to crop_mask()
* Do not crop in predict.py
* crop always
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Merge master
* Add vid-stride from master PR
* Update seg model outputs
* Update seg model outputs
* Add segmentation benchmarks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add segmentation benchmarks
* Add segmentation benchmarks
* Add segmentation benchmarks
* Fix DetectMultiBackend for OpenVINO
* update Annotator.masks
* fix val plot
* revert val plot
* clean up
* revert pil
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix CI error
* fix predict log
* remove upsample
* update interpolate
* fix validation plot logging
* Annotator.masks() cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Remove segmentation_model definition
* Restore 0.99999 decimals
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: Laughing-q <1185102784@qq.com>
Co-authored-by: Jiacong Fang <zldrobit@126.com>
2022-09-16 06:12:46 +08:00
|
|
|
preds, train_out = model(im) if compute_loss else (model(im, augment=augment), None)
|
2021-05-24 19:23:09 +08:00
|
|
|
|
2021-11-09 23:45:02 +08:00
|
|
|
# Loss
|
2021-05-24 19:23:09 +08:00
|
|
|
if compute_loss:
|
2022-09-04 21:39:57 +08:00
|
|
|
loss += compute_loss(train_out, targets)[1] # box, obj, cls
|
2021-05-24 19:23:09 +08:00
|
|
|
|
2021-11-09 23:45:02 +08:00
|
|
|
# NMS
|
2022-03-12 21:00:48 +08:00
|
|
|
targets[:, 2:] *= torch.tensor((width, height, width, height), device=device) # to pixels
|
2021-05-24 19:23:09 +08:00
|
|
|
lb = [targets[targets[:, 0] == i, 1:] for i in range(nb)] if save_hybrid else [] # for autolabelling
|
2022-08-19 01:55:38 +08:00
|
|
|
with dt[2]:
|
YOLOv5 segmentation model support (#9052)
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix duplicate plots.py
* Fix check_font()
* # torch.use_deterministic_algorithms(True)
* update doc detect->predict
* Resolve precommit for segment/train and segment/val
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit for utils/segment
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit min_wh
* Resolve precommit utils/segment/plots
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit utils/segment/general
* Align NMS-seg closer to NMS
* restore deterministic init_seeds code
* remove easydict dependency
* update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* restore output_to_target mask
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update
* cleanup
* Remove unused ImageFont import
* Unified NMS
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* DetectMultiBackend compatibility
* segment/predict.py update
* update plot colors
* fix bbox shifted
* sort bbox by confidence
* enable overlap by default
* Merge detect/segment output_to_target() function
* Start segmentation CI
* fix plots
* Update ci-testing.yml
* fix training whitespace
* optimize process mask functions (can we merge both?)
* Update predict/detect
* Update plot_images
* Update plot_images_and_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* Add train to CI
* fix precommit
* fix precommit CI
* fix precommit pycocotools
* fix val float issues
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix masks float float issues
* suppress errors
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix no-predictions plotting bug
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add CSV Logger
* fix val len(plot_masks)
* speed up evaluation
* fix process_mask
* fix plots
* update segment/utils build_targets
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optimize utils/segment/general crop()
* optimize utils/segment/general crop() 2
* minor updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* torch.where revert
* downsample only if different shape
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup
* loss cleanup 2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup 3
* update project names
* Rename -seg yamls from _underscore to -dash
* prepare for yolov5n-seg.pt
* precommit space fix
* add coco128-seg.yaml
* update coco128-seg comments
* cleanup val.py
* Major val.py cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* precommit fix
* precommit fix
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optional pycocotools
* remove CI pip install pycocotools (auto-installed now)
* seg yaml fix
* optimize mask_iou() and masks_iou()
* threaded fix
* Major train.py update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Major segments/val/process_batch() update
* yolov5/val updates from segment
* process_batch numpy/tensor fix
* opt-in to pycocotools with --save-json
* threaded pycocotools ops for 2x speed increase
* Avoid permute contiguous if possible
* Add max_det=300 argument to both val.py and segment/val.py
* fix onnx_dynamic
* speed up pycocotools ops
* faster process_mask(upsample=True) for predict
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* eliminate permutations for process_mask(upsample=True)
* eliminate permute-contiguous in crop(), use native dimension order
* cleanup comment
* Add Proto() module
* fix class count
* fix anchor order
* broadcast mask_gti in loss for speed
* Cleanup seg loss
* faster indexing
* faster indexing fix
* faster indexing fix2
* revert faster indexing
* fix validation plotting
* Loss cleanup and mxyxy simplification
* Loss cleanup and mxyxy simplification 2
* revert validation plotting
* replace missing tanh
* Eliminate last permutation
* delete unneeded .float()
* Remove MaskIOULoss and crop(if HWC)
* Final v6.3 SegmentationModel architecture updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add support for TF export
* remove debugger trace
* add call
* update
* update
* Merge master
* Merge master
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update dataloaders.py
* Restore CI
* Update dataloaders.py
* Fix TF/TFLite export for segmentation model
* Merge master
* Cleanup predict.py mask plotting
* cleanup scale_masks()
* rename scale_masks to scale_image
* cleanup/optimize plot_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add Annotator.masks()
* Annotator.masks() fix
* Update plots.py
* Annotator mask optimization
* Rename crop() to crop_mask()
* Do not crop in predict.py
* crop always
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Merge master
* Add vid-stride from master PR
* Update seg model outputs
* Update seg model outputs
* Add segmentation benchmarks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add segmentation benchmarks
* Add segmentation benchmarks
* Add segmentation benchmarks
* Fix DetectMultiBackend for OpenVINO
* update Annotator.masks
* fix val plot
* revert val plot
* clean up
* revert pil
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix CI error
* fix predict log
* remove upsample
* update interpolate
* fix validation plot logging
* Annotator.masks() cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Remove segmentation_model definition
* Restore 0.99999 decimals
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: Laughing-q <1185102784@qq.com>
Co-authored-by: Jiacong Fang <zldrobit@126.com>
2022-09-16 06:12:46 +08:00
|
|
|
preds = non_max_suppression(preds,
|
|
|
|
conf_thres,
|
|
|
|
iou_thres,
|
|
|
|
labels=lb,
|
|
|
|
multi_label=True,
|
|
|
|
agnostic=single_cls,
|
|
|
|
max_det=max_det)
|
2020-05-30 08:04:54 +08:00
|
|
|
|
2021-11-09 23:45:02 +08:00
|
|
|
# Metrics
|
YOLOv5 segmentation model support (#9052)
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix duplicate plots.py
* Fix check_font()
* # torch.use_deterministic_algorithms(True)
* update doc detect->predict
* Resolve precommit for segment/train and segment/val
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit for utils/segment
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit min_wh
* Resolve precommit utils/segment/plots
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit utils/segment/general
* Align NMS-seg closer to NMS
* restore deterministic init_seeds code
* remove easydict dependency
* update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* restore output_to_target mask
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update
* cleanup
* Remove unused ImageFont import
* Unified NMS
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* DetectMultiBackend compatibility
* segment/predict.py update
* update plot colors
* fix bbox shifted
* sort bbox by confidence
* enable overlap by default
* Merge detect/segment output_to_target() function
* Start segmentation CI
* fix plots
* Update ci-testing.yml
* fix training whitespace
* optimize process mask functions (can we merge both?)
* Update predict/detect
* Update plot_images
* Update plot_images_and_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* Add train to CI
* fix precommit
* fix precommit CI
* fix precommit pycocotools
* fix val float issues
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix masks float float issues
* suppress errors
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix no-predictions plotting bug
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add CSV Logger
* fix val len(plot_masks)
* speed up evaluation
* fix process_mask
* fix plots
* update segment/utils build_targets
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optimize utils/segment/general crop()
* optimize utils/segment/general crop() 2
* minor updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* torch.where revert
* downsample only if different shape
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup
* loss cleanup 2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup 3
* update project names
* Rename -seg yamls from _underscore to -dash
* prepare for yolov5n-seg.pt
* precommit space fix
* add coco128-seg.yaml
* update coco128-seg comments
* cleanup val.py
* Major val.py cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* precommit fix
* precommit fix
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optional pycocotools
* remove CI pip install pycocotools (auto-installed now)
* seg yaml fix
* optimize mask_iou() and masks_iou()
* threaded fix
* Major train.py update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Major segments/val/process_batch() update
* yolov5/val updates from segment
* process_batch numpy/tensor fix
* opt-in to pycocotools with --save-json
* threaded pycocotools ops for 2x speed increase
* Avoid permute contiguous if possible
* Add max_det=300 argument to both val.py and segment/val.py
* fix onnx_dynamic
* speed up pycocotools ops
* faster process_mask(upsample=True) for predict
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* eliminate permutations for process_mask(upsample=True)
* eliminate permute-contiguous in crop(), use native dimension order
* cleanup comment
* Add Proto() module
* fix class count
* fix anchor order
* broadcast mask_gti in loss for speed
* Cleanup seg loss
* faster indexing
* faster indexing fix
* faster indexing fix2
* revert faster indexing
* fix validation plotting
* Loss cleanup and mxyxy simplification
* Loss cleanup and mxyxy simplification 2
* revert validation plotting
* replace missing tanh
* Eliminate last permutation
* delete unneeded .float()
* Remove MaskIOULoss and crop(if HWC)
* Final v6.3 SegmentationModel architecture updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add support for TF export
* remove debugger trace
* add call
* update
* update
* Merge master
* Merge master
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update dataloaders.py
* Restore CI
* Update dataloaders.py
* Fix TF/TFLite export for segmentation model
* Merge master
* Cleanup predict.py mask plotting
* cleanup scale_masks()
* rename scale_masks to scale_image
* cleanup/optimize plot_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add Annotator.masks()
* Annotator.masks() fix
* Update plots.py
* Annotator mask optimization
* Rename crop() to crop_mask()
* Do not crop in predict.py
* crop always
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Merge master
* Add vid-stride from master PR
* Update seg model outputs
* Update seg model outputs
* Add segmentation benchmarks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add segmentation benchmarks
* Add segmentation benchmarks
* Add segmentation benchmarks
* Fix DetectMultiBackend for OpenVINO
* update Annotator.masks
* fix val plot
* revert val plot
* clean up
* revert pil
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix CI error
* fix predict log
* remove upsample
* update interpolate
* fix validation plot logging
* Annotator.masks() cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Remove segmentation_model definition
* Restore 0.99999 decimals
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: Laughing-q <1185102784@qq.com>
Co-authored-by: Jiacong Fang <zldrobit@126.com>
2022-09-16 06:12:46 +08:00
|
|
|
for si, pred in enumerate(preds):
|
2020-05-30 08:04:54 +08:00
|
|
|
labels = targets[targets[:, 0] == si, 1:]
|
2022-04-22 11:06:57 +08:00
|
|
|
nl, npr = labels.shape[0], pred.shape[0] # number of labels, predictions
|
2021-07-19 16:43:01 +08:00
|
|
|
path, shape = Path(paths[si]), shapes[si][0]
|
2022-04-22 11:06:57 +08:00
|
|
|
correct = torch.zeros(npr, niou, dtype=torch.bool, device=device) # init
|
2020-05-30 08:04:54 +08:00
|
|
|
seen += 1
|
|
|
|
|
2022-04-22 11:06:57 +08:00
|
|
|
if npr == 0:
|
2020-05-30 08:04:54 +08:00
|
|
|
if nl:
|
2022-07-08 02:42:09 +08:00
|
|
|
stats.append((correct, *torch.zeros((2, 0), device=device), labels[:, 0]))
|
2022-07-29 20:06:23 +08:00
|
|
|
if plots:
|
|
|
|
confusion_matrix.process_batch(detections=None, labels=labels[:, 0])
|
2020-05-30 08:04:54 +08:00
|
|
|
continue
|
|
|
|
|
2020-11-19 05:50:21 +08:00
|
|
|
# Predictions
|
2021-04-26 02:05:16 +08:00
|
|
|
if single_cls:
|
|
|
|
pred[:, 5] = 0
|
2020-11-19 05:50:21 +08:00
|
|
|
predn = pred.clone()
|
2022-09-24 22:02:41 +08:00
|
|
|
scale_boxes(im[si].shape[1:], predn[:, :4], shape, shapes[si][1]) # native-space pred
|
2020-11-19 05:50:21 +08:00
|
|
|
|
2021-07-19 16:43:01 +08:00
|
|
|
# Evaluate
|
2020-05-30 08:04:54 +08:00
|
|
|
if nl:
|
2021-07-19 16:43:01 +08:00
|
|
|
tbox = xywh2xyxy(labels[:, 1:5]) # target boxes
|
2022-09-24 22:02:41 +08:00
|
|
|
scale_boxes(im[si].shape[1:], tbox, shape, shapes[si][1]) # native-space labels
|
2021-07-19 16:43:01 +08:00
|
|
|
labelsn = torch.cat((labels[:, 0:1], tbox), 1) # native-space labels
|
|
|
|
correct = process_batch(predn, labelsn, iouv)
|
2020-11-23 22:27:14 +08:00
|
|
|
if plots:
|
2021-07-19 16:43:01 +08:00
|
|
|
confusion_matrix.process_batch(predn, labelsn)
|
2022-04-22 11:06:57 +08:00
|
|
|
stats.append((correct, pred[:, 4], pred[:, 5], labels[:, 0])) # (correct, conf, pcls, tcls)
|
2021-07-19 16:43:01 +08:00
|
|
|
|
|
|
|
# Save/log
|
|
|
|
if save_txt:
|
2023-12-27 06:58:32 +08:00
|
|
|
(save_dir / 'labels').mkdir(parents=True, exist_ok=True)
|
2022-07-31 20:17:23 +08:00
|
|
|
save_one_txt(predn, save_conf, shape, file=save_dir / 'labels' / f'{path.stem}.txt')
|
2021-07-19 16:43:01 +08:00
|
|
|
if save_json:
|
|
|
|
save_one_json(predn, jdict, path, class_map) # append to COCO-JSON dictionary
|
2021-11-09 23:45:02 +08:00
|
|
|
callbacks.run('on_val_image_end', pred, predn, path, names, im[si])
|
2020-05-30 08:04:54 +08:00
|
|
|
|
|
|
|
# Plot images
|
2020-11-16 20:35:34 +08:00
|
|
|
if plots and batch_i < 3:
|
2022-05-14 22:12:08 +08:00
|
|
|
plot_images(im, targets, paths, save_dir / f'val_batch{batch_i}_labels.jpg', names) # labels
|
YOLOv5 segmentation model support (#9052)
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix duplicate plots.py
* Fix check_font()
* # torch.use_deterministic_algorithms(True)
* update doc detect->predict
* Resolve precommit for segment/train and segment/val
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit for utils/segment
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit min_wh
* Resolve precommit utils/segment/plots
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit utils/segment/general
* Align NMS-seg closer to NMS
* restore deterministic init_seeds code
* remove easydict dependency
* update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* restore output_to_target mask
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update
* cleanup
* Remove unused ImageFont import
* Unified NMS
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* DetectMultiBackend compatibility
* segment/predict.py update
* update plot colors
* fix bbox shifted
* sort bbox by confidence
* enable overlap by default
* Merge detect/segment output_to_target() function
* Start segmentation CI
* fix plots
* Update ci-testing.yml
* fix training whitespace
* optimize process mask functions (can we merge both?)
* Update predict/detect
* Update plot_images
* Update plot_images_and_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* Add train to CI
* fix precommit
* fix precommit CI
* fix precommit pycocotools
* fix val float issues
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix masks float float issues
* suppress errors
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix no-predictions plotting bug
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add CSV Logger
* fix val len(plot_masks)
* speed up evaluation
* fix process_mask
* fix plots
* update segment/utils build_targets
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optimize utils/segment/general crop()
* optimize utils/segment/general crop() 2
* minor updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* torch.where revert
* downsample only if different shape
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup
* loss cleanup 2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup 3
* update project names
* Rename -seg yamls from _underscore to -dash
* prepare for yolov5n-seg.pt
* precommit space fix
* add coco128-seg.yaml
* update coco128-seg comments
* cleanup val.py
* Major val.py cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* precommit fix
* precommit fix
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optional pycocotools
* remove CI pip install pycocotools (auto-installed now)
* seg yaml fix
* optimize mask_iou() and masks_iou()
* threaded fix
* Major train.py update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Major segments/val/process_batch() update
* yolov5/val updates from segment
* process_batch numpy/tensor fix
* opt-in to pycocotools with --save-json
* threaded pycocotools ops for 2x speed increase
* Avoid permute contiguous if possible
* Add max_det=300 argument to both val.py and segment/val.py
* fix onnx_dynamic
* speed up pycocotools ops
* faster process_mask(upsample=True) for predict
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* eliminate permutations for process_mask(upsample=True)
* eliminate permute-contiguous in crop(), use native dimension order
* cleanup comment
* Add Proto() module
* fix class count
* fix anchor order
* broadcast mask_gti in loss for speed
* Cleanup seg loss
* faster indexing
* faster indexing fix
* faster indexing fix2
* revert faster indexing
* fix validation plotting
* Loss cleanup and mxyxy simplification
* Loss cleanup and mxyxy simplification 2
* revert validation plotting
* replace missing tanh
* Eliminate last permutation
* delete unneeded .float()
* Remove MaskIOULoss and crop(if HWC)
* Final v6.3 SegmentationModel architecture updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add support for TF export
* remove debugger trace
* add call
* update
* update
* Merge master
* Merge master
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update dataloaders.py
* Restore CI
* Update dataloaders.py
* Fix TF/TFLite export for segmentation model
* Merge master
* Cleanup predict.py mask plotting
* cleanup scale_masks()
* rename scale_masks to scale_image
* cleanup/optimize plot_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add Annotator.masks()
* Annotator.masks() fix
* Update plots.py
* Annotator mask optimization
* Rename crop() to crop_mask()
* Do not crop in predict.py
* crop always
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Merge master
* Add vid-stride from master PR
* Update seg model outputs
* Update seg model outputs
* Add segmentation benchmarks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add segmentation benchmarks
* Add segmentation benchmarks
* Add segmentation benchmarks
* Fix DetectMultiBackend for OpenVINO
* update Annotator.masks
* fix val plot
* revert val plot
* clean up
* revert pil
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix CI error
* fix predict log
* remove upsample
* update interpolate
* fix validation plot logging
* Annotator.masks() cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Remove segmentation_model definition
* Restore 0.99999 decimals
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: Laughing-q <1185102784@qq.com>
Co-authored-by: Jiacong Fang <zldrobit@126.com>
2022-09-16 06:12:46 +08:00
|
|
|
plot_images(im, output_to_target(preds), paths, save_dir / f'val_batch{batch_i}_pred.jpg', names) # pred
|
2020-11-01 03:16:35 +08:00
|
|
|
|
YOLOv5 segmentation model support (#9052)
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix duplicate plots.py
* Fix check_font()
* # torch.use_deterministic_algorithms(True)
* update doc detect->predict
* Resolve precommit for segment/train and segment/val
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit for utils/segment
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit min_wh
* Resolve precommit utils/segment/plots
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit utils/segment/general
* Align NMS-seg closer to NMS
* restore deterministic init_seeds code
* remove easydict dependency
* update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* restore output_to_target mask
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update
* cleanup
* Remove unused ImageFont import
* Unified NMS
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* DetectMultiBackend compatibility
* segment/predict.py update
* update plot colors
* fix bbox shifted
* sort bbox by confidence
* enable overlap by default
* Merge detect/segment output_to_target() function
* Start segmentation CI
* fix plots
* Update ci-testing.yml
* fix training whitespace
* optimize process mask functions (can we merge both?)
* Update predict/detect
* Update plot_images
* Update plot_images_and_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* Add train to CI
* fix precommit
* fix precommit CI
* fix precommit pycocotools
* fix val float issues
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix masks float float issues
* suppress errors
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix no-predictions plotting bug
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add CSV Logger
* fix val len(plot_masks)
* speed up evaluation
* fix process_mask
* fix plots
* update segment/utils build_targets
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optimize utils/segment/general crop()
* optimize utils/segment/general crop() 2
* minor updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* torch.where revert
* downsample only if different shape
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup
* loss cleanup 2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup 3
* update project names
* Rename -seg yamls from _underscore to -dash
* prepare for yolov5n-seg.pt
* precommit space fix
* add coco128-seg.yaml
* update coco128-seg comments
* cleanup val.py
* Major val.py cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* precommit fix
* precommit fix
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optional pycocotools
* remove CI pip install pycocotools (auto-installed now)
* seg yaml fix
* optimize mask_iou() and masks_iou()
* threaded fix
* Major train.py update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Major segments/val/process_batch() update
* yolov5/val updates from segment
* process_batch numpy/tensor fix
* opt-in to pycocotools with --save-json
* threaded pycocotools ops for 2x speed increase
* Avoid permute contiguous if possible
* Add max_det=300 argument to both val.py and segment/val.py
* fix onnx_dynamic
* speed up pycocotools ops
* faster process_mask(upsample=True) for predict
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* eliminate permutations for process_mask(upsample=True)
* eliminate permute-contiguous in crop(), use native dimension order
* cleanup comment
* Add Proto() module
* fix class count
* fix anchor order
* broadcast mask_gti in loss for speed
* Cleanup seg loss
* faster indexing
* faster indexing fix
* faster indexing fix2
* revert faster indexing
* fix validation plotting
* Loss cleanup and mxyxy simplification
* Loss cleanup and mxyxy simplification 2
* revert validation plotting
* replace missing tanh
* Eliminate last permutation
* delete unneeded .float()
* Remove MaskIOULoss and crop(if HWC)
* Final v6.3 SegmentationModel architecture updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add support for TF export
* remove debugger trace
* add call
* update
* update
* Merge master
* Merge master
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update dataloaders.py
* Restore CI
* Update dataloaders.py
* Fix TF/TFLite export for segmentation model
* Merge master
* Cleanup predict.py mask plotting
* cleanup scale_masks()
* rename scale_masks to scale_image
* cleanup/optimize plot_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add Annotator.masks()
* Annotator.masks() fix
* Update plots.py
* Annotator mask optimization
* Rename crop() to crop_mask()
* Do not crop in predict.py
* crop always
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Merge master
* Add vid-stride from master PR
* Update seg model outputs
* Update seg model outputs
* Add segmentation benchmarks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add segmentation benchmarks
* Add segmentation benchmarks
* Add segmentation benchmarks
* Fix DetectMultiBackend for OpenVINO
* update Annotator.masks
* fix val plot
* revert val plot
* clean up
* revert pil
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix CI error
* fix predict log
* remove upsample
* update interpolate
* fix validation plot logging
* Annotator.masks() cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Remove segmentation_model definition
* Restore 0.99999 decimals
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: Laughing-q <1185102784@qq.com>
Co-authored-by: Jiacong Fang <zldrobit@126.com>
2022-09-16 06:12:46 +08:00
|
|
|
callbacks.run('on_val_batch_end', batch_i, im, targets, paths, shapes, preds)
|
2022-04-06 23:23:34 +08:00
|
|
|
|
2021-11-09 23:45:02 +08:00
|
|
|
# Compute metrics
|
2022-04-22 11:06:57 +08:00
|
|
|
stats = [torch.cat(x, 0).cpu().numpy() for x in zip(*stats)] # to numpy
|
2020-07-16 09:25:34 +08:00
|
|
|
if len(stats) and stats[0].any():
|
2021-11-20 08:04:56 +08:00
|
|
|
tp, fp, p, r, f1, ap, ap_class = ap_per_class(*stats, plot=plots, save_dir=save_dir, names=names)
|
2021-01-28 09:10:53 +08:00
|
|
|
ap50, ap = ap[:, 0], ap.mean(1) # AP@0.5, AP@0.5:0.95
|
2020-05-30 08:04:54 +08:00
|
|
|
mp, mr, map50, map = p.mean(), r.mean(), ap50.mean(), ap.mean()
|
2022-07-31 20:17:23 +08:00
|
|
|
nt = np.bincount(stats[3].astype(int), minlength=nc) # number of targets per class
|
2020-05-30 08:04:54 +08:00
|
|
|
|
|
|
|
# Print results
|
2022-08-21 21:50:02 +08:00
|
|
|
pf = '%22s' + '%11i' * 2 + '%11.3g' * 4 # print format
|
2021-11-02 01:22:13 +08:00
|
|
|
LOGGER.info(pf % ('all', seen, nt.sum(), mp, mr, map50, map))
|
2022-07-29 20:45:29 +08:00
|
|
|
if nt.sum() == 0:
|
2022-09-18 22:15:25 +08:00
|
|
|
LOGGER.warning(f'WARNING ⚠️ no labels found in {task} set, can not compute metrics without labels')
|
2020-05-30 08:04:54 +08:00
|
|
|
|
|
|
|
# Print results per class
|
2021-01-21 10:27:38 +08:00
|
|
|
if (verbose or (nc < 50 and not training)) and nc > 1 and len(stats):
|
2020-05-30 08:04:54 +08:00
|
|
|
for i, c in enumerate(ap_class):
|
2021-11-02 01:22:13 +08:00
|
|
|
LOGGER.info(pf % (names[c], seen, nt[c], p[i], r[i], ap50[i], ap[i]))
|
2020-05-30 08:04:54 +08:00
|
|
|
|
|
|
|
# Print speeds
|
2022-08-19 01:55:38 +08:00
|
|
|
t = tuple(x.t / seen * 1E3 for x in dt) # speeds per image
|
2020-05-30 08:04:54 +08:00
|
|
|
if not training:
|
2021-06-09 22:25:17 +08:00
|
|
|
shape = (batch_size, 3, imgsz, imgsz)
|
2021-11-02 01:22:13 +08:00
|
|
|
LOGGER.info(f'Speed: %.1fms pre-process, %.1fms inference, %.1fms NMS per image at shape {shape}' % t)
|
2020-05-30 08:04:54 +08:00
|
|
|
|
2020-11-30 23:44:14 +08:00
|
|
|
# Plots
|
|
|
|
if plots:
|
|
|
|
confusion_matrix.plot(save_dir=save_dir, names=list(names.values()))
|
2022-09-07 23:28:46 +08:00
|
|
|
callbacks.run('on_val_end', nt, tp, fp, p, r, f1, ap, ap50, ap_class, confusion_matrix)
|
2020-11-30 23:44:14 +08:00
|
|
|
|
2020-05-30 08:04:54 +08:00
|
|
|
# Save JSON
|
2020-07-19 02:31:22 +08:00
|
|
|
if save_json and len(jdict):
|
2020-10-26 01:19:44 +08:00
|
|
|
w = Path(weights[0] if isinstance(weights, list) else weights).stem if weights is not None else '' # weights
|
2022-12-04 06:41:08 +08:00
|
|
|
anno_json = str(Path('../datasets/coco/annotations/instances_val2017.json')) # annotations
|
2023-07-23 09:51:12 +08:00
|
|
|
if not os.path.exists(anno_json):
|
|
|
|
anno_json = os.path.join(data['path'], 'annotations', 'instances_val2017.json')
|
2023-02-18 08:06:24 +08:00
|
|
|
pred_json = str(save_dir / f'{w}_predictions.json') # predictions
|
2021-11-02 01:22:13 +08:00
|
|
|
LOGGER.info(f'\nEvaluating pycocotools mAP... saving {pred_json}...')
|
2020-11-14 20:09:04 +08:00
|
|
|
with open(pred_json, 'w') as f:
|
2020-10-26 01:19:44 +08:00
|
|
|
json.dump(jdict, f)
|
2020-05-30 08:04:54 +08:00
|
|
|
|
2020-07-16 10:04:15 +08:00
|
|
|
try: # https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoEvalDemo.ipynb
|
2022-12-27 22:41:03 +08:00
|
|
|
check_requirements('pycocotools>=2.0.6')
|
2020-05-30 08:04:54 +08:00
|
|
|
from pycocotools.coco import COCO
|
|
|
|
from pycocotools.cocoeval import COCOeval
|
|
|
|
|
2020-11-14 20:09:04 +08:00
|
|
|
anno = COCO(anno_json) # init annotations api
|
|
|
|
pred = anno.loadRes(pred_json) # init predictions api
|
|
|
|
eval = COCOeval(anno, pred, 'bbox')
|
|
|
|
if is_coco:
|
2022-03-06 23:16:17 +08:00
|
|
|
eval.params.imgIds = [int(Path(x).stem) for x in dataloader.dataset.im_files] # image IDs to evaluate
|
2020-11-14 20:09:04 +08:00
|
|
|
eval.evaluate()
|
|
|
|
eval.accumulate()
|
|
|
|
eval.summarize()
|
|
|
|
map, map50 = eval.stats[:2] # update results (mAP@0.5:0.95, mAP@0.5)
|
2020-07-20 06:44:11 +08:00
|
|
|
except Exception as e:
|
2021-11-02 01:22:13 +08:00
|
|
|
LOGGER.info(f'pycocotools unable to run: {e}')
|
2020-05-30 08:04:54 +08:00
|
|
|
|
|
|
|
# Return results
|
2021-02-25 13:03:21 +08:00
|
|
|
model.float() # for training
|
2020-11-09 19:24:11 +08:00
|
|
|
if not training:
|
2020-11-23 20:38:47 +08:00
|
|
|
s = f"\n{len(list(save_dir.glob('labels/*.txt')))} labels saved to {save_dir / 'labels'}" if save_txt else ''
|
2021-11-02 01:22:13 +08:00
|
|
|
LOGGER.info(f"Results saved to {colorstr('bold', save_dir)}{s}")
|
2020-05-30 08:04:54 +08:00
|
|
|
maps = np.zeros(nc) + map
|
|
|
|
for i, c in enumerate(ap_class):
|
|
|
|
maps[c] = ap[i]
|
|
|
|
return (mp, mr, map50, map, *(loss.cpu() / len(dataloader)).tolist()), maps, t
|
|
|
|
|
|
|
|
|
2021-06-19 18:06:59 +08:00
|
|
|
def parse_opt():
|
2021-09-18 20:16:19 +08:00
|
|
|
parser = argparse.ArgumentParser()
|
2021-09-28 08:40:20 +08:00
|
|
|
parser.add_argument('--data', type=str, default=ROOT / 'data/coco128.yaml', help='dataset.yaml path')
|
YOLOv5 segmentation model support (#9052)
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix duplicate plots.py
* Fix check_font()
* # torch.use_deterministic_algorithms(True)
* update doc detect->predict
* Resolve precommit for segment/train and segment/val
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit for utils/segment
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit min_wh
* Resolve precommit utils/segment/plots
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit utils/segment/general
* Align NMS-seg closer to NMS
* restore deterministic init_seeds code
* remove easydict dependency
* update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* restore output_to_target mask
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update
* cleanup
* Remove unused ImageFont import
* Unified NMS
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* DetectMultiBackend compatibility
* segment/predict.py update
* update plot colors
* fix bbox shifted
* sort bbox by confidence
* enable overlap by default
* Merge detect/segment output_to_target() function
* Start segmentation CI
* fix plots
* Update ci-testing.yml
* fix training whitespace
* optimize process mask functions (can we merge both?)
* Update predict/detect
* Update plot_images
* Update plot_images_and_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* Add train to CI
* fix precommit
* fix precommit CI
* fix precommit pycocotools
* fix val float issues
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix masks float float issues
* suppress errors
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix no-predictions plotting bug
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add CSV Logger
* fix val len(plot_masks)
* speed up evaluation
* fix process_mask
* fix plots
* update segment/utils build_targets
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optimize utils/segment/general crop()
* optimize utils/segment/general crop() 2
* minor updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* torch.where revert
* downsample only if different shape
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup
* loss cleanup 2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup 3
* update project names
* Rename -seg yamls from _underscore to -dash
* prepare for yolov5n-seg.pt
* precommit space fix
* add coco128-seg.yaml
* update coco128-seg comments
* cleanup val.py
* Major val.py cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* precommit fix
* precommit fix
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optional pycocotools
* remove CI pip install pycocotools (auto-installed now)
* seg yaml fix
* optimize mask_iou() and masks_iou()
* threaded fix
* Major train.py update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Major segments/val/process_batch() update
* yolov5/val updates from segment
* process_batch numpy/tensor fix
* opt-in to pycocotools with --save-json
* threaded pycocotools ops for 2x speed increase
* Avoid permute contiguous if possible
* Add max_det=300 argument to both val.py and segment/val.py
* fix onnx_dynamic
* speed up pycocotools ops
* faster process_mask(upsample=True) for predict
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* eliminate permutations for process_mask(upsample=True)
* eliminate permute-contiguous in crop(), use native dimension order
* cleanup comment
* Add Proto() module
* fix class count
* fix anchor order
* broadcast mask_gti in loss for speed
* Cleanup seg loss
* faster indexing
* faster indexing fix
* faster indexing fix2
* revert faster indexing
* fix validation plotting
* Loss cleanup and mxyxy simplification
* Loss cleanup and mxyxy simplification 2
* revert validation plotting
* replace missing tanh
* Eliminate last permutation
* delete unneeded .float()
* Remove MaskIOULoss and crop(if HWC)
* Final v6.3 SegmentationModel architecture updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add support for TF export
* remove debugger trace
* add call
* update
* update
* Merge master
* Merge master
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update dataloaders.py
* Restore CI
* Update dataloaders.py
* Fix TF/TFLite export for segmentation model
* Merge master
* Cleanup predict.py mask plotting
* cleanup scale_masks()
* rename scale_masks to scale_image
* cleanup/optimize plot_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add Annotator.masks()
* Annotator.masks() fix
* Update plots.py
* Annotator mask optimization
* Rename crop() to crop_mask()
* Do not crop in predict.py
* crop always
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Merge master
* Add vid-stride from master PR
* Update seg model outputs
* Update seg model outputs
* Add segmentation benchmarks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add segmentation benchmarks
* Add segmentation benchmarks
* Add segmentation benchmarks
* Fix DetectMultiBackend for OpenVINO
* update Annotator.masks
* fix val plot
* revert val plot
* clean up
* revert pil
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix CI error
* fix predict log
* remove upsample
* update interpolate
* fix validation plot logging
* Annotator.masks() cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Remove segmentation_model definition
* Restore 0.99999 decimals
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: Laughing-q <1185102784@qq.com>
Co-authored-by: Jiacong Fang <zldrobit@126.com>
2022-09-16 06:12:46 +08:00
|
|
|
parser.add_argument('--weights', nargs='+', type=str, default=ROOT / 'yolov5s.pt', help='model path(s)')
|
2021-06-10 04:50:27 +08:00
|
|
|
parser.add_argument('--batch-size', type=int, default=32, help='batch size')
|
2021-06-10 03:36:10 +08:00
|
|
|
parser.add_argument('--imgsz', '--img', '--img-size', type=int, default=640, help='inference size (pixels)')
|
2021-06-10 04:50:27 +08:00
|
|
|
parser.add_argument('--conf-thres', type=float, default=0.001, help='confidence threshold')
|
|
|
|
parser.add_argument('--iou-thres', type=float, default=0.6, help='NMS IoU threshold')
|
YOLOv5 segmentation model support (#9052)
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix duplicate plots.py
* Fix check_font()
* # torch.use_deterministic_algorithms(True)
* update doc detect->predict
* Resolve precommit for segment/train and segment/val
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit for utils/segment
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit min_wh
* Resolve precommit utils/segment/plots
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Resolve precommit utils/segment/general
* Align NMS-seg closer to NMS
* restore deterministic init_seeds code
* remove easydict dependency
* update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* restore output_to_target mask
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* update
* cleanup
* Remove unused ImageFont import
* Unified NMS
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* DetectMultiBackend compatibility
* segment/predict.py update
* update plot colors
* fix bbox shifted
* sort bbox by confidence
* enable overlap by default
* Merge detect/segment output_to_target() function
* Start segmentation CI
* fix plots
* Update ci-testing.yml
* fix training whitespace
* optimize process mask functions (can we merge both?)
* Update predict/detect
* Update plot_images
* Update plot_images_and_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* Add train to CI
* fix precommit
* fix precommit CI
* fix precommit pycocotools
* fix val float issues
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix masks float float issues
* suppress errors
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix no-predictions plotting bug
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add CSV Logger
* fix val len(plot_masks)
* speed up evaluation
* fix process_mask
* fix plots
* update segment/utils build_targets
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optimize utils/segment/general crop()
* optimize utils/segment/general crop() 2
* minor updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* torch.where revert
* downsample only if different shape
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup
* loss cleanup 2
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* loss cleanup 3
* update project names
* Rename -seg yamls from _underscore to -dash
* prepare for yolov5n-seg.pt
* precommit space fix
* add coco128-seg.yaml
* update coco128-seg comments
* cleanup val.py
* Major val.py cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* precommit fix
* precommit fix
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* optional pycocotools
* remove CI pip install pycocotools (auto-installed now)
* seg yaml fix
* optimize mask_iou() and masks_iou()
* threaded fix
* Major train.py update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Major segments/val/process_batch() update
* yolov5/val updates from segment
* process_batch numpy/tensor fix
* opt-in to pycocotools with --save-json
* threaded pycocotools ops for 2x speed increase
* Avoid permute contiguous if possible
* Add max_det=300 argument to both val.py and segment/val.py
* fix onnx_dynamic
* speed up pycocotools ops
* faster process_mask(upsample=True) for predict
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* eliminate permutations for process_mask(upsample=True)
* eliminate permute-contiguous in crop(), use native dimension order
* cleanup comment
* Add Proto() module
* fix class count
* fix anchor order
* broadcast mask_gti in loss for speed
* Cleanup seg loss
* faster indexing
* faster indexing fix
* faster indexing fix2
* revert faster indexing
* fix validation plotting
* Loss cleanup and mxyxy simplification
* Loss cleanup and mxyxy simplification 2
* revert validation plotting
* replace missing tanh
* Eliminate last permutation
* delete unneeded .float()
* Remove MaskIOULoss and crop(if HWC)
* Final v6.3 SegmentationModel architecture updates
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add support for TF export
* remove debugger trace
* add call
* update
* update
* Merge master
* Merge master
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update dataloaders.py
* Restore CI
* Update dataloaders.py
* Fix TF/TFLite export for segmentation model
* Merge master
* Cleanup predict.py mask plotting
* cleanup scale_masks()
* rename scale_masks to scale_image
* cleanup/optimize plot_masks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add Annotator.masks()
* Annotator.masks() fix
* Update plots.py
* Annotator mask optimization
* Rename crop() to crop_mask()
* Do not crop in predict.py
* crop always
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Merge master
* Add vid-stride from master PR
* Update seg model outputs
* Update seg model outputs
* Add segmentation benchmarks
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Add segmentation benchmarks
* Add segmentation benchmarks
* Add segmentation benchmarks
* Fix DetectMultiBackend for OpenVINO
* update Annotator.masks
* fix val plot
* revert val plot
* clean up
* revert pil
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Fix CI error
* fix predict log
* remove upsample
* update interpolate
* fix validation plot logging
* Annotator.masks() cleanup
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Remove segmentation_model definition
* Restore 0.99999 decimals
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: Laughing-q <1185102784@qq.com>
Co-authored-by: Jiacong Fang <zldrobit@126.com>
2022-09-16 06:12:46 +08:00
|
|
|
parser.add_argument('--max-det', type=int, default=300, help='maximum detections per image')
|
2021-03-13 14:08:42 +08:00
|
|
|
parser.add_argument('--task', default='val', help='train, val, test, speed or study')
|
2020-05-30 08:04:54 +08:00
|
|
|
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
|
2021-12-02 23:49:50 +08:00
|
|
|
parser.add_argument('--workers', type=int, default=8, help='max dataloader workers (per RANK in DDP mode)')
|
2020-05-30 08:04:54 +08:00
|
|
|
parser.add_argument('--single-cls', action='store_true', help='treat as single-class dataset')
|
|
|
|
parser.add_argument('--augment', action='store_true', help='augmented inference')
|
|
|
|
parser.add_argument('--verbose', action='store_true', help='report mAP by class')
|
2020-07-16 11:00:48 +08:00
|
|
|
parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')
|
2020-12-09 10:15:39 +08:00
|
|
|
parser.add_argument('--save-hybrid', action='store_true', help='save label+prediction hybrid results to *.txt')
|
2020-10-25 23:50:21 +08:00
|
|
|
parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels')
|
2021-07-19 16:43:01 +08:00
|
|
|
parser.add_argument('--save-json', action='store_true', help='save a COCO-JSON results file')
|
2021-09-28 08:40:20 +08:00
|
|
|
parser.add_argument('--project', default=ROOT / 'runs/val', help='save to project/name')
|
2020-11-13 06:37:46 +08:00
|
|
|
parser.add_argument('--name', default='exp', help='save to project/name')
|
|
|
|
parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
|
2021-06-09 00:47:13 +08:00
|
|
|
parser.add_argument('--half', action='store_true', help='use FP16 half-precision inference')
|
2021-11-09 23:45:02 +08:00
|
|
|
parser.add_argument('--dnn', action='store_true', help='use OpenCV DNN for ONNX inference')
|
2020-05-30 08:04:54 +08:00
|
|
|
opt = parser.parse_args()
|
2021-09-28 08:40:20 +08:00
|
|
|
opt.data = check_yaml(opt.data) # check YAML
|
2020-07-16 10:06:39 +08:00
|
|
|
opt.save_json |= opt.data.endswith('coco.yaml')
|
2021-06-10 03:36:10 +08:00
|
|
|
opt.save_txt |= opt.save_hybrid
|
2022-03-31 23:11:43 +08:00
|
|
|
print_args(vars(opt))
|
2021-06-19 18:06:59 +08:00
|
|
|
return opt
|
|
|
|
|
|
|
|
|
|
|
|
def main(opt):
|
2023-05-21 06:18:12 +08:00
|
|
|
check_requirements(ROOT / 'requirements.txt', exclude=('tensorboard', 'thop'))
|
2020-05-30 08:04:54 +08:00
|
|
|
|
2021-03-13 14:08:42 +08:00
|
|
|
if opt.task in ('train', 'val', 'test'): # run normally
|
2021-11-08 23:04:31 +08:00
|
|
|
if opt.conf_thres > 0.001: # https://github.com/ultralytics/yolov5/issues/1466
|
2022-09-18 22:15:25 +08:00
|
|
|
LOGGER.info(f'WARNING ⚠️ confidence threshold {opt.conf_thres} > 0.001 produces invalid results')
|
2022-08-20 23:17:35 +08:00
|
|
|
if opt.save_hybrid:
|
2022-09-18 22:15:25 +08:00
|
|
|
LOGGER.info('WARNING ⚠️ --save-hybrid will return high mAP from hybrid labels, not from predictions alone')
|
2021-06-21 23:25:04 +08:00
|
|
|
run(**vars(opt))
|
2020-10-25 23:50:21 +08:00
|
|
|
|
2021-11-10 23:48:38 +08:00
|
|
|
else:
|
|
|
|
weights = opt.weights if isinstance(opt.weights, list) else [opt.weights]
|
2022-11-21 23:54:36 +08:00
|
|
|
opt.half = torch.cuda.is_available() and opt.device != 'cpu' # FP16 for fastest results
|
2021-11-10 23:48:38 +08:00
|
|
|
if opt.task == 'speed': # speed benchmarks
|
|
|
|
# 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
|
2023-02-13 22:00:31 +08:00
|
|
|
subprocess.run(['zip', '-r', 'study.zip', 'study_*.txt'])
|
2021-11-10 23:48:38 +08:00
|
|
|
plot_val_study(x=x) # plot
|
2022-12-07 06:48:17 +08:00
|
|
|
else:
|
|
|
|
raise NotImplementedError(f'--task {opt.task} not in ("train", "val", "test", "speed", "study")')
|
2021-06-19 18:06:59 +08:00
|
|
|
|
|
|
|
|
2023-02-18 08:06:24 +08:00
|
|
|
if __name__ == '__main__':
|
2021-06-19 18:06:59 +08:00
|
|
|
opt = parse_opt()
|
|
|
|
main(opt)
|