Skip to content

[New feature] Add TV-L1 optical flow algorithm - #3983

Merged
alexdesiqueira merged 38 commits into
scikit-image:masterfrom
rfezzani:Add_tvl1_optical_flow_method
Sep 11, 2019
Merged

alexdesiqueira merged 38 commits into
scikit-image:masterfrom
rfezzani:Add_tvl1_optical_flow_method

Conversation

@rfezzani

@rfezzani rfezzani commented Jun 28, 2019 •

Copy link
Copy Markdown
Member

Description

As I previously proposed on the mailing list (sorry for the delay), this PR is about the implementation of the TV-L1 algorithm for the estimation of optical flow [1].

I integrated it in the sub-package skimage.registration because I did not found a better place for it. I hope that there is no conflict with the Meta issue #3810.

Concerning the tests, I actually implemented only the zero motion case. I also can implement a non regression test by saving the output of the algorithm in the data sub-package.

For the examples, I used the stereo_motorcycle sequence that is already included in skimage, but I can also add any of the train sequences from the middlebury dataset : I already asked the maintainer of the project if there is any copyright restriction concerning the distribution of the Middlebury dataset, and there is not.

[1]: Zach, C., Pock, T., & Bischof, H. (2007, September). A duality based approach for realtime TV-L 1 optical flow. In Joint pattern recognition symposium (pp. 214-223). Springer, Berlin, Heidelberg.

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 Jun 28, 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 55:13: E226 missing whitespace around arithmetic operator
Line 69:39: E226 missing whitespace around arithmetic operator
Line 74:49: E226 missing whitespace around arithmetic operator
Line 74:61: E226 missing whitespace around arithmetic operator
Line 76:40: E226 missing whitespace around arithmetic operator
Line 78:19: E226 missing whitespace around arithmetic operator
Line 81:45: E226 missing whitespace around arithmetic operator
Line 87:32: E226 missing whitespace around arithmetic operator
Line 93:47: E226 missing whitespace around arithmetic operator
Line 93:60: E226 missing whitespace around arithmetic operator
Line 97:43: E226 missing whitespace around arithmetic operator
Line 107:31: E226 missing whitespace around arithmetic operator
Line 109:31: E226 missing whitespace around arithmetic operator
Line 121:31: E226 missing whitespace around arithmetic operator
Line 124:31: E226 missing whitespace around arithmetic operator
Line 130:26: E226 missing whitespace around arithmetic operator

Line 38:25: E226 missing whitespace around arithmetic operator

Line 29:50: E226 missing whitespace around arithmetic operator
Line 29:64: E226 missing whitespace around arithmetic operator
Line 29:70: E226 missing whitespace around arithmetic operator
Line 30:31: E226 missing whitespace around arithmetic operator

Comment last updated at 2019-09-11 07:09:51 UTC

@jni

jni commented Jul 2, 2019

Copy link
Copy Markdown
Member

@rfezzani thanks for this contribution! I apologise that I don't have time to do a proper review right now, but one concern I have is that we are aiming to be a fully n-dimensional library, minimising the amount of 2D-only code as much as possible. From what I can tell, this algorithm generalises quite readily to n-dimensions. Do you think that would be something you would be comfortable doing? Otherwise we can certainly take over at this point!

@hmaarrfk

hmaarrfk commented Jul 2, 2019

Copy link
Copy Markdown
Member

@jni do we really want to "ndify" the whole library at the start of every contribution? This seems quite intense and I feel like it make make the higher dimension stuff rushed.

@jni

jni commented Jul 2, 2019

Copy link
Copy Markdown
Member

@hmaarrfk

do we really want to "ndify" the whole library at the start of every contribution?

yes. Anything else is taking on debt that we generally can't afford to pay back. I put merging a non-nD request (except where the feature is intrinsically 2D) on almost the same level as merging something without tests with the idea that we'll add them later. The truth is that none of us have time for that. A large proportion of my work on the library for the past 6 years has been patching 2D-only stuff to support nD.

This is not to say that the contributor needs to do it, which is why I offered to take over. But I think the 2D tests should be passing before I do. (Again, no time for a full review right now, but if no one else comes in before then, I can help after the SciPy conference.)

