我正在尝试实现一个从TFRecords二进制文件读取的模型的输入管道;每个二进制文件包含一个示例(图像、标签、其他我需要的东西)。
我有一个带有文件路径列表的文本文件,然后:
问题在于,同一个示例可以多次读取,有些示例可能根本无法访问;我将步骤数设置为图像总数除以批处理大小;因此,我希望在最后一步结束时访问所有输入示例,但事实并非如此;相反,有些示例被多次访问,有些从未(随机)访问过;这使得我的测试评估完全无法实现。
如果有人知道我做错了什么,请告诉我。
下面是我的模型测试代码的简化版本,谢谢!
def my_input(file_list, batch_size)
filename = []
f = open(file_list, 'r')
for line in f:
filename.append(params.TEST_RECORDS_DATA_DIR + line[:-1])
filename_queue = tf.train.string_input_producer(filename)
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(
serialized_example,
features={
'image_raw': tf.FixedLenFeature([], tf.string),
'label_raw': tf.FixedLenFeature([], tf.string),
'name': tf.FixedLenFeature([], tf.string)
})
image = tf.decode_raw(features['image_raw'], tf.uint8)
image.set_shape(params.IMAGE_HEIGHT*params.IMAGE_WIDTH*3)
image = tf.reshape(image, (params.IMAGE_HEIGHT,params.IMAGE_WIDTH,3))
image = tf.cast(image, tf.float32)/255.0
image = preprocess(image)
label = tf.decode_raw(features['label_raw'], tf.uint8)
label.set_shape(params.NUM_CLASSES)
name = features['name']
images, labels, image_names = tf.train.batch([image, label, name],
batch_size=batch_size, num_threads=2,
capacity=1000 + 3 * batch_size, min_after_dequeue=1000)
return images, labels, image_names
def main()
with tf.Graph().as_default():
# call input operations
images, labels, image_names = my_input(file_list=params.TEST_FILE_LIST, batch_size=params.BATCH_SIZE)
# load a trained model and make predictions
prediction = infer(images, labels, image_names)
with tf.Session() as sess:
for step in range(params.N_STEPS):
prediction_values = sess.run([prediction])
# process output
return发布于 2016-12-16 16:47:44
我的猜测是,tf.train.string_input_producer(filename)被设置为无限期地生成文件名,如果在多个(2)线程中对示例进行批处理,则可能是一个线程第二次开始处理该文件,而另一个线程尚未完成第一轮。要正确读取每个示例,请使用:
tf.train.string_input_producer(filename, num_epochs=1)并在会话开始时初始化局部变量:
sess.run(tf.initialize_local_variables())https://stackoverflow.com/questions/41188816
复制相似问题