2017-08-10 18 views
0

我是R用户,对Python和TensorFlow都是新手,并且一直在努力让我的再培训图像分类器在修改label_image.py以便与Mobilenets一起使用时实际做出预测。我发现了这个问题,并知道我需要实现从this tutorial的最后一行,但我无法弄清楚如何。如何在Python中为Tensorflow Mobilenet预测将字节图像从[0,255]转换为[-1,1]浮点范围?

If you're going to be using the Mobilenet models in label_image or your own programs, you'll need to feed in an image of the specified size converted to a float range into the 'input' tensor. Typically 24-bit images are in the range [0,255], and you must convert them to the [-1,1] float range expected by the model with the formula (image - 128.)/128..

在R我习惯于处理JPEG作为3维数组。如果是这种格式,我会知道该怎么做,但从tf.gfile.FastGFile("fileName.jpg", 'rb').read()返回的图像类型是bytes。我真的不明白这是什么。直接应用它们给图像对象的公式返回TypeError: unsupported operand type(s) for -: 'bytes' and 'float'。我假设在改变范围之后,我仍然需要它以bytes的格式将它馈送到网络中,但我还没有100%清楚。任何澄清这个对象类型是什么以及如何使用它将非常感激。

回答

0

tf.gfile.FastGfile..read()返回一个二进制字符串,以获取您必须解码的图像值。

imagedata = tf.gfile.FastGFile("fileName.jpg", 'rb').read() 
img_decoded = tf.image.decode_jpeg(imagedata, dct_method="INTEGER_ACCURATE") 
image_standardized = tf.image.per_image_standardization(image) 
image_standardized = tf.clip_by_value(image_standardized, -1.0, 1.0) 
+0

谢谢!我认为在第三行你的意思是要img_decoded的图像,对吗?这是我需要的一部分,但现在它是一个张量对象。我如何将它恢复到之前的同一种二进制字符串? –

+0

是的,它返回解码后的jpeg图像,将解码的张量再次编码为二进制串,可以使用'tf.image.encode_jpeg(some_image)' –

+0

这是我的怀疑,但它给了我一个“TypeError:Input”图像'的'EncodeJpeg'Op的类型为float32,与uint8的预期类型不匹配。“ API中列出的所有参数在我看来都没有明显的修正,所以我认为我在错误的轨道上。 –

0

bytes是字面上的原始字节,它处在范围[0,255]的缓冲液中。您可以通过遍历它来获取int值。然后你可以正常化。

image = b'\x20\x30\x40' 
normalized = [(x-128.)/128 for x in image] 
print(normalized) # [-0.75, -0.625, -0.5] 
相关问题