我试图使用h5py存储数据,作为(图像、角度)的元组列表。图像是来自uint8类型的OpenCV大小的numpy数组(240,320,3),而角度只是一些类型的float16。
使用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]))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.发布于 2016-12-15 03:12:44
@Eric创建的@Eric应该同时与numpy和h5py一起工作。但我想知道你是否真的想要或者需要这个。另一种方法是在numpy、images和angles中有两个数组,一个是4d uint8,另一个是浮点。在h5py中,您可以创建一个group,并将这2个数组存储为datasets。
您可以选择ith'映像的值
images[i,...], angles[i] # or
data[i]['image'], data[i]['angle']例如:
import h5py
dt = np.dtype([('angle', np.float16), ('image', np.uint8, (40,20,3))])
data = np.ones((3,), dt)
f = h5py.File('test.h5','w')
g = f.create_group('data')具有复合dtype的数据集:
g.create_dataset('data', (3,), dtype=dt)
g['data'][:] = data或两个数组的数据集。
g.create_dataset('image', (3,40,20,3), dtype=np.uint8)
g.create_dataset('angle', (3,), dtype=np.float16)
g['image'][:] = data['image']
g['angle'][:] = data['angle']从任一数据集中获取角数组:
g['data']['angle'][:]
g['angle'][:]发布于 2016-12-15 02:48:20
在numpy中,可以使用结构化数组存储数据:
dtype = np.dtype([('angle', np.float16), ('image', np.uint8, (240,320,3))])
data = np empty(10, dtype=dtype)
data[0]['angle'] = ... # etchttps://stackoverflow.com/questions/41155504
复制相似问题