Skip to content

Allow warping float32 images without upcasting to double - #4128

Merged
stefanv merged 19 commits into
scikit-image:masterfrom
rfezzani:Fix_resize_output_detype_inconsistency
Sep 27, 2019
Merged

stefanv merged 19 commits into
scikit-image:masterfrom
rfezzani:Fix_resize_output_detype_inconsistency

Conversation

@rfezzani

@rfezzani rfezzani commented Aug 29, 2019 •

Copy link
Copy Markdown
Member

Description

This PR fixes #4108 and #4190.

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

rfezzani and others added 5 commits August 29, 2019 18:46
if preserve_range is True and the image is already float, it is no more converted to double
Adding float32 support to _warp_fast caused test_iradon_bias_circular_phantom to fail.
In this test image.dtype is bool.

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

Good cythonizing. I know this isn't easy but it looks on the right track. The test looks thorough too.

Maybe this is what I needed in my previous attempt to speed up wrap for different data tyoes.

Comment thread skimage/transform/_warps_cy.pyx Outdated
cdef floating[:, ::1] img = np.ascontiguousarray(image)
cdef floating[:, ::1] M = np.ascontiguousarray(H)

if floating is float:

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.

Is float ambiguous here? I'm concerned there is a difference between python floats and c floats.

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.

In Cython context, I think that float belongs to cython.float. So I suppose that it is not ambiguous...

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.

I took it from cython doc in Type Checking Specializations section ;-).

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.

The fact that you used the word "think" and "not ambiguous" in the same sentence to me makes it ambiguous.

The challenge comes when having code like this:

image

float clearly means different things when used in python+numpy, or in cython.

I would just replace float with np.float32_t, making it explicit that you mean float32.

@rfezzani rfezzani Aug 30, 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.

OK, I was thinking that you where talking about computer ambiguity, not understanding ambiguity. Sorry for my poor English speaking/understanding.
On python side, the ambiguity is clearly there, but in cython context I don't think so, the whole test block is explicit:

    if floating is float:
        dtype = np.float32
    else:
        dtype = np.double

We can make it even more explicit using:

    if floating is float:
        dtype = np.float32
    elif floating is double:
        dtype = np.double
    else:
        raise TypeError("Only float32 and float64 arrays are supported.")

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.

The last part of my answer is wrong, since floating can only be float or double. Sorry ☹️

Comment thread skimage/_shared/utils.py

def convert_to_float(image, preserve_range):
"""Convert input image to double image with the appropriate range.
"""Convert input image to float image with the appropriate range.

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 to say something like:

Floating point types will maintain their precision, while integer types will be converted to double.

@rfezzani rfezzani Aug 30, 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.

This also true for al the functions defined in _warp_cy.pyx and for radon. It is in my TODO list ;-)

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.

Sorry, I am not awake yet :-)
I will also fix the docstrings for radon and the function in _warp_cy.

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.

According to my comment, since not all the floating point precision are supported, I added a Notes section in the docstring specifying this: "Input images with float32 data type are not upcasted."

