Skip to main content

Compute IoU matrices and one-to-one matching for segmentation evaluation

Project description

ioumatch

ioumatch is a lightweight Python library for evaluating instance segmentation masks.
It automatically computes the IoU matrix, performs a one-to-one matching between predicted and ground-truth objects (greedy or Hungarian), and outputs True Positives (TP), False Positives (FP), False Negatives (FN), and the F1-score.

It seamlessly handles:

  • Binary masks (0/1 or 0/255), automatically converted into instance labels,
  • Already-labeled masks (each object has its own integer ID),
  • Image files (PNG, TIFF, etc.) or NumPy arrays directly.

Installation

pip install ioumatch

Core Features

Function Description
evaluate_image(pred, gt) Evaluate a single image (NumPy arrays)
evaluate_paths(pred_path, gt_path) Evaluate two mask image files
evaluate_pairs(pred_glob, gt_glob) Evaluate a batch of image pairs using glob patterns
to_instance_labels(mask) Convert a binary mask into connected-component labels
iou_matrix_from_labels(pred, gt) Compute the IoU matrix between prediction and GT
match_greedy / match_hungarian One-to-one object matching algorithms

Quick Example

import numpy as np
import ioumatch

# --- Example with NumPy arrays ---
pred = np.array([
    [0, 1, 1],
    [0, 0, 1],
    [0, 0, 0]
], dtype=np.uint8)

gt = np.array([
    [0, 1, 1],
    [0, 0, 0],
    [0, 0, 2]
], dtype=np.uint8)

res = ioumatch.evaluate_image(pred, gt, threshold=0.5)
print(res)
#  {'tp': 1, 'fp': 1, 'fn': 1, 'f1': 0.5, 'iou_matrix': array(...)}

# --- Example with files ---
res = ioumatch.evaluate_paths("pred_masks/img_01.png",
                              "gt_masks/img_01.png",
                              threshold=0.3)
print(res["f1"])

# --- Evaluate multiple pairs ---
agg, per_image = ioumatch.evaluate_pairs("pred_masks/*.png", "gt_masks/*.png", threshold=0.5)
print("Global F1:", agg["f1"])

Key Parameters

Parameter Type Default Description
threshold float 0.5 Minimum IoU to consider a valid match
method str "greedy" "greedy" = simple matching, "hungarian" = optimal if SciPy is installed
inclusive bool False If True, accepts IoU values equal to the threshold
normalize bool True If True, automatically converts binary masks to instance-labeled masks

How It Works

  1. Input normalization
    Binary masks (0/1 or 0/255) are converted to instance-labeled masks using connected components.

  2. IoU matrix computation
    An IoU value is computed for each pair (predicted_object, ground_truth_object).

  3. One-to-one matching

    • Greedy mode: iteratively picks the highest IoU and removes the corresponding row and column.
    • Hungarian mode: uses optimal global assignment (via scipy.optimize.linear_sum_assignment).
  4. Metric computation

    • TP – matched pairs
    • FP – predictions with no corresponding GT
    • FN – GT objects not detected
    • F1 – harmonic mean:
      F1 = 2 * TP / (2 * TP + FP + FN)
      

Example Visualization

import matplotlib.pyplot as plt
from ioumatch import iou_matrix_from_labels, to_instance_labels

pred = to_instance_labels(pred)
gt = to_instance_labels(gt)
M = iou_matrix_from_labels(pred, gt)

plt.imshow(M, cmap='hot', interpolation='nearest')
plt.title("IoU Matrix: Predictions vs Ground Truth")
plt.xlabel("Ground Truth objects")
plt.ylabel("Predicted objects")
plt.colorbar()
plt.show()

API Summary

Module Content
ioumatch.iou Builds IoU matrices between labeled masks
ioumatch.matching Matching algorithms (greedy and Hungarian)
ioumatch.metrics Computes TP, FP, FN, F1, and handles image/batch evaluation
ioumatch.utils Image I/O and binary→label conversions

Dependencies

Required

  • numpy

Recommended

  • opencv-python – for connected components in binary masks
  • matplotlib – for IoU matrix visualization
  • scipy – for the Hungarian optimal matching


Example Output

When evaluating one image pair:

TP = 2
FP = 1
FN = 1
F1 = 0.6667

And the IoU matrix might look like this:

[[0.9, 0.0],
 [0.2, 0.7]]

Where:

  • Row = predicted objects
  • Column = ground truth objects

License

MIT License © 2025
You’re free to use, modify, and distribute ioumatch.
If you use it in research or a public project, a short mention is appreciated.

ioumatch - a simple, clean way to evaluate segmentation models without writing another 200-line script.

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

ioumatch-0.1.1.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

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

ioumatch-0.1.1-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file ioumatch-0.1.1.tar.gz.

File metadata

  • Download URL: ioumatch-0.1.1.tar.gz
  • Upload date:
  • Size: 12.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for ioumatch-0.1.1.tar.gz
Algorithm Hash digest
SHA256 932c6414f18450fd933a558931da3a72e06c45e2e1bd72df9ac9251e341eba4d
MD5 906c6a5dbb1bea564bb8df0f9f32ba35
BLAKE2b-256 e6d8d10bad06e4657af1a62bbe12ee38d81db2f590ce9d8d8d6979b9ffb5384f

See more details on using hashes here.

File details

Details for the file ioumatch-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: ioumatch-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for ioumatch-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f1bdcf08be0c583b3b02730c865adb5a25f4d398e14318f56be06e4f5a0d73d1
MD5 ce2e09392c7cdcd78d2ee6f013ecc66b
BLAKE2b-256 34636b8e6e9b407baf22f6d9363516e9f59b0d2f6c98ce1ade0961e30c6eb7e5

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