Skip to main content

Tile-based inference for segmentation of large images.

Project description

Conflux Segmentation

A Python library for tile-based inference for segmentation of large images.

Assuming you have a segmentation model that operates on tiles (e.g. 512 x 512), this library provides the plumbing to apply that model on a large image -- handling the padding, striding, and blending required.

Installation

pip install conflux-segmentation

Usage

The main Segmenter class assumes that the underlying tile-based segmenter outputs a multidimensional array of shape N x K x H x W where H and W are the height and width of a tile (e.g. 512), N is the batch size, and K is the output dimension (e.g. 1 for binary and > 1 for multiclass or multilabel).

Below we show an example of binary segmentation, although multiclass and multilabel are also supported. In this case, we assume the tile model outputs logits, so we specify "sigmoid" for the activation.

First, construct the Segmenter:

For PyTorch (e.g. with Segmentation Models PyTorch):

# $ pip install segmentation-models-pytorch
import segmentation_models_pytorch as smp
import torch
from conflux_segmentation import Segmenter

net = smp.Unet(encoder_name="tu-mobilenetv3_small_100", encoder_weights=None, activation=None)
net.load_state_dict(torch.load("/path/to/weights", weights_only=True))
net.eval()
segmenter = Segmenter.from_torch(net, activation="sigmoid")
# Alternatively, if your model already has a Sigmoid layer at the end:
# import torch.nn as nn
# sigmoid_net = nn.Sequential(net, nn.Sigmoid()).eval()
# segmenter = Segmenter.from_torch(net)

Or, for ONNX Runtime:

import onnxruntime as ort
from conflux_segmentation import Segmenter

session = ort.InferenceSession("/path/to/model.onnx")
segmenter = Segmenter.from_onnx(session, activation="sigmoid")

Then, to segment a large image:

# $ pip install opencv-python-headless
import cv2

# H x W x 3 image array of np.uint8
image = cv2.cvtColor(cv2.imread("/path/to/large/image"), cv2.COLOR_BGR2RGB)

result = segmenter(image).to_binary()
# H x W boolean array
mask = result.get_mask()
assert mask.shape == image.shape[:2]
assert (mask == True).sum() + (mask == False).sum() == mask.size

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

conflux_segmentation-0.2.0.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

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

conflux_segmentation-0.2.0-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file conflux_segmentation-0.2.0.tar.gz.

File metadata

  • Download URL: conflux_segmentation-0.2.0.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.12.3 Linux/6.8.0-1021-azure

File hashes

Hashes for conflux_segmentation-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e2389ea14e2c66137997fc7dbbd938b1d9b56f98fb5bd13bb431eb55ae49d082
MD5 8dda477a2b9e17bb3c437efbee994043
BLAKE2b-256 f27d1f3e7db42409c4dab3e9d3b05c0ea0a7795e44f85e1907e4e818bd9557ff

See more details on using hashes here.

File details

Details for the file conflux_segmentation-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: conflux_segmentation-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 13.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.12.3 Linux/6.8.0-1021-azure

File hashes

Hashes for conflux_segmentation-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 401a729fce69274d91bcd2e3e43479594ef39b43825bbd6e956c8c17f59ff0df
MD5 02afa3b229bf1496b64f913e455f7e16
BLAKE2b-256 b2e0cd4eb0abde12c6e2a0c72d7d95c8a05508a4cd05b326b9542d2a0b110980

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