Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert TFRecords into numpy arrays?

Tags:

tensorflow

The main idea is to convert TFRecords into numpy arrays. Assume that the TFRecord stores images. Specifically:

  1. Read a TFRecord File and convert each image into a numpy array.
  2. Write the image into 1.jpg, 2.jpg, etc.
  3. At the same time, write the file name and label to the text file like this:
    1.jpg 2
    2.jpg 4
    3.jpg 5
    

I currently use the following code:

import tensorflow as tf
import os

def read_and_decode(filename_queue):
  reader = tf.TFRecordReader()
  _, serialized_example = reader.read(filename_queue)
  features = tf.parse_single_example(
      serialized_example,
      # Defaults are not specified since both keys are required.
      features={
          'image_raw': tf.FixedLenFeature([], tf.string),
          'label': tf.FixedLenFeature([], tf.int64),
          'height': tf.FixedLenFeature([], tf.int64),
          'width': tf.FixedLenFeature([], tf.int64),
          'depth': tf.FixedLenFeature([], tf.int64)
      })
  image = tf.decode_raw(features['image_raw'], tf.uint8)
  label = tf.cast(features['label'], tf.int32)
  height = tf.cast(features['height'], tf.int32)
  width = tf.cast(features['width'], tf.int32)
  depth = tf.cast(features['depth'], tf.int32)
  return image, label, height, width, depth

with tf.Session() as sess:
  filename_queue = tf.train.string_input_producer(["../data/svhn/svhn_train.tfrecords"])
  image, label, height, width, depth = read_and_decode(filename_queue)
  image = tf.reshape(image, tf.pack([height, width, 3]))
  image.set_shape([32,32,3])
  init_op = tf.initialize_all_variables()
  sess.run(init_op)
  print (image.eval())

I'm just reading trying to get at least one image for starters. The code just gets stuck when I run this.

like image 732
jkschin Avatar asked Mar 16 '16 04:03

jkschin


People also ask

How do I read a TFRecord file in Python?

TFRecordReader() file = tf. train. string_input_producer("record. tfrecord") _, serialized_record = reader.

What is a TFRecord file?

The TFRecord format is a simple format for storing a sequence of binary records. Protocol buffers are a cross-platform, cross-language library for efficient serialization of structured data. Protocol messages are defined by . proto files, these are often the easiest way to understand a message type.

How do I convert NumPy to tensor?

a NumPy array is created by using the np. array() method. The NumPy array is converted to tensor by using tf. convert_to_tensor() method.

How do you make TFRecord?

Creating TFRecord Files with Code Most often we have labeled data in PASCAL VOC XML or COCO JSON. Creating a TFRecord file from this data requires following a multistep process: (1) creating a TensorFlow Object Detection CSV (2) Using that TensorFlow Object Detection CSV to create TFRecord files.


1 Answers

Oops, it was a silly mistake on my part. I used a string_input_producer but forgot to run the queue_runners.

with tf.Session() as sess:
  filename_queue = tf.train.string_input_producer(["../data/svhn/svhn_train.tfrecords"])
  image, label, height, width, depth = read_and_decode(filename_queue)
  image = tf.reshape(image, tf.pack([height, width, 3]))
  image.set_shape([32,32,3])
  init_op = tf.initialize_all_variables()
  sess.run(init_op)
  coord = tf.train.Coordinator()
  threads = tf.train.start_queue_runners(coord=coord)
  for i in range(1000):
    example, l = sess.run([image, label])
    print (example,l)
  coord.request_stop()
  coord.join(threads)
like image 57
jkschin Avatar answered Oct 06 '22 10:10

jkschin