首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >合并大型h5数据集

合并大型h5数据集
EN

Stack Overflow用户
提问于 2021-04-08 17:37:31
回答 1查看 101关注 0票数 0

我有8个大型h5文件(每个文件~100 G),每个文件都有许多不同的数据集(例如“x”、“y”、“z”、“h”)。我想将所有8个'x‘和'y’数据集合并到一个test.h5和train.h5文件中。有什么快速的方法吗?总共有800080行,所以我首先创建了列车文件save_file = h5py.File(os.path.join(base_path,'data/train.h5'),'w',libver='latest'),在计算了随机分割后,我创建了数据集:

代码语言:javascript
复制
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)

然后,我尝试遍历我的8个文件中的每一个,并保存训练/测试。

代码语言:javascript
复制
    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()

但根据我的计算,这将需要12天的时间。有更快的方法吗?提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-08 20:17:49

如果我理解你的方法,它有800,080读/写操作。是大量的“写作”让你丧命。为了提高性能,您必须重新排序I/O操作,以便每次读取和写入大量数据。

通常,我会将整个数据集读入数组中,然后将其写入新文件。我通过您的代码阅读,并看到您使用train_indeces随机选择要写入train_filetest_file的数据行。这会使事情变得“有点复杂”。:-)

为了复制随机性,我使用np.where()查找训练和测试行。然后,我使用NumPy“想像力索引”将数据作为数组访问(在转换成列表之后)。然后,我将该数组写入适当数据集中的下一个打开的槽。(我重用了您的三个计数器:indeces_indexlast_train_indexlast_test_index来跟踪事物。)

我认为这能做你想做的事:

警告:我99%肯定这会起作用,但没有用真实的数据进行测试。

代码语言:javascript
复制
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()

您应该考虑使用Python的with/as:上下文管理器打开文件。用这个:

代码语言:javascript
复制
with h5py.File(f'{base_path}data/{e}', 'r', libver='latest') as rnd_file:

您不需要使用上下文管理器的rnd_file.close

而不是这样:

代码语言:javascript
复制
rnd_file = h5py.File(f'{base_path}data/{e}', 'r', libver='latest')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67009165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档