tf.image.convert_image_dtype(image, dtype)

在将ROS图像消息转换并输入神经网络时,遇到抓取框坐标异常问题。原因在于`tf.image.convert_image_dtype()`函数在从uint8转换为float32时不正确地处理了数据范围。当数据直接从ROS的uint8图像转换为float32时,网络接收到的不是0-1区间的值,导致问题。解决方案是确保在转换前或转换过程中正确缩放数据。

前几天把网上找的一个检测抓取框的代码改成ROS node发现结果一直很离谱,抓取框坐标比图片还大,今天突然想起来又看了看代码,发现了问题出在tf.image.convert_image_dtype(image, dtype)这个函数在进行类型转换时自动的scale上。
这个是我一开始写的用来读取png或者jpg图片,然后输入到网络里的代码:

def image_input(image_path):
    height =224
    width = 224
    image = tf.image.decode_png(tf.read_file(image_path), channels=3)
    image = tf.image.convert_image_dtype(image, dtype=tf.float32)
    image = tf.image.resize_images(image, [height,width])
    return image

这里tf.image.decode_png 得到的是uint8格式,范围在0-255之间,经过convert_image_dtype 就会被转换为区间在0-1之间的float32格式,网络输出的结果也没毛病。

在ROS node里,因为要从我的相机那里订阅图片的message,所以用了cv3_bridge先把ros msg转换成opencv的图片格式,再转成numpy.array输入到placeholder里。我犯的错误就是把placeholder的数据类型写成了float32。

self.images_original = tf.placeholder(tf.uint8, [None,None,3], name="input_images")
self.image = self.image_process(self.images_original)
...
    def image_process(self, image):
        height =224
        width = 224
        image = tf.image.convert_image_dtype(image, dtype=tf.float32)
        image = tf.image.resize_images(image, [height,width])
        return image

这样的话,0-255区间的uint8格式的numpy.array被直接转成了float32格式,而tf.image.convert_image_dtype(image, dtype) 对于输入为float类型的数认为已经处在0-1区间,就不会进行scale了,所以最后输入到网络中的图像并不是0-1区间的值。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值