2017-07-05 42 views
0

我在下面提到的代碼中收到此錯誤。請幫助我解決這個問題。 這段代碼每次運行這段代碼時都會打印變量的批次數。我無法找出錯誤。在張量流中使用隊列的輸入流水線中的錯誤

OutOfRangeError異常(參見上述用於回溯):FIFOQueue '_2_batch/fifo_queue' 被關閉,並且沒有足夠的元件 (請求15,電流大小0)[[節點:批次= QueueDequeueManyV2 [component_types = [DT_FLOAT] ,timeout_ms = -1, _device = 「/作業:本地主機/複製:0 /任務:0/CPU:0」](批次/ fifo_queue,批次/ N)]]

import tensorflow as tf 
import numpy as np 
import os 

batch_size = 16 
min_queue = 256 


def load_image(): 
    length = calculate_size("./Coco_subset_5356") 

    names = tf.train.match_filenames_once("./Coco_subset_5356/*.jpg") 

    # Extracted names from the file 
    filename_queue = tf.train.string_input_producer(names) 

    #Initialised the File reader 
    reader = tf.WholeFileReader() 

    key, value = reader.read(filename_queue) 
    my_img = tf.image.decode_jpeg(value, channels = 3) 
    my_img = tf.image.resize_images(my_img, [256,256]) 
    my_img.set_shape((256,256,3)) 


    print(length) 

    images = tf.train.batch(
     [my_img], 
     batch_size=batch_size, 
     num_threads=1, 
     capacity=min_queue + 3*batch_size) 

    print(images) 

with tf.Session() as sess: 
    #sess.run(init_op) 
    tf.local_variables_initializer().run() 
    #print(tf.get_collection(tf.GraphKeys.LOCAL_VARIABLES)) 

    #For coordination between queue runner and the reader 
    coord = tf.train.Coordinator() 
    threads = tf.train.start_queue_runners(coord = coord) 
    for j in range(length/batch_size): 
     #optimizer.run(feed_dict={input_batch: images[i]) 
     x = sess.run(images)  
     print(j) 
     print(x.shape) 

    coord.request_stop() 
    coord.join(threads) 

def calculate_size(img_dir): 
    file = [] 
    for subdir, dirs, files in os.walk(img_dir): 
     for i in files: 
      i = os.path.join(img_dir, i) 
      file.append(i) 
     length = len(file) 
    return length 

load_image() 
+0

我有一個類似的問題,在我的情況下,它是一些圖像沒有得到正確解碼(在我的數據集中,我有1x1像素的圖像,設法通過過濾過程..)。你確定*所有*你的圖像可以正確打開並解碼爲jpg文件嗎? (它們也不能被截斷,默認情況下tensorflow會在處理截斷圖像時引發錯誤) – GPhilo

+0

目錄中的* .jpg文件的數量是多少?它是15的倍數嗎?如果在'tf.train.batch'方法中設置'allow_smaller_final_batch = True'會怎樣? – npf

+0

你也不介意編輯你的問題,使錯誤更具可讀性(例如使用>標記)? – npf

回答

0

聲明:這不是一個正確的答案,但是我不能在代碼中發佈代碼,這可能會指向你正確的方向ction。

正如我對這個問題的評論所預料的那樣,我在其中一個數據集上發生了同樣的錯誤。在我的情況下,問題是不是我的數據集中的所有圖像都可以解碼爲jpg文件。截斷的圖像,在我的情況下,1x1像素的圖像隱藏在我的數據中,每當我嘗試使用數據集時,我都會讓隊列關閉報告錯誤,我很難找到答案。

解決了我寫一個小過濾器腳本通過的所有文件一個接一個進入並記錄那些它不能處理的問題:

import tensorflow as tf 
import glob 

image_data_placeholder = tf.placeholder(tf.string) 

filenames = glob.glob(r'C:\my_test_folder\*.jpg') 
decoded_img_op = tf.image.decode_jpeg(image_data_placeholder) 

with tf.Session() as sess: 
    for fn in filenames: 
    with open(fn, 'rb') as fp: 
     image_data = fp.read() 
    try: 
     sess.run(decoded_img_op, feed_dict={image_data_placeholder:image_data}) 
    except: 
     # log the filename o the broken image (or delete/move/etc) 
     print('Cannot decode file {}'.format(fn)) 

這不是最有效的實現,但它是不夠好爲我的用例。