Skip to content

resize output data type is not consistent with input data type #4108

Closed

Description

@rfezzani

Description

The resize function actually always returns float64 arrays whatever is the data type of the input array to be resized.

Way to reproduce

from skimage.transform import resize
from skimage import data, img_as_float32, img_as_float
img_u8 = data.camera()
img_f32 = img_as_float32(img_u8)
img_f64 = img_as_float(img_u8)
print(resize(img_u8, (256, 256)).dtype,
      resize(img_f32, (256, 256)).dtype,
      resize(img_f64, (256, 256)).dtype)

# outputs: float64 float64 float64

Version information

3.7.3 | packaged by conda-forge | (default, Jul  1 2019, 21:52:21) 
[GCC 7.3.0]
Linux-4.15.0-58-generic-x86_64-with-debian-buster-sid
scikit-image version: 0.15.0
numpy version: 1.16.4

Activity

  1. changed the title [-]`resize` output data type not consistent with input data type[/-] [+]resize output data type is not consistent with input data type[/+] on Aug 22, 2019
  2. jni commented on Aug 22, 2019

    @jni
    Member

    @rfezzani this is expected behaviour. For more information, please see this page:

    https://scikit-image.org/docs/dev/user_guide/data_types.html

    Functions in skimage are designed so that they accept any of these dtypes, but, for efficiency, may return an image of a different dtype (see Output types). If you need a particular dtype, skimage provides utility functions that convert dtypes and properly rescale image intensities (see Input types).

    To get back the original data type, use skimage.util.img_as_{ubyte,uint16,uint32} etc.

    We realise that this can be confusing, but haven't found a satisfactory solution yet. See #3009 for discussion. The main issue here is that we want to use float for chaining operations, or you can unwittingly get accumulation of rounding errors in complicated pipelines.

    I'm going to close this as a duplicate of #3009. I suggest we continue discussing there, unless you think there is something very specific about resize that you want to say. =)

  3. hmaarrfk commented on Aug 22, 2019

    @hmaarrfk
    Member

    Float 32 should not be upcast IMO. This might be a one liner PR.

  4. rfezzani commented on Aug 22, 2019

    @rfezzani
    MemberAuthor

    I understand @jni, but as you said, it is a little bit confusing.
    As pointed out by @hmaarrfk, float32 should not be upcast. This behavior was rectified by PR #3110 for the img_as_float function.
    In the PR #3983, I ended up using scipy.ndimage.zoom instead of resize to allow computation using any floating point precision.

  5. jni commented on Aug 23, 2019

    @jni
    Member

    I traced it to here:

    def convert_to_float(image, preserve_range):
    """Convert input image to double image with the appropriate range.
    Parameters
    ----------
    image : ndarray
    Input image.
    preserve_range : bool
    Determines if the range of the image should be kept or transformed
    using img_as_float. Also see
    https://scikit-image.org/docs/dev/user_guide/data_types.html
    Returns
    -------
    image : ndarray
    Transformed version of the input.
    """
    if preserve_range:
    image = image.astype(np.double)
    else:
    image = img_as_float(image)
    return image

    I don't think it'll be an easy fix. I presume @rfezzani you are using preserve_range=True. This should not be necessary for float32 images, since we stopped (iirc) range-checking float images. So if you use preserve_range=False, it should go through img_as_float which will not touch the image (I think) if it is float32. Could you give that a go?

    I'll reopen this because it's a specific issue with skimage._shared.utils.convert_to_float, rather than our usual machinery.

  6. reopened this on Aug 23, 2019
  7. rfezzani commented on Aug 29, 2019

    @rfezzani
    MemberAuthor

    Hi @jni, sorry my late answer. preserve_range is set to False by default so, looking at the "way to reproduce" example in the PR description, this parameter doesn't provide an easy solution to our problem.
    I investigated some more. skimage._shared.utils.convert_to_float is not the only offending function. This is the easy part of the fix since, if preserve_range is True, we can simply add a check for the input image dtype and convert it to double only if it is not already a float type.
    The true guilty function is skimage.transform._warps_cy._warp_fast that is specialized to the double type, and that's the hard part! 🙂

  8. rfezzani commented on Aug 29, 2019

    @rfezzani
    MemberAuthor

    I solved this issue by:

    • adding float32 support to _warp_fast,
    • making convert_to_float not converting a float32 array to double when preserve_range is True.

    This fix exposed a bug in radon function. In fact, the test_iradon_bias_circular_phantom failed because radon was called with a bool array while it is stipulated in radon doc that it supports only float arrays.
    I fixed this last bug by adding convert_to_float call in radon function
    I also added the test_resize_dtype unitary test.
    I hope you like it ;-)

  9. added this to the 0.17 milestone on Aug 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions