YOLOv5: Output prediction boxes by different confidence thresholds for each category

YOLOv5: Output prediction boxes according to different confidence thresholds for each category

  • Preface
  • Prerequisites
  • Related introduction
  • YOLOv5: Output prediction boxes by different confidence thresholds for each category
    • predict
      • Modify detect.py
      • Output results
    • verify
      • Modify val.py
      • Output results
  • refer to



Foreword

  • Due to my limited level, errors and omissions will inevitably occur. Please criticize and correct me.
  • For more exciting content, you can click to enter the YOLO series of columns and natural language processing
    View the column or my personal homepage
  • Face disguise detection based on DETR
  • YOLOv7 trains its own data set (mask detection)
  • YOLOv8 trains its own data set (football detection)
  • YOLOv5: TensorRT accelerates YOLOv5 model inference
  • YOLOv5: IoU, GIoU, DIoU, CIoU, EIoU
  • Playing with Jetson Nano (5): TensorRT accelerates YOLOv5 target detection
  • YOLOv5: Add SE, CBAM, CoordAtt, ECA attention mechanism
  • YOLOv5: Interpretation of yolov5s.yaml configuration file and adding small target detection layer
  • Python converts COCO format instance segmentation data set to YOLO format instance segmentation data set
  • YOLOv5: Use version 7.0 to train your own instance segmentation model (instance segmentation of vehicles, pedestrians, road signs, lane lines, etc.)
  • Use Kaggle GPU resources to experience the Stable Diffusion open source project for free

Prerequisite

  • Familiar with Python

Related introduction

  • Python is a cross-platform computer programming language. It is a high-level scripting language that combines interpretation, compilation, interactivity and object-oriented. It was originally designed for writing automated scripts (shells). As the version is constantly updated and new language features are added, it is increasingly used for the development of independent, large-scale projects.
  • PyTorch is a deep learning framework that encapsulates many network and deep learning related tools for our convenience, instead of having to write them one by one. It is divided into CPU and GPU versions. Other frameworks include TensorFlow, Caffe, etc. PyTorch was launched by Facebook Artificial Intelligence Research (FAIR) based on Torch. It is a Python-based sustainable computing package that provides two advanced functions: 1. Tensor computing with powerful GPU acceleration (such as NumPy); 2. , Automatic differentiation mechanism when building deep neural networks.
  • YOLOv5 is a single-stage target detection algorithm. This algorithm adds some new improvement ideas on the basis of YOLOv4, which greatly improves its speed and accuracy. It is a family of object detection architectures and models pre-trained on the COCO dataset and represents Ultralytics’ open-source research into future visual AI approaches, incorporating lessons learned and best practices developed through thousands of hours of research and development. .

YOLOv5: Output prediction boxes according to different confidence thresholds for each category

Prediction

Modify detect.py

 # filter with label, class, map
                filter_score_maps = {<!-- -->
                    0 : 0.45, # person
                    5 : 0.85, #bus
                }

                # Write results
                for *xyxy, conf, cls in reversed(det):
                    # filter with label, class # Filter boxes according to the confidence of each category box
                    if int(cls) in filter_score_maps.keys() and conf < filter_score_maps[int(cls)]:
                        continue
                    if save_txt: # Write to file
                        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(txt_path + '.txt', 'a') as f:
                            f.write(('%g ' * len(line)).rstrip() % line + '\\
')

                    if save_img or save_crop or view_img: # Add bbox to image
                        c = int(cls) # integer class
                        label = None if hide_labels else (names[c] if hide_conf else f'{<!-- -->names[c]} {<!-- -->conf:.2f}')
                        annotator.box_label(xyxy, label, color=colors(c, True))
                        if save_crop:
                            save_one_box(xyxy, imc, file=save_dir / 'crops' / names[c] / f'{<!-- -->p.stem}.jpg', BGR=True)

Output results

Verification

Modify val.py

 # filter with label, class, map
            filter_score_maps = {<!-- -->
                0 : 0.45, # person
                5 : 0.85, # bus
            }

            # Write results
            filter_pred = []
            for i,(*xyxy, conf, cls) in enumerate(pred):
                # filter with label, class # Filter boxes according to the confidence of each category box
                # print((i,conf, cls))
                if (int(cls) not in filter_score_maps.keys()) or (int(cls) in filter_score_maps.keys() and conf > filter_score_maps[int(cls)]):
                    filter_pred.append(pred[i].tolist())
                
            pred = torch.Tensor(filter_pred).to(device)

Output results

Reference

[1] https://github.com/ultralytics/yolov5.git

  • Due to my limited level, errors and omissions will inevitably occur. Please criticize and correct me.
  • For more exciting content, you can click to enter the YOLO series of columns and natural language processing
    View the column or my personal homepage
  • Face disguise detection based on DETR
  • YOLOv7 trains its own data set (mask detection)
  • YOLOv8 trains its own data set (football detection)
  • YOLOv5: TensorRT accelerates YOLOv5 model inference
  • YOLOv5: IoU, GIoU, DIoU, CIoU, EIoU
  • Playing with Jetson Nano (5): TensorRT accelerates YOLOv5 target detection
  • YOLOv5: Add SE, CBAM, CoordAtt, ECA attention mechanism
  • YOLOv5: Interpretation of yolov5s.yaml configuration file and adding small target detection layer
  • Python converts COCO format instance segmentation data set to YOLO format instance segmentation data set
  • YOLOv5: Use version 7.0 to train your own instance segmentation model (instance segmentation of vehicles, pedestrians, road signs, lane lines, etc.)
  • Use Kaggle GPU resources to experience the Stable Diffusion open source project for free