首页 > 解决方案 > OutOfRangeError:张量流迭代器未在运行之间重新初始化

问题描述

我正在使用以下设置通过 tensorflow 微调 Inception 模型,并提供批处理tf.DatasetAPI。但是,每次我尝试训练这个模型时(在成功检索任何批次之前),我都会收到一个 OutOfRangeError 声称迭代器已用尽:

Caught OutOfRangeError. Stopping Training. End of sequence
     [[node IteratorGetNext (defined at <ipython-input-8-c768436e70d8>:13)  = IteratorGetNext[output_shapes=[[?,224,224,3], [?,1]], output_types=[DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](OneShotIterator)]]
with tf.Graph().as_default():

我创建了一个函数来输入硬编码的批次作为 的结果get_batch,它运行和收敛没有任何问题,让我相信图形和会话代码工作正常。我还测试了get_batch在会话中迭代的函数,这不会导致错误。我期望的行为是重新开始训练(尤其是重置笔记本等)会在数据集上产生一个新的迭代器。

训练模型的代码:

with tf.Graph().as_default():

    tf.logging.set_verbosity(tf.logging.INFO)
    images, labels = get_batch(filenames=tf_train_record_path+train_file)
    # Create the model, use the default arg scope to configure the batch norm parameters.
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        logits, ax = inception.inception_v1(images, num_classes=1, is_training=True)

    # Specify the loss function:
    tf.losses.mean_squared_error(labels,logits)
    total_loss = tf.losses.get_total_loss()
    tf.summary.scalar('losses/Total_Loss', total_loss)


     # Specify the optimizer and create the train op:
    optimizer = tf.train.AdamOptimizer(learning_rate=0.01)
    train_op = slim.learning.create_train_op(total_loss, optimizer)

    # Run the training:
    final_loss = slim.learning.train(
        train_op,
        logdir=train_dir,
        init_fn=get_init_fn(),
        number_of_steps=1)

使用数据集获取批次的代码

def get_batch(filenames):
    dataset = tf.data.TFRecordDataset(filenames=filenames)

    dataset = dataset.map(parse)
    dataset = dataset.batch(2)

    iterator = dataset.make_one_shot_iterator()
    data_X, data_y = iterator.get_next()

    return data_X, data_y 

这个先前提出的问题类似于我遇到的问题,但是,我没有使用batch_join电话。如果这是 slim.learning.train、从检查点或范围恢复的问题,我不是。任何帮助,将不胜感激!

标签: pythontensorflowdatasetslim

解决方案


您的输入管道看起来不错。问题可能与损坏的 TFRecords 文件有关。您可以使用随机数据尝试您的代码,或者将您的图像用作带有tf.data.Dataset.from_tensor_slices(). 此外,您的 parse 函数可能会导致问题。尝试使用 打印您的图像/标签sess.run

我建议使用 Estimator API 作为 train_op。它更方便,slim 很快就会被弃用。


推荐阅读