Bilinear upsample in tensorflow?
tf.image.resize_images should do what you need. It accepts both 3d (single image) and 4d (batch of images) tensors, with arbitrary depth (number of channels). So this should hopefully work:
# it's height, width in TF - not width, height
new_height = int(round(old_height * scale))
new_width = int(round(old_width * scale))
resized = tf.image.resize_images(input_tensor, [new_height, new_width])
Bilinear interpolation is the default so you don't need to specify it. You could also use resize_bilinear directly.
I would suggest not to use any of the tf.image.resize_*
functions as they suffer from a nasty bug that will not be fixed.
A new, different set of image resampling functions is apparently in the pipeline. In the meantime, you can find some examples on the web on how to do that yourself using e.g. transposed convolutions. It is unfortunately much less efficient that per-channel upsampling, but correct is better than fast.
EDIT
They finally fixed this bug in TF 2.0:
image.resize
now considers proper pixel centers (...).
This fix currently does not pass gradient through, which is... a bug that will hopefully also get fixed.
Keras supports 'nearest'
and 'bilinear'
interpolation now with tensorflow backend. Check the documentation. But the default size value is indeed (2,2) or int value, so in that case your upsampling will be at least double.
This code needs Keras 2.2.3
or above. If you are using tf.keras
, unfortunately, you cannot install keras separately, it is bundled as part of tensorflow installation. Hence either find the tf
version or install the nightly build.
pip3 install --upgrade tf-nightly