python - 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 似乎根本没有读取该文件。但是,我找不到任何表明出现问题的错误消息。我不知道如何解决这个问题,任何帮助将不胜感激!
解决方案
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资源,帮助您进一步了解!
推荐阅读
- python - "No module named numpy" in CDAP python transformation
- amazon-web-services - com.amazonaws.AbortedException while releasing dynamoDB lock
- php - 处理@抑制的错误的正确方法?
- java - 使用 Hibernate 4 进行批处理
- javascript - 单击时隐藏按钮并在视频结束时显示第二个按钮
- matlab - 谁能给我一些关于 MATLAB 中的 fprintf('How about single quote('')?\n') 的解释?
- r - 如何保存每次优化迭代的系数?
- c++ - C++ 通用模板类算术
- php - 将数据从 php 发送到 ajax(使用 mysqli_fetch_assoc 和 json_encode)
- node.js - 使用app.get调用错误端点时如何处理?