Skip to content

Streaming for itkResampleImageFilter in case of linear transforms#469

Merged
thewtex merged 1 commit intoInsightSoftwareConsortium:masterfrom
romangrothausmann:streaming4ResampleImageFilter_master
Mar 29, 2019
Merged

Streaming for itkResampleImageFilter in case of linear transforms#469
thewtex merged 1 commit intoInsightSoftwareConsortium:masterfrom
romangrothausmann:streaming4ResampleImageFilter_master

Conversation

@romangrothausmann
Copy link
Member

This PR corresponds to #82, rebased onto master, meant for inclusion into master (ITK-5 instead of ITK-4.13.1).
Inspired by the discussion of the discourse topic Why ResampleImageFilter is slow?, this PR is a try to realize streaming for at least some transforms for the itkResampleImageFilter.
It is one of the most general filters in ITK and much used and the essential one for isotropic down-sampling of large datasets. Especially for that case it would be very helpful to have streaming capabilities, in order to generate smaller datasets for filters that cannot stream. Apart from a reduced memory footprint, streaming can lead to a significant speed up because a chunk can be processed while the next is loaded from disc, drastically reducing loading latencies.

As the filter is of great importance, some new tests cases (failing with the current implementation) were created (#392, now merged) that are expected to succeed (partially) when streaming is realized. With these tests (and possibly #468) master is equipped for testing the new contributions to itkResampleImageFilter, which enable streaming for linear transforms. Manual test showed that streaming already works but (even for an identity transform) the result's extent is larger than without streaming.

Therefore, itkResampleImageTest7 is meant to compare the current results of itkResampleImageFilter in various cases with those obtained when streaming is employed. There, a reader and a writer are avoided to rule out any interference concerning streaming limitations in respect to a chosen file format.
itkResampleImageTest2s is based on itkResampleImageTest2 adjusted to write MHAs, which support streaming.

@romangrothausmann
Copy link
Member Author

romangrothausmann commented Feb 5, 2019

The individual commits are in https://github.com/romangrothausmann/ITK/commits/streaming4ResampleImageFilter_incr, in particular with romangrothausmann@2cdc0ee the streaming tests are now expected to pass, which they do not (https://circleci.com/gh/InsightSoftwareConsortium/ITK/3182?utm_campaign=vcs-integration-link&utm_medium=referral&utm_source=github-build-link):

In ResampleImageTest7 the monitor filter reports:
Down stream filter didn't execute PropagateRequestedRegion well
and
ResampleImageTest2Streaming
reports:
Description: Requested region is (at least partially) outside the largest possible region.
which could hint towards the problem I was experiencing before (https://discourse.itk.org/t/why-resampleimagefilter-is-slow/1217/17?u=grothausmann.roman), i.e. that the output extent is different with streaming.

It is unclear to me where this problem comes from and would need some help on this. @thewtex @blowekamp any idea?

@romangrothausmann romangrothausmann changed the title ENH: streaming for itkResampleImageFilter in case of linear transforms WIP: streaming for itkResampleImageFilter in case of linear transforms Feb 5, 2019
Copy link
Member

@dzenanz dzenanz left a comment

Choose a reason for hiding this comment

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

Looks good after a glance. Someone else should look too!

@romangrothausmann
Copy link
Member Author

Many thanks @dzenanz for taking a look. I would say this should not be merged until the extent problem is solved and the tests pass as expected.

@thewtex thewtex self-requested a review February 5, 2019 21:29
thewtex
thewtex previously requested changes Feb 5, 2019
Copy link
Member

@thewtex thewtex left a comment

Choose a reason for hiding this comment

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

@romangrothausmann a few comments inline.

@romangrothausmann romangrothausmann force-pushed the streaming4ResampleImageFilter_master branch from fb647ca to d4e1177 Compare February 7, 2019 15:05
romangrothausmann added a commit to romangrothausmann/ITK that referenced this pull request Feb 7, 2019
Squashed commit of the following:

commit ae8eb62
Author: Roman Grothausmann <romangrothausmann@users.noreply.github.com>
Date:   Thu Feb 7 15:56:57 2019 +0100

    BUG: corrections from @thewtex in PR InsightSoftwareConsortium#469

commit 2cdc0ee
Author: Roman Grothausmann <romangrothausmann@users.noreply.github.com>
Date:   Tue Feb 5 12:56:06 2019 +0100

    Revert "ENH: new test are expected to fail as long as:":

    This reverts commit e5d421f.
    because InsightSoftwareConsortium#392 (replaced InsightSoftwareConsortium#84) was merged in bc4e197

commit d62cbd4
Author: Roman Grothausmann <romangrothausmann@users.noreply.github.com>
Date:   Fri Sep 28 11:55:28 2018 +0200

    BUG: use ITK_NULLPTR instead of identityTransform

commit 9c0b04c
Author: Roman Grothausmann <romangrothausmann@users.noreply.github.com>
Date:   Thu Sep 27 12:45:50 2018 +0200

    ENH: add transform to ImageAlgorithm::EnlargeRegionOverBox

commit 5b8eaf5
Author: Roman Grothausmann <romangrothausmann@users.noreply.github.com>
Date:   Tue Sep 25 14:07:50 2018 +0200

    ENH: use ImageAlgorithm::EnlargeRegionOverBox instead

commit d6c44ea
Author: Roman Grothausmann <romangrothausmann@users.noreply.github.com>
Date:   Thu Aug 30 13:05:31 2018 +0200

    ENH: Method to compute InputRequestedRegion in itkResampleImageFilter

    based on suggestions from:
    https://discourse.itk.org/t/why-resampleimagefilter-is-slow/1217/14
    https://itk.org/pipermail/insight-users/2015-April/051877.html
    and code from itkImageAlgorithm of v4.13.1 (based on commit 8510db2)
@romangrothausmann
Copy link
Member Author

romangrothausmann commented Feb 7, 2019

Many thanks @thewtex for taking the close look. Applied your suggestions (which all make sense) but the tests still fail for me locally with the same errors.

romangrothausmann added a commit to romangrothausmann/ITK that referenced this pull request Feb 7, 2019
@romangrothausmann
Copy link
Member Author

The test also fail on CI. @thewtex any ideas would could be wrong with the current SDI implementation?

@thewtex
Copy link
Member

thewtex commented Feb 13, 2019

@romangrothausmann I'll dig in... 🏊‍♂️

@thewtex
Copy link
Member

thewtex commented Mar 28, 2019

Tests are passing now 🎉 :-)

There were a few tricky items:

point = transformPtr->TransformPoint(currentCornerIndex);

should be

point = transformPtr->TransformPoint(point);
  1. We needed to handle the cases
  • Input requested region is completely outside the largest possible region.
  • Input requested region is partially outside the largest possible region.
  • Input requested region is completely inside the largest possible region.
  • Input requested region completely surrounds the largest possible region.
  1. There were a number of details in the tests that needed to be fixed to prevent false positive failures.

@thewtex thewtex force-pushed the streaming4ResampleImageFilter_master branch from d4e1177 to b1ee7d7 Compare March 28, 2019 01:50
@thewtex thewtex dismissed their stale review March 28, 2019 01:50

Items addressed

@thewtex thewtex force-pushed the streaming4ResampleImageFilter_master branch from b1ee7d7 to edd815b Compare March 28, 2019 11:37
romangrothausmann added a commit to romangrothausmann/ITK that referenced this pull request Mar 28, 2019
@romangrothausmann
Copy link
Member Author

Wow, many thanks @thewtex. I'm trying to understand your changes.
With some rebasing magic I think I managed to create a commit that should represent your changes on top (romangrothausmann@29db4ad), at least it looks similar to the first force-push view.
Is there any easy way to "pull" that view into the local git such that I would get the same view in gitk that viewing romangrothausmann@29db4ad gives me?

@romangrothausmann
Copy link
Member Author

point = transformPtr->TransformPoint(point);

True, many thanks for catching that, otherwise romangrothausmann@29db4ad#diff-43a4309c31e92fe375554af9562c5a04R221 makes no sense.

So

Input requested region is completely inside the largest possible region.

is covered by:
romangrothausmann@29db4ad#diff-a446710011999e954fe6b67b68d64bebR546

Not sure about
romangrothausmann@29db4ad#diff-a446710011999e954fe6b67b68d64bebR542
To my understanding it tests if "Input requested region" partially overlaps with "largest possible region".

@thewtex
Copy link
Member

thewtex commented Mar 28, 2019

Is there any easy way to "pull" that view into the local git such that I would get the same view in gitk that viewing romangrothausmann/ITK@29db4ad gives me?

It is possible to pull and create a new branch, then do the diff. git series is also another tool for this.

So

Input requested region is completely inside the largest possible region.

is covered by:
romangrothausmann@29db4ad#diff-a446710011999e954fe6b67b68d64bebR546

Not sure about
romangrothausmann@29db4ad#diff-a446710011999e954fe6b67b68d64bebR542
To my understanding it tests if "Input requested region" partially overlaps with "largest possible region".

I will add some comments to the code...

@dzenanz
Copy link
Member

dzenanz commented Mar 28, 2019

Is there any easy way to "pull" that view into the local git such that I would get the same view in gitk that viewing romangrothausmann/ITK@29db4ad gives me?

You could try using git cherry-pick:

git checkout 29db4ad
git cherry-pick edd815b

These should give you only the relevant changes from Matt's commit.

@romangrothausmann
Copy link
Member Author

@romangrothausmann
Copy link
Member Author

Thanks @thewtex, @dzenanz for your feedback.
The git cherry-pick lead to quite a few merge conflicts, so in the end I rebased my streaming4ResampleImageFilter_incr onto the same parent as edd815b and did:

git checkout  origin/streaming4ResampleImageFilter_master -- `git diff-tree --no-commit-id --name-only -r origin/streaming4ResampleImageFilter_master`

Have to check git series. Something new every day...

@thewtex
Copy link
Member

thewtex commented Mar 28, 2019

How do you know "that last sampled chunk is completely outside the input"?

I spent a bit of time debugging the test :-).

