Skip to main content

A vision library for performing sliced inference on large images/small objects

Project description

SAHI: Slicing Aided Hyper Inference

Downloads PyPI version Conda version CI

A vision library for performing sliced inference on large images/small objects

teaser

Overview

Object detection and instance segmentation are by far the most important fields of applications in Computer Vision. However, detection of small objects and inference on large images are still major issues in practical usage. Here comes the SAHI to help developers overcome these real-world problems.

Getting started

Blogpost

Check the official SAHI blog post.

Installation

  • Install sahi using conda:
conda install -c obss sahi
  • Install sahi using pip:
pip install sahi
  • Install your desired version of pytorch and torchvision:
pip install torch torchvision
  • Install your desired detection framework (such as mmdet):
pip install mmdet

Usage

  • Sliced inference:
result = get_sliced_prediction(
    image,
    detection_model,
    slice_height = 256,
    slice_width = 256,
    overlap_height_ratio = 0.2,
    overlap_width_ratio = 0.2
)

Refer to inference notebook for detailed usage.

  • Slice an image:
from sahi.slicing import slice_image

slice_image_result, num_total_invalid_segmentation = slice_image(
    image=image_path,
    output_file_name=output_file_name,
    output_dir=output_dir,
    slice_height=256,
    slice_width=256,
    overlap_height_ratio=0.2,
    overlap_width_ratio=0.2,
)
  • Slice a coco formatted dataset:
from sahi.slicing import slice_coco

coco_dict, coco_path = slice_coco(
    coco_annotation_file_path=coco_annotation_file_path,
    image_dir=image_dir,
    slice_height=256,
    slice_width=256,
    overlap_height_ratio=0.2,
    overlap_width_ratio=0.2,
)

predict.py script usage:

python scripts/predict.py --source image/file/or/folder --model_path path/to/model --config_path path/to/config

will perform sliced inference on default parameters and export the prediction visuals to runs/predict/exp folder.

You can specify sliced inference parameters as:

python scripts/predict.py --slice_width 256 --slice_height 256 --overlap_height_ratio 0.1 --overlap_width_ratio 0.1 --iou_thresh 0.25 --source image/file/or/folder --model_path path/to/model --config_path path/to/config

If you want to export prediction pickles and cropped predictions add --pickle and --crop arguments. If you want to change crop extension type, set it as --visual_export_format JPG.

If you want to perform standard prediction instead of sliced prediction, add --standard_pred argument.

python scripts/predict.py --coco_file path/to/coco/file --source coco/images/directory --model_path path/to/model --config_path path/to/config

will perform inference using provided coco file, then export results as a coco json file to runs/predict/exp/results.json

If you don't want to export prediction visuals, add --novisual argument.

coco2yolov5.py script usage:

python scripts/coco2yolov5.py --coco_file path/to/coco/file --source coco/images/directory --train_split 0.9

will convert given coco dataset to yolov5 format and export to runs/coco2yolov5/exp folder.

coco_error_analysis.py script usage:

python scripts/coco_error_analysis.py results.json output/folder/directory --ann coco/annotation/path

will calculate coco error plots and export them to given output folder directory.

If you want to specify mAP result type, set it as --types bbox mask.

If you want to export extra mAP bar plots and annotation area stats add --extraplots argument.

If you want to specify area regions, set it as --areas 1024 9216 10000000000.

Adding new detection framework support

sahi library currently only supports MMDetection models. However it is easy to add new frameworks.

All you need to do is, creating a new class in model.py that implements DetectionModel class. You can take the MMDetection wrapper as a reference.

Contributers

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sahi-0.3.2.tar.gz (36.7 kB view hashes)

Uploaded Source

Built Distribution

sahi-0.3.2-py3-none-any.whl (40.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page