Tensorflow:从 TFRecords 文件中提取图像和标签 [英] Tensorflow: Extracting image and label from TFRecords file

查看:70
本文介绍了Tensorflow:从 TFRecords 文件中提取图像和标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TFRecords 文件,其中包含带有标签、名称、大小等的图像.我的目标是将标签和图像提取为一个 numpy 数组.

I have a TFRecords file which contains images with their labels, name, size, etc. My goal is to extract the label and the image as a numpy array.

我执行以下操作来加载文件:

I do the following to load the file:

def extract_fn(data_record):
    features = {
        # Extract features using the keys set during creation
        "image/class/label":    tf.FixedLenFeature([], tf.int64),
        "image/encoded":        tf.VarLenFeature(tf.string),
    }
    sample = tf.parse_single_example(data_record, features)
    #sample = tf.cast(sample["image/encoded"], tf.float32)
    return sample

filename = "path\train-00-of-10"
dataset = tf.data.TFRecordDataset(filename)
dataset = dataset.map(extract_fn)
iterator = dataset.make_one_shot_iterator()
next_element = iterator.get_next()

with tf.Session() as sess:
    while True:
        data_record = sess.run(next_element)
        print(data_record)

图像保存为字符串.如何将图像转换为 float32?我试过 sample = tf.cast(sample["image/encoded"], tf.float32) 这不起作用.我希望 data_record 是一个列表,其中包含作为 numpy-array 的图像和作为 np.int32 数字的标签.我该怎么做?

The image is saved as a string. How can I convert the image to float32? I tried sample = tf.cast(sample["image/encoded"], tf.float32) which does not work. I want data_record to be a list containing the image as a numpy-array and the label as a np.int32 number. How can I do that?

现在 data_record 看起来像这样:

Right now data_record looks like this:

{'image/encoded': SparseTensorValue(indices=array([[0]]), values=array([b'\xff\xd8\ ... 8G\xff\xd9'], dtype=object), dense_shape=array([1])), 'image/class/label': 394}

我不知道如何处理它.我将不胜感激

I have no idea how I can work with that. I would appreciate any help

编辑

如果我在 extract_fn() 中打印 samplesample['image/encoded'] 我得到以下内容:

If I print sample and sample['image/encoded'] in extract_fn() I get the following:

print(sample) ={'image/encoded': <tensorflow.python.framework.sparse_tensor.SparseTensor object at 0x7fe41ec15978>, 'image/class/label': <tf.Tensor 'ParseSingleExample/ParseSingleExample:3' shape=(6)>;}

print(sample['image/encoded'] =SparseTensor(indices=Tensor("ParseSingleExample/ParseSingleExample:0", shape=(?, 1), dtype=int64), values=Tensor("ParseSingleExample/ParseSingleExample:1", shape=(?,), dtype=string),dense_shape=Tensor("ParseSingleExample/ParseSingleExample:2", shape=(1,), dtype=int64))

图像似乎是一个稀疏张量,tf.image.decode_image 抛出错误.将图像提取为 tf.float32 张量的正确方法是什么?

It seems that the image is a sparse tensor and tf.image.decode_image throws an error. What is the right way to extract the image as an tf.float32 tensor?

推荐答案

我相信您将图像存储为 JPEG 或 PNG 或其他一些格式.所以,在阅读时,你必须解码它们:

I believe you store images encoded as JPEG or PNG or some other format. So, when reading, you have to decode them:

def extract_fn(data_record):
    features = {
        # Extract features using the keys set during creation
        "image/class/label":    tf.FixedLenFeature([], tf.int64),
        "image/encoded":        tf.VarLenFeature(tf.string),
    }
    sample = tf.parse_single_example(data_record, features)
    image = tf.image.decode_image(sample['image/encoded'], dtype=tf.float32) 
    label = sample['image/class/label']
    return image, label

...

with tf.Session() as sess:
    while True:
        image, label = sess.run(next_element)
        image = image.reshape(IMAGE_SHAPE)

更新:似乎您将数据作为稀疏张量中的单个单元格值.尝试将其转换回密集并在解码前后进行检查:

Update: It seems you got your data as a single cell value in a sparse Tensor. Try to convert it back to dense and inspect before and after decoding:

def extract_fn(data_record):
    features = {
        # Extract features using the keys set during creation
        "image/class/label":    tf.FixedLenFeature([], tf.int64),
        "image/encoded":        tf.VarLenFeature(tf.string),
    }
    sample = tf.parse_single_example(data_record, features)
    label = sample['image/class/label']
    dense = tf.sparse_tensor_to_dense(sample['image/encoded'])

    # Comment it if you got an error and inspect just dense:
    image = tf.image.decode_image(dense, dtype=tf.float32) 

    return dense, image, label

这篇关于Tensorflow:从 TFRecords 文件中提取图像和标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