python - 无论如何使用TensorFlow分别加载数据和标签
问题描述
我正在研究一个分类问题,我有一堆图像存储在两个目录中:
my_data/Total/M0
my_data/Total/B0
其中 M0 是一个类的子目录,B0 是另一个类的子目录。
当我使用:
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
train_dir,
validation_split=0.1,
subset="training",
seed=123,
image_size=(img_height, img_width),
batch_size=30336)
它返回一批元组,第一个元素是数据,第二个元素是标签我想知道是否有任何方式加载这些数据,以便数据和标签分开,如:
x_train , y_train = ...
解决方案
如果我理解正确,train_ds
应该会产生所需的输出。
看看这段代码:
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
train_dir,
validation_split=0.1,
subset="training",
seed=123,
image_size=(300, 300), # Note that I changed the image size to (300, 300)
batch_size=30 # Note that I changed the batch size to 30
)
for x, y in train_ds:
print(x.shape)
break
这将输出(30, 300, 300, 3)
应该与您想要的(batch, width, height, dim)
.
推荐阅读
- java - 如何获取字符串数组并将它们进一步分成对象?
- javascript - 检测包含 http 请求的 forEach 循环的结尾?
- amazon-web-services - 为什么使用 SNS 触发 lambda 函数,而不是 API 网关?
- artificial-intelligence - 迭代依赖搜索和最佳优先搜索是否具有相同的含义?
- excel - GET请求中的VBA XML HTTP setrequest标头基于先前的getresponseheader(令牌)值
- javascript - 如何解决此错误“以下任务未完成:默认,您是否忘记发出异步完成信号?'
- azure - 使用变量时,通过 Azure CLI 将多个标签分配给资源会产生一个大标签
- javascript - Javascript 通知 API - Chrome 不在顶部显示最新的
- angular-material - 行跨度的灵活高度
- git - 从某些 git 提交中删除文件,同时保持本地版本不变