在我最初读到你提到的Hackernoon文章之后,我也遇到了
this article
本文对OpenCV、tf1.X和其他一些DL框架中双线性插值的不同实现进行了很好的总结。
我在tf2.0文档中找不到任何关于这方面的内容,所以我复制了文章中给出的示例来测试2.0中的双线性插值。
当我使用TensorFlow 2.0运行以下代码时,测试通过,因此迁移到TF2.0将为您提供一个与OpenCV实现匹配的双线性插值实现(因此解决了Hackernoon文章中提出的问题):
def test_tf2_resample_upsample_matches_opencv_methodology():
"""
According to the article below, the Tensorflow 1.x implementation of bilinear interpolation for resizing images did
not reproduce the pixel-area-based approach adopted by OpenCV. The `align_corners` option was set to False by
default due to some questionable legacy reasons but users were advised to set it to True in order to get a
'reasonable' output: https://jricheimer.github.io/tensorflow/2019/02/11/resize-confusion/
This appears to have been fixed in TF 2.0 and this test confirms that we get the results one would expect from a
pixel-area-based technique.
We start with an input array whose values are equivalent to their column indices:
input_arr = np.array([
[[0], [1], [2], [3], [4], [5]],
[[0], [1], [2], [3], [4], [5]],
])
And then resize this (holding the rows dimension constant in size, but increasing the column dimnesion to 12) to
reproduce the OpenCV example from the article. We expect this to produce the following output:
expected_output = np.array([
[[0], [0.25], [0.75], [1.25], [1.75], [2.25], [2.75], [3.25], [3.75], [4.25], [4.75], [5]],
[[0], [0.25], [0.75], [1.25], [1.75], [2.25], [2.75], [3.25], [3.75], [4.25], [4.75], [5]],
])
"""
input_tensor = tf.convert_to_tensor(
np.array([
[[0], [1], [2], [3], [4], [5]],
[[0], [1], [2], [3], [4], [5]],
]),
dtype=tf.float32,
)
output_arr = tf.image.resize(
images=input_tensor,
size=(2,12),
method=tf.image.ResizeMethod.BILINEAR).numpy()
expected_output = np.array([
[[0], [0.25], [0.75], [1.25], [1.75], [2.25], [2.75], [3.25], [3.75], [4.25], [4.75], [5]],
[[0], [0.25], [0.75], [1.25], [1.75], [2.25], [2.75], [3.25], [3.75], [4.25], [4.75], [5]],
])
np.testing.assert_almost_equal(output_arr, expected_output, decimal=2)