2017-01-31 6 views
0

Ich habe das Skript ähnlich dem here verwendet, um mein Dataset in sharded tfrecords zu konvertieren. Aber wenn ich versuche, es mit dem Skript unter Tensorflow friert zu lesen, muss ich den Prozess mit kill beenden. (Hinweis: Im Moment arbeite ich im CPU-Modus)Tensorflow: JPEG-Bilder können nicht visualisiert werden

def parse_example_proto(example_serialized): 
    feature_map = { 
     'image/encoded': tf.FixedLenFeature([], dtype=tf.string, 
              default_value=''), 
     'image/class/label': tf.FixedLenFeature([1], dtype=tf.int64, 
               default_value=-1), 
     'image/class/text': tf.FixedLenFeature([], dtype=tf.string, 
              default_value=''), 
    } 

    features = tf.parse_single_example(example_serialized, feature_map) 

    init_image = tf.image.decode_jpeg(features['image/encoded'], channels = 3) 
    init_image.set_shape([800,480,3]) 
    image = tf.reshape(init_image,tf.pack([800, 480, 3])) 
    float_image = tf.image.convert_image_dtype(image, dtype=tf.float32) 
    label = tf.cast(features['image/class/label'], dtype=tf.int32) 

    return float_image , label, features['image/class/text'] 


def batch_inputs(batch_size, train,sess, num_preprocess_threads=4, 
       num_readers=1): 

    with tf.name_scope('batch_processing'): 
     tf_record_pattern = os.path.join('/home/raarora/', '%s-*' % 'train') 
     data_files = tf.gfile.Glob(tf_record_pattern) 
     if data_files is None: 
      raise ValueError('No data files found for this dataset') 
#  print data_files 
     # Create filename_queue 
     if train: 
      filename_queue = tf.train.string_input_producer(data_files, 
                  shuffle=True, 
                  capacity=8) 
     else: 
      filename_queue = tf.train.string_input_producer(data_files, 
                  shuffle=False, 
                  capacity=1) 

     reader =tf.TFRecordReader() 
     _, example_serialized = reader.read(filename_queue) 

     image, label, _ = parse_example_proto(example_serialized) 

     examples_per_shard = 201 
     min_queue_examples = examples_per_shard * 2 

     images, labels = tf.train.shuffle_batch(
      [image, label], batch_size=batch_size, num_threads=4, 
      capacity=min_queue_examples + 3 * batch_size, 
      min_after_dequeue=min_queue_examples) 
     print images.eval(session=sess) 
     return s,images,labels 


if __name__ == '__main__': 

    sess = tf.Session() 
    coord = tf.train.Coordinator() 
    threads = tf.train.start_queue_runners(sess=sess, coord=coord) 
    s,_,_ = batch_inputs(2,1,sess) 
+0

Hier ist ein Fehler abgelegt, die https://github.com/tensorflow/tensorflow/issues/6702 auf gleiches Problem beziehen, jemand irgendwelche Erkenntnisse hat? – rajat

Antwort

0

Konnte dies beheben. Ich dachte TFRecord ist eine Art Wörterbuch und Sie müssen nur die benötigten Schlüssel angeben, aber nachdem Sie die gesamte Feature-Map mit kleinen Änderungen an der späteren Bildverarbeitung versehen haben, funktionierte es.

Ein weiterer Fehler, den ich machte, war, dass queue_runner nach dem Aufruf von tf.train.shuffle_batch() gestartet werden sollte. Ich weiß nicht, ob es ein Fehler oder eine Lücke in meinem Verständnis ist

Hier ist der Arbeits Code zum Lesen der Daten

def getImage(filename): 
    # convert filenames to a queue for an input pipeline. 
    filenameQ = tf.train.string_input_producer([filename],num_epochs=None) 

    # object to read records 
    recordReader = tf.TFRecordReader() 

    # read the full set of features for a single example 
    key, fullExample = recordReader.read(filenameQ) 

    # parse the full example into its' component features. 
    features = tf.parse_single_example(
     fullExample, 
     features={ 
      'image/height': tf.FixedLenFeature([], tf.int64), 
      'image/width': tf.FixedLenFeature([], tf.int64), 
      'image/colorspace': tf.FixedLenFeature([], dtype=tf.string,default_value=''), 
      'image/channels': tf.FixedLenFeature([], tf.int64),    
      'image/class/label': tf.FixedLenFeature([],tf.int64), 
      'image/class/text': tf.FixedLenFeature([], dtype=tf.string,default_value=''), 
      'image/format': tf.FixedLenFeature([], dtype=tf.string,default_value=''), 
      'image/filename': tf.FixedLenFeature([], dtype=tf.string,default_value=''), 
      'image/encoded': tf.FixedLenFeature([], dtype=tf.string, default_value='') 
     }) 


    # now we are going to manipulate the label and image features 

    label = features['image/class/label'] 
    image_buffer = features['image/encoded'] 

    # Decode the jpeg 
    with tf.name_scope('decode_jpeg',[image_buffer], None): 
     # decode 
     image = tf.image.decode_jpeg(image_buffer, channels=3) 

     # and convert to single precision data type 
     image = tf.image.convert_image_dtype(image, dtype=tf.float32) 


    # cast image into a single array, where each element corresponds to the greyscale 
    # value of a single pixel. 
    # the "1-.." part inverts the image, so that the background is black. 
    # re-define label as a "one-hot" vector 
    # it will be [0,1] or [1,0] here. 
    # This approach can easily be extended to more classes. 
    image=tf.reshape(image,[height,width,3]) 
    label=tf.pack(tf.one_hot(label-1, nClass)) 

    return label, image 


label, image = getImage("train-00000-of-00001") 
imageBatch, labelBatch = tf.train.shuffle_batch(
    [image, label], batch_size=2, 
    capacity=20, 
    min_after_dequeue=10) 

sess = tf.InteractiveSession() 
sess.run(tf.initialize_all_variables()) 
# start the threads used for reading files 
coord = tf.train.Coordinator() 
threads = tf.train.start_queue_runners(sess=sess,coord=coord) 
batch_xs, batch_ys = sess.run([imageBatch, labelBatch]) 
print batch_xs 

coord.request_stop() 
coord.join(threads) 

Hinweis: Ich bin nicht klar sharded Aufzeichnungen so dass ich nur eine Scherbe verwendet.

Credits https://agray3.github.io/2016/11/29/Demystifying-Data-Input-to-TensorFlow-for-Deep-Learning.html

Verwandte Themen