Skip to main content
 首页 » 编程设计

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

2024年10月17日8Leo_wl

我试图让一个非常简单的 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 将被创建为局部变量。这里发生的事情是队列中没有任何东西可供它读取,因此它说请求 1 当前 0。只需添加:

tf.local_variables_initializer()

我已经推送了更新。