1

I've been trying to implement the Spatial Pyramid Pooling (https://arxiv.org/abs/1406.4729), but I've been having a problem with the input size.

My input has shape (batch_size, None, n_feature_maps) and I have the following code:

self.y_conv_unstacked = tf.unstack(self.conv_output, axis=0)
    self.y_maxpool = []
    for tensor in self.y_conv_unstacked:
        for size_pool in self.out_pool_size:
            self.w_strd = self.w_size = math.ceil(float(tensor.get_shape()[1]) / size_pool)
            self.pad_w = int(size_pool * self.w_size - tensor.get_shape()[1])
            self.padded_tensor = tf.pad(tensor, tf.constant([[0, 0], [0, 0], [0, self.pad_w], [0, 0]]))
            self.max_pool = tf.nn.max_pool(self.padded_tensor, ksize=[1, 1, self.w_size, 1], strides=[1, 1, self.w_strd, 1], padding='SAME')
            self.spp_tensor = tf.concat([self.spp_tensor, tf.reshape(self.max_pool, [1, size_pool, self.n_fm1])], axis=1)
        self.y_maxpool.append(spp_tensor)

Since the inputs in the batch have different sizes, I am unstacking them and pooling each tensor separately. However when using tensor.get_shape()[1], it returns "?". If I use tensor.get_shape().as_list()[1], it returns None.

I would like to know how I can work around this nondefined size. Is it possible to get the tensor's shape at runtime?

Edit: Using tf.shape, I get a tensor. How can I use this tensor to create the ksize, strides and paddings I need?

1 Answers1

1

I would like to know how I can work around this nondefined size. Is it possible to get the tensor's shape at runtime?

Use tf.shape() op to get the dynamic shape of a tensor instead of the x.get_shape() which returns the static shape of x.

This is explained in detail here.

In the above code replace tensor.get_shape()[1] with tf.shape(tensor)[1]

Vijay Mariappan
  • 16,921
  • 3
  • 40
  • 59
  • I don't need the shape of the tensor, but the size of the input. For example, if my batch is composed by two arrays with sizes n and m, I need to get n and m. – Gabriel Lima May 14 '18 at 15:55
  • Since it is a tensor, it returns the following error: TypeError: float() argument must be a string or a number, not 'Tensor' Also, when printing the tf.shape(tensor)[1], I get: Tensor("strided_slice:0", shape=(), dtype=int32) – Gabriel Lima May 14 '18 at 16:01
  • You need to replace the float operations with tensor operations. – Vijay Mariappan May 14 '18 at 16:05
  • How can I create the paddings, ksize and strides given that w_strd, w_size and pad_w are tensors that only contain a part of the information about paddings, ksize and strides? – Gabriel Lima May 14 '18 at 16:52