Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

The environment

  • Ubuntu 18.04 64 – bit
  • Sahi 0.8.4
  • Yolov5 5.0
  • Pytorch 1.7.1 + cu101

preface

Object detection and instance segmentation are the most important application fields in computer vision so far. Various target detection networks emerge endlessly. However, there are still many problems in the practical application of small target detection and reasoning on large image. SAHI (Slicing Aided Hyper Inference) is used to help developers solve these realistic problems. It is a lightweight visual library. The detection rate of small targets can be improved without retraining the original detection model (currently supporting YoloV5 and MMDetection), and the use of GPU resources is not significantly improved.

Install sahi

The installation of the library is very simple, using PIP directly to install the latest version and execute the command

pip install sahi
Copy the code

Yolov5 detection

Sahi uses YOLOv5, which we described earlier, to install YOLOv5 as PIP

Install torch and TorchVision on GPU PIP install Torch ==1.7.1+ Cu101 TorchVision ==0.8.2+ Cu101 Torchaudio ==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html # install other rely on PIP install yolov5Copy the code

Find an image from the SAHI project to test

Wget https://github.com/ultralytics/yolov5/releases/download/v5.0/yolov5s6.pt # # download model wget download test images https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg # command line detection yolov5 detect - source small-vehicles1.jpeg --weights yolov5s6.ptCopy the code

It is obvious that some of the vehicle targets at the top of the image are not detected

sahi

Start by installing the two base dependency libraries

pip install fiftyone imantics
Copy the code

Take a look at the sample code below

from sahi.utils.yolov5 import download_yolov5s6_model from sahi.model import Yolov5DetectionModel from sahi.utils.file Import download_from_URL from sahi.predict import get_sliced_prediction yolov5_model_path = 'yolov5s6.pt' # download model Download_yolov5s6_model (destination_path=yolov5_model_path) # download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg', Vehicles1.jpeg ') # using the YOLOv5 detection model, using GPU acceleration, Detectionmodel (model_path= Yolov5_model_path, confidence_threshold=0.3, Device =" CUDA ", The # or 'CPU') # method divides the image to be tested into several small images (default 256x256), detects each image separately, and finally splices the image. The default height and width overlap of the small picture box is 0.2, which means 256x0.2=51pixel when converted into pixels. If you need to detect the images folder, You can use the method predict result = get_sliced_prediction("small-vehicles1.jpeg", detection_model, slice_height = 256, Slice_width = 256, overlap_height_ratio = 0.2, Export_visuals (export_dir="result/")Copy the code

Execute the code above and get

As you can see, the SAhi library allows YoloV5 to detect more targets on the same test image using the same model

Sahi’s slice reasoning principle is shown below

Sahi provides the command line tool SAHI, which can be used for quick detection

sahi predict --source small-vehicles1.jpeg --model_type yolov5 --model_path yolov5s6.pt --model_device cuda --export_visual --project results 
Copy the code

For more parameters and help with using sahi predict –help

Add a new detection model

Currently saHI only supports yoloV5 and MMDetection, but we can easily add new framework support by creating a new class in the sahi/sahi/model.py file that inherits from DetectionModel, And then implement it in turn Load_model, perform_inference, _create_object_prediction_list_FROM_original_Predictions, _create_original_predictions_from_ Object_prediction_list can be used. For details, see the implementation of Yolov5DetectionModel

Related reading

  • Ubuntu CUDA and cuDNN are installed
  • YOLOv5 model training
  • YOLOv5 version 5.0
  • PIP install YOLOv5

The resources

  • github.com/obss/sahi