Skip to content

Add mask support for slic - #3850

Merged
emmanuelle merged 82 commits into
scikit-image:masterfrom
rfezzani:Add-mask-support-for-slic
May 7, 2020
Merged

emmanuelle merged 82 commits into
scikit-image:masterfrom
rfezzani:Add-mask-support-for-slic

Conversation

@rfezzani

@rfezzani rfezzani commented Apr 18, 2019 •

Copy link
Copy Markdown
Member

Description

Closes #1569, Closes #3722

To address #1569 and based on the work of @benjaminirving on maskSLIC, I added the mask support to slic.

The method is outlined here.

Masked area is label -1 in the returned labels. This adds some confusion to the discussion #3722 :-).

Checklist

For reviewers

  • Check that the PR title is short, concise, and will make sense 1 year
    later.
  • Check that new functions are imported in corresponding __init__.py.
  • Check that new features, API changes, and deprecations are mentioned in
    doc/release/release_dev.rst.
  • Consider backporting the PR with @meeseeksdev backport to v0.14.x

@pep8speaks

pep8speaks commented Apr 18, 2019 •

Copy link
Copy Markdown

Hello @rfezzani! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

Line 239:78: W504 line break after binary operator
Line 240:70: W504 line break after binary operator
Line 241:67: W504 line break after binary operator

Comment last updated at 2020-05-07 07:06:58 UTC

@jni jni left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@rfezzani just some high level comments because I'm travelling:

  1. Oh, man, this does throw more fuel into the fire for #3722! Personally I would prefer if we used this PR to pull off the band-aid and use 0 for the masked areas. It does mean deprecating the current behaviour when no mask is given, but I think it's worth it.
  2. There's some weird dtype choices here — why use int instead of Py_ssize_t for the segments, and why use int for the mask instead of bint? Then the check in the loop would be, imho, cleaner: if mask.size > 0 and not mask:
  3. What's the story with only_dist? This seems like an unrelated modification?
  4. Why reimplement regular_grid?

Apologies for the brevity, but thanks for tackling this! I'm pretty excited to get mask support for SLIC!

Comment thread skimage/segmentation/_slic.pyx Outdated
cdef Py_ssize_t[:, :, ::1] nearest_segments \
= np.empty((depth, height, width), dtype=np.intp)
cdef int[:, :, ::1] nearest_segments \
= np.full((depth, height, width), -1, dtype=np.int32)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why did you change this dtype?

@rfezzani rfezzani Apr 19, 2019 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@jni , first of all, thank you for your review. I tried to stick as much as possible to the reference implementation, so some of the implementation decisions come from this.

  1. I agree with you, introducing this feature can be the occasion to also close Consider returning segments starting from 1 from SLIC #3722 by considering that masked areas are 0 labelled.
  2. segments dtype did not change, it is still double. bint for the mask seems a better choice. The mask.size test is indeed cleaner.
  3. The only_dist parameter is for updating centroids positions without considering pixels color (it is the step 2 in the maskSLIC algorithm). It makes me think that renaming the parameter ignore_color is may be more clear.
  4. regular_grid reimplementation is just a matter of code style and code lines count reduction :-).

Concerning the last dtype modification that you point, labeling masked area with -1 in the current implementation prevents the use of the unsigned Py_size_t.

I will take your remarks into consideration and come with an other proposition soon.
Thanks again.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

THX, the double s makes sens then :-)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

:-)

Comment thread skimage/segmentation/_slic.pyx Resolved
@hmaarrfk

Copy link
Copy Markdown
Member

Could you please add a benchmark? I'm not saying that "I will only accept things that don't slow down scikit-image", but it would be good to at least understand how much "we will slow people down" if we add these conditions in the tight loop.

rfezzani and others added 5 commits April 19, 2019 15:29
@rfezzani

Copy link
Copy Markdown
Member Author

The last commit addresses the issue #3722 by deprecating the current behavior when no mask is given as suggested by @jni

@rfezzani

Copy link
Copy Markdown
Member Author

@hmaarrfk, I added a benchmark. I did not observed any significant performance drop if no mask is provided.
If a mask is provided, computing seeds positions may be slow, because a distance map over the mask is computed for each seed.
If the performance is an issue for this feature, one may consider computing seeds positions at lower scale, but this is not in the reference implementation of @benjaminirving.

