Matt Fisher
Matt Fisher

Reputation: 213

Resizing tensorflow images with unknown size

I have a tensorflow UNet-style network. Currently I specify the input and target images as follows:

self.inputTensors = tf.placeholder(tf.float32, [None, opt.inputHeight, opt.inputWidth, opt.inputChannels], name='inputTensors')
self.targetColors = tf.placeholder(tf.float32, [None, opt.inputHeight, opt.inputWidth, opt.outputChannels], name='targetColors')

But I would like it to be able to operate on variable width and height images as well, i.e.

self.inputTensors = tf.placeholder(tf.float32, [None, None, None, opt.inputChannels], name='inputTensors')
self.targetColors = tf.placeholder(tf.float32, [None, None, None, opt.outputChannels], name='targetColors')

And infer the width and height of the intermediate layers. This works fine for my pooling layers or strided convolution layers, but for the upsampling layers I am using tf.image.resize_bilinear (although the question is valid for any of tf.image.resize_images.) Currently my resize bilinear code looks like:

def unpool2xBilinear(inputs, name = 'unpool2xBilinear'):
    sh = inputs.get_shape().as_list()
    newShape = (sh[1] * 2, sh[2] * 2)
    return tf.image.resize_bilinear(inputs, newShape)

However, this cannot handle unknown input shapes, giving

TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'

Is there a way to allow resize images to accept input-dependent sizes? Or do I have to build an entirely new graph for each different input image size?

Upvotes: 2

Views: 2270

Answers (1)

javidcf
javidcf

Reputation: 59731

Use tf.shape instead:

def unpool2xBilinear(inputs, name = 'unpool2xBilinear'):
    sh = tf.shape(inputs)
    newShape = 2 * sh[1:3]
    return tf.image.resize_bilinear(inputs, newShape)

Upvotes: 4

Related Questions