python-Tensorflow:从TFRecords文件中提取图像和标签

前端之家收集整理的这篇文章主要介绍了python-Tensorflow:从TFRecords文件中提取图像和标签 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

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数组的图像和作为np.int32数字的标签.我怎样才能做到这一点?

现在,data_record看起来像这样:

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

我不知道该如何处理.我将不胜感激任何帮助

编辑

如果我在extract_fn()中打印样本和sample [‘image / encoded’],则会得到以下结果:

打印(样本)=
{‘图像/编码’:< tensorflow.python.framework.sparse_tensor.SparseTensor对象位于0x7fe41ec15978&gt ;,'图像/类/标签':< tf.Tensor'ParseSingleExample / ParseSingleExample:3'shape =()dtype = int64&gt ;} print(sample [‘image / encoded’] =
SparseTensor(indices = Tensor(“ ParseSingleExample / ParseSingleExample:0”,shape =(?,1),dtype = int64),values = Tensor(“ ParseSingleExample / ParseSingleExample:1”,),dtype = string),density_shape = Tensor(“ ParseSingleExample / ParseSingleExample:2”,shape =(1,dtype = int64))

图像似乎是稀疏张量,并且tf.image.decode_image引发错误.什么是将图像提取为tf.float32张量的正确方法

最佳答案
我相信您存储的图像编码为JPEG或PNG或其他格式.因此,在阅读时,您必须对它们进行解码:

def extract_fn(data_record):
    features = {
        # Extract features using the keys set during creation
        "image/class/label":    tf.FixedLenFeature([],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)

更新:
看来您将数据作为稀疏Tensor中的单个单元格值获取了.尝试将其转换回密集状态,并在解码前后进行检查:

def extract_fn(data_record):
    features = {
        # Extract features using the keys set during creation
        "image/class/label":    tf.FixedLenFeature([],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
原文链接:https://www.f2er.com/python/533135.html

猜你在找的Python相关文章