读取tfrecord时的功能格式错误(无法将ndarray转换为Tensor或Operation)

时间:2017-04-02 13:28:24

标签: python tensorflow

我一直在创建自己的数据库来训练CNN。现在,我从tfrecord文件中读取数据时遇到问题。我已经成功保存了一个tfrecord文件,其中包含两个功能:图像和标签。当我尝试阅读它时,它只是读取第一批,然后我收到一条错误消息。

保存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.)

我尝试过不同的方法但没有成功。对这个问题的任何建议?

1 个答案:

答案 0 :(得分:0)

这是代码,有一些小改动有助于解决问题。

#READ TFRECORD FILE

import tensorflow as tf
import skimage.io as io
import Image

IMAGE_HEIGHT = 24
IMAGE_WIDTH = 24
IMAGE_CHANNELS = 3
BATCH_SIZE = 5
MIN_AFTER_DEQUEUE = 10000
CAPACITY = MIN_AFTER_DEQUEUE+3*BATCH_SIZE
NUM_THREADS = 2

tfrecords_filename = ['/home/ag/Dropbox/DL/6_CNN_BD/data_resized/TFrecords/DS01.tfrecord', '/home/ag/Dropbox/DL/6_CNN_BD/data_resized/TFrecords/DS02.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 = BATCH_SIZE,
                                           capacity = CAPACITY,
                                           num_threads = NUM_THREADS,
                                           min_after_dequeue = MIN_AFTER_DEQUEUE)
    #images, label = tf.train.batch([image_reshape, label_reshape], batch_size = 2, capacity = 30, num_threads = 2, min_after_dequeue = 10)

    return images, label
    #return image_reshape, label_reshape

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(1000):

        img, lbl = sess.run([image, label])
        print(i, img.shape, lbl)

        print('current batch')

        #img_save = Image.fromarray(img, 'RGB')    
        #img_save.save("/home/ag/Dropbox/DL/6_CNN_BD/data_resized/02GraspableGraspingRectangles_RGB/" + str(i) + "-train.png")

        #io.imshow(img[0, :, :, :])
        #io.show()

        #io.imshow(img[1, :, :, :])
        #io.show()

    coord.request_stop()
    coord.join(threads)