How to resize a 3D image in a keras CNN model?

609 views Asked by At

I have a 3D PET scan (None, 128, 128, 60, 1). I want to resize it to be (None, 64, 64, 30, 1). Sometimes I need it to be even smaller. I want to do this inside a keras model. I was thinking of something like tf.image.resize but for 3D images.

1

There are 1 answers

2
Shubham Panchal On

We can remove the last dimension of the 3D image ( None , 128 , 128 , 60 , 1) so that the shape of the transformed tensor is ( None , 128 , 128 , 60 ) that matches with the required shape for tf.image which is ( height , width , channels ). ( Note: the required shape can also be ( batch_size , height, width, num_channels ) )

We will use tf.squeeze for this purpose.

import tensorflow as tf

# Some random image
image = tf.random.normal( shape=( 128 , 128 , 60 , 1 ) )
# Remove dimensions of size 1 from `image`
image = tf.squeeze( image )
# Resize the image
resized_image = tf.image.resize( image , size=( 100 , 100 ) )
print( tf.shape( resized_image ) )

The output

tf.Tensor([100 100  60], shape=(3,), dtype=int32)

To restore the deleted dimension of size 1, you may use tf.expand_dims,

resized_image = tf.expand_dims( resized_image , axis=3 )
print( tf.shape( resized_image ))

The output,

tf.Tensor([100 100  60   1], shape=(4,), dtype=int32)