Perform Sliced (Tiled) Inference and Detailed Error Analysis using YOLOv5 Models

Fatih Cagatay Akyon
Analytics Vidhya
Published in
3 min readFeb 25, 2022
  • Do you need sliced (tiled) inference for small object detection on satellite images or high-resolution images?
  • Do you need explainable metrics for your detection model (insights on possible improvements)?
  • Do you need an interactive UI to visualize faulty predictions?
  • Do you rely on YOLOv5 models?

All of them is possible with the YOLOv5 integration of SAHI 🚀

This post will walk you through installation, sliced inference, error analysis, and interactive visualization steps for your YOLOv5 models.

Installation

✔️ Install Pytorch:

conda install pytorch=1.10.0 torchvision=0.11.1 cudatoolkit=11.3 -c pytorch

✔️ Install SAHI:

conda install sahi -c conda-forge
or
pip install -U sahi

✔️ Install YOLOv5:

pip install -U yolov5

Sliced Prediction

Gif showing sliced prediction capabilities of SAHI.

✔️ Use your weight path to perform inference:

sahi predict --source image_dir/ --model_type yolov5 --model_path yolov5s.pt --slice_height 512 --slice_width 512

Error Analysis Plots/Metrics

✔️ Create COCO formatted prediction results using COCO formatted dataset:

Gif showing COCO formatted dataset prediction capabilities of SAHI.
sahi predict --source image_dir/ --dataset_json_path dataset.json --model_type yolov5 --model_path yolov5s.pt --no_sliced_prediction

✔️ Create error analysis plots using the created result.json:

Gif showing error analysis capabilities of SAHI.
sahi coco analyse --dataset_json_path dataset.json --result_json_path result.json

🎯 Meaning of the metrics:

C75: Results at 0.75 IOU threshold
C50: Results at 0.50 IOU threshold
Loc: Results after ignoring localization errors
Sim: Results after ignoring supercategory false positives
Oth: Results after ignoring all category confusions
BG: Results after ignoring all false positives
FN: Results after ignoring all false negatives

📈 Possible model improvements:

C75-C50 and C50-Loc=Potential gain with more accurate bounding box prediction
Loc-Sim=Potential gain after fixing supercategory confusions
Loc-Oth=Potential gain after fixing category confusions
Oth-BG=Potential gain after fixing all false positives
BG-FN=Potential gain after fixing all false negatives

Interactive Visualization

✔️ Install fiftyone:

pip install -U fiftyone

✔️ Start a fiftyone web app with your prediction results:

Gif showing interactive visualization capabilities of SAHI.
sahi coco fiftyone --dataset_json_path dataset.json --image_dir image_dir/ result.json

--

--