首页 > 解决方案 > 将 TF 数据集从分类数据更改为数值/回归数据

问题描述

这是我第一次尝试从现成的数据集和模型扩展到我自己拼凑的东西。使用 tensorflow,我正在尝试加载图像数据集,其中为每个图像分配一个标准化的数值,以便我可以尝试在其上构建回归 CNN。对我来说不幸的是,tf.keras.preprocessing.image_dataset_from_directory期望数据集被离散分类。有没有一种直接的方法可以BatchDataset 对象转换为数字标签?

为了进一步澄清,如果我要执行dir(my_dataset)or my_dataset.__dict__,我想知道标签在哪里。

标签: pythontensorflowconv-neural-networktensorflow2.0

解决方案


我可以在BatchDataSettf.keras.preprocessing.image_dataset_from_directory

因为这些对象是非常专业的迭代器,所以访问单个“行”并不简单。以下是我如何从 BatchDataSet 直接访问此信息所在的位置。

training_ds = tf.keras.preprocessing.image_dataset_from_directory(
    "assets/",
    validation_split=0.2,
    subset="training",
    seed=123,
    color_mode="rgb",
    image_size=(IMG_SIZE, IMG_SIZE),
    batch_size=32)
# pull one batch from the BatchDataSet
one_batch = training_ds.take(1)
# transform the TakeDataset into a python iterator and then pull one batch using next()
training_data, labels = next(iter(batch))
# labels is a numpy array of int32 where each integer is the index of the class inside training_ds.class_names 

推荐阅读