代码之家  ›  专栏  ›  技术社区  ›  krishnab

Tensorflow:是的tf.image.resize仍然不对齐角点?

  •  0
  • krishnab  · 技术社区  · 6 年前

    我在看这个 blog post 在Hackernoon关于如何 Tensorflow's tf.image.resize_area()

    作者接着说,用户不应该使用 tf.image.resize 函数,因为潜在的不可预测的行为。这篇文章是从2018年1月开始的,所以不久前。实际上,我查看了文章的评论部分,没有人提到问题已经修复。

    我只是想知道这些问题是否仍然存在,解决办法是什么?的后续版本中的任何更改 tensorflow . 我能用吗 tf.keras

    0 回复  |  直到 6 年前
        1
  •  4
  •   UpstatePedro    5 年前

    在我最初读到你提到的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)
    
    
        2
  •  1
  •   irudyak    5 年前

    tf.resize 在真实的图像上,我不能得到相同的图像。因此,准备好根据您的培训结果尝试不同的库。查看详细信息 here .

    # we may get the same image
    image_tf = tf.io.read_file(str(image_path))
    image_tf = tf.image.decode_jpeg(image_tf, channels=3, dct_method='INTEGER_ACCURATE')
    
    image_cv = cv2.imread(str(image_path))
    image_cv = cv2.cvtColor(image_cv, cv2.COLOR_BGR2RGB)
    
    np.sum(np.abs(image_cv - image_tf)) # 0
    
    # but not the same resized image
    image_tf_res = tf.image.resize(image_tf, IMAGE_SIZE, method='bilinear')
    image_cv_res = cv2.resize(image_cv, tuple(IMAGE_SIZE), interpolation=cv2.INTER_LINEAR)
    
    # this is NOT 0
    np.sum(np.abs(image_pil_res - image_tf_res)), np.sum(np.abs(image_cv_res - image_tf_res))
    
        3
  •  0
  •   kingfischer    4 年前

    Hackernoon article . 你可以找到一个短笔记本 here PIL scikit-image .

    推荐文章