我试图让一个非常简单的 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()
问题是当我运行它时,出现以下错误:
所以,最后一天我一直在思考这个问题。我不知道该怎么做,甚至不知道为什么它不起作用。这似乎是一个足够简单的例子,应该没有问题。我正在使用 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/