前面寫了TFRecordWriter的生成。這次寫TFRecordReader。
代碼附上:
def read_and_decode(filename):
#根據文件名生成一個隊列
filename_queue = tf.train.string_input_producer([filename])
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue) #返回文件名和文件
features = tf.parse_single_example(serialized_example,
features={
'label': tf.FixedLenFeature([], tf.int64),
'img_raw' : tf.FixedLenFeature([], tf.string),
})
img = tf.decode_raw(features['img_raw'], tf.uint8)
img = tf.reshape(img, [224, 224, 3])
# img = tf.reshape(img, [39, 39, 3])
img = tf.cast(img, tf.float32) * (1. / 255) - 0.5
label = tf.cast(features['label'], tf.int32)
print img,label
return img, label
這里我碰到了一個非常奇怪的問題,困擾了我大半天。百思不得其解。
問題的報錯是:讀入沒有任何問題,在把讀入的數據輸入tensorflow中訓練模型的時候,前50次都是好的,
然后就開始報錯:
img = tf.reshape(img, [224, 224, 3])
輸入的tensor是200704,而期望的tensor是150528
200704=224*224*4,
150528=224*224*3.是不是通道數不對?我用opencv讀入后打印出來的通道數都是3。
真是奇怪的問題。
后來把原始圖片中的png和jpeg格式的圖片刪除了。重新生成TFRecord
。沒有報錯了
估計還是圖片的底層屬性問題