Is that the reason to remove itkPipelineMonitorImageFilter in itkResampleImageTest7.cxx (https://github.com/InsightSoftwareConsortium/ITK/compare/d4e1177f61ba9414f1ae05852384e5ec4e725c31..b1ee7d74b5fc198ea61c4cd6d54d18a14ad58b4c#diff-dfa5983cf67253ce0e03aca56a207db2L23)?

It needed to be removed because the PipelineMonitorImageFilter did not behave correctly with a generated image as an input versus a reader or another filter as the input on multiple runs -- the buffered region was being reset to zeros. Since we already covered it in ResampleImageTest2Streaming, I removed it in ResampleImageTest7.

@romangrothausmann
Copy link
Member Author

It needed to be removed because the PipelineMonitorImageFilter did not behave correctly with a generated image as an input versus a reader or another filter as the input on multiple runs -- the buffered region was being reset to zeros. Since we already covered it in ResampleImageTest2Streaming, I removed it in ResampleImageTest7.

Hm, I see, but shouldn't we include some means to check that the streaming actually happens? In a case where the streaming does not happen (though requested) the test would always succeed.
On the other hand I though it would be good to avoid a reader in this test (such that it is not dependent on the input file format) and just to check that streaming an no-streaming leads to the same result.

@thewtex
Copy link
Member

thewtex commented Mar 28, 2019

Hm, I see, but shouldn't we include some means to check that the streaming actually happens? In a case where the streaming does not happen (though requested) the test would always succeed.
On the other hand I though it would be good to avoid a reader in this test (such that it is not dependent on the input file format) and just to check that streaming an no-streaming leads to the same result.

Good point. I will add a check at the end of streaming to verify that we are setting the requested region to a smaller chunk when streaming.

Method to compute InputRequestedRegion in itk::ResampleImageFilter
based on suggestions from:

   https://discourse.itk.org/t/why-resampleimagefilter-is-slow/1217/14
   https://itk.org/pipermail/insight-users/2015-April/051877.html
   and code from itkImageAlgorithm of v4.13.1 (based on commit 8510db2)

Co-authored-by: Matt McCormick <matt.mccormick@kitware.com>
@thewtex
Copy link
Member

thewtex commented Mar 28, 2019

Next patch set adds the streaming verification to itkResampleImageTest2Streaming and fixes the failing test by removing the Superclass::GenerateInputRequestRegion(); call -- this copies the output requested region to the input, but we do not want that behavior in this case.

@thewtex thewtex force-pushed the streaming4ResampleImageFilter_master branch from edd815b to 6093197 Compare March 28, 2019 20:55
@thewtex thewtex changed the title WIP: streaming for itkResampleImageFilter in case of linear transforms Streaming for itkResampleImageFilter in case of linear transforms Mar 29, 2019
@thewtex thewtex merged commit 2c648ef into InsightSoftwareConsortium:master Mar 29, 2019
@romangrothausmann
Copy link
Member Author

Way cool! Many, many thanks @thewtex for catching the problem with Superclass::GenerateInputRequestRegion();, all your help and merging.

A comment of yours

// Note: We will only request the input 4 times because that last sampled
// chunk is completely outside the input and not requested
if( !monitor->VerifyInputFilterExecutedStreaming(4) )

makes me wonder if I do not understand the pipeline execution correctly.
Aren't we asking for 8 divisions in:
writer1->SetNumberOfStreamDivisions(8); //split into 8 pieces for streaming.

which according the the docs (https://itk.org/Doxygen/html/classitk_1_1PipelineMonitorImageFilter.html#a1e98d85c0f5a7ca8814a9c0d9d0d59e4) would have to match those checked by VerifyInputFilterExecutedStreaming as long as the argument is not given as a negative number?
Your comment however sounds like the monitor-filter is doing the requesting not the writer. Does monitor->VerifyInputFilterExecutedStreaming(4) override writer1->SetNumberOfStreamDivisions(8)?

How did you come up with the values in these checks:

TEST_SET_GET_VALUE( 0, finalRequestedRegion.GetIndex(0) );
TEST_SET_GET_VALUE( 49, finalRequestedRegion.GetIndex(1) );
TEST_SET_GET_VALUE( 59, finalRequestedRegion.GetSize(0) );
TEST_SET_GET_VALUE( 10, finalRequestedRegion.GetSize(1) );

I assume they are dependent on the input image extents and the chosen splitter (ImageRegionSplitterMultidimensional for the used StreamingImageFilter), are they?

@thewtex
Copy link
Member

thewtex commented Mar 29, 2019

Your comment however sounds like the monitor-filter is doing the requesting not the writer. Does monitor->VerifyInputFilterExecutedStreaming(4) override writer1->SetNumberOfStreamDivisions(8)?

Yes, this was a bug -- fixed here :-): #654

How did you come up with the values in these checks:

This is the final request region chunk -- yes, indeed, this is determined by the logic in StreamingImageFilter and the data size.

@romangrothausmann romangrothausmann deleted the streaming4ResampleImageFilter_master branch May 17, 2019 10:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants