我正在尝试将瓶颈值保存到新创建的 hdf5 文件中。
瓶颈值呈批量形状(120,10,10, 2048)
。
单独保存一个批次就占用了超过 16 个千兆字节,而 Python 似乎在该批次上冻结了。根据最近的发现(请参阅更新,似乎 hdf5 占用大内存是可以的,但冻结部分似乎是一个小故障。
我只是想拯救第一个2批次用于测试目的,并且仅
训练数据集(再次,这是一个测试运行),但我什至无法通过第一批。它只是在第一批中停止,并且不会循环到下一次迭代。如果我尝试检查 hdf5,资源管理器将变得缓慢,Python 将冻结。如果我尝试终止 Python(即使不检查 hdf5 文件),Python 也不会正确关闭,并且会强制重新启动。
以下是相关代码和数据:
总数据点约为 90,000 个左右,分 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 GB,在我强行杀死之前)。实际上,我什至无法使用任务管理器强制杀死,我必须重新启动操作系统,才能在这种情况下真正杀死Python。
Update
在玩了一下我的代码后,似乎有一个奇怪的错误/行为。
相关部分在这里:
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
如果我运行其中任何一行,我的脚本将进行迭代,并按预期自动中断。因此,如果我运行“非此即彼”,则不会冻结。它发生得也相当快——不到一分钟。
如果我运行第一行('train_bottle')
,我的内存占用了大约 69-72 gig,即使只是几个批次。如果我尝试更多批次,内存是相同的。所以我假设train_bottle
根据我分配数据集的大小参数决定存储,而不是实际上当它被填充时。
因此,尽管有 72 场演出,但它的运行速度相当快(一分钟)。
如果我运行第二行,train_labels
,我的内存占用了几兆。
迭代没有问题,执行break语句。
然而,现在问题是,如果我尝试运行这两行(在我的情况下这是必要的,因为我需要保存“train_bottle”和“train_labels”),我在第一次迭代时遇到冻结,并且它即使在 20 分钟后,也不会继续进行第二次迭代。 Hdf5 文件正在缓慢增长,但如果我尝试访问它,Windows 资源管理器会慢如蜗牛,而且我无法关闭 Python - 我必须重新启动操作系统。
所以我不确定尝试运行两条线时出现什么问题——就好像我运行内存很饿一样train_data
线路,如果工作正常并在一分钟内结束。