ValueError:使用tf.image.crop_to_bounding_box时,具有dtype float32的Tensor的Tensor转换请求dtype int32

时间:2018-07-11 05:32:52

标签: python tensorflow image-processing keras

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

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

感谢您提供任何解决方法的帮助

2 个答案:

答案 0 :(得分:1)

TensorFlow使用tf.slice裁剪图像,这要求张量total_heighttotal_widthyminxmin的数据类型为{{1 }}:

int32

答案 1 :(得分:0)

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

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