【问题标题】:ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32 while using tf.image.crop_to_bounding_boxValueError:在使用 tf.image.crop_to_bounding_box 时,张量转换为具有 dtype float32 的张量请求 dtype int32
【发布时间】:2023-03-31 11:05:01
【问题描述】:

我正在尝试根据边界框裁剪图像。

bbox = [{'width': '500', 'ymin': '125', 'depth': '3', 'xmax': '387', 'xmin': '29', 'height': '375', 'ymax': '245'}]

ymin = float(bbox['ymin'])/float(bbox['height'])
ymax = float(bbox['ymax'])/float(bbox['height'])
xmin = float(bbox['xmin'])/float(bbox['width'])
xmax = float(bbox['xmax'])/float(bbox['width'])

total_height = tf.convert_to_tensor(ymax_int -  ymin_int)
total_width = tf.convert_to_tensor(xmax -  xmin) 
ymin =  tf.convert_to_tensor(ymin)
xmin  = tf.convert_to_tensor(xmin)

img=mpimg.imread(filename)
img = tf.convert_to_tensor(img)
image = tf.image.crop_to_bounding_box(img,  ymin,  xmin,  total_height, total_width)

我收到以下错误:

 ValueError: Tensor conversion requested dtype int32 for Tensor with dtype 
   float32: 'Tensor("Const_7:0", shape=(), dtype=float32)'

   Const_7:0 is ymin

感谢任何有关如何解决此问题的帮助

【问题讨论】:

  • tf.convert_to_tensor(arg, dtype=tf.float32) 有帮助吗?

标签: python tensorflow image-processing keras


【解决方案1】:

TensorFlow使用tf.slice裁剪图像,要求张量total_heighttotal_widthyminxmin的数据类型为int32

total_height = tf.convert_to_tensor(ymax_int -  ymin_int, dtype=tf.int32)
total_width = tf.convert_to_tensor(xmax -  xmin, dtype=tf.int32) 
ymin =  tf.convert_to_tensor(ymin, dtype=tf.int32)
xmin  = tf.convert_to_tensor(xmin, dtype=tf.int32)

【讨论】:

    【解决方案2】:

    尝试将total_height 变量设置为以下值:

    total_height = tf.cast(tf.convert_to_tensor(ymax_int - ymin_int, dtype = tf.float32), dtype = tf.int32)
    

    【讨论】:

      猜你喜欢
      • 2019-04-28
      • 1970-01-01
      • 2020-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-10
      • 2019-08-26
      • 1970-01-01
      相关资源
      最近更新 更多