2017-10-05 73 views
1

我目前正在使用大图像数据集(〜60GB)来训练CNN(Keras/Tensorflow)以进行简单的分类任务。 这些图像是视频帧,因此时间高度相关,所以在生成巨大的.hdf5文件时,我已经洗过一次数据... 将数据传送到CNN时无需一次将整个数据集加载到内存中我写了一个简单的批生成器(见下面的代码)。 现在我的问题: 通常建议在每个训练时期后洗牌数据? (对于SGD收敛的原因?)但要这样做,我将不得不在每个时期加载整个数据集并对其进行洗牌,这正是我想要避免使用批生成器的原因...... 所以:它真的那么重要吗?在每个纪元后洗牌数据集,如果是的话我怎么能尽可能有效地做到这一点? 这里是我的批处理发电机的电流代码:大HDF5数据集,如何在每个纪元后有效地洗牌

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) 
+2

速度的一个重要方面是将hdf5文件中的chuck_size设置为图像的大小。这可以提高效率,同时从hdf5获取单个图像 –

回答

0

呀,洗牌改善,因为在每个时间可以让你在不理想的地区陷入相同的顺序运行数据的表现。

不要洗牌整个数据。在数据中创建一个索引列表,然后改变它。然后顺序移动索引列表并使用它的值从数据集中选取数据。