Keras 当我们有一个巨大的数据集时,如何训练模型以避免错误记忆
我正在尝试用一个超过250k图像的数据集来训练神经网络。但我的电脑有限,内存为16GB,交换容量为32GB,所以我被卡住了。在我加载所有图像之前,它会给我一个内存错误。所以我想知道是否有一种方法可以利用我所有的图像来训练我的神经网络?例如,我们可以不使用RAM内存来加载numpy阵列中的图像,而是将其加载到可用空间磁盘上吗 编辑1:Keras 当我们有一个巨大的数据集时,如何训练模型以避免错误记忆,keras,Keras,我正在尝试用一个超过250k图像的数据集来训练神经网络。但我的电脑有限,内存为16GB,交换容量为32GB,所以我被卡住了。在我加载所有图像之前,它会给我一个内存错误。所以我想知道是否有一种方法可以利用我所有的图像来训练我的神经网络?例如,我们可以不使用RAM内存来加载numpy阵列中的图像,而是将其加载到可用空间磁盘上吗 编辑1: def get_array_image(file, path): return cv2.imread(path+file) def generator(fe
def get_array_image(file, path):
return cv2.imread(path+file)
def generator(features, labels, num_classes, batch_size, path=''):
# Create empty arrays to contain batch of features and labels#
batch_features = np.zeros((batch_size, 28, 28, 3))
batch_labels = np.zeros((batch_size, 1))
while True:
for cpt in range(0, len(features), batch_size):
for i in range(0, batch_size):
index = cpt + i
#print('images : ', index)
batch_features[i] = get_array_image(features[index], path)
batch_labels[i] = labels[index]
yield batch_features, keras.utils.to_categorical(batch_labels, num_classes)
这是我的发电机,我使用fit_发电机功能。但我有一个准确性的问题。事实上,我用一个smal神经网络在mnist数据集上进行了尝试。如果我使用拟合函数加载所有图像(约60k图像用于训练,60k图像用于测试),一个历元后的精度约为0.68。但是使用fit_生成器,我只能得到0.1。我的发电机出毛病了吗?当我打印索引变量时,它看起来很好
编辑2:我解决了我的问题,但我不明白它为什么有效。事实上,当我在循环外部创建阵列时,我获得了较低的精度,但当它在内部时,使用fit_生成器的精度很好。有人知道我错过了什么吗
def generator(features, labels, num_classes, batch_size, path='', dtype=np.uint8):
# Create empty arrays to contain batch of features and labels#
# batch_features = np.ndarray(shape=(batch_size, 28, 28, 3), dtype=dtype)
# batch_labels = np.ndarray(shape=(batch_size, 1), dtype=dtype)
while True:
for cpt in range(0, len(features), batch_size):
batch_features = np.ndarray(shape=(batch_size, 28, 28, 3), dtype=dtype)
batch_labels = np.ndarray(shape=(batch_size, 1), dtype=dtype)
for i in range(0, batch_size):
# index= random.randint(0, len(features)-1)
index = cpt + i
#print('images : ', index)
batch_features[i] = get_array_image(features[index], path)
batch_labels[i] = labels[index]
# print(batch_labels[i])
# cv2.imshow('image', batch_features[i])
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# print(features[index])
print(batch_features.shape)
yield batch_features, keras.utils.to_categorical(batch_labels, num_classes)
查看fit generator谢谢我正在尝试!