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.0.tar.gz (12.2 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.0-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ioumatch-0.1.0.tar.gz
  • Upload date:
  • Size: 12.2 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.0.tar.gz
Algorithm Hash digest
SHA256 d951a8c2ec81cf6d08769bb5cbf9dc273cb0827269ac9de499f51d7c87fe7d01
MD5 e2d11c6d2be84a01484e914958cad568
BLAKE2b-256 03028d569b71a4246dfa6142c724b4b8ff9f8a5cc546e3892d3b731a2249113b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ioumatch-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ec612ff8965331c8ae356a8cb406ad5e706f7f3cd5fad35665a5a910a5bdbf75
MD5 79cbe2a27f65e04b1f0be57668b4a6cc
BLAKE2b-256 3800e9913bfe9af66bcbe4ef0e5330758251567a7b75b05507be464d989d9abc

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