首页 > 解决方案 > Pytables 值错误(附加对象的等级和“...”EArray 不同)

问题描述

我正在尝试使用 pytables 来存储我的图像数据集。我正在使用 Earray 在读取每个图像时附加它。我的 Earray 和图像的尺寸是相同的(除了第一个,附加完成)。我正在使用以下代码:

atom = Atom.from_dtype(np.dtype(np.uint32,(278,278,1)))
i=0
for <read each image from folder using os into img>:
    im = cv2.imread(img.path,0)
    im = np.expand_dims(im,2) #this is because keras needs 3d images and grayscale images are 2d
    if not i:
        X = data.create_earray(dataGroup,"X",atom,(0,)+im.shape,chunkshape=(20,20,20,1))
   X.append(np.expand_dims(im,0)) #as appending require same dim.
   i=1

但是当我运行代码时,它仍然给我的 ValueError 说我的对象等级是 1,X 等级是 4。当我使用 im 分配 X 大小时,这怎么可能?我什至尝试打印 im 的形状,它给出了 (278,278,1)。那么,问题是什么?我是第一次使用 Pytables,所以不深入了解它们。

标签: arrayspython-3.xnumpypytables

解决方案


首先,请注意您不必在加载第一个图像数据集之前创建 EArray。Pytables 足够聪明,可以从第一个对象中确定原子和形状定义。
如果没有完整的示例和您的数据,我很难练习您的代码。因此,我创建了一个非常简单的示例,用于np.arange()创建几个(278,278)图像数组,然后在 2 和 0 方向上扩展它们。希望这能模仿您尝试加载到 EArray 的数据。2 个 Pytables 函数 (file.create_earrayearray.append) 创建 2 行数据,每个“图像”1 行。运行后,image_data1.h5用 HDFView 打开并检查数据。
也许这将帮助您了解如何将图像加载到 HDF5 Earrays:

import tables as tb, numpy as np
data = tb.open_file("image_data1.h5", mode='w')
dataGroup = data.create_group(data.root, 'MyData')

im = np.arange(278*278).reshape((278,278))
im = np.expand_dims(im,2)
im = np.expand_dims(im,0)

X = data.create_earray( dataGroup,"X",obj=im )

print ('dim=', X.ndim, ', rows = ', X.nrows)

im = np.arange(278*278, 278*278+278*278).reshape((278,278))
im = np.expand_dims(im,2)
im = np.expand_dims(im,0)

X.append( im )

print ('dim=', X.ndim, ', rows = ', X.nrows)

data.close()

推荐阅读