实施CNN时keras中的内存错误

Memory error in keras while implementing CNN

我创建了如下模型(带有 theano 后端的 keras)。当我 运行 它在我的 CPU 上时,它给我内存错误。我有 8GB DDR3 内存,在调用 model1.fit 之前我的内存消耗了 2.3GB。我也可以使用高达 7.5GB 的 RAM 并且程序崩溃。我也在 GPU (Nvedia GeForce GTX 860M) 4GB 上尝试了 运行ning,但我仍然遇到内存错误。

def get_model_convolutional():
    model = keras.models.Sequential()
    model.add(Conv2D(128, (3, 3), activation='relu', strides = (1,1), input_shape=(1028, 1028, 3)))
    model.add(Conv2D(3, (3, 3), strides = (1,1), activation=None))
    sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(loss='categorical_crossentropy', optimizer=sgd)
    return model

if __name__ == "__main__":
    model1 = get_model_convolutional()
    train_x = np.ones((108, 1208, 1208, 3), dtype=np.uint8)
    train_y = np.ones((108, 1204, 1204, 3), dtype = np.uint8)    
    model1.fit(x_train, y_train, verbose = 2,epochs=20, batch_size=4)

我尝试打印 model.summary() 时的输出也是

Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 1026, 1026, 128)   3584      
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 1024, 1024, 3)     3459      
=================================================================
Total params: 7,043
Trainable params: 7,043
Non-trainable params: 0
_________________________________________________________________

为什么需要这么多内存?我试着计算了一下,但我认为应该需要 1.5GB 左右的内存。这是我的第一个模型。

计算中间输出需要内存,在这种情况下,由于没有池化,中间输出变得非常大。一些解决方案是减少过滤器的数量,减小图像大小(即使用裁剪图像,然后将它们堆叠在一起),减少批量大小。