leonard
leonard

Reputation: 845

How to resize elements in a ragged tensor in TensorFlow

I would like to resize every element in a ragged tensor. For example, if I have a ragged tensor of various sized images, how can I resize each one so that the dimensions are the same?

For example,

digits = tf.ragged.constant([np.zeros((1,60,60,1)), np.zeros((1,46,75,1))])
resize_lambda = lambda x: tf.image.resize(x, (60,60))
res = tf.ragged.map_flat_values(resize_lambda, digits)

I wish res to be a tensor of shape (2,60,60,1). How can I achieve this?

To clarify, this would be useful if within a custom layer we wanted to slice or crop sections from a single image to batch for inference in the next layer. In my case, I am attempting to combine two models (a model to segment an image into multiple cropped images of varying size and a classifier to predict each sub-image). I am also using tf 2.0

Upvotes: 3

Views: 2183

Answers (1)

thushv89
thushv89

Reputation: 11333

You should be able to do the following.

import tensorflow as tf
import numpy as np
digits = tf.ragged.constant([np.zeros((1,60,60,1)), np.zeros((1,46,75,1))])

res = tf.concat(
   [tf.image.resize(digits[i].to_tensor(), (60,60)) for i in tf.range(digits.nrows())], 
   axis=0)

Upvotes: 1

Related Questions