Skip to content

Add segmentation evaluation functions - #3354

Closed
seanbudd wants to merge 26 commits into
scikit-image:masterfrom
seanbudd:measure-compare-gala-port
Closed

seanbudd wants to merge 26 commits into
scikit-image:masterfrom
seanbudd:measure-compare-gala-port

Conversation

@seanbudd

@seanbudd seanbudd commented Aug 15, 2018 •

Copy link
Copy Markdown
Contributor

Description

As requested in this issue we have merged relevant code from gala.evaluate under the BSD license.

Added the following segmentation metrics:

  • raw edit distance
  • adapted rand error
  • vi and split vi (variation of information)

Checklist

  • Clean style in the spirit of PEP8
  • Docstrings for all functions
  • Gallery example in ./doc/examples (new features only)
  • Benchmark in ./benchmarks, if your changes aren't covered by an
    existing benchmark
  • Unit tests
  • rename params from im_true/im_test

[For detailed information on these and other aspects see scikit-image contribution guidelines]

References

closes issue #2345

For reviewers

(Don't remove the checklist below.)

  • 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

@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.

@seanbudd thanks for finally getting this done! There's a few changes to be made, and in addition to the small changes we need both tests and a gallery example.

I recommend looking at Ch5 from Elegant SciPy for some inspiration. (You should use an image from the gallery, though, such as coffee, rather than an external image.)

Comment thread skimage/measure/__init__.py Resolved
Comment thread skimage/measure/segmentation_metrics.py Resolved
Comment thread skimage/measure/segmentation_metrics.py Resolved
Comment thread skimage/measure/segmentation_metrics.py Outdated

return (are, precision, recall)

def compare_rand_by_threshold(im_true, im_test, npoints=None):

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.

We probably want to adapt these names for scikit-image more specifically:

im_true -> image_reference
im_test -> image_test
npoints -> n_points

But actually, remove rand_by_threshold altogether. 😂

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.

I like pytorch convention: input_ and target.

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.

Another option is: image_pred, image_true.

Copy link
Copy Markdown
Contributor Author

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

Choose a reason for hiding this comment

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

Hmmm, I consider this among the consistency warts in the scikit-image API that I want to fix before 1.0. @soupault are you really suggesting a parameter name with a trailing underscore??? At any rate I still prefer "reference" to "target", with the latter being too machine learning-specific for my tastes. We are also already using 'reference' in register_translation, too.

Comment thread skimage/measure/segmentation_metrics.py Resolved
Comment thread skimage/measure/segmentation_metrics.py Resolved
Comment thread skimage/measure/segmentation_metrics.py Resolved
Comment thread skimage/measure/segmentation_metrics.py Resolved
Comment thread skimage/measure/__init__.py Outdated
'moments',
'moments_central',
'moments_coords',
'moments_coords_central'

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.

Whoa! That is, actually, a bug, and the fix has to be backported.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

should this be done via a different request?

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.

!!!! wow! Good catch @seanbudd (and @soupault, I missed this when reading the diff!). Yes, please submit a different PR with just this fix.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

14f4fc5

Here's the git blame results for reference, I'll prepare another PR.

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.

@seanbudd at scikit-image, we usually prefer not to throw our colleagues under the bus, except for mea culpas, =) or where the error is super-subtle and looking at the commit actually aids in understanding it. But, thanks for the reference. =)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@jni oops! Was definitely not trying to throw anyone under the bus!

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.

👍

git blame is an unfortunate name for the command. Humorous at times, but on the whole a negative imho!

Comment thread skimage/measure/segmentation_metrics.py Outdated

return (are, precision, recall)

def compare_rand_by_threshold(im_true, im_test, npoints=None):

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.

I like pytorch convention: input_ and target.

Comment thread skimage/measure/segmentation_metrics.py Outdated
]

def compare_raw_edit_distance(im_true, im_test, size_threshold=1000):
"""Compute the edit distance between two segmentations.

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.

I think for metrics it is highly important to provide a comprehensive description and include the corresponding formula's.

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.

Yes I agree. Actually I'm not positive that this particular metric computation is correct, so I suggest we remove it for now.

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.

Having said that, for the other metrics, please provide references to papers or other implementations. They are mostly easy to find. =)

Comment thread skimage/measure/segmentation_metrics.py Outdated

return (are, precision, recall)

def compare_rand_by_threshold(im_true, im_test, npoints=None):

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.

Another option is: image_pred, image_true.

Comment thread skimage/measure/segmentation_metrics.py Outdated
vi
"""
_, _, _ , hxgy, hygx, _, _ = vi_tables(im1, im2)
# false merges, false splits

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.

