nka*_*zig 5 python hdf5 deep-learning keras tensorflow
我目前正在使用大型图片数据集(约60GB)来训练CNN(Keras / Tensorflow)以完成简单的分类任务。图像是视频帧,因此在时间上高度相关,因此在生成巨大的.hdf5文件时,我已经对数据进行了一次重洗。一个简单的批处理生成器(请参见下面的代码)。现在我的问题是:通常建议在每个训练时期之后对数据进行混洗,对吗?(出于SGD收敛的原因?)但是要做到这一点,我必须在每个时期之后加载整个数据集并对其进行混洗,这正是我要避免使用批处理生成器的原因……所以:在每个时期之后重新整理数据集真的重要吗?如果是,我如何才能尽可能高效地做到这一点?这是我的批处理生成器的当前代码:
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)
Run Code Online (Sandbox Code Playgroud)
是的,洗牌可以提高性能,因为每次以相同的顺序运行数据可能会让您陷入次优区域。
不要打乱整个数据。创建数据索引列表,然后对其进行打乱。然后按顺序移动索引列表并使用其值从数据集中选取数据。