简体   繁体   English

合并大型 h5 数据集

[英]Merging large h5 datasets

I have 8 large h5 files (~ 100G each), each with many different datasets (say 'x','y','z','h').我有 8 个大的 h5 文件(每个约 100G),每个文件都有许多不同的数据集(比如'x'、'y'、'z'、'h')。 I'd like merge all 8 of the 'x' and 'y' datasets into a test.h5 and train.h5 file.我想将所有 8 个“x”和“y”数据集合并到一个 test.h5 和 train.h5 文件中。 Is there a fast way to do this?有没有快速的方法来做到这一点? In total I have 800080 rows so I create first my train file save_file = h5py.File(os.path.join(base_path,'data/train.h5'),'w',libver='latest') and after calculating a random split I create the datasets:我总共有 800080 行所以我首先创建我的火车文件save_file = h5py.File(os.path.join(base_path,'data/train.h5'),'w',libver='latest')并在计算后随机拆分我创建数据集:

train_file.create_dataset('x', (num_train, 256, 256, 1))
train_file.create_dataset('y',(num_train,1))

[similarly for test_file]

train_indeces = np.asarray([1]*num_train + [0]*num_test)
np.random.shuffle(train_indeces)

then I try iterating over each of my 8 files and saving train/test.然后我尝试遍历我的 8 个文件中的每一个并保存训练/测试。

    indeces_index = 0
    last_train_index = 0
    last_test_index = 0
    for e in files:
        print(f'FILE:  {e}')
        rnd_file = h5py.File(f'{base_path}data/{e}', 'r', libver='latest')

        for j in tqdm(range(rnd_file['x'].shape[0] )):
            if train_indeces[indeces_index]==1:
                train_file['x'][last_train_index] = rnd_file['x'][j]
                train_file['y'][last_train_index] = rnd_file['y'][j]
                last_train_index+=1
            else:
                test_file['x'][last_test_index] = rnd_file['x'][j]
                test_file['y'][last_test_index] = rnd_file['y'][j]
                last_test_index +=1

            indeces_index +=1
        rnd_file.close()

But by my calculations this would take ~12 days to run.但根据我的计算,这需要大约 12 天才能运行。 Is there a (much) faster way to do this?有没有(更快)的方法来做到这一点? Thanks in advance.提前致谢。

If I understand your method, it has 800,080 read/write operations.如果我理解你的方法,它有 800,080 次读/写操作。 It's the large # of "writes" that are killing you.大量的“写入”正在杀死你。 To improve performance, you have to reorder I/O operations to read and write large amounts of data each time.为了提高性能,您必须重新排序 I/O 操作以每次读取和写入大量数据。

Typically I would read an entire dataset into an array, then write it to the new file.通常我会将整个数据集读入一个数组,然后将其写入新文件。 I read thru your code and see you use train_indeces to randomly select rows of data to write to train_file or test_file .我通过您的代码阅读并看到您使用train_indeces随机 select 行数据写入train_filetest_file That complicates things "a little bit".这使事情“有点”复杂化。 :-) :-)

To replicate the randomness, I used np.where() to find the the training and testing rows.为了复制随机性,我使用np.where()来查找训练和测试行。 Then I used NumPy "fancy indexing" to access the data as an array (after converting to a list).然后我使用 NumPy “花式索引”将数据作为数组访问(转换为列表后)。 Then, I write that array to the next open slot in the appropriate dataset.然后,我将该数组写入相应数据集中的下一个开放槽。 (I reused your 3 counters: indeces_index , last_train_index , and last_test_index to keep track of things.) (我重用了你的 3 个计数器: indeces_indexlast_train_indexlast_test_index来跟踪事情。)

I with think this will do what you want:我认为这会做你想要的:
[Caveat: I'm 99% sure this will work, but it was not tested with real data.] [警告:我有 99% 的把握这会奏效,但并未使用真实数据进行测试。]

for e in files:
    print(f'FILE:  {e}')
    rnd_file = h5py.File(f'{base_path}data/{e}', 'r', libver='latest')
    
    rnd_size = rnd_file['x'].shape[0]
    # get an array with the next "rnd_size" indices
    ind_arr = train_indeces[indeces_index:indeces_index+rnd_size]

    # Get training data indices where index==1
    train_idx = np.where(ind_arr==1)[0]  # np.where() returns a tuple
    train_size = len(train_idx)
    
    x_train_arr = rnd_file['x'][train_idx.tolist()]
    train_file['x'][last_train_index:last_train_index+train_size] = x_train_arr
    
    y_train_arr = rnd_file['y'][train_idx.tolist()]
    train_file['y'][last_train_index:last_train_index+train_size] = y_train_arr
    
    # Get test data indices where index==0
    test_idx  = np.where(ind_arr==0)[0]   # np.where() returns a tuple
    test_size = len(test_idx)

    x_test_arr = rnd_file['x'][test_idx.tolist()]
    test_file['x'][last_test_index:last_test_index+test_size] = x_test_arr

    y_test_arr = rnd_file['y'][test_idx.tolist()]
    test_file['y'][last_test_index:last_test_index+test_size] = y_test_arr
    
    indeces_index   += rnd_size 
    last_train_index+= train_size
    last_test_index += test_size
  
    rnd_file.close()

You should consider opening the file with Python's with/as: context manager.您应该考虑使用 Python 的with/as:上下文管理器打开文件。 Use this:用这个:

with h5py.File(f'{base_path}data/{e}', 'r', libver='latest') as rnd_file:

You do not need rnd_file.close with the context manager.您不需要rnd_file.close与上下文管理器。

Instead of this:而不是这个:

rnd_file = h5py.File(f'{base_path}data/{e}', 'r', libver='latest')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM