python - 从tensorflow记录数据集中批量提取图像数据集
问题描述
我最近开始使用 tensorflow 研究 CNN,发现 tfrecords 对加快训练非常有帮助,但是我在数据 API 方面遇到了困难。
解析后,我的数据集由(图像,标签)元组组成,这对于训练来说很好,但是我试图在另一个数据集中提取图像以调用 keras.predict() 。
我试过这个解决方案:
test_set = get_set_tfrecord(test_path, _parse_function, num_parallel_calls = 4)
lab = []
f = True
for image, label in test_set.take(600):
if f:
img = tf.data.Dataset.from_tensors(image)
f = False
else:
img = img.concatenate(tf.data.Dataset.from_tensors(image))
lab.append(label.numpy())
天真,不是很好的代码,但它的工作原理是为了执行连接(即堆叠),它将每个图像加载到 RAM 中。
这样做的正确方法是什么?
解决方案
您可以使用map
来自. tf.data.Dataset
您可以编写以下代码。
result = test_set.map(lambda image, label: image)
# You can iterate and check what you have received at the end.
# I expect only the images.
for image in result.take(1):
print(image)
我希望使用上面的代码可以解决您的问题,并且这个答案可以很好地为您服务。
推荐阅读
- php - 注册表单中PHP中的图像上传验证
- python - 如何合并熊猫df列中的特定值
- jenkins - 如何在 jenkins ssh 代理中接受 ssh 主机验证
- xml - 使用交互式代理从 python 3 中的特定标签中提取基本数据
- javascript - 进度百分比可以在控制台中看到,但不能在 html 中看到
- java - 使用 java.time 确定一天中的小时数
- openstack - 无法使用打包器制作 openstack 映像
- python - Python 二等分搜索未运行且没有错误消息。麻省理工学院
- button - 居中按钮和响应式设计
- git - BitBucket 创建本地仓库并通过 gitCMD 将其克隆到 BitBucket