python - 将 TF 数据集从分类数据更改为数值/回归数据
问题描述
这是我第一次尝试从现成的数据集和模型扩展到我自己拼凑的东西。使用 tensorflow,我正在尝试加载图像数据集,其中为每个图像分配一个标准化的数值,以便我可以尝试在其上构建回归 CNN。对我来说不幸的是,tf.keras.preprocessing.image_dataset_from_directory
期望数据集被离散分类。有没有一种直接的方法可以将BatchDataset 对象转换为数字标签?
为了进一步澄清,如果我要执行dir(my_dataset)
or my_dataset.__dict__
,我想知道标签在哪里。
解决方案
我可以在BatchDataSet
从tf.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
推荐阅读
- android - 如何获取房间数据库的版本号?
- kubernetes - stackdriver-metadata-agent-cluster-level 很多失败与 [2a00:1450:400c:c09::5f]:443: i/o timeout
- javascript - Bootstrap-4:将面包屑与标题对齐
- c++ - 为什么在 C++ 20 中从标准库容器中删除了比较运算符?
- python - pathlib 的 `glob` 方法在运行之间的顺序是否一致?
- python - 使用 Matplotlib,如何显示以 HH24:MI 格式排序的 Y 轴值
- python - 如何正确设置包?
- symfony - Symfony 5 没有教义
- javascript - 如何通过多次单击按钮来解决 React.js 状态?
- python - 如何在 pandas/matplotlib 中绘制索引列?