arrays - 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,所以不深入了解它们。
解决方案
首先,请注意您不必在加载第一个图像数据集之前创建 EArray。Pytables 足够聪明,可以从第一个对象中确定原子和形状定义。
如果没有完整的示例和您的数据,我很难练习您的代码。因此,我创建了一个非常简单的示例,用于np.arange()
创建几个(278,278)
图像数组,然后在 2 和 0 方向上扩展它们。希望这能模仿您尝试加载到 EArray 的数据。2 个 Pytables 函数 (file.create_earray
和earray.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()
推荐阅读
- python - 如何在不删除以前数据的情况下写入现有的 excel 10000 次文件?
- webcam - Webcam.js 后置摄像头未正确旋转
- gitlab - kubectl 通过 gitlab 抛出错误创建秘密
- php - MySQL Query 通过 PHP PDO 失败,通过 phpMyAdmin 成功
- javascript - 我可以提供存储在共享路径(UNC 路径)中的文件的链接吗
- java - mvn install or package 失败,因为无法从工作区中的另一个包中找到符号
- javascript - 使用 Object.fromEntries() 深度克隆对象
- iis-8 - 如何修复 500.19 - 内部服务器错误,错误代码为 0x8007000d
- javascript - 如何将数据从 NodeJS 发送到 React?
- javascript - Async/Await 与 Vuex 调度