python - 无法让简单的 TFRecord 阅读器工作

标签 python tensorflow protocol-buffers

我试图让一个非常简单的 TFRecord 阅读器工作,但没有成功。 (我可以让作家很好地工作)。

来自this github repo ,有一个 reader.py 文件,它看起来像这样:

import tensorflow as tf
import numpy as np
import time
from PIL import Image

def read_and_decode(filename_queue):
    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)
    features = tf.parse_single_example(
            serialized_example,
            # Defaults are not specified since both keys are required.
            features={
                    'height':tf.FixedLenFeature([], tf.int64),
                    'image_raw': tf.FixedLenFeature([], tf.string),
                    'label': tf.FixedLenFeature([], tf.int64)
            })
    image = tf.decode_raw(features['image_raw'], tf.uint8)
    image = tf.reshape(image,[478, 717, 3])
    image = tf.cast(image, tf.float32) * (1. / 255) - 0.5
    label = tf.cast(features['label'], tf.int32)
    return image


'''
Pointers:   Remember to run init_op
            tf.reshape may not be the ideal way.
'''
def run():
    with tf.Graph().as_default():
        filename_queue = tf.train.string_input_producer(["sample.tfrecords"],num_epochs=1)
        images = read_and_decode(filename_queue)
        image_shape = tf.shape(images)
        init_op = tf.initialize_all_variables()
        with tf.Session() as sess:
            sess.run(init_op)
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(coord=coord)
            img = sess.run([images])
            coord.request_stop()
            coord.join(threads)
run()

问题是当我运行它时,出现以下错误:

enter image description here

所以,最后一天我一直在思考这个问题。我不知道该怎么做,甚至不知道为什么它不起作用。这似乎是一个足够简单的例子,应该没有问题。我正在使用 TF010。

谢谢

最佳答案

在新版本的 TensorFlow 中:

tf.initialize_all_variables() is deprecated.

他们提到你必须使用:

tf.global_variables_initializer()

这并不能解决问题。如果我们查看 tf.train.string_input_ Producer() 的较新 API,它提到 num_epochs 将被创建为局部变量。这里发生的情况是队列中没有任何内容可供读取,因此它显示requested 1 current 0。只需添加以下内容:

tf.local_variables_initializer()

我已经推送更新了。

关于python - 无法让简单的 TFRecord 阅读器工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41488276/

相关文章:

tensorflow - 如何降低 Tensorflow 中的 RAM 消耗?

protocol-buffers - Protobuf 重复消息选项

go - 使用 google.protobuf.Timestamp 在 Go 中解析具有时区偏移的日期时间戳

python - 如何在 Google App Engine 中包含第三方 Python 库?

python - 使用 Selenium Python 保存大型 html 文件时出现 WebDriverException : Message: Exception. .. "Failure"nsresult: "0x80004005 (NS_ERROR_FAILURE)"

python - 从使用 Power BI 的网站抓取数据 - 从网站上的 Power BI 检索数据

python - 有没有有效的方法来通过适当的通知来验证许多变量?

python - 如何自定义ImageDataGenerator以修改目标变量值?

python - 从命令行强制 TensorFlow-GPU 使用 CPU

C++ 唯一持久类标识符