@JDWarner

JDWarner commented Jul 2, 2019

Copy link
Copy Markdown
Contributor

While I am also firmly in the belief that n-D is the right path forward, it does result in us not having nice things, or getting them significantly late at times. As just one example, I had perfectly functional and performant 2D code for flood filling in 2015 - simple Cython, still have the gist published - but we wanted it n-D. Flood fill itself didn't get merged until 3 years later. There's a lot we could have done with a 2D or 2D/3D version in the meantime. I'm mostly playing the Devil's Advocate here, but food for thought.

@jni

jni commented Jul 2, 2019

Copy link
Copy Markdown
Member

@JDWarner 👋 and looking forward to seeing you next week! =) =) =)

The flip side of that coin is that we have nD flood fill now, which I believe might not be true if we'd had 2D already. Not only that, but I think the code ended up much neater in the end. In the current stage of scikit-image's development, I think long-term maintainability is a much greater concern than short term development speed, and that balance is only going to become more lopsided.

2D implementations can live in branches, gists, and WIP PRs and still be useful to the community while nD support is worked out...

@rfezzani

rfezzani commented Jul 2, 2019

Copy link
Copy Markdown
Member Author

Hi all and thanks for your feedback.
Optical flow in n-D looks affordable, but it needs much more work and even more validation. I even don't know any data set or challenge concerning volumetric optical flow.
I don't believe that I will be able to develop it in the near future :-(.

@hmaarrfk

hmaarrfk commented Jul 3, 2019

Copy link
Copy Markdown
Member

Is there a just middle we can hit? Where we ensure the API is compatible with ND, but not actually implement it until followup PRs?

I think the most valuable aspect is being able to contribute a small useful chunk at the same time. That ensures that we don't loose interest from the many parties involved in a contribution.

The desire to go to ND has for example made it difficult to move the color module over because it is hard to ensure backward compatibility with the majic that was decided long ago.

For example, I was able to do something like this in numpy where I was able to improve the np.block algorithm without having to write it in C. For a certain usecase, it was actually slower than the naive implementation. It was decided that python workarounds (falling back on the previous implementation) were allowed and that the C Port could be delayed until further notice.

@jni

jni commented Jul 3, 2019

Copy link
Copy Markdown
Member

Is there a just middle we can hit? Where we ensure the API is compatible with ND, but not actually implement it until followup PRs?

This might be possible. Already this PR doesn't meet that bar (at first glance) because it uses warp, which uses reversed coordinates, again, partly because warp assumed 2D images when it was written long ago.

The desire to go to ND has for example made it difficult to move the color module over because it is hard to ensure backward compatibility with the majic that was decided long ago.

For me this makes the complete opposite point: moving forward has been extremely difficult because of code that assumed 2D inputs. There's many examples like this.

I think the most valuable aspect is being able to contribute a small useful chunk at the same time. That ensures that we don't loose interest from the many parties involved in a contribution.

This is definitely a huge concern, and thanks for keeping it in focus! But it is one among several. My proposed solution to "fast contribution experiences" is that maintainers should help contributors by pushing commits to the PR branch, not that we lower our standards for long-term maintainability.

@rfezzani sorry that the discussion has gone off track a little bit. I just saw that your tests are passing, so at this point you can take a break. =) Thanks for your contribution! We'll do some review, maybe argue a little bit =P, and then help push it to nD, but again, from me at least this has to happen after SciPy.

@rfezzani

rfezzani commented Jul 3, 2019

Copy link
Copy Markdown
Member Author

This might be possible. Already this PR doesn't meet that bar (at first glance) because it uses warp, which uses reversed coordinates, again, partly because warp assumed 2D images when it was written long ago.

warp does not assume 2D images, here is a perfectly working demo toy:

from skimage.transform import warp
import numpy as np
img = np.random.randn(256, 256, 256)
I, J, K = np.meshgrid(np.arange(256), np.arange(256), np.arange(256))
img = warp(img, np.array([I, J, K]))

I may be don't understood what you mean, but even if warp was not supporting nD, this doesn't prevent the API from being compatible with nD as suggested by @hmaarrfk.