Here you want to update the note to explain that this comment is true when image_1 is the test and image_2 is the reference (or vice versa, I'm not too sure right now. =P) I might even vote to change the argument names to be more prescriptive (image_test, image_reference), even though conceptually the VI is symmetric and this wouldn't make any practical difference. But it's better for newcomers to understand.

Comment thread skimage/measure/segmentation_metrics.py Resolved
@pep8speaks

pep8speaks commented Aug 23, 2018 •

Copy link
Copy Markdown

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

Line 31:61: W291 trailing whitespace
Line 32:45: W291 trailing whitespace

Line 8:1: E302 expected 2 blank lines, found 1
Line 230:53: E226 missing whitespace around arithmetic operator
Line 230:68: W291 trailing whitespace

Line 8:1: E302 expected 2 blank lines, found 1
Line 13:1: E302 expected 2 blank lines, found 1
Line 15:31: E231 missing whitespace after ','

Comment last updated at 2019-03-18 07:32:07 UTC

@codecov-io

codecov-io commented Aug 23, 2018 •

Copy link
Copy Markdown

Codecov Report

❗ No coverage uploaded for pull request base (master@4c1cbad). Click here to learn what that means.
The diff coverage is 72.72%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master    #3354   +/-   ##
=========================================
  Coverage          ?   86.74%           
=========================================
  Files             ?      342           
  Lines             ?    27588           
  Branches          ?        0           
=========================================
  Hits              ?    23932           
  Misses            ?     3656           
  Partials          ?        0
Impacted Files Coverage Δ
skimage/measure/__init__.py 100% <100%> (ø)
skimage/measure/tests/test_segmentation_metrics.py 100% <100%> (ø)
skimage/measure/segmentation_metrics.py 68.96% <68.96%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4c1cbad...c1171b1. Read the comment docs.

@seanbudd seanbudd mentioned this pull request Aug 23, 2018
9 tasks

@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.

Hey @seanbudd! Thanks for the changes! The code is already much cleaner. There's still a few changes to be made, and tests need to be added. (I think tests/test_evaluate.py in gala should have some already.)

Comment thread skimage/measure/__init__.py Outdated
'compare_adapted_rand_error',
'compare_raw_edit_distance',
'compare_split_vi',
'compare_vi',

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.

These need updating!

Comment thread skimage/measure/__init__.py Outdated
compare_rand_by_threshold,
compare_raw_edit_distance,
compare_split_vi,
compare_vi)

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.

(minor quibble: align with the open parenthesis at the top)

Comment thread skimage/measure/segmentation_metrics.py Outdated
Parameters
----------
im_true : ndarray
Ground-truth image.

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.

ground truth label image

Comment thread skimage/measure/segmentation_metrics.py Outdated

Parameters
----------
im_true : ndarray

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.

ndarray of int

Comment thread skimage/measure/segmentation_metrics.py Outdated

return (are, precision, recall)

def compare_variation_of_information(im1, im2, weights=np.ones(2)):

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.

Let's go with im_true and im_test here also, for consistency. We can update the API later if we decide to go with e.g. reference, test.

Also, let's make weights a keyword-only argument.

Comment thread skimage/measure/segmentation_metrics.py Outdated
[1] Meila, M. (2007). Comparing clusterings - an information based
distance. Journal of Multivariate Analysis 98, 873-895.
"""
return np.dot(weights, compare_split_variation_of_information(im1, im2))

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.

Let's use Python 3 matmul syntax here: weights @ compare_split_variation_of_information()

Comment thread skimage/measure/segmentation_metrics.py Outdated
"""
return np.dot(weights, compare_split_variation_of_information(im1, im2))

def compare_split_variation_of_information(im1, im2):

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.

As above: im_true, im_test.

"""
_, _, _ , hxgy, hygx, _, _ = _vi_tables(im1, im2)
# false merges, false splits
return np.array([hygx.sum(), hxgy.sum()])

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.

Depending on the order of im_true and im_test, you might need to reorder "false merges, false splits". You can test this with the simple "images": [1, 1], [1, 2]. (The second one is "split" relative to the first. So depending on which is im_true, you have either a false split and no false merges, or a false merge and no false splits.

Comment thread skimage/measure/segmentation_metrics.py Outdated
return out

def _vi_tables(im1, im2):
"""Return probability tables used for calculating VI.

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.

