python - 保存到 hdf5 非常慢(Python 卡住)

标签 python numpy keras hdf5

我正在尝试将瓶颈值保存到新创建的 hdf5 文件中。 瓶颈值以形状 (120,10,10, 2048) 的形式出现。 保存一个单独的批处理占用超过 16 个演出,而 python 似乎在那批处理中卡住。根据最近的发现(见更新,hdf5占用大内存似乎还可以,但卡住部分似乎是一个故障。

我只是想保存前 2 批处理用于测试目的,并且只保存 训练数据集(再一次,这是一次测试运行),但我什至无法通过第一批。它只是停在第一批,不会循环到下一次迭代。如果我尝试检查 hdf5,资源管理器将变得缓慢,Python 将卡住。如果我尝试终止 Python(即使不检查 hdf5 文件),Python 也不会正确关闭并强制重启。

相关代码和数据如下:

总数据点约90,000 ish,以120个为一批发布。

Bottleneck shape is (120,10,10,2048)

所以我要保存的第一批是(120,10,10,2048)

以下是我尝试保存数据集的方式:

with h5py.File(hdf5_path, mode='w') as hdf5:
                hdf5.create_dataset("train_bottle", train_shape, np.float32)
                hdf5.create_dataset("train_labels", (len(train.filenames), params['bottle_labels']),np.uint8)
                hdf5.create_dataset("validation_bottle", validation_shape, np.float32)
                hdf5.create_dataset("validation_labels",
                                              (len(valid.filenames),params['bottle_labels']),np.uint8)



 #this first part above works fine

                current_iteration = 0
                print('created_datasets')
                for x, y in train:

                    number_of_examples = len(train.filenames) # number of images
                    prediction = model.predict(x)
                    labels = y
                    print(prediction.shape) # (120,10,10,2048)
                    print(y.shape) # (120, 12)
                    print('start',current_iteration*params['batch_size']) # 0
                    print('end',(current_iteration+1) * params['batch_size']) # 120

                    hdf5["train_bottle"][current_iteration*params['batch_size']: (current_iteration+1) * params['batch_size'],...] = prediction
                    hdf5["train_labels"][current_iteration*params['batch_size']: (current_iteration+1) * params['batch_size'],...] = labels
                    current_iteration += 1
                    print(current_iteration)
                    if current_iteration == 3:
                       break

这是打印语句的输出:

(90827, 10, 10, 2048) # print(train_shape)

(6831, 10, 10, 2048)  # print(validation_shape)
created_datasets
(120, 10, 10, 2048)  # print(prediction.shape)
(120, 12)           #label.shape
start 0             #start of batch
end 120             #end of batch

# Just stalls here instead of printing `print(current_iteration)`

它只是在这里停顿了一段时间(20 分钟以上),hdf5 文件的大小慢慢变大(现在大约 20 gig,在我强行杀死之前)。实际上我什至不能用任务管理器强行杀死,我必须重新启动操作系统,在这种情况下才能真正杀死 Python。

更新

在研究了我的代码之后,似乎出现了一个奇怪的错误/行为。

相关部分在这里:

          hdf5["train_bottle"][current_iteration*params['batch_size']: (current_iteration+1) * params['batch_size'],...] = prediction
                hdf5["train_labels"][current_iteration*params['batch_size']: (current_iteration+1) * params['batch_size'],...] = labels

如果我运行这两行中的任何一行,我的脚本将经历迭代,并按预期自动中断。所以如果我运行 either-or 就不会卡住。它也发生得相当快——不到一分钟。

如果我运行第一行 ('train_bottle'),我的内存将占用大约 69-72 gig,即使它只是几个批处理。如果我尝试更多批处理,内存是相同的。所以我假设 train_bottle 根据我分配给数据集的大小参数决定存储,而不是在它被填满时。 因此,尽管有 72 个演出,但它运行得相当快(一分钟)。

如果我运行第二行 train_labels ,我的内存会占用几兆字节。 迭代没有问题,执行break语句。

但是,现在问题来了,如果我尝试运行两条线(在我的情况下这是必要的,因为我需要同时保存“train_bottle”和“train_labels”),我在第一次迭代时遇到了卡住,即使在 20 分钟后,它也不会继续进行第二次迭代。 Hdf5 文件正在缓慢增长,但如果我尝试访问它,Windows 资源管理器会慢得像蜗牛一样,我无法关闭 Python——我必须重新启动操作系统。

所以我不确定尝试运行这两条线时的问题是什么——就好像我运行内存不足的 train_data 行一样,如果运行完美并在一分钟内结束。

最佳答案

将数据写入HDF5

如果您在不指定 block 形状的情况下写入分块数据集,h5py 会自动为您执行此操作。由于 h5py 无法知道您不想从数据集中写入或读取数据,这通常会导致性能不佳。

您还使用 1 MB 的默认 block 缓存大小。如果您只写入一个 block 的一部分并且该 block 不适合缓存(这很可能是 1MP block 缓存大小),整个 block 将在内存中读取,修改并写回磁盘。如果多次发生这种情况,您将看到远远超出 HDD/SSD 顺序 IO 速度的性能。

在下面的例子中,我假设你只沿着你的第一个维度读或写。如果不是,则必须根据您的需要进行修改。

import numpy as np
import tables #register blosc
import h5py as h5
import h5py_cache as h5c
import time

batch_size=120
train_shape=(90827, 10, 10, 2048)
hdf5_path='Test.h5'
# As we are writing whole chunks here this isn't realy needed,
# if you forget to set a large enough chunk-cache-size when not writing or reading 
# whole chunks, the performance will be extremely bad. (chunks can only be read or written as a whole)
f = h5c.File(hdf5_path, 'w',chunk_cache_mem_size=1024**2*200) #200 MB cache size
dset_train_bottle = f.create_dataset("train_bottle", shape=train_shape,dtype=np.float32,chunks=(10, 10, 10, 2048),compression=32001,compression_opts=(0, 0, 0, 0, 9, 1, 1), shuffle=False)
prediction=np.array(np.arange(120*10*10*2048),np.float32).reshape(120,10,10,2048)
t1=time.time()
#Testing with 2GB of data
for i in range(20):
    #prediction=np.array(np.arange(120*10*10*2048),np.float32).reshape(120,10,10,2048)
    dset_train_bottle[i*batch_size:(i+1)*batch_size,:,:,:]=prediction

f.close()
print(time.time()-t1)
print("MB/s: " + str(2000/(time.time()-t1)))

编辑 循环中的数据创建花费了很多时间,所以我在时间测量之前创建数据。

这应该至少提供 900 MB/s 的吞吐量(CPU 受限)。使用真实数据和较低的压缩率,您应该可以轻松达到硬盘的顺序 IO 速度。

如果您错误地多次调用此 block ,则使用 with 语句打开 HDF5 文件也会导致性能不佳。这将关闭并重新打开文件,删除 block 缓存。

为了确定正确的 block 大小,我还建议: https://stackoverflow.com/a/48405220/4045774 https://stackoverflow.com/a/44961222/4045774

关于python - 保存到 hdf5 非常慢(Python 卡住),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48672130/

相关文章:

tensorflow - 如果 Keras 模型中样本大小不能被batch_size整除怎么办

python - 使用Gstreamer的音频录制脚本质量很差

python - 如何有效地执行这个 numpy 数组操作?

python - Tensorflow教程日志丢失实现

python - 为什么 np.save() 在磁盘上占用这么多空间

python - 使用 PyInstaller 时没有命名模块

python-2.7 - 用于Python和Chollet深度学习的Docker容器

python - 当 channel 数增加时,ResNet 快捷连接的零填充

python - 方法签名中的 "iterable[, key]"是什么意思?

python - 为什么 dict({4 :None}) returns {4: None} but dict([{4, None}]) 返回 {None : 4} in Python 3. 11