Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Region Properties Performance Overhaul - Part 4: Moment-Based Properties #846

Merged
merged 20 commits into from
Apr 4, 2025

Conversation

grlee77
Copy link
Contributor

@grlee77 grlee77 commented Mar 3, 2025

Please review #843 first as that explains the general approach in more detail.

Overview

This MR implements many properties based on image moments. These include centroids, normalized/central moments and measurements based on the inertia-tensor. These come in both unweighted and "weighted" variants where the weighted versions rely on values in a corresponding intensity_image.

The primary kernels that run on all image pixels are the computation of moments or moments_weighted. Other properties are typically derived from these raw moments and are very fast to compute once the moments of the required order have been computed.

Unweighted properties implemented are:

  • axis_major_length
  • axis_minor_length
  • centroid
  • centroid_local
  • moments
  • moments_central
  • moments_normalized
  • moments_hu
  • inertia_tensor
  • inertia_tensor_eigvals

Weighted properties implemented are:

  • centroid_weighted
  • centroid_weighted_local
  • moments_weighted
  • moments_weighted_central
  • moments_weighted_normalized
  • moments_weighted_hu

And a couple of properties that are useful, but no currently in scikit-image

  • axis_lengths
    • retrieves lengths of all ellipsoid axes not just the largest and smallest (axis_major_length is axis_lengths[:, 0] while -axis_minor_length is axis_lengths[:, -1])
  • inertia_tensor_eigenvectors
    • need this to know the spatial direction the ellipsoid axes correspond to

Benchmarks

