调整大小未知的张量流图像 [英] Resizing tensorflow images with unknown size

查看:118
本文介绍了调整大小未知的张量流图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个tensorflow UNet风格的网络。目前,我将输入图像和目标图像指定如下:

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')

并推断中间层的宽度和高度。这对我的池化层或跨步卷积层工作正常,但对于上采样层,我正在使用tf.image.resize_bilinear(尽管问题对tf.image.resize_images均有效。)目前,我的调整大小双线性代码如下:

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?

推荐答案

使用 tf .shape 代替:

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

这篇关于调整大小未知的张量流图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