繁体   English   中英

对图像使用TFRecords

[英]Using TFRecords for images

我想使用TFRecords序列化一堆PNG文件。 因为我研究了初始存储库 给出的示例适用于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时,这很有意义。 但是我只有1个通道,所以文件已经是灰度的。

您使用tf.reshape([None, img_height*img_width]) 首先,没有第一个参数(您要重塑的确切形状)。 它应该是你的image 同样最有可能您希望以这种方式重塑tf.reshape(image, [img_height, img_width])tf.reshape(image, [img_height, img_width])

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM