python - Assign class label based on parent folder name when using Tf.data
问题描述
I have prepared a dataset the way I used to when using Datagenerator.flow_from_directory
from Keras. So basically, I have three folders "Train" "Valid" and "Test" and inside each one, I have folders named after the class they represent. However, instead of images, I have saved my data in those subfolders as compressed numpy files .npz.
I found that it is possible to create an input data pipeline to read .npz files using Tf.data
, however, the example in the documentation only shows how load a dataset that has labels saved in the .npz files as follows:
with np.load(path) as data:
train_examples = data['x_train']
train_labels = data['y_train']
test_examples = data['x_test']
test_labels = data['y_test']
train_dataset = tf.data.Dataset.from_tensor_slices((train_examples, train_labels))
And there is no explanation of how to generate a dataset and automatically assign labels to the data based on its parent folder (the way it is done in flow_from_directory).
Is there a way to achieve that or should I manually import data from each folder and assign a hot-encoded label to each subset? Thank youu !!
解决方案
推荐阅读
- laravel - Laravel 护照 500 内部服务器错误
- spring-boot - 如何向 Camunda Spring Boot 应用程序添加测试覆盖率?
- python - 在 Python 3.6 中解码 %xx
- javascript - Javascript 碰撞(非碰撞检测)
- python - 如何使python脚本在特定时间运行?
- rxjs - 当另一个流发出时如何动态更改流间隔
- android - RPi 3 Android Things UART双向通信使应用程序崩溃
- python-3.x - Dump series back into InfluxDB after querying with replaced field value
- android - 当应用程序在后台运行时将事件存储到数据库
- c - 将可为空的项目写入 Avro C 中的 avro 记录