0
我一直在努力創建自己的數據庫來訓練CNN。現在,我從tfrecord文件讀取數據時遇到問題。我已成功保存了一個tfrecord文件,其中包含兩個功能:圖像和標籤。當我嘗試閱讀它時,它只是讀取第一批,然後我收到一條錯誤消息。讀取tfrecord時的功能格式錯誤(無法將ndarray轉換爲張量或操作)
用於保存tfrecord文件中的代碼(我認爲只有5由於時間的關係的文件):
#SAVE TFRECORD FILE
import tensorflow as tf
import numpy as np
import Image
image_filename = [('/home/ag/Dropbox/DL/6_CNN_BD/data_resized/01GraspableGraspingRectangles_RGB/00%03d.png' % x) for x in range(1,6)]
records_filename = '/home/ag/Dropbox/DL/6_CNN_BD/data_resized/01GraspableGraspingRectangles_RGB/DS.tfrecord'
writer = tf.python_io.TFRecordWriter(records_filename)
original_images = []
for img_path in image_filename:
image = np.array(Image.open(img_path))
#img_label = 'GP'
img_label = b'\x01'
img_raw = image.tostring()
example = tf.train.Example(features=tf.train.Features(feature={
'image_raw': tf.train.Feature(bytes_list = tf.train.BytesList(value = [img_raw])),
'label': tf.train.Feature(bytes_list = tf.train.BytesList(value = [img_label])),
}))
writer.write(example.SerializeToString())
writer.close()
用於讀取tfrecord文件的代碼是:
#READ TFRECORD FILE
import tensorflow as tf
import skimage.io as io
IMAGE_HEIGHT = 24
IMAGE_WIDTH = 24
IMAGE_CHANNELS = 3
BATCH_SIZE = 2
tfrecords_filename = '/home/ag/Dropbox/DL/6_CNN_BD/data_resized/01GraspableGraspingRectangles_RGB/DS.tfrecord'
def read_and_decode(filename_queue):
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(
serialized_example, features={
'image_raw': tf.FixedLenFeature([], tf.string),
'label': tf.FixedLenFeature([], tf.string),
})
image = tf.decode_raw(features['image_raw'], tf.uint8)
image_reshape = tf.reshape(image, [IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_CHANNELS])
label = tf.cast(features['label'], tf.string)
label_reshape = label
images, label = tf.train.shuffle_batch([image_reshape, label_reshape],
batch_size = 2,
capacity = 30,
num_threads = 2,
min_after_dequeue = 10)
return images, label
filename_queue = tf.train.string_input_producer([tfrecords_filename], num_epochs=10)
image, label = read_and_decode(filename_queue)
init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
with tf.Session() as sess:
sess.run(init_op)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord = coord)
for i in range(5):
img, label = sess.run([image, label])
print(img.shape)
print(label)
print('current batch')
io.imshow(img[0, :, :, :])
io.show()
io.imshow(img[1, :, :, :])
io.show()
coord.request_stop()
coord.join(threads)
重要的是要提及如果我更改img, label = sess.run([image, label])
爲img = sess.run(image)
我沒有錯誤。這讓我覺得這個問題與標籤功能的格式有關。
>>>
(2, 24, 24, 3)
['\x01' '\x01']
current batch
Traceback (most recent call last):
File "/home/ag/Dropbox/DL/6_CNN_BD/DS2.py", line 52, in <module>
img, label = sess.run([image, label])
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 767, in run
run_metadata_ptr)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 952, in _run
fetch_handler = _FetchHandler(self._graph, fetches, feed_dict_string)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 408, in __init__
self._fetch_mapper = _FetchMapper.for_fetch(fetches)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 230, in for_fetch
return _ListFetchMapper(fetch)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 337, in __init__
self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches]
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 238, in for_fetch
return _ElementFetchMapper(fetches, contraction_fn)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 271, in __init__
% (fetch, type(fetch), str(e)))
TypeError: Fetch argument array(['\x01', '\x01'], dtype=object) has invalid type <type 'numpy.ndarray'>, must be a string or Tensor. (Can not convert a ndarray into a Tensor or Operation.)
我以不同的方式,但沒有成功嘗試:以
錯誤畫面是相似的。對這個問題的任何建議?