【发布时间】:2017-11-22 12:56:12
【问题描述】:
我创建了如下模型(带有 theano 后端的 keras)。当我在我的 CPU 上运行它时,它给了我内存错误。我有 8GB DDR3 内存,在调用 model1.fit 之前,我的内存消耗了 2.3 GB。我也可以使用高达 7.5GB 的 RAM 并且程序崩溃。我还尝试在 GPU (Nvedia GeForce GTX 860M) 4GB 上运行它,但仍然出现内存错误。
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 左右的内存。这是我的第一个模型。
【问题讨论】:
-
查看 Karpathy 关于 ConvNets 内存计算的笔记:cs231n.github.io/convolutional-networks(最后几段“计算考虑”)。我建议您减少图像大小和过滤器数量并增加过滤器大小。我注意到您没有在顶部添加任何完全连接的层。这可能是故意的,但如果不是,你也必须考虑到这些,记住。
标签: keras theano conv-neural-network keras-layer