2017-04-02 1 views
0

Ich habe lange an der Erstellung meiner eigenen Datenbank gearbeitet, um ein CNN zu trainieren. Jetzt habe ich ein Problem mit dem Lesen von Daten aus der Tfrecord-Datei. Ich habe erfolgreich eine TFrecord-Datei gespeichert, die zwei Funktionen enthält: Bild und Label. Wenn ich versuche, es zu lesen, liest es nur den ersten Stapel und dann erhalte ich eine Fehlermeldung.Feature-Format-Fehler beim Lesen von tfrecord (Kann ein ndarray nicht in einen Tensor oder eine Operation konvertieren)

Der Code der tfrecord Datei zum Speichern (I wegen Zeit nur 5 Dateien angenommen):

#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() 

Der Code, der tfrecord Datei zum Lesen ist:

#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) 

Es ist wichtig, Erwähnen Sie, dass, wenn ich img, label = sess.run([image, label]) für img = sess.run(image) ändern, habe ich keinen Fehler. Das lässt mich glauben, dass das Problem mit dem Format der Label-Funktion zusammenhängt.

Der Fehlerbildschirm ähnelt:

>>> 
(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.) 

ich auf unterschiedliche Weise, aber ohne Erfolg versucht haben. Irgendwelche Vorschläge für dieses Problem?

Antwort

0

Hier ist der Code, mit einigen kleinen Änderungen, die geholfen haben, das Problem zu beheben.

#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) 
Verwandte Themen