5

I am using Tensorflow 1.4.0

The Tensorflow tf.image.resize_bilinear() has an argument called 'align_corners' and I am confused with the behavior when we set it to be False. In the official document, it says:

align_corners: An optional bool. Defaults to False. If true, the centers of the 4 corner pixels of the input and output tensors are aligned, preserving the values at the corner pixels. Defaults to false.

When I use tf.image.resize_bilinear() with align_corners=True in the following program:

import tensorflow as tf
sess = tf.Session()
x = tf.Variable(tf.Variable([[[[1],[2]],[[3],[4]]]]))
pooling_output_size = [4, 4]
pool_output = tf.image.resize_bilinear(x, pooling_output_size,align_corners=True)
sess.run(tf.global_variables_initializer())
print pool_output.eval(session=sess)

it outputs

[[[[1.       ]
   [1.3333334]
   [1.6666667]
   [2.       ]]

  [[1.6666667]
   [2.       ]
   [2.3333335]
   [2.6666667]]

  [[2.3333335]
   [2.6666665]
   [3.       ]
   [3.3333335]]

  [[3.       ]
   [3.3333333]
   [3.6666667]
   [4.       ]]]]

which corners are correctly aligned.

However when I set the align_corners=False, I got the following weird outputs

[[[[1. ]
   [1.5]
   [2. ]
   [2. ]]

  [[2. ]
   [2.5]
   [3. ]
   [3. ]]

  [[3. ]
   [3.5]
   [4. ]
   [4. ]]

  [[3. ]
   [3.5]
   [4. ]
   [4. ]]]]

Is there anyone who understand why Tensorflow will use this weird implementation? I didn't find any explanation anywhere.

Actually PyTorch's bilinear upsampling has the align_corner argument too, when you set it to True, it works well. But if you set it to False, it performs a differnet behaviour to Tensorflow's. I am totally confused with their implementations now (maybe just use align_corners=True will be fine).

Depu
  • 51
  • 1
  • 3
  • hmm... found this post, that seems to explain what align corners does and why to always enable align corners https://hackernoon.com/how-tensorflows-tf-image-resize-stole-60-days-of-my-life-aba5eb093f35 – Diana Jun 28 '18 at 09:08
  • @Diana Yes, I read this post too but the implementation in Tensorflow when align_corners=False is much more weird. It seems that in the top left part, it applies bilinear, in the below right part it applies nearest, in the below left and top right part, it applies linear upsampling. I can't really understand why. Most interestingly, I found that using align_corners=False will achieve better accuracy on PSPNet on cityscapes... – Depu Jun 28 '18 at 09:35

1 Answers1

2

For backward compatability, it seems. The link

https://hackernoon.com/how-tensorflows-tf-image-resize-stole-60-days-of-my-life-aba5eb093f35

suggests to me to always use align_corners=True

tyrex
  • 8,208
  • 12
  • 43
  • 50
  • 2
    I discussed this problem with my friends and we find out that if we want to align the image manually (following the DeepLab's 4 alignment rules), we should use align_corner = True. But if we don't want to care about alignment, the align_corner = False can guarantee that pixels in the top left corner are perfect aligned. P.S. your link is unavailable – Depu Oct 07 '18 at 01:26