Engine
Engine

Reputation: 5420

Using TFRecords for images

I want to use TFRecords to serialized a bunch of PNG files. for I took a look into inception repository. the give example is for RGB JPEG files, since my files are in grayscale, I had to change the code. but I've managed to generate the records files. The problem is when I try to read them:

def getImage(filename):
with tf.device('/cpu:0'):
    # 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 PNG 
    with tf.name_scope('decode_img',[image_buffer], None):
        # decode
        image = tf.image.decode_png( image_buffer, channels=1)

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

    image =tf.reshape([None, img_height*img_width])# here is the problem 

    label=tf.stack(tf.one_hot(label-1, numberOFclasses))
    return label, image

the problem is the reshape line,the program crashes when I try it this . this how it's been used:

with tf.name_scope('decode_img',[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.
        image=tf.reshape(1-tf.image.rgb_to_grayscale(image),[img_height*img_width])

which make sens since, when the file are RGB. but I have only 1 channel so the file are already grayscale.

Upvotes: 0

Views: 700

Answers (1)

Salvador Dali
Salvador Dali

Reputation: 222461

You use tf.reshape([None, img_height*img_width]). First of all there is no first parameter (what exactly you are trying to reshape). It should be your image. Also most probably you want to reshape it in this way: tf.reshape(image, [img_height, img_width]).

Not related to your problem, but why do you need so many features in your TFRecord file?

Upvotes: 1

Related Questions