Tensorflow TFRecordDataset.map 错误

我在为一个任务创建一个TensorFlow的输入管道。我已经设置了一个TFRecord数据集,并将其保存到磁盘上的文件中。

我尝试使用以下代码加载数据集(以便进行批处理并发送到实际的机器学习算法):

dataset = tf.data.TFRecordDataset(filename)
print("开始映射...")
dataset = dataset.map(map_func = read_single_record)
print("映射完成")
buffer = 500 # 我们将从多大的缓冲区中进行抽样?
batch_size = 125
capacity = buffer + 2 * batch_size
print("正在打乱数据集...")
dataset = dataset.shuffle(buffer_size = buffer)
print("正在批处理数据集...")
dataset = dataset.batch(batch_size)
dataset = dataset.repeat()
print("创建迭代器...")
iterator = dataset.make_one_shot_iterator()
examples_batch, labels_batch = iterator.get_next()

然而,在dataset.map()这一行我遇到了错误。错误信息如下: TypeError: Expected int64, got <tensorflow.python.framework.sparse_tensor.SparseTensor object at 0x00000000085F74A8> of type 'SparseTensor' instead.

read_single_record()函数如下所示:

keys_to_features = {                "image/pixels": tf.FixedLenFeature([], tf.string, default_value = ""),                "image/label/class": tf.FixedLenFeature([], tf.int64, default_value = 0),                "image/label/numbb": tf.FixedLenFeature([], tf.int64, default_value = 0),                "image/label/by": tf.VarLenFeature(tf.float32),                "image/label/bx": tf.VarLenFeature(tf.float32),                "image/label/bh": tf.VarLenFeature(tf.float32),                "image/label/bw": tf.VarLenFeature(tf.float32)            }
features = tf.parse_single_example(record, keys_to_features)
image_pixels = tf.image.decode_image(features["image/pixels"])
print("特征: {0}".format(features))
example = image_pixels  # 可能在某些时候想对这个进行一些处理
label = [features["image/label/class"],        features["image/label/numbb"],        features["image/label/by"],        features["image/label/bx"],        features["image/label/bh"],        features["image/label/bw"]]
return example, label

我不确定问题出在哪里。我从TensorFlow API文档中获取了这个代码的灵感,并根据我的需要稍作修改。我真的不知道从哪里开始尝试修复这个问题。

为了参考,这是我用于生成TFRecord文件的代码:

def parse_annotations(in_file, img_filename, cell_width, cell_height):    """ 解析注释文件以获取单个图像的边界框    """
    y_mins = []
    x_mins = []
    heights = []
    widths = []
    grids_x = []
    grids_y = []
    classes = [0]
    num_faces = int(in_file.readline().rstrip())
    img_width, img_height = get_image_dims(img_filename)
    for i in range(num_faces):
        clss,  x, y, width, height = in_file.readline().rstrip().split(',')
        x = float(x)
        y = float(y)
        width = float(width)
        height = float(height)
        x = x - (width / 2.0)
        y = y - (height / 2.0)
        y_mins.append(y)
        x_mins.append(x)
        heights.append(height)
        widths.append(width)
        grid_x, grid_y = get_grid_loc(x, y, width, height, img_width, img_height, cell_width, cell_height)
    pixels = get_image_pixels(img_filename)
    example = tf.train.Example(features = tf.train.Features(feature = {
        "image/pixels": bytes_feature(pixels),
        "image/label/class": int_list_feature(classes),
        "image/label/numbb": int_list_feature([num_faces]),
        "image/label/by": float_list_feature(y_mins),
         "image/label/bx": float_list_feature(x_mins),
         "image/label/bh": float_list_feature(heights),
         "image/label/bw": float_list_feature(widths)
    }))
    return example, num_faces
if len(sys.argv) < 4:
    print("使用方法: python convert_to_tfrecord.py [处理后的注释文件路径] [训练输出文件路径] [验证输出文件路径] [训练比例]")
else:
    processed_fn = sys.argv[1]
    train_fn = sys.argv[2]
    valid_fn = sys.argv[3]
    train_frac = float(sys.argv[4])
    if(train_frac > 1.0 or train_frac < 0.0):
        print("训练比例(f)必须是 0 <= f <= 1")
    else:
        with tf.python_io.TFRecordWriter(train_fn) as writer:
            with tf.python_io.TFRecordWriter(valid_fn) as valid_writer:
                with open(processed_fn) as f:
                    for line in f:
                        ex, n_faces = parse_annotations(f, line.rstrip(), 30, 30)
                        randVal = rand.random()
                        if(randVal < train_frac):
                            writer.write(ex.SerializeToString())
                        else:
                            valid_writer.write(ex.SerializeToString())

请注意,我已经删除了一些与实际序列化/创建TFRecords文件无关的代码。


回答:

虽然没有测试,但似乎映射函数不能返回SparseTensorTensor的列表。

tf.VarLenFeature(tf.float32)返回一个SparseTensor,但tf.FixedLenFeature([], tf.int64)返回一个Tensor

为了使批处理能够正常工作,我建议你只使用Tensor

关于如何推导label的建议:

label = {
    "image/label/class" : features["image/label/class"],
    "image/label/numbb" : features["image/label/numbb"],
    "image/label/by" : tf.sparse_tensor_to_dense(features["image/label/by"], default_value=-1),
    "image/label/bx" : tf.sparse_tensor_to_dense(features["image/label/bx"], default_value=-1),
    "image/label/bh" : tf.sparse_tensor_to_dense(features["image/label/bh"], default_value=-1),
    "image/label/bw" : tf.sparse_tensor_to_dense(features["image/label/bw"], default_value=-1)
}

关于如何处理此映射输出的灵感,我建议你参考这个讨论

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注