2017-04-02 10 views
0

私は自分のデータベースを作成してCNNを訓練しています。今、私はtfrecordファイルからデータを読み込む際に問題があります。私は正常に2つの機能:画像とラベルが含まれている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])を変更した場合、私は誤りがないことに言及します。これは、問題がラベル機能のフォーマットに関連していると思うようになります。私はさまざまな方法が、ありません成功して試してみました

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

エラー画面は似ています。その問題の提案はありますか?

答えて

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) 
関連する問題