I've made some improvements to the code in this function in Ch5 of Elegant SciPy. Can you copy over those improvements as well?

@seanbudd

Copy link
Copy Markdown
Contributor Author

I'm currently working on the gallery example, should it be under the segmentation section?

Comment thread skimage/measure/segmentation_metrics.py Outdated

See Also
--------
vi

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.

This needs updating

def test_vi():
im_true = np.array([1, 2, 3, 4])
im_test = np.array([1, 1, 8, 8])
assert_equal(compare_variation_of_information(im_true, im_test), -1)

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.

I'm not sure what's happening here, but VI is always non-negative.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

both vi and split vi are returning negative numbers every time. I'm not sure whats going on either


segments_slic = slic(img, n_segments=100, compactness=10, sigma=1)
gradient = sobel(rgb2gray(img))
segments_watershed = watershed(gradient, markers=2, compactness=0.001)

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.

There's a couple of coins examples that get basically a perfect segmentation. I suggest the following approach for this example:

  • find the example where every coin is perfectly segmented. Run the same code here and call the result im_true.
  • find the compact watershed example where all the coins are vastly oversegmented. Call that im_test1.
  • find another example where most of the coins are properly segmented but a few are background, or merged. Call that im_test2.
  • find a final example where most of the coins are merged with the background. (Or maybe even an example where all the coins are merged with the background, ie np.ones(coins.shape, dtype=int). But better if one or two coins are properly segmented. Call that im_test3.

Then, plot the following things:

  • a scatter plot, with a legend, or text labels, plotting the split_vi[0] against split_vi[1] (or vice versa. The splits should be on the vertical axis, so that the compact watershed example is on the top-left corner, and the np.ones example is on the bottom right corner. See the section on VI in my PLoS ONE 2013 paper for what this plot looks like.)
  • another scatter plot, with a legend, or text labels, plotting the precision against the recall for the three points.
  • all four segmentations. (true, and the three tests.)

This all would fit quite nicely in a 2 row x 3 column figure.

Hopefully that's clear enough!

@seanbudd

seanbudd commented Sep 11, 2018 •

Copy link
Copy Markdown
Contributor Author

Ok, I've sent a preliminary push. From what I can tell the following needs to be fixed (WIP)

  • Improve the doc strings for the doc file, reference the papers and explain whats going on
  • I'm not sure if the heights of the plots are correct but I can't find a way to fix the row height, I assume plt.tight_layout() will fix it for publishing?
  • Something might be wrong with the adapted_rand_error function as the plots are not what I was expecting.

ax[5].set_title('Morphological GAC')
ax[5].set_axis_off()

plt.tight_layout()

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.

It is so weird that this isn't working correctly at all here. We'll need to figure this out also...

@jni jni changed the title Import segmentation metric functions to the measure module from gala.… Add segmentation evaluation functions Oct 24, 2018
@stefanv

stefanv commented Oct 24, 2018

Copy link
Copy Markdown
Member

Thanks for this addition @seanbudd! I'm looking forward to seeing it develop.

@soupault soupault self-assigned this Oct 24, 2018
@seanbudd

Copy link
Copy Markdown
Contributor Author

any updates here?

@jni

jni commented Mar 18, 2019

Copy link
Copy Markdown
Member

@scikit-image/core I've made a few changes that make the example much more didactic. I think this is ready for another pass of reviews and maybe even a merge! Output of the example:

segmentation-metrics-example

@soupault soupault added this to the 0.16 milestone Apr 4, 2019
@rbavery

rbavery commented Jul 14, 2019

Copy link
Copy Markdown
Contributor

I've refactored the most recent updates to this pull request into an evaluate submodule on my fork. My test currently imports evaluate wrong, ModuleNotFoundError: No module named 'skimage.evaluate.evaluate' I'm going to continue working on getting this and #3912 at scipy, with some help from @jni

@rbavery rbavery mentioned this pull request Jul 14, 2019
4 of 9 tasks
@seanbudd

Copy link
Copy Markdown
Contributor Author

Superseded by #4025

@seanbudd seanbudd closed this Jul 24, 2019
@jni

jni commented Jul 24, 2019

Copy link
Copy Markdown
Member

Finally in, @seanbudd! =) Thanks for your work here! =)

@seanbudd

Copy link
Copy Markdown
Contributor Author

no problem @jni glad it eventually made it in!!

@seanbudd
seanbudd deleted the measure-compare-gala-port branch July 24, 2019 04:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants