When we deal with the size of the images in Tensorflow, tf.image.resize_images
API will get the job done.
Please check the official documents for this API here.
In the official documents, we know that tf.image.resize_images
has the form of
def resize_images(
images,
size,
method=ResizeMethod.BILINEAR,
align_corners=False
)
where the arguments are,
images
: 4-D Tensor of shape [batch, height, width, channels] or 3-D Tensor of shape [height, width, channels].size
: A 1-D int32 Tensor of 2 elements: new_height, new_width. The new size for the images.method
: ResizeMethod. Defaults to ResizeMethod.BILINEAR.align_corners
: bool. If true, exactly align all 4 corners of the input and output. Defaults to false.
Note that size
argument is also the tensor.
I had to perform random down-sampling for given images tensor, so the necessary code snippet would be,
# assuming 'images' has the shape [batch_size, height, width, channels]
images_shape = tf.shape(images)
height = image_shape[1]
width = image_shape[2]
# randomly select subsample factor to be 2 or 4
subsample = tf.floor(tf.random_uniform([], 0, 2))
down_height, down_width = tf.cond(tf.equal(subsample, 1),
lambda: (tf.to_int32(height/2), tf.to_int32(width/2)),
lambda: (tf.to_int32(height/4), tf.to_int32(width/4)))
downsampled_images = tf.image.resize_images(images,
[down_height, down_width])
Don't be confused with tensor indexing!! When we slice the tensor, the indexing should be done with actual integer form. I had to slice the tensor into two groups, so the necessary code snippet would be,
predictions_shape = class_predictions_with_background.get_shape().as_list()
first_half_class_predictions_with_background \
= class_predictions_with_background[:predictions_shape[0]/2]
second_half_class_predictions_with_background \
= class_predictions_with_background[predictions_shape[0]/2:]
Note that, we should get the shape of the tensor as list.