Quick Start¶
Welcome to SAHI! This guide will get you up and running with the core features of the library, including installation, performing predictions, and using the command-line interface.
1. Installation¶
Install SAHI using pip. For object detection, it's recommended to also install ultralytics
.
Install
Install or update the sahi
package using pip by running pip install -U sahi
. For more details on the sahi
package, visit the Python Package Index (PyPI).
You can also install sahi
directly from the Sahi GitHub repository. This can be useful if you want the latest development version. Ensure you have the Git command-line tool installed, and then run:
Conda can be used as an alternative package manager to pip. For more details, visit Anaconda. The Sahi feedstock repository for updating the conda package is available at GitHub.
Note
If you are installing in a CUDA environment, it is best practice to install ultralytics
, pytorch
, and pytorch-cuda
in the same command. This allows the conda package manager to resolve any conflicts. Alternatively, install pytorch-cuda
last to override the CPU-specific pytorch
package if necessary.
Clone the Sahi GitHub repository if you are interested in contributing to development or wish to experiment with the latest source code. After cloning, navigate into the directory and install the package in editable mode -e
using pip.
See the sahi
pyproject.toml file for a list of dependencies.
2. Sliced Prediction with Python¶
Sliced inference is the core feature of SAHI, allowing you to detect small objects in large images. Here's a simple example using the Python API:
from sahi import AutoDetectionModel
from sahi.predict import get_sliced_prediction
# Initialize a YOLOv8 model
detection_model = AutoDetectionModel.from_pretrained(
model_type='yolov8',
model_path='yolov8n.pt', # or any other YOLOv8 model
confidence_threshold=0.25,
device="cuda:0", # or "cpu"
)
# Run sliced prediction
result = get_sliced_prediction(
"path/to/your/image.jpg",
detection_model,
slice_height=512,
slice_width=512,
overlap_height_ratio=0.2,
overlap_width_ratio=0.2
)
# Export visualizations
result.export_visuals(export_dir="demo_data/")
# Get predictions as a list of objects
predictions = result.object_prediction_list
3. Prediction with the CLI¶
SAHI also provides a powerful command-line interface for quick predictions without writing any Python code.
sahi predict --model_path yolov8n.pt --model_type yolov8 --source /path/to/images/ --slice_height 512 --slice_width 512
This command will run sliced inference on all images in the specified directory and save the results.
Next Steps¶
You've now seen the basics of SAHI! To dive deeper, check out these resources:
- Prediction In-Depth: For advanced prediction options, see the Prediction Utilities guide.
- Demos: Explore our interactive notebooks in the demo directory for hands-on examples with different models.
- COCO Tools: Learn how to create, manipulate, and convert datasets in the COCO Utilities guide.
- All CLI Commands: See the full list of commands in the CLI documentation.