Python Tensorflow read_file()不执行任何操作

Python Tensorflow read_file()不执行任何操作,python,tensorflow,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)

我正在尝试使用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)
张量(“DecodeJpeg:0”,shape=(?,?,3),dtype=uint8)


看起来Tensorflow根本没有读取该文件。但是,我找不到任何错误消息,表明出现了问题。我不知道如何才能解决这个问题,任何帮助都将不胜感激

Tensorflow创建一个计算图,然后对其进行计算。您在结果中看到的是创建的op。您需要定义一个会话对象来获得操作的结果

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)
with tf.Session() as sess:
     f, img = sess.run([image_file, image_decoded])
     print(f)
     print(img)

查看此资源以帮助您进一步了解

当我们第一次尝试使用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, 

你解决问题了吗?