Skip to main content

Simple Python inference SDK for ONNX models

Project description

SW Inference SDK

Lightweight Python SDK for running inference with ONNX models.

Supports: Object detection and instance segmentation models (BBox-S, BBox-M, BBox-L, Segm-S, Segm-M).

Installation

Requirements: Python ≥ 3.10

pip install sw-inference[cpu]

For GPU support:

pip install sw-inference[gpu]   # minimal
pip install sw-inference[cuda]  # with pre-installed cuda and cudnn as python package

GPU support requires CUDA 12.x and CuDNN. See here for more details. If unsure, use the extra [cuda] to install everything in the python environment. This is only valid for Linux distributions.

Quick Start

Command Line Usage

Preparation: Unzip the downloaded model.

Run inference on an image and save visualization:

python examples/basic_inference.py \
  --model-path path/to/model_dir \
  --input-image-path image.jpg \
  --output-image-path output.jpg

Python API Usage

from sw_inference import SWInference
import cv2

# Load model
model = SWInference("path/to/model_dir", device="cuda")

# Run inference
image = cv2.imread("image.jpg")
detections = model.infer_image(image)

# Access results
print(f"Found {len(detections)} objects")
for i in range(len(detections)):
    bbox = detections.xyxy[i]
    confidence = detections.confidence[i]
    class_name = model.get_class_name(detections.class_id[i])
    print(f"{class_name}: {confidence:.2f}")

Process multiple images:

for img_path in image_paths:
    image = cv2.imread(img_path)
    detections = model.infer_image(image)
    # ... process detections

Patching

Patching enables high-resolution inference by splitting large images into overlapping patches, running inference on each patch, and merging the results. This is useful when objects in your images are small relative to the image size.

Automatic Configuration

The optimal patching configuration can be automatically included in the exported model based on your validation set (or test set, if available).

Manual Configuration

If you have an existing exported model and want to add or modify patching, add a patching section to your config_export.json:

{
  "patching": {
    "enabled": true,
    "resize_to_img_size": [512, 512],
    "minimum_crop_width": 256,
    "maximum_crop_width": 1024,
    "overlap_size": 50,
    "merge_threshold": 0.5,
    "num_scales": 1,
    "overlap_filter": "NMM"
  }
}

Configuration Options

Parameter Type Default Description
enabled bool true Enable/disable patching
resize_to_img_size [int, int] [512, 512] Size to resize each patch to [width, height]
minimum_crop_width int required Minimum patch crop width
maximum_crop_width int required Maximum patch crop width
overlap_size int 50 Overlap in pixels between adjacent patches
merge_threshold float 0.5 IoU threshold for merging overlapping detections
num_scales int 1 Number of patching scales (higher values increase inference time)
overlap_filter string "NMM" Strategy for handling overlapping detections: "NMM" or "NMS"

Development

# Install with dev dependencies
pip install -e ".[dev]"

# Run tests
pytest -v .

# Lint and format
./scripts/format.sh

License

MIT License - see LICENSE for details.

Project details


Download files

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

Source Distribution

sw_inference-1.0.0.tar.gz (10.4 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sw_inference-1.0.0-py3-none-any.whl (10.5 MB view details)

Uploaded Python 3

File details

Details for the file sw_inference-1.0.0.tar.gz.

File metadata

  • Download URL: sw_inference-1.0.0.tar.gz
  • Upload date:
  • Size: 10.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.3 {"installer":{"name":"uv","version":"0.10.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for sw_inference-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a4526f0c57ec1d72a327faf049d24f9e5bd89f60c679c095624435c68d22e5bf
MD5 ef85ec69b2c1c89a775951cdb0fc71b4
BLAKE2b-256 24f4d3772628226332a3c8f981863510c630277256e01c6d5d0857296dc7ea9d

See more details on using hashes here.

File details

Details for the file sw_inference-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: sw_inference-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 10.5 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.3 {"installer":{"name":"uv","version":"0.10.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for sw_inference-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aa244379a0b885e9431b0d008e91b6d46f98eccaf7a1cad816aa23843d57acf0
MD5 c6abe7d85a806866a9feb0f67cb8783a
BLAKE2b-256 0f80af102ff66830f3eb8458cc329face9921f6fca425fd02fb168344ba816ea

See more details on using hashes here.

Supported by

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