简体   繁体   English

为什么 tf.image.decode_jpeg 可以解码 png?

[英]Why can tf.image.decode_jpeg decode a png?

Today I was testing some tensorflow (python) codes.今天我正在测试一些 tensorflow (python) 代码。 It's a neural network on the famous MNIST set.它是著名的 MNIST 集上的神经网络。

Everything worked well, so I just read through the codes and studied the structure of that network.一切都运行良好,所以我只是通读了代码并研究了该网络的结构。

When it came to the image input, I found the following code:当涉及到图像输入时,我发现了以下代码:

image_string = tf.read_file(filename)

image_decoded = tf.image.decode_jpeg(image_string, channels=3)

The code is using "decode_jpeg" instead of "decode_png".该代码使用“decode_jpeg”而不是“decode_png”。 And I didn't see any error.而且我没有看到任何错误。

However, I am 100% sure that the images are in PNG format.但是,我 100% 确定图像是 PNG 格式的。

I have used我用过

od -c -b 1.png

to look into those images and they are PNGs.查看这些图像,它们是 PNG。

So, why could "decode_jpeg" work on PNGs?那么,为什么“decode_jpeg”可以在 PNG 上工作呢? And is there any potential issue that might be caused by this?是否有任何可能由此引起的潜在问题?

Thedocumentation for tensorflow states: tensorflow 的文档说明:

This op also supports decoding PNGs and non-animated GIFs since the interface is the same, though it is cleaner to use tf.image.decode_image.此操作还支持解码 PNG 和非动画 GIF,因为界面相同,但使用 tf.image.decode_image 更清晰。

So essentially it was a design decision that because the interface is the same to just handle it, rather than throw an error that forces user to use the correct API call.所以本质上这是一个设计决定,因为接口是相同的,只是处理它,而不是抛出一个错误,迫使用户使用正确的 API 调用。

img = tf.image.decode_png(file_bytes, channels=3) 

you can use decode_png for png files你可以对 png 文件使用decode_png

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM