tf.image.resize_bilinear vs cv2.resize

2019-03-19 16:35发布

问题:

The results from tf.image.resize_bilinear are quite different from cv2.resize.

I found this a little bothersome. Set align_corners=True is not always reasonable because the four corners are not always supposed to be fixed in the corner. So is there anyway to make it a little more "symmetry"?

Code to reproduce:

import tensorflow as tf
import numpy as np
import cv2
np.set_printoptions(precision=3)
resize_shape = (10, 10)

a = np.ones((1, 2, 2, 1), dtype=np.float32)
a[0, 0, 0, 0] = 5.0
a[0, 1, 1, 0] = 5.0

b = tf.constant(a, dtype=tf.float32)
c = tf.image.resize_bilinear(b, resize_shape)

with tf.Session() as sess:
    np_c = sess.run(c)
    print np_c[0, :, :, 0]

print cv2.resize(a[0], resize_shape, interpolation=cv2.INTER_LINEAR)

Obtained results:

# tf.image.resize_bilinear
[[ 5.    4.2   3.4   2.6   1.8   1.    1.    1.    1.    1.  ]
 [ 4.2   3.72  3.24  2.76  2.28  1.8   1.8   1.8   1.8   1.8 ]
 [ 3.4   3.24  3.08  2.92  2.76  2.6   2.6   2.6   2.6   2.6 ]
 [ 2.6   2.76  2.92  3.08  3.24  3.4   3.4   3.4   3.4   3.4 ]
 [ 1.8   2.28  2.76  3.24  3.72  4.2   4.2   4.2   4.2   4.2 ]
 [ 1.    1.8   2.6   3.4   4.2   5.    5.    5.    5.    5.  ]
 [ 1.    1.8   2.6   3.4   4.2   5.    5.    5.    5.    5.  ]
 [ 1.    1.8   2.6   3.4   4.2   5.    5.    5.    5.    5.  ]
 [ 1.    1.8   2.6   3.4   4.2   5.    5.    5.    5.    5.  ]
 [ 1.    1.8   2.6   3.4   4.2   5.    5.    5.    5.    5.  ]]
# cv2.resize
[[ 5.    5.    5.    4.2   3.4   2.6   1.8   1.    1.    1.  ]
 [ 5.    5.    5.    4.2   3.4   2.6   1.8   1.    1.    1.  ]
 [ 5.    5.    5.    4.2   3.4   2.6   1.8   1.    1.    1.  ]
 [ 4.2   4.2   4.2   3.72  3.24  2.76  2.28  1.8   1.8   1.8 ]
 [ 3.4   3.4   3.4   3.24  3.08  2.92  2.76  2.6   2.6   2.6 ]
 [ 2.6   2.6   2.6   2.76  2.92  3.08  3.24  3.4   3.4   3.4 ]
 [ 1.8   1.8   1.8   2.28  2.76  3.24  3.72  4.2   4.2   4.2 ]
 [ 1.    1.    1.    1.8   2.6   3.4   4.2   5.    5.    5.  ]
 [ 1.    1.    1.    1.8   2.6   3.4   4.2   5.    5.    5.  ]
 [ 1.    1.    1.    1.8   2.6   3.4   4.2   5.    5.    5.  ]]

EDITED

When setting align_corners=True, 4 corners of images and resized images are aligned but only 4 pixels.

Considering resizing images, the 4 corners in the image should present the areas in 4 corners of the resized image (like cv2.resize does), instead of 4 points at the very corner.

# tf.image.resize_bilinear(b, resize_shape, align_corners=True)
[[ 5.    4.56  4.11  3.67  3.22  2.78  2.33  1.89  1.44  1.  ]
 [ 4.56  4.21  3.86  3.52  3.17  2.83  2.48  2.14  1.79  1.44]
 [ 4.11  3.86  3.62  3.37  3.12  2.88  2.63  2.38  2.14  1.89]
 [ 3.67  3.52  3.37  3.22  3.07  2.93  2.78  2.63  2.48  2.33]
 [ 3.22  3.17  3.12  3.07  3.02  2.98  2.93  2.88  2.83  2.78]
 [ 2.78  2.83  2.88  2.93  2.98  3.02  3.07  3.12  3.17  3.22]
 [ 2.33  2.48  2.63  2.78  2.93  3.07  3.22  3.37  3.52  3.67]
 [ 1.89  2.14  2.38  2.63  2.88  3.12  3.37  3.62  3.86  4.11]
 [ 1.44  1.79  2.14  2.48  2.83  3.17  3.52  3.86  4.21  4.56]
 [ 1.    1.44  1.89  2.33  2.78  3.22  3.67  4.11  4.56  5.  ]]

回答1:

This is a known issue, please see https://github.com/tensorflow/tensorflow/issues/6720