【发布时间】:2018-03-16 19:48:41
【问题描述】:
我目前正在使用一个大型图像数据集 (~60GB) 来训练 CNN (Keras/Tensorflow) 以完成简单的分类任务。 这些图像是视频帧,因此在时间上高度相关,所以我在生成巨大的 .hdf5 文件时已经对数据进行了一次混洗...... 为了将数据输入 CNN 而不必一次将整个数据集加载到内存中,我编写了一个简单的批处理生成器(参见下面的代码)。 现在我的问题: 通常建议在每个训练时期之后对数据进行洗牌,对吗? (出于 SGD 收敛的原因?)但要这样做,我必须在每个 epoch 之后加载整个数据集并对其进行洗牌,这正是我想避免使用批处理生成器的原因...... 所以:在每个时期之后洗牌数据集真的那么重要吗?如果是的话,我怎么能尽可能有效地做到这一点? 这是我的批处理生成器的当前代码:
def generate_batches_from_hdf5_file(hdf5_file, batch_size, dimensions, num_classes):
"""
Generator that returns batches of images ('xs') and labels ('ys') from a h5 file.
"""
filesize = len(hdf5_file['labels'])
while 1:
# count how many entries we have read
n_entries = 0
# as long as we haven't read all entries from the file: keep reading
while n_entries < (filesize - batch_size):
# start the next batch at index 0
# create numpy arrays of input data (features)
xs = hdf5_file['images'][n_entries: n_entries + batch_size]
xs = np.reshape(xs, dimensions).astype('float32')
# and label info. Contains more than one label in my case, e.g. is_dog, is_cat, fur_color,...
y_values = hdf5_file['labels'][n_entries:n_entries + batch_size]
#ys = keras.utils.to_categorical(y_values, num_classes)
ys = to_categorical(y_values, num_classes)
# we have read one more batch from this file
n_entries += batch_size
yield (xs, ys)
【问题讨论】:
-
速度的重要方面是将 hdf5 文件中的chuck_size 设置为图像的大小。这提高了效率,同时从 hdf5 获取单个图像
标签: python tensorflow deep-learning keras hdf5