Comment thread skimage/_shared/utils.py
"""
if preserve_range:
image = image.astype(np.double)
if image.dtype.char not in 'df':

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.

Suggested change
if image.dtype.char not in 'df':
if not np.issubdtype(image.dtype, np.floating):
>>> import numpy as np
>>> np.issubdtype(np.float32, np.floating)
True
>>> np.issubdtype(np.float64, np.floating)
True
>>> np.issubdtype(np.float16, np.floating)
True
>>> np.issubdtype(np.uint8, np.floating)
False

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.

That's definitely more readable ;-). I excluded float16 and float128 because I though that these types are not supported by skimage.

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.

This will cause a bug since float16 is not supported by cython.floating (only float and double) :-(

>>> a = np.ones((5, 5), dtype=np.float16)
>>> resize(a, (10, 10))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "skimage/transform/_warps_cy.pyx", line 70, in skimage.transform._warps_cy._warp_fast
    def _warp_fast(floating[:, :] image, floating[:, :] H, output_shape=None,
ValueError: Does not understand character buffer dtype format string ('e')
  • We can use
    if image.dtype not in [float, np.float, np.double, np.float32, np.float64]:
    but I am not sure that it is more readable...
  • The other option is to keep the original test (if image.dtype.char not in 'df') and to add a comment in the code.

What do you think?

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.

Just ignore float16, the whole library is broken with it.

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.

All right, I will have to ignore np.float128 also:

>>> a = ones((5, 5), dtype=np.float128)
>>> resize(a, (10, 10))
ValueError: Buffer dtype mismatch, expected 'double' but got 'long double'

This is because cython.floating only supports single and double precision floating points.
Do you prefer if not (np.issubdtype(image.dtype, np.floating) or image.dtype in [np.float16, np.float128]) instead of if image.dtype.char not in 'df'?

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 once wrote a fused numerics file. You can add things there.

That said, neither of those two datatypes are efficient in hardware, nor are they well tested.

If you want to, you can add to the fused numerics file maybe. Not exactly sure what will happen to the rest of the library.

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.

Honestly, you could just add a comment. That said, you could also add a Todo, and raise an issue that float16 and float128 have yet undefined behaviours in the img_as routines.

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.

I already tried some explorations with skimage._shared.fused_numerics.np_floats, but numpy doesn't declare float16_t and float128_t for cimport :-(

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.

yeah, float16 and float128 are pretty badly supported in general. Adding their definitions to the cimport could be added as an issue upstream.

x = np.zeros((5, 5))
x_f32 = x.astype(np.float32)
x_u8 = x.astype(np.uint8)
x_b = x.astype(bool)

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 we care about supporting boolean data types. @jni?

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.

Eh. If it works, why not keep it working?

warped = _warp_fast(image, matrix,
output_shape=output_shape,
order=order, mode=mode, cval=cval)
warped = _warp_fast[ctype](image, matrix,

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 won't select things automatially? Maybe that was my mistake....

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.

Nop, it wasn't working without this specialization...

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.

🎉 very nice, thanks @rfezzani for this discovery!

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

Just a few small comments. Looks good otherwise!

@hmaarrfk hmaarrfk changed the title Fix resize output detype inconsistency Fix resize output dtype inconsistency Aug 30, 2019
Comment thread skimage/transform/_warps.py Outdated
if matrix is not None:
matrix = matrix.astype(np.double)
matrix = matrix.astype(image.dtype)
ctype = 'float' if image.dtype.char == 'f' else 'double'

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 would really like it if you found a way to call these float32 and float64. Mostly float is ambiguous, double not so much.

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.

I was happy to make it work without the explicit import here to cython: I did not knew that passing strings for specialization works :-D
Well, I will give it a try.

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.

Infortunatly, this doesn't work :-( This is because _warp_cy.__signatures__.keys() only contains "float" and "double".

I added a comment trying to alleviate the ambiguity:

# Manage _warp_fast specialization according to input data
# type floating point precision (single or double)
ctype = 'float' if image.dtype.char == 'f' else 'double'

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 needs a sentence like:

Contrary to Python, Cython float follows the meaning of float in C, and is equivalent to float32.

@rfezzani

Copy link
Copy Markdown
Member Author

Thank you @hmaarrfk for your review, I tried in my last push to address all your concerns.

@sciunto sciunto added this to the 0.17 milestone Aug 30, 2019
@hmaarrfk

Copy link
Copy Markdown
Member

I think what would make me happy is that if you included this sentence

Contrary to Python and numpy, Cython float follows the meaning of float in C, and is equivalent to float32.

Whenver you used float in cython. Otherwise, maybe use our fused numerics, which would translate to np.float32_t which is much less ambiguous, maybe...

@rfezzani

rfezzani commented Aug 30, 2019 •

Copy link
Copy Markdown
Member Author

Using skimage.__shared.fused_numercs.np_floats instead of cython.floating removes the ambiguity since

ctype = 'float' if image.dtype.char == 'f' else 'double'

becomes

ctype = 'float32_t' if image.dtype == np.float32 else 'float64_t'

and

   if floating is float:
        dtype = np.float32
    else:
        dtype = np.double

becomes

    if np_floats is cnp.float32_t:
        dtype = np.float32
    else:
        dtype = np.float64

I think that there is no need then for more comments in the code. Thx @hmaarrfk ;-)

Comment thread skimage/transform/radon_transform.py Resolved
Comment thread skimage/transform/radon_transform.py Resolved
@hmaarrfk

Copy link
Copy Markdown
Member

The cython stuff seems fine for me. I think the radon fix should be more fleshed out, and ideally left to a different PR if it can be.

@hmaarrfk

hmaarrfk commented Aug 30, 2019 via email

Copy link
Copy Markdown
Member

@rfezzani

Copy link
Copy Markdown
Member Author

Isn't it #3799?

@hmaarrfk

Copy link
Copy Markdown
Member

Thats the one!

Although that was a bug, I think that behaviour definitely needs to be deprecated too.

If you try to add the fix for that in here, then the review time will be multiplicative, instead of additive, and will delay this getting merged.

@rfezzani

Copy link
Copy Markdown
Member Author

Fixing the radon function is a one line fix + doc update: just add a call to convert_to_float :-)
I am about to complete filling the issue.

@rfezzani

Copy link
Copy Markdown
Member Author

Thats the one!

Although that was a bug, I think that behaviour definitely needs to be deprecated too.

If you try to add the fix for that in here, then the review time will be multiplicative, instead of additive, and will delay this getting merged.

So what do you recommend? Should we fix #4130 directly in this PR and close both related issues?

@hmaarrfk

hmaarrfk commented Aug 30, 2019 via email

Copy link
Copy Markdown
Member

@rfezzani

Copy link
Copy Markdown
Member Author

OK, but this will cause test_iradon_bias_circular_phantom to fail in this PR :-(

@hmaarrfk

Copy link
Copy Markdown
Member

So you need the preserve range to be set to true? If so, that is fine, add a note #3799

@rfezzani

Copy link
Copy Markdown
Member Author

So you need the preserve range to be set to true? If so, that is fine, add a note #3799

Not necessarly, only the failing test has to be fixed ;-)

@rfezzani

Copy link
Copy Markdown
Member Author

As expected, the CI tests pass again after merging #4136. 🎉

Comment thread skimage/_shared/utils.py Resolved
Comment thread skimage/_shared/utils.py Resolved

@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 brilliant! I've added a couple fo suggestions but they are minor, this should be ready to go with those! Very excited about this!

@jni jni changed the title Fix resize output dtype inconsistency Allow warping float32 images without upcasting to double Sep 26, 2019
rfezzani and others added 2 commits September 26, 2019 15:45
Co-Authored-By: Juan Nunez-Iglesias <[email protected]>
Co-Authored-By: Juan Nunez-Iglesias <[email protected]>
@rfezzani

Copy link
Copy Markdown
Member Author

Thank you for the review @jni ;-)

@jni

jni commented Sep 27, 2019

Copy link
Copy Markdown
Member

@stefanv any chance you can review this? I think it's a pretty major improvement, which would be nice to get into 0.16.

@stefanv

stefanv commented Sep 27, 2019

Copy link
Copy Markdown
Member

This looks good to me, merging. If there are any other minor updates, @hmaarrfk, feel free to add a refinement PR.

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.

resize output data type is not consistent with input data type

5 participants