Tensorflow迭代器迭代失败。

问题描述 投票:0回答:1

我正在做一个与实例分割有关的项目。我试图用我自己的图像数据集来训练一个SegNet,这个数据集包括一组图像和它们对应的掩码,我已经成功地使用tf.Dataset来加载我的数据。但是每次我使用可馈送迭代器将数据集馈送到SegNet时,我的程序总是在没有任何错误或警告的情况下终止。我的代码如下所示。

load_satellite_image() 用来读取图片的文件名和 dataset() 是用来加载图像与tf.Dataset。似乎迭代器无法更新输入管道。

     train_path = "data_example/train.txt"
     val_path = "data_example/test.txt"

     config_file = 'config.json'
     with open(config_file) as f:
         config = json.load(f)

     train_img, train_mask = load_satellite_image(train_path)   
     val_img, val_mask = load_satellite_image(val_path)   

     train_dataset = dataset(train_img, train_mask, config, True, 0, 1)  
     val_dataset = dataset(val_img, val_mask, config, True, 0, 1)  

     train_iter = train_dataset.make_initializable_iterator()
     validation_iter = val_dataset.make_initializable_iterator()

     handle = tf.placeholder(tf.string, shape=[])    

     iterator =  tf.data.Iterator.from_string_handle(handle,
                 train_dataset.output_types,train_dataset.output_shapes)

     next_element = iterator.get_next()

     with tf.Session() as Sess:
         sess.run(train_iter.initializer)
         sess.run(validation_iter.initializer)
         train_iter_handle = sess.run(train_iter.string_handle())
         val_iter_handle = sess.run(validation_iter.string_handle())

         for i in range(2):
             print("1")
             try:
                 while True:

                     for i in range(5):
                         print(sess.run(next_element,feed_dict={handle:train_iter_handle}))
                         print('----------------------------','\n')

                     for i in range(2):
                         print(sess.run(next_element,feed_dict={handle:val_iter_handle}))
             except tf.errors.OutOfRangeError:
                     pass

运行上述代码后,我得到了。

         In [2]: runfile('D:/python_code/tensorflow_study/SegNet/load_data.py', 
         wdir='D:/python_code/tensorflow_study/SegNet')

        (tf.float32, tf.int32)
        (TensorShape([Dimension(360), Dimension(480), Dimension(3)]), TensorShape([Dimension(360), 
        Dimension(480), Dimension(1)]))
        (tf.float32, tf.int32)
        (TensorShape([Dimension(360), Dimension(480), Dimension(3)]), TensorShape([Dimension(360), 
        Dimension(480), Dimension(1)]))
        WARNING:tensorflow:From D:\Anaconda\envs\tensorflow-gpu\lib\site- 
        packages\tensorflow\python\data\ops\dataset_ops.py:1419: colocate_with (from 
        tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
        Instructions for updating:
        Colocations handled automatically by placer.

        In [1]:

我很困惑,我的代码无缘无故被终止了。正如你所看到的,我可以得到训练验证图像和掩模的形状和数据类型,这意味着问题与我的数据集无关。然而,我的 for 循环中 tf.Session() 没有被执行,我也无法得到以下结果 print("1"). 迭代器不是由 sess.run() 也是。有谁以前遇到过这个问题?

谢谢!!!

python tensorflow tensorflow-datasets
1个回答
0
投票

问题解决了。这是一个愚蠢的错误,浪费了我很多时间。我的程序之所以会被终止而没有错误信息,是因为我用愚蠢的Spyder来写代码,我不知道为什么它不显示错误信息。其实,TensorFlow是存在一个错误信息产生的。巧合的是,我通过Anaconda的命令窗口运行我的代码,我得到了这个错误信息。

2020-04-30 17:31:03.591207: W tensorflow/core/framework/op_kernel.cc:1401] OP_REQUIRES failed at whole_file_read_ops.cc:114 : Invalid argument: NewRandomAccessFile failed to Create/Open: D:\Study\PhD\python_code\tensorflow_study\SegNet\data_example\trainannot\ges_517405_679839_21.jpg

迭代器不工作,因为Tensorflow找不到掩模位置。图像和掩模位置存储在这样的文本文件中。

data_example\train\ges_517404_679750_21.jpg,data_example\trainannot\ges_517404_679750_21.jpg
data_example\train\ges_517411_679762_21.jpg,data_example\trainannot\ges_517411_679762_21.jpg

左边是原始图像的位置 右边是遮罩的位置。一开始,我用了 split(",") 来分别获得图像和遮罩的位置,但似乎遮罩的位置有问题。于是我检查了用于生成文本文件的代码。

file.writelines([Train_path[i],',',TrainAnnot_path[i],'\n'])

文本文件的每一行都是以 \n这就是为什么Tensorflow无法获得掩模的位置。所以我用 file.writelines([Train_path[i],',',TrainAnnot_path[i],'\n'])file.writelines([Train_path[i],' ',TrainAnnot_path[i],'\n'])和使用 strip().split(" ") 而非 split(" "). 这就解决了问题。

© www.soinside.com 2019 - 2024. All rights reserved.