首页 > 解决方案 > TensorFlow read_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) Tensor("DecodeJpeg:0", shape=(?, ?, 3), dtype=uint8)

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

标签: pythontensorflow

解决方案


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

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资源,帮助您进一步了解!


推荐阅读