ArunKumar
ArunKumar

Reputation: 105

How to resize (interpolate) a tensor in Keras?

I want to resize a tensor (between layers) of size say (None, 2, 7, 512) to (None, 2, 8, 512), by interpolating it (say using nearest neighbor), similar to this function tf.image.resize_nearest_neighbor available in Tensorflow.

Is there any way to do that?

I tried directly using the Tensorflow function tf.image.resize_nearest_neighbor and the pass the tensors to the next Keras layer, but with the next layer this error was thrown:

AttributeError: 'Tensor' object has no attribute '_keras_history'

I believe this is due to some attributes that are missing in Tensorflow tensors, which makes sense as the layer expects Keras tensors to be passed.

Upvotes: 1

Views: 6273

Answers (2)

ArunKumar
ArunKumar

Reputation: 105

Surprisingly there is no existing layer/function in keras that does such an interpolation of a tensor (as pointed out by xtof54). So, I implemented it using a lambda layer, and it worked fine.

    def resize_like(input_tensor, ref_tensor): # resizes input tensor wrt. ref_tensor
        H, W = ref_tensor.get_shape()[1], ref_tensor.get_shape()[2]
        return tf.image.resize_nearest_neighbor(input_tensor, [H.value, W.value])

The problem, in the first place, was due to the use of a tensor directly from tensorflow in a Keras layer, as a few additional attributes (required for a keras tensor) that are missing. In addition, though Lambda layer is quite easy to use, it would be really convenient if keras allows the use of tensors (if possible) from tensorflow directly in keras layers, in the future.

Upvotes: 4

xtof54
xtof54

Reputation: 1383

I would use Repeat to add one element and implement the interpolation as a new lambda layer. I don't think there's an existing layer for this in keras.

Upvotes: 2

Related Questions