0
$\begingroup$

When using the Keras UpSampling2D layer to resize a float64 array the result is cast to float32. The following code snippet demonstrates this behavior.

import numpy as np
from keras import layers

arr = np.ones((4,4), np.float64).reshape((1,4,4,1))
print(arr.dtype) # float64

upsample = layers.UpSampling2D(interpolation='bilinear')
arr_upsampled = upsample(arr)

print(arr_upsampled.dtype) # float32

Why is this happening, and is it possible to keep the resulting tensor float64?

$\endgroup$

1 Answer 1

2
$\begingroup$

This is what happens: UpSampling2D invokes keras.backend.resize_images, which invokes tensorflow.image.resize. In its documentation, we find that:

The return value has type float32, unless the method is ResizeMethod.NEAREST_NEIGHBOR, then the return dtype is the dtype of images:

As you specified 'bilinear' as the interpolation method, the resulting type is float32. If you use 'nearest', you should get a float64 result.

This constraint seems to be due to the internal implementation of the bilinear interpolation to be implemented only for float32. This limitation is permeated to all functionalities that depend on it.

$\endgroup$
1
  • $\begingroup$ Thanks for the information! $\endgroup$
    – Churchjm
    Commented Apr 11, 2022 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.