Repository navigation
Add mask support for slic - #3850
Conversation
|
Hello @rfezzani! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:
Comment last updated at 2020-05-07 07:06:58 UTC |
jni
left a comment
There was a problem hiding this comment.
@rfezzani just some high level comments because I'm travelling:
- 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.
- There's some weird dtype choices here — why use int instead of
Py_ssize_tfor 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: - What's the story with
only_dist? This seems like an unrelated modification? - Why reimplement
regular_grid?
Apologies for the brevity, but thanks for tackling this! I'm pretty excited to get mask support for SLIC!
| 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) |
There was a problem hiding this comment.
@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.
- 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
0labelled. - segments dtype did not change, it is still
double.bintfor the mask seems a better choice. Themask.sizetest is indeed cleaner. - The
only_distparameter 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 parameterignore_coloris may be more clear. regular_gridreimplementation 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.
There was a problem hiding this comment.
FYI: Py_ssize_t = signed size type
There was a problem hiding this comment.
THX, the double s makes sens then :-)
There was a problem hiding this comment.
If that makes sense to you, maybe you can help me make sense of these
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/snprintf-s-snprintf-s-l-snwprintf-s-snwprintf-s-l?view=vs-2019
boolean array is not supported by Cython (see https://cython-docs2.readthedocs.io/en/latest/src/tutorial/numpy.html) Thats why it is declared as uint8 in _slic.pyx
|
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. |
Masked areas are now labeled 0. When no mask is provided, current behavior (labelling starting from 0) is deprecated.
|
@hmaarrfk, I added a benchmark. I did not observed any significant performance drop if no mask is provided. |
Co-Authored-By: Stefan van der Walt <[email protected]>
Co-Authored-By: Stefan van der Walt <[email protected]>
…t-image into Add-mask-support-for-slic
|
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: |
I found this docstring: 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? |
|
@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. |
|
Is this PR candidate for 0.17? |
|
I already approved so maybe someone can take a look ? |
grlee77
left a comment
There was a problem hiding this comment.
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.
| 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] | ||
|
|
There was a problem hiding this comment.
It looks like this color distance computation can be moved within the if not ignore_color block below
| * 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. |
There was a problem hiding this comment.
isn't this text outdated? I think you switched to using k-means?
Co-authored-by: Gregory R. Lee <[email protected]>
Co-authored-by: Gregory R. Lee <[email protected]>
Co-authored-by: Gregory R. Lee <[email protected]>
Co-authored-by: Gregory R. Lee <[email protected]>
Co-authored-by: Gregory R. Lee <[email protected]>
Co-authored-by: Gregory R. Lee <[email protected]>
Co-authored-by: Gregory R. Lee <[email protected]>
|
@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. |
|
thanks @grlee77 I was not sure of your time zone or schedule :-). Yes it'd be helpful if you're willing to approve. |
|
Thank you @grlee77 for your review, I will open a PR ASAP to address your two last comments ;) |
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-1in the returned labels. This adds some confusion to the discussion #3722 :-).Checklist
./doc/examples(new features only)./benchmarks, if your changes aren't covered by anexisting benchmark
For reviewers
later.
__init__.py.doc/release/release_dev.rst.@meeseeksdev backport to v0.14.x