Tensorflow read_file()什么都不做

时间:2018-10-10 20:02:26

标签: python tensorflow

我正在尝试使用Tensorflow读取和解码图像文件。我有以下代码:

dir_path = os.path.dirname(os.path.realpath(__file__))
filename = dir_path + '/images/cat/cat1.jpg'
image_file = tf.read_file(filename)
image_decoded = tf.image.decode_jpeg(image_file, channels=3)

print(image_file)
print(image_decoded)

这将产生以下输出:

Tensor("ReadFile:0", shape=(), dtype=string) Tensor("DecodeJpeg:0", shape=(?, ?, 3), dtype=uint8)

似乎Tensorflow根本没有读取文件。但是,我找不到任何错误消息,表明出了点问题。我不知道该如何解决,将不胜感激!

2 个答案:

答案 0 :(得分:3)

Tensorflow创建一个计算图,然后应对其进行评估。您在结果中看到的就是创建的操作。您需要定义一个Session对象以获取操作结果。

Name
----
Cris     1
Cris     2
Pablo    1
Pablo    2
Pablo    3

请查看此tensorflow资源,以帮助您进一步了解!

答案 1 :(得分:0)

当我们第一次尝试使用Tensorflow时,这是一个很大的障碍。

现在,Tensorflow团队提出了解决方案。

import tensorflow as tf
tf.enable_eager_execution()

在程序的开头运行以上两行。

然后您的打印功能将产生类似以下的内容。

<tf.Tensor: id=15, shape=(), dtype=string, numpy=b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\
<tf.Tensor: id=17, shape=(747, 1024, 3), dtype=uint8, numpy=
array([[[ 0,  0,  0],
        [ 0,  0, 

Tensorflow团队称其为渴望执行。现在,我们不需要运行tf.Session来查看张量中的内容。