SAHI with YOLOv5 for Sliced Inference¶
0. Preparation¶
- Install latest version of SAHI and YOLOv5:
In [ ]:
Copied!
!pip install -U torch sahi==0.11.21 yolov5==7.0.14
!pip install -U torch sahi==0.11.21 yolov5==7.0.14
In [ ]:
Copied!
import os
os.getcwd()
import os
os.getcwd()
- Import required modules:
In [1]:
Copied!
# arrange an instance segmentation model for test
from sahi.utils.yolov5 import (
download_yolov5s6_model,
)
# import required functions, classes
from sahi import AutoDetectionModel
from sahi.utils.cv import read_image
from sahi.utils.file import download_from_url
from sahi.predict import get_prediction, get_sliced_prediction, predict
from IPython.display import Image
# arrange an instance segmentation model for test
from sahi.utils.yolov5 import (
download_yolov5s6_model,
)
# import required functions, classes
from sahi import AutoDetectionModel
from sahi.utils.cv import read_image
from sahi.utils.file import download_from_url
from sahi.predict import get_prediction, get_sliced_prediction, predict
from IPython.display import Image
- Download a yolov5 model and two test images:
In [2]:
Copied!
# download YOLOV5S6 model to 'models/yolov5s6.pt'
yolov5_model_path = 'models/yolov5s6.pt'
download_yolov5s6_model(destination_path=yolov5_model_path)
# download test images into demo_data folder
download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg', 'demo_data/small-vehicles1.jpeg')
download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png', 'demo_data/terrain2.png')
# download YOLOV5S6 model to 'models/yolov5s6.pt'
yolov5_model_path = 'models/yolov5s6.pt'
download_yolov5s6_model(destination_path=yolov5_model_path)
# download test images into demo_data folder
download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/small-vehicles1.jpeg', 'demo_data/small-vehicles1.jpeg')
download_from_url('https://raw.githubusercontent.com/obss/sahi/main/demo/demo_data/terrain2.png', 'demo_data/terrain2.png')
1. Standard Inference with a YOLOv5 Model¶
- Instantiate a detection model by defining model weight path and other parameters:
In [18]:
Copied!
detection_model = AutoDetectionModel.from_pretrained(
model_type='yolov5',
model_path=yolov5_model_path,
confidence_threshold=0.3,
device="cpu", # or 'cuda:0'
)
detection_model = AutoDetectionModel.from_pretrained(
model_type='yolov5',
model_path=yolov5_model_path,
confidence_threshold=0.3,
device="cpu", # or 'cuda:0'
)
- Perform prediction by feeding the
get_prediction
function with an image path and a DetectionModel instance:
In [4]:
Copied!
result = get_prediction("demo_data/small-vehicles1.jpeg", detection_model)
result = get_prediction("demo_data/small-vehicles1.jpeg", detection_model)
- Or perform prediction by feeding the
get_prediction
function with a numpy image and a DetectionModel instance:
In [5]:
Copied!
result = get_prediction(read_image("demo_data/small-vehicles1.jpeg"), detection_model)
result = get_prediction(read_image("demo_data/small-vehicles1.jpeg"), detection_model)
- Visualize predicted bounding boxes and masks over the original image:
In [6]:
Copied!
result.export_visuals(export_dir="demo_data/")
Image("demo_data/prediction_visual.png")
result.export_visuals(export_dir="demo_data/")
Image("demo_data/prediction_visual.png")
Out[6]:
2. Sliced Inference with a YOLOv5 Model¶
- To perform sliced prediction we need to specify slice parameters. In this example we will perform prediction over slices of 256x256 with an overlap ratio of 0.2:
In [7]:
Copied!
result = get_sliced_prediction(
"demo_data/small-vehicles1.jpeg",
detection_model,
slice_height = 256,
slice_width = 256,
overlap_height_ratio = 0.2,
overlap_width_ratio = 0.2
)
result = get_sliced_prediction(
"demo_data/small-vehicles1.jpeg",
detection_model,
slice_height = 256,
slice_width = 256,
overlap_height_ratio = 0.2,
overlap_width_ratio = 0.2
)
Performing prediction on 15 number of slices.
- Visualize predicted bounding boxes and masks over the original image:
In [8]:
Copied!
result.export_visuals(export_dir="demo_data/")
Image("demo_data/prediction_visual.png")
result.export_visuals(export_dir="demo_data/")
Image("demo_data/prediction_visual.png")
Out[8]:
3. Prediction Result¶
- Predictions are returned as sahi.prediction.PredictionResult, you can access the object prediction list as:
In [9]:
Copied!
object_prediction_list = result.object_prediction_list
object_prediction_list = result.object_prediction_list
In [10]:
Copied!
object_prediction_list[0]
object_prediction_list[0]
Out[10]:
ObjectPrediction< bbox: BoundingBox: <(447, 308, 496, 342), w: 49, h: 34>, mask: None, score: PredictionScore: <value: 0.9154329299926758>, category: Category: <id: 2, name: car>>
- ObjectPrediction's can be converted to COCO annotation format:
In [11]:
Copied!
result.to_coco_annotations()[:3]
result.to_coco_annotations()[:3]
Out[11]:
[{'image_id': None, 'bbox': [447, 308, 49, 34], 'score': 0.9154329299926758, 'category_id': 2, 'category_name': 'car', 'segmentation': [], 'iscrowd': 0, 'area': 1666}, {'image_id': None, 'bbox': [321, 321, 62, 41], 'score': 0.887977659702301, 'category_id': 2, 'category_name': 'car', 'segmentation': [], 'iscrowd': 0, 'area': 2542}, {'image_id': None, 'bbox': [382, 278, 37, 26], 'score': 0.8796820640563965, 'category_id': 2, 'category_name': 'car', 'segmentation': [], 'iscrowd': 0, 'area': 962}]
- ObjectPrediction's can be converted to COCO prediction format:
In [12]:
Copied!
result.to_coco_predictions(image_id=1)[:3]
result.to_coco_predictions(image_id=1)[:3]
Out[12]:
[{'image_id': 1, 'bbox': [447, 308, 49, 34], 'score': 0.9154329299926758, 'category_id': 2, 'category_name': 'car', 'segmentation': [], 'iscrowd': 0, 'area': 1666}, {'image_id': 1, 'bbox': [321, 321, 62, 41], 'score': 0.887977659702301, 'category_id': 2, 'category_name': 'car', 'segmentation': [], 'iscrowd': 0, 'area': 2542}, {'image_id': 1, 'bbox': [382, 278, 37, 26], 'score': 0.8796820640563965, 'category_id': 2, 'category_name': 'car', 'segmentation': [], 'iscrowd': 0, 'area': 962}]
- ObjectPrediction's can be converted to imantics annotation format:
In [13]:
Copied!
result.to_imantics_annotations()[:3]
result.to_imantics_annotations()[:3]
Out[13]:
[<imantics.annotation.Annotation at 0x7f9d84c8fa60>, <imantics.annotation.Annotation at 0x7f9d84d118e0>, <imantics.annotation.Annotation at 0x7f9d84d11e50>]
- ObjectPrediction's can be converted to fiftyone detection format:
In [14]:
Copied!
result.to_fiftyone_detections()[:3]
result.to_fiftyone_detections()[:3]
Out[14]:
[<Detection: { 'id': '633305c1e757fa81eb2b8348', 'attributes': BaseDict({}), 'tags': BaseList([]), 'label': 'car', 'bounding_box': BaseList([ 0.41853932584269665, 0.5310344827586206, 0.04588014981273408, 0.05862068965517241, ]), 'mask': None, 'confidence': 0.9154329299926758, 'index': None, }>, <Detection: { 'id': '633305c1e757fa81eb2b8349', 'attributes': BaseDict({}), 'tags': BaseList([]), 'label': 'car', 'bounding_box': BaseList([ 0.300561797752809, 0.553448275862069, 0.05805243445692884, 0.0706896551724138, ]), 'mask': None, 'confidence': 0.887977659702301, 'index': None, }>, <Detection: { 'id': '633305c1e757fa81eb2b834a', 'attributes': BaseDict({}), 'tags': BaseList([]), 'label': 'car', 'bounding_box': BaseList([ 0.35767790262172283, 0.4793103448275862, 0.03464419475655431, 0.04482758620689655, ]), 'mask': None, 'confidence': 0.8796820640563965, 'index': None, }>]
4. Batch Prediction¶
- Set model and directory parameters:
In [3]:
Copied!
model_type = "yolov5"
model_path = yolov5_model_path
model_device = "cpu" # or 'cuda:0'
model_confidence_threshold = 0.4
slice_height = 256
slice_width = 256
overlap_height_ratio = 0.2
overlap_width_ratio = 0.2
source_image_dir = "demo_data/"
model_type = "yolov5"
model_path = yolov5_model_path
model_device = "cpu" # or 'cuda:0'
model_confidence_threshold = 0.4
slice_height = 256
slice_width = 256
overlap_height_ratio = 0.2
overlap_width_ratio = 0.2
source_image_dir = "demo_data/"
- Perform sliced inference on given folder:
In [4]:
Copied!
predict(
model_type=model_type,
model_path=model_path,
model_device=model_device,
model_confidence_threshold=model_confidence_threshold,
source=source_image_dir,
slice_height=slice_height,
slice_width=slice_width,
overlap_height_ratio=overlap_height_ratio,
overlap_width_ratio=overlap_width_ratio,
)
predict(
model_type=model_type,
model_path=model_path,
model_device=model_device,
model_confidence_threshold=model_confidence_threshold,
source=source_image_dir,
slice_height=slice_height,
slice_width=slice_width,
overlap_height_ratio=overlap_height_ratio,
overlap_width_ratio=overlap_width_ratio,
)
There are 3 listed files in folder: demo_data/
Performing inference on images: 0%| | 0/3 [00:00<?, ?it/s]
Performing prediction on 20 number of slices.
Performing inference on images: 33%|███▎ | 1/3 [00:01<00:02, 1.01s/it]
Prediction time is: 971.29 ms Performing prediction on 15 number of slices.
Performing inference on images: 67%|██████▋ | 2/3 [00:01<00:00, 1.59it/s]
Prediction time is: 318.99 ms Performing prediction on 15 number of slices.
Performing inference on images: 100%|██████████| 3/3 [00:01<00:00, 1.77it/s]
Prediction time is: 292.02 ms Prediction results are successfully exported to runs/predict/exp12