我试图用h5py将数据存储为(图像,角度)的元组列表。图像是来自OpenCV的uint8类型的大小为(240,320,3)的numpy数组,而角度只是float16类型的数字。H5PY/Numpy - 设置numpy数组的内部形状(对于h5py)
使用h5py时,您需要有一个预定义的形状以保持可用的读/写速度。 H5py会用任意值预装整个数据集,您可以在其中稍后索引并将这些值设置为任何您想要的值。
我想知道如何在初始化h5py数据集的形状时设置内部numpy数组的形状。我相信同样的解决方案也适用于numpy。
import h5py
import numpy as np
dset_length = 100
# fake data of same shape
images = np.ones((dset_length,240,320,3), dtype='uint8') * 255
# fake data of same shape
angles = np.ones(dset_length, dtype='float16') * 90
f = h5py.File('dataset.h5', 'a')
dset = f.create_dataset('dset1', shape=(dset_length,2))
for i in range(dset_length):
# does not work since the shape of dset[0][0] is a number,
# and can't store an array datatype
dset[i] = np.array((images[i],angles[i]))
Recreateing在numpy的问题是这样的:
import numpy as np
a = np.array([
[np.array([0,0]), 0],
[np.array([0,0]), 0],
[np.array([0,0]), 0]
])
a.shape # (3, 2)
b = np.empty((3,2))
b.shape # (3, 2)
a[0][0] = np.array([1,1])
b[0][0] = np.array([1,1]) # ValueError: setting an array element with a sequence.
'B = np.empty((3,2),D型细胞=对象)'将使其行为类似于'A'。但是,这不是你想要的行为。 – Eric