python - 将图像文件夹转换为 .h5 文件?

标签 python numpy h5py

目前我正在 coursera 上 Andrew NG 的深度学习类(class)。他们在那里打开来自 .h5 的图像数据集。文件。我想对我自己的数据集尝试相同的方法:我想将图像文件夹转换为 .h5文件。

我尝试了以下操作,但是当我打开 train_x.h5它显示所有条目为零。

h5_train = h5py.File("train_x.h5", 'w')
h5_train.create_dataset("data_train", data=np.array(train_x))
print(h5_train)
h5_train.close()

在这里train_xshape(270000,500) 的数组, 其中第一个条目是 pixels(300*300*3) 的数量第二个条目是文件夹中的图像数量。

最佳答案

我认为要么:

  • train_x 不包含您期望的内容。
  • 您查看文件的方式不正确。

您的语法似乎完全正确。运行此示例时,一切都按预期工作:

import numpy as np
import h5py

array = np.arange(25).reshape(5,5)

archive = h5py.File('test.h5', 'w')

archive.create_dataset('/array', data=array)

archive.close()

我们可以验证它是正确的

import h5py

archive = h5py.File('test.h5','r')

for key in archive:
  print(key)
  print(archive[key][...])

这给出了我们放入的内容:

array
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]
 [20 21 22 23 24]]

注意这里也可以使用图形 HDFview或命令行工具 h5dump检查您的文件。

最后,最好注意第二种写作语法:

import numpy as np
import h5py

array = np.arange(25).reshape(5,5)

archive = h5py.File('test.h5', 'w')

archive['array'] = array

archive.close()

关于python - 将图像文件夹转换为 .h5 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49314261/

相关文章:

python - 在 Python 上解决端 View 谜题

模式= 1的数组中的Python PIL位图/png

python - 2d 数组的 numpy 掩码,其中所有值都在 1d 数组中

python - OSError 无法创建文件 - 参数无效

python - 两个图在 python 中是否同构,没有导入

python - 如何在 python 中左对齐 UTF-8 编码的字符串?

python - 查找行中第一个 NaN 值的索引

python - h5py 接受什么 numpy dtypes?

python - 插入许多 HDF5 数据集非常慢

python - 在 Python 中过滤 Pandas 数据框的值