Performance vs. Image Size (with # regions fixed)

The following show performance for a small fixed number of label regions at different spatial scale in both 2D and 3D

In 2D, there are 16 labeled regions for shapes ranging from (64, 64) up to (8192, 8192)
regionprops_moment_vs_size

In 3D, there are 8 labeled regions for shapes ranging from (32, 32) up to (512, 512, 512)
regionprops_3d_moment_vs_size

Note that "multi-moment" is the time to compute the following list of region properties rather than just a single property

properties = ["moments", "moments_central", "moments_normalized", "inertia_tensor", "inertia_tensor_eigvals", "axis_minor_length", "axis_major_length", "centroid", "centroid_local"]

and `"multi-moment-weighted" corresponds to

properties = ["moments_weighted", "moments_weighted_central", "moments_weighted_normalized", "centroid_weighted", "centroid_weighted_local"]

Performance vs. Region Size (with image shape fixed)

Here a single large 2D image (7680, 4320) is used, but with varying numbers of labeled regions within it. The total % of foreground vs. background voxels remains similar (i.e. regions are larger when there are fewer of them). The number of regions range from 4 up through 16,384.
regionprops_moment_vs_object_size

Here a single large 3D image (384, 384, 384) is used, but with varying numbers of labeled regions within it. The number of regions range from 8 up through 1,728.
regionprops_3d_moment_vs_object_size

Benchmark conclusions

Note: The results for the older GPU-based regionprops from cuCIM are not shown here. However, for many properties that implementation became much slower as the number of regions increased. We can see that for the new implementation proposed here, performance does not continuously decline once a certain number of objects are reached. There is still better acceleration for a small number of objects, but acceleration holds even for very large numbers of objects.

The functions introduced here are not being added to the public API. They will
be used behind the scenes from `regionprops_table` to enable orders of magnitude
faster computation of region properties for all labels in an image. The basic
approach here is to compute a property for all labels in an image from a single
CUDA kernel call. This is in contrast to the approach from the `RegionProperties`
class which first splits the full image into small sub-images corresponding to
each region and then loops over these small sub-images, computing the requested
property for each small region in turn. That approach is not amenable to good
acceleration on the GPU as individual regions are typically small.

Provides batch implementation that computes the following properties for all properties
in a single kernel call:

- bbox
- label_filled (creates version of label_image with all holes filled)
- num_pixels
- num_pixels_filled
- num_perimeter_pixels (number of pixels at perimeter of each labeled region)
- num_boundary_pixels (number of pixels touching the image boundary for each region)

The following properties are simple transformations of the properties above and
have negligable additional cost to compute:

- area
- area_bbox
- area_filled
- equivalent_diameter_area
- equivalent_spherical_perimeter (as in ITK)
- extent
- perimeter_on_border_ratio (as in ITK)
- slice

The following split the label image into a list of sub-images or subsets of coordinates
where each element in the list corresponds to a label. The background of the label image
has value 0 and is not represented in the sequences. Sequence entry `i` corresponds to
label `i + 1`. In most cases, these will not be needed as properties are now computed
for all regions at once from the labels image, but they are provided for completeness
and to match the scikit-image API.

- coords
- coords_scaled
- image (label mask subimages)
- image_convex (convex label mask subimages)
- image_intensity (intensity_image subimages)
- image_filled (subimages of label mask but with holes filled)
- label (sequence of integer label ids)

Test cases are added that compare the results of these batch computations to results
from scikit-image `regionprops_table`.
This function operates similarly to `regionprops_table`. In a future commit,
once all properties have been supported, it will be used within the existing
regionprops_table function so that it will provide much higher performance.
- intensity_mean
- intensity_std
- intensity_min
- intensity_max

Both single and multi-channel intensity images are supported
@grlee77 grlee77 requested review from a team as code owners March 3, 2025 15:28
@grlee77 grlee77 requested a review from gforsyth March 3, 2025 15:28
@grlee77 grlee77 force-pushed the regionprops_part4_moments branch from 15280b1 to a7f5274 Compare March 3, 2025 15:36
@grlee77 grlee77 added improvement Improves an existing functionality non-breaking Introduces a non-breaking change performance Performance improvement labels Mar 3, 2025
@grlee77 grlee77 added this to cucim Mar 3, 2025
@grlee77 grlee77 added this to the v25.04.00 milestone Mar 3, 2025
Copy link
Contributor

@gforsyth gforsyth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving for packaging codeowners

@grlee77 grlee77 force-pushed the regionprops_part4_moments branch from a7f5274 to df640e3 Compare March 4, 2025 00:55
Copy link
Contributor

@gigony gigony left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @grlee77 ! It looks good to me!

tmp2 = m00 - m11;
tmp2 *= tmp2;
tmp2 += tmp1;
tmp2 = sqrt(tmp2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is needed but suggesting here.

Suggested change
tmp2 = sqrt(tmp2);
// add numerical stability check
tmp2 = max(tmp2, 0.0); // ensure non-negative before sqrt
tmp2 = sqrt(tmp2);
// more robust handling of small values
const double eps = 1e-10;
if (fabs(tmp1) < eps) {
tmp1 = 0.0;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think tmp2 and tmp1 individually cannot be negative due to squares that happen above (e.g. tmp1 = m01 * m01 and tmp2 *= tmp2).

I think the problematic case is when the magnitude of tmp1 is within numerical precision of tmp2 (e.g. for a perfectly circular region we would expect equal eigenvalues). The max(tmp1 - tmp2, 0.0) is to handle that case. The other max in max(tmp1 + tmp2, 0.0) seems unnecessary and could be removed.

@grlee77 grlee77 marked this pull request as draft April 3, 2025 19:17
Copy link

copy-pr-bot bot commented Apr 3, 2025

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@grlee77 grlee77 changed the base branch from branch-25.04 to branch-25.06 April 3, 2025 19:17
@grlee77 grlee77 force-pushed the regionprops_part4_moments branch from 433d166 to aa3a89a Compare April 3, 2025 22:09
@grlee77 grlee77 marked this pull request as ready for review April 4, 2025 01:13
@grlee77
Copy link
Contributor Author

grlee77 commented Apr 4, 2025

/merge

@rapids-bot rapids-bot bot merged commit 3799368 into rapidsai:branch-25.06 Apr 4, 2025
48 checks passed
@github-project-automation github-project-automation bot moved this to Done in cucim Apr 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement Improves an existing functionality non-breaking Introduces a non-breaking change performance Performance improvement
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants