Tensorflow从csv和映射创建数据集

时间:2018-04-20 12:31:14

标签: python dictionary tensorflow dataset

我尝试使用pandas创建的CSV文件为Tensorflow创建数据集。

csv文件如下所示:

feature1     feature2     filepath     label
    0.25         0.35    test1.jpg         A
    0.33         0.15    test2.jpg         B

我像这样读了数据框

mydf = pd.read_csv("TraingDatafinal.csv",header=0)

现在我已经定义了一个应该返回数据帧的函数。这完全符合the quickstart guide

def train_input_fn(features, labels, batch_size):
    """An input function for training"""
    # Convert the inputs to a Dataset.
    dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels))

    # Shuffle, repeat, and batch the examples.
    dataset = dataset.shuffle(1000).repeat().batch(batch_size)
    dataset = dataset.map(mappingfunction)
    # Return the dataset
    return dataset

我这样称呼这个函数;

mydataset = train_input_fn(mydf.drop(["label"],axis=1),mydf["label"],200)

如果我删除了映射,但是在打印形状时出现问号,这是有效的。为什么?维度似乎已明确界定。

这是真正的斗争开始的地方。我想创建一个映射函数,用一个图像数组替换文件路径。

我试图通过编写这个映射函数来实现它

def mappingfunction(feature,label):
    print(feature['Filename'])
    image = tf.read_file(feature['Filename'])
    image = tf.image.decode_image(image)
    return image,label

这只会返回图像和标签。我不知道如何实现它返回除文件路径之外的所有功能。

但即便是这种简化的版本也无法发挥作用。我得到一个"预期的二进制或unicode字符串"错误。你能救我吗?

1 个答案:

答案 0 :(得分:0)

映射函数应返回所有要素和标签。例如:

def mappingfunction(feature,label):
    print(feature['Filename'])
    image = tf.read_file(feature['Filename'])
    image = tf.image.decode_image(image)
    features['image'] = image
    return features, label

这会在功能字典中添加image键。

相关问题