简体   繁体   中英

tensorflow tf.pad shape of output

I have this function:

def resize_image(input_layer, counter ,width):

    shape = input_layer.get_shape().as_list()

    H = tf.cast((width * shape[2] / shape[1]),  tf.int32)
    print (H)
    resized_images = tf.image.resize_images(input_layer, [width, H], tf.image.ResizeMethod.BICUBIC)
    print (resized_images)
    pad_diff = width - H
    padd_images = tf.pad(resized_images, [[0, 0], [0, pad_diff], [0, 0], [0, 0]] , 'CONSTANT')
    return padd_images, counter

When I run this :

sess = tf.InteractiveSession()

I = tf.random_uniform([15, 15, 13, 5], minval = -5, maxval = 10, dtype = tf.float32) 
padd_images, counter = resize_image(I, 1, 5)
print (I)
print(padd_images)
sess.run(padd_images)

I get this:

Tensor("Cast/x:0", shape=(), dtype=int32)
Tensor("ResizeBicubic:0", shape=(15, 5, 4, 5), dtype=float32)
Tensor("random_uniform:0", shape=(15, 15, 13, 5), dtype=float32)
Tensor("Pad:0", shape=(?, ?, ?, ?), dtype=float32)

Why there are ? in the shape of padd_images ? Is there a way to know its shape?

The problem is a the line

H = tf.cast((width * shape[2] / shape[1]),  tf.int32)

Here you're defining a tensor. Thus when you compute:

pad_diff = width - H

you're defining an operation into the graph.

Thus you don't know at compile time what the pad_diff value is, but you'll now it only at runtime.

Since you don't need to have H as a tensor, just use the regular python cast operation, changing thus H with

H = int(width * shape[2] / shape[1])

In this way, the next operations that use H are executed within the python environment and thus the value are known at "compile time".

After that you'll see:

Tensor("Pad:0", shape=(15, 6, 4, 5), dtype=float32)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM