使用TFRecords进行图像处理

时间:2017-07-03 14:29:12

标签: python tensorflow

我想使用TFRecords来序列化一堆PNG个文件。因为我开始研究repository。给出示例是RGB JPEG个文件,因为我的文件是灰度的,我不得不更改代码。 但我设法生成了记录文件。 问题是当我尝试阅读它们时:

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

问题是重塑线,当我尝试这个程序时程序崩溃了。 这是如何使用的:

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])
当文件是RGB时,

表示sens。但我只有1个频道,所以文件已经是灰度。

1 个答案:

答案 0 :(得分:1)

您使用tf.reshape([None, img_height*img_width])。首先,没有第一个参数(你究竟想要重塑什么)。它应该是你的image。也很可能你想以这种方式重塑它:tf.reshape(image, [img_height, img_width])

与您的问题无关,但为什么在TFRecord文件中需要这么多功能呢?