有没有办法从HDF5数据集中删除行?

时间:2020-04-28 18:46:46

标签: python dataset hdf5 h5py

我创建了一个H5PY数据集,其中包含约210万个实例。问题是我已经填满了除最后一行以外的所有行。我想删除最后一行,但不确定是否可行或安全。

这是如何创建数据集的摘要:

shape = (dataset_length, args.batch_size, 2048, 1, 1)

with h5py.File(path, mode='a') as hdf5_file:
       array_40 = hdf5_file.create_dataset(
                  f'{phase}_40x_arrays',  shape, maxshape=(None, args.batch_size, 2048, 1, 1)


# either new or checkpointed file exists
# load file and create references to exisitng h5 datasets
with h5py.File(path, mode='r+') as hdf5_file:
      array_40 = hdf5_file[f'{phase}_40x_arrays']

     for i, (inputs40x, labels) in enumerate(dataloaders_dict):

          inputs40x = inputs40x.to(device)
          x40 = resnet(inputs40x)
          array_40[batch_idx, ...] = x40.cpu()

          hdf5_file.flush()

我不确定是否需要将所有实例复制到新数据集中。我尝试调整大小,但这没用...

干杯

1 个答案:

答案 0 :(得分:0)

这是一个非常简单的示例,用于演示一个数据集的dataset.resize()

import numpy as np
import h5py

arr = np.random.rand(100).reshape(20,5)

with h5py.File('SO_61487687.h5', mode='a') as h5f:
     h5f.create_dataset('array1',  data=arr, maxshape=(None, 5) )

with h5py.File('SO_61487687.h5', mode='r+') as h5f:
     print ('Before:', h5f['array1'].shape)
     h5f['array1'].resize(10,axis=0)
     print ('After:', h5f['array1'].shape)
     h5f.flush()
相关问题