Skip to content Skip to sidebar Skip to footer

Resizing Images With Dynamic Shape In Tensorflow

I want to resize 3D images with a dynamic shape, for instance go from shape (64,64,64,1) to (128,128,128,1). The idea is to unstack the image along one axis, then use tf.image.resi

Solution 1:

tf.image.resize_images can resize multiple images at the same time, but it does not allow you to pick the batch axis. However, you can manipulate the dimensions of the tensor to put the axis that you want first, so it is used as batch dimension, and then put it back after resizing:

import tensorflow as tf

defresize_by_axis(image, dim_1, dim_2, ax):
    # Make permutation of dimensions to put ax first
    dims = tf.range(tf.rank(image))
    perm1 = tf.concat([[ax], dims[:ax], dims[ax + 1:]], axis=0)
    # Transpose to put ax dimension first
    image_tr = tf.transpose(image, perm1)
    # Resize
    resized_tr = tf.image.resize_images(image_tr, [dim_1, dim_2],
                                        method=1, align_corners=True)
    # Make permutation of dimensions to put ax in its place
    perm2 = tf.concat([dims[:ax] + 1, [0], dims[ax + 1:]], axis=0)
    # Transpose to put ax in its place
    resized = tf.transpose(resized_tr, perm2)
    return resized

In your example:

import tensorflow as tf
import numpy as np

X = tf.placeholder(tf.float32, shape=[None, None, None, 1])

# Get new shape
shape = tf.cast(tf.shape(X), dtype=tf.float32) * tf.constant(2, dtype=tf.float32)
x_new = tf.cast(shape[0], dtype=tf.int32)
y_new = tf.cast(shape[1], dtype=tf.int32)
z_new = tf.cast(shape[2], dtype=tf.int32)

# Reshape
X_reshaped_along_xy = resize_by_axis(X, dim_1=x_new, dim_2=y_new, ax=2)
X_reshaped_along_xyz = resize_by_axis(X_reshaped_along_xy, dim_1=x_new, dim_2=z_new, ax=1)

init = tf.global_variables_initializer()

# Runwith tf.Session() as sess:
    sess.run(init)
    result = X_reshaped_along_xyz.eval(feed_dict={X : np.zeros((64, 64, 64, 1))})
    print(result.shape)
    # (128, 128, 128, 1)

Post a Comment for "Resizing Images With Dynamic Shape In Tensorflow"