【问题标题】:How to apply crop_to_bounding_box in TensorFlow 2 to a symbolic tensor?如何将 TensorFlow 2 中的crop_to_bounding_box 应用于符号张量?
【发布时间】:2021-05-20 17:37:20
【问题描述】:

我有以下代码,旨在查找像素强度 >= 25 的第一个和最后一个像素,然后裁剪到该边界框:

white_pixels = tf.where(input_img >= 25)
first_white_pixel = white_pixels[:, 0]
first_white_pixel = tf.cast(first_white_pixel, dtype=tf.int32)
last_white_pixel = white_pixels[:, -1]
last_white_pixel = tf.cast(last_white_pixel, dtype=tf.int32)
cropped = tf.image.crop_to_bounding_box(input_img, first_white_pixel[0], 0, last_white_pixel[0] - first_white_pixel[0], 299)

但是,我不断收到错误消息,指出 tf.image.crop_to_bounding_box 中的 target_height 必须高于 0。在我所有的图像中,last_white_pixel[0] - first_white_pixel[0] 的结果肯定高于 0。代码是在 TensorFlow 2.3 中作为符号张量执行,并且在非符号设置中工作正常(因为缺少更好的术语)。

【问题讨论】:

    标签: python tensorflow2.0 tensor


    【解决方案1】:

    我的错误似乎是切片错误。而不是写

    first_white_pixel = white_pixels[:, 0]
    last_white_pixel = white_pixels[:, -1]
    

    我应该写的

    first_white_pixel = white_pixels[0, :]
    last_white_pixel = white_pixels[-1, :]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-14
      • 1970-01-01
      • 2020-09-22
      • 2020-04-29
      相关资源
      最近更新 更多