无法让简单的 TFRecord reader 工作
Cannot get a simple TFRecord reader to work
我试图让一个非常简单的 TFRecord reader 工作,但无济于事。 (我可以让作家工作得很好)。
来自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.
谢谢
问题也可能不在于 TFRecords,而在于 string_input_producer 在设置 num_epochs 时使用 LOCAL_VARIABLES 集合。参见 here。
op tf.initialize_all_variables()
没有初始化 all 变量,正如它的名字所暗示的那样。作为快速修复,请使用以下内容:
init_op = tf.group(tf.initialize_all_variables(), tf.initialize_local_variables())
,但考虑转到 Tensorflow r0.12,该操作已弃用,取而代之的是 tf.global_variables_initializer
和 tf.local_variables_initializer
在较新版本的 TensorFlow 中:
tf.initialize_all_variables()
is deprecated.
他们提到你必须使用:
tf.global_variables_initializer()
这不能解决问题。如果我们查看 tf.train.string_input_producer()
的较新的 API,它提到 num_epochs 将被创建为局部变量。这里发生的事情是队列中没有任何东西可以读取,因此它说请求 1 当前 0。只需添加:
tf.local_variables_initializer()
我已经推送了更新
我试图让一个非常简单的 TFRecord reader 工作,但无济于事。 (我可以让作家工作得很好)。
来自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.
谢谢
问题也可能不在于 TFRecords,而在于 string_input_producer 在设置 num_epochs 时使用 LOCAL_VARIABLES 集合。参见 here。
op tf.initialize_all_variables()
没有初始化 all 变量,正如它的名字所暗示的那样。作为快速修复,请使用以下内容:
init_op = tf.group(tf.initialize_all_variables(), tf.initialize_local_variables())
,但考虑转到 Tensorflow r0.12,该操作已弃用,取而代之的是 tf.global_variables_initializer
和 tf.local_variables_initializer
在较新版本的 TensorFlow 中:
tf.initialize_all_variables()
is deprecated.
他们提到你必须使用:
tf.global_variables_initializer()
这不能解决问题。如果我们查看 tf.train.string_input_producer()
的较新的 API,它提到 num_epochs 将被创建为局部变量。这里发生的事情是队列中没有任何东西可以读取,因此它说请求 1 当前 0。只需添加:
tf.local_variables_initializer()
我已经推送了更新