前言
在使用tensorflow TFRecord的過程中,讀取*.tfrecord文件時出現錯誤,本文解決這個錯誤。
錯誤描述:
OutOfRangeError (see above for traceback): RandomShuffleQueue '_1_shuffle_batch/random_shuffle_queue' is closed and has insufficient elements (requested 64, current size 62) [[Node: shuffle_batch = QueueDequeueManyV2[component_types=[DT_UINT8, DT_INT32, DT_FLOAT, DT_FLOAT], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/device:CPU:0"](shuffle_batch/random_shuffle_queue, shuffle_batch/n)]]
源碼:
image_batch, label_batch, roi_batch, landmark_batch = tf.train.shuffle_batch([img,label,roi,landmark], batch_size=batch_size, capacity=capacity, min_after_dequeue=min_after_dequeue, num_threads=7)
錯誤原因:
在執行訓練的時候,隊列會被后台的線程填充好。如果設置了最大訓練迭代數(epoch),在某些時候,樣本出隊的操作可能會拋出一個tf.OutOfRangeError的錯誤。這是因為tensorflow的隊列已經到達了最大實際的最大迭代數,沒有更多可用的樣本了。這也是為何推薦代碼模板需要用try..except ..finally結構來處理這種錯誤。
一般遇到這個錯誤,代碼本身並沒有什么問題,基本上都是參數設置不一致或者和不合適導致的,要注意檢查各個參數。
對於博主來說,更改的參數有:
1. 數據格式的不一致性,要與生成tfrecord的數據格式保持一致。
features = tf.parse_single_example(serialized_example, features={ 'img':tf.FixedLenFeature([],tf.string), 'label':tf.FixedLenFeature([],tf.int64), 'roi':tf.FixedLenFeature([4],tf.float32), 'landmark':tf.FixedLenFeature([10],tf.float32), })
將label的數據格式由int32改為int64;
這方面,還有一個更改的地方是輸入圖像數據的大小。
# img = tf.reshape(img, [48,48,3]) img = tf.reshape(img, [img_size,img_size,3])
這里博主測試的是MTCNN中生成的pos_12_train.tfrecord的數據,故此處img_size應該是12;
2. batch_size的大小;
這個是需要和運行環境匹配的,也就是批量大小過大,系統可能處理不過來造成的;
3. 根據情況可能還有其他參數需要檢查,比如num_epochs、num_threads等等。
4. 也可以更換更好的硬件設備,比如更好的GPU顯卡;
參考
1. 理解tfrecord讀取數據;
2. TensorFlow bug;
完