Skip to main content

fastlabelrle

Fast direct COCO RLE encoding for integer instance-label images.

Most COCO tooling encodes binary masks. If a segmentation already exists as a single integer label image, a common workflow is therefore:

for instance_id in ids:
    binary = labels == instance_id
    rle = encode(binary)

That repeatedly scans the full image and materializes one full-resolution binary mask per instance. fastlabelrle instead scans the integer label image directly and emits compressed COCO RLE counts for every nonzero label.

Usage

import numpy as np
from fastlabelrle import encode

labels = np.array(
    [
        [0, 0, 17, 17],
        [0, 91, 91, 17],
        [0, 91, 0, 5002],
    ],
    dtype=np.uint32,
)

encoded = encode(labels)
print(encoded.ids)     # [  17   91 5002]
print(encoded.counts)  # compressed COCO RLE bytes, one entry per ID

The original sparse label IDs are preserved. uint32 and uint64 label images are supported. The RLE image size is labels.shape.

To construct standard COCO segmentation dictionaries:

rles = [
    {"size": list(labels.shape), "counts": counts}
    for counts in encoded.counts
]

Why it is fast

The speedup comes from avoiding the binary-mask abstraction entirely. fastlabelrle scans the integer label image once in COCO column-major order, collects foreground runs for each observed label, and converts those runs directly to compressed COCO counts.

The comparison libraries below are already fast binary-mask encoders. The expensive part in this use case is constructing one full-resolution binary mask per instance before those encoders can run.

Benchmarks

These are end-to-end timings from the same uint32 label image to COCO RLEs for every instance. They include binary-mask construction for libraries whose APIs require binary masks. They are not encoder-only benchmarks.

Environment: macOS, CPython 3.12.13, NumPy 2.5.2, pycocotools 2.0.11, rpycocotools 0.0.7, hotcoco 0.5.0.

Label image Instances fastlabelrle pycocotools rpycocotools hotcoco
1024 x 1024 1,024 3.01 ms 4.57 s 2.40 s 5.08 s
2048 x 2048 4,096 35.72 ms 84.92 s 49.06 s 95.33 s

On the 2048 x 2048 / 4,096-instance case, that is approximately 2,378x faster than the pycocotools workflow, 1,374x faster than rpycocotools, and 2,669x faster than hotcoco.

Batching does not remove the representation cost. With batch size 32 on the same 2048 x 2048 case, pycocotools took 84.55 s and hotcoco took 106.89 s, while each temporary binary batch was 128 MiB. The original uint32 label image was 16 MiB.

The benchmark uses deterministic non-overlapping ellipse-like instances. Results will vary with image size, instance count, mask fragmentation, hardware, and library versions.

Run it yourself:

uv sync --group benchmark
uv run python benchmarks/benchmark.py --size 1024 --instances 1024 --repeats 5
uv run python benchmarks/benchmark.py --size 2048 --instances 4096 --repeats 1 --batch-sizes 32

Development

uv sync --group dev
uv run pytest
uv run ruff check .
uv run mypy src

Scope

fastlabelrle intentionally has one job: direct 2D integer-label-image to compressed COCO RLE encoding on CPU. It has no dependency on fastlabelops and does not relabel or otherwise modify instance IDs.

Metadata

Release files for fastlabelrle 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastlabelrle 0.1.0
File Size Uploaded Provenance
fastlabelrle-0.1.0.tar.gz 13.0 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for fastlabelrle 0.1.0
File Interpreter ABI Platform
fastlabelrle-0.1.0-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details

Total release size: 33.8 kB

Release files / fastlabelrle-0.1.0.tar.gz

Download URL fastlabelrle-0.1.0.tar.gz
Size 13.0 kB
Tags Source
SHA-256 checksum
How to use checksums
55ee1846c6b07e4811efc283ea59aaf8ce4705b0fe63ef342725f77be1402172
BLAKE2b-256 checksum
How to use checksums
3e389b857648f4932dd9e3eabca75c1df171223575ef8156a000629ae2254651
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.23 {"installer":{"name":"uv","version":"0.11.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / fastlabelrle-0.1.0-cp312-cp312-macosx_11_0_arm64.whl

Download URL fastlabelrle-0.1.0-cp312-cp312-macosx_11_0_arm64.whl
Size 20.8 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
66a82e19e1fc3ce3d13daa6865ed53cccbaf88c70ef7b0b7e8e186fcf6b4a83c
BLAKE2b-256 checksum
How to use checksums
1bf2773a4d9d292221cc15daabf609c58d11516efdaebc27c73808ddcce3ea6d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.23 {"installer":{"name":"uv","version":"0.11.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for fastlabelrle 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page