@rfezzani sorry that the discussion has gone off track a little bit.

Don't worry @jni, this discussion is a big concern: is the nD support a condition for the inclusion of a new feature in skimage? In my opinion, this is a huge requirement, difficult to reach in many cases.

By the way, listing all the actual skimage functions only supporting 2D and creating the corresponding issues for extending their support to nD can be interesting. What do you think?

@rfezzani

Copy link
Copy Markdown
Member Author

Hi everyone,
after some efforts, I finally managed to implement the nD support for the TV-L1 algorithm ;-).
I would be happy if someone finds some time to review my code. Thx in advance.

@rfezzani

Copy link
Copy Markdown
Member Author

Thank you both, @jni and @alexdesiqueira, for your review and comments. The code looks much better now.
Concerning the tests, I agree with you @jni, it is far from being perfect. I don't have any satisfactory idea of tests to implement. As I mentioned in the PR description, I can implement a non regression test by saving the output of the algorithm in the data sub-package, but I found no other option.
There is effectively some 2D datasets providing ground truth solutions, middlebury is one of them and I already asked its maintainer, and there is no restriction to the distribution of any sequence from this dataset.
The stereo_motorcycle sequence that is already included in skimage comes from the stereo challenge of Middlebury, What about adding a sequence from the optical flow challenge of Middlebury?
I unfortunately never heard about any volumetric dataset for optical flow problem, and I don't really know how to test it :-(.

assert np.all(flow == 0)


def test_no_motion_3d():

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.

Great! This will at least exercise the code. =)

Another test would be to use ndimage transforms that are expected to produce a particular pattern of flow. For example, wouldn't shrinking the image about its center produce a radially shrinking field with predictable flow? Or, more simply, a translation of x, y, z pixels should produce that flow uniformly across the image, right?

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.

Yes, good suggestion, but the resulting optical flow will not be equal to the ground truth, all we can do is expect a result reasonably close to the true solution. I will give this a try ASAP ;-)

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

I'm into it! 😂 I've made a minor and optional suggestion for a new test, but it can come later I think. I'll let someone else look it over for completeness.

@rfezzani

Copy link
Copy Markdown
Member Author

Hi @jni, I finally managed to add non trivial tests for the optical flow estimation. Is it OK for you?
Thx again.

@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 this is just fantastic right now! I've left some very minor comments that should be addressed before merging, but otherwise I'm amazed where you've got this to. 👏 👏 👏 The new test is 👌 perfect.

I still would rename the main function just "optical_flow", but I definitely understand the arguments for not doing so.

Other than the changes proposed, it's up to @alexdesiqueira to merge! =)

Comment thread skimage/registration/_optical_flow.py Resolved
Comment thread skimage/registration/_optical_flow.py Resolved
Comment thread skimage/registration/_optical_flow.py Resolved
Comment thread skimage/registration/_optical_flow.py Resolved
@rfezzani

Copy link
Copy Markdown
Member Author

Thank you again @jni for your review, this PR is much better now then its first implementation ;-).

@alexdesiqueira

Copy link
Copy Markdown
Member

Hey @rfezzani, I really think this could have better name variables, once I read it and don't understand that much yet. However, how I liked the algorithm! This seems a lot useful (and I'm seeing applications for that right now). But @jni pointed several points and you dealed with them all, therefore I won't hold it.
Thank you very much for your hard work, we really appreciate it!

@alexdesiqueira
alexdesiqueira merged commit 9dce7e9 into scikit-image:master Sep 11, 2019
@rfezzani

Copy link
Copy Markdown
Member Author

Hi @alexdesiqueira, thank you again for your review. I am really happy if this new feature can help you! I am sorry if you feel that variable names can be more explicit, this may be addressed in a documentation improvement issue? Let me know if you have any idea ;-)

@alexdesiqueira

Copy link
Copy Markdown
Member

Hey @rfezzani,
I like to think that the code by itself should be as readable as it could be, without referring too much on documentation on a first moment (no one reads the manual, right? 😄).
Thanks again for your work!

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.

7 participants