You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
resize output data type is not consistent with input data type #4108
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
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. =)
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.
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.
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! 🙂
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 ;-)
Activity
[-]`resize` output data type not consistent with input data type[/-][+]resize output data type is not consistent with input data type[/+]jni commentedon Aug 22, 2019
@rfezzani this is expected behaviour. For more information, please see this page:
https://scikit-image.org/docs/dev/user_guide/data_types.html
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. =)
hmaarrfk commentedon Aug 22, 2019
Float 32 should not be upcast IMO. This might be a one liner PR.
rfezzani commentedon Aug 22, 2019
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_floatfunction.In the PR #3983, I ended up using
scipy.ndimage.zoominstead ofresizeto allow computation using any floating point precision.jni commentedon Aug 23, 2019
I traced it to here:
scikit-image/skimage/_shared/utils.py
Lines 228 to 249 in 6aaac3e
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 usepreserve_range=False, it should go throughimg_as_floatwhich 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.rfezzani commentedon Aug 29, 2019
Hi @jni, sorry my late answer.
preserve_rangeis set toFalseby 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_floatis not the only offending function. This is the easy part of the fix since, ifpreserve_rangeisTrue, we can simply add a check for the input image dtype and convert it todoubleonly if it is not already a float type.The true guilty function is
skimage.transform._warps_cy._warp_fastthat is specialized to thedoubletype, and that's the hard part! 🙂rfezzani commentedon Aug 29, 2019
I solved this issue by:
float32support to_warp_fast,convert_to_floatnot converting afloat32array todoublewhenpreserve_rangeis True.This fix exposed a bug in
radonfunction. In fact, thetest_iradon_bias_circular_phantomfailed becauseradonwas called with aboolarray while it is stipulated inradondoc that it supports onlyfloatarrays.I fixed this last bug by adding
convert_to_floatcall inradonfunctionI also added the
test_resize_dtypeunitary test.I hope you like it ;-)