@emmanuelle

Copy link
Copy Markdown
Member

About the kmeans algorithm to get the centroids, probably you can accelerate this part (15 ms on a 100*100 array is not negligible) by using a very small number of iterations (the default number is 10 in scipy's algo, 2 or 3 should be enough I believe). Disclaimer: there is a somewhat mysterious sentence in the docstring which I don't fully understand, maybe @stefanv would have a better understanding of it:

iter : int, optional
    Number of iterations of the k-means algorithm to run. Note
    that this differs in meaning from the iters parameter to
    the kmeans function.

@stefanv

stefanv commented Apr 14, 2020

Copy link
Copy Markdown
Member

About the kmeans algorithm to get the centroids, probably you can accelerate this part (15 ms on a 100*100 array is not negligible) by using a very small number of iterations (the default number is 10 in scipy's algo, 2 or 3 should be enough I believe). Disclaimer: there is a somewhat mysterious sentence in the docstring which I don't fully understand, maybe @stefanv would have a better understanding of it:

iter : int, optional
    Number of iterations of the k-means algorithm to run. Note
    that this differs in meaning from the iters parameter to
    the kmeans function.

I found this docstring:

iter : int, optional
   The number of times to run k-means, returning the codebook
   with the lowest distortion. This argument is ignored if
   initial centroids are specified with an array for the
   ``k_or_guess`` parameter. This parameter does not represent the
   number of iterations of the k-means algorithm.

I think it refers to the fact that kmeans is randomly initialized, so you can run kmeans to convergence several times over for different starting conditions, and then see which codebook is best?

@emmanuelle

Copy link
Copy Markdown
Member

@stefanv thank you for the explanation. So maybe this parameter was not what I thought and we can leave optimization of this kmeans for future work.

@rfezzani

rfezzani commented May 6, 2020

Copy link
Copy Markdown
Member Author

Is this PR candidate for 0.17?

@emmanuelle

Copy link
Copy Markdown
Member

I already approved so maybe someone can take a look ?

@grlee77 grlee77 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks good to me, but I only did a superficial review of the algorithm itself as this is not an area of expertise. The example looks good.

I have just a few minor changes requested.

Comment on lines 162 to +169
dist_color = 0
for c in range(3, n_features):
t = image_zyx[z, y, x, c - 3] - segments[k, c]
dist_color += t * t

if slic_zero:
dist_center += dist_color / max_dist_color[k]
else:
dist_color /= max_dist_color[k]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It looks like this color distance computation can be moved within the if not ignore_color block below

Comment thread skimage/segmentation/_slic.pyx Resolved
Comment thread skimage/segmentation/_slic.pyx Resolved
Comment on lines +177 to +179
* If spacing is None and mask is provided, chamfer distance
transform is used to homogeneously place seed points over the
mask, otherwise, the exact euclidian distance transform is used.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

isn't this text outdated? I think you switched to using k-means?

@emmanuelle

Copy link
Copy Markdown
Member

@rfezzani can you confirm that you addressed all comments raised by @grlee77 ? Given the time zone difference and the fact that I want to release ASAP I'm going to merge this one but I wanted to be sure.

@grlee77

grlee77 commented May 7, 2020

Copy link
Copy Markdown
Contributor

@emanuelle: I am okay with the remaining two comments being addressed in a followup PR after release if you want to get this in now.

One unresolved comment was for a minor efficiency improvement (no change in behavior). The other is an outdated docstring statement regarding the method of seed point initialization used.

Let me know and I can change my review to "Approved" if needed.

@emmanuelle

Copy link
Copy Markdown
Member

thanks @grlee77 I was not sure of your time zone or schedule :-). Yes it'd be helpful if you're willing to approve.

@grlee77 grlee77 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

marking as approved. please follow-up on the final two comments after merge. Thanks for your work on this @rfezzani

@emmanuelle
emmanuelle merged commit 6d4aaa0 into scikit-image:master May 7, 2020
@rfezzani

rfezzani commented May 7, 2020

Copy link
Copy Markdown
Member Author

Thank you @grlee77 for your review, I will open a PR ASAP to address your two last comments ;)
And thank you all for your valuable suggestions. 🍾

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider returning segments starting from 1 from SLIC Consider adding mask support for SLIC

8 participants