使用keras时出现内存错误

时间:2016-08-11 06:58:43

标签: memory-leaks keras

我正在为CNN使用keras,但问题是存在内存泄漏。错误是

        anushreej@cpusrv-gpu-109:~/12EC35005/MTP_Workspace/MTP$ python cnn_implement.py
        Using Theano backend.
        [INFO] compiling model...
        Traceback (most recent call last):
          File "cnn_implement.py", line 23, in <module>
            model = CNNModel.build(width=150, height=150, depth=3)
          File "/home/ms/anushreej/12EC35005/MTP_Workspace/MTP/cnn/networks/model_define.py", line 27, in build
            model.add(Dense(depth*height*width))
          File "/home/ms/anushreej/anaconda3/lib/python3.5/site-packages/keras/models.py", line 146, in add
            output_tensor = layer(self.outputs[0])
          File "/home/ms/anushreej/anaconda3/lib/python3.5/site-packages/keras/engine/topology.py", line 458, in __call__
            self.build(input_shapes[0])
          File "/home/ms/anushreej/anaconda3/lib/python3.5/site-packages/keras/layers/core.py", line 604, in build
            name='{}_W'.format(self.name))
          File "/home/ms/anushreej/anaconda3/lib/python3.5/site-packages/keras/initializations.py", line 61, in glorot_uniform
            return uniform(shape, s, name=name)
          File "/home/ms/anushreej/anaconda3/lib/python3.5/site-packages/keras/initializations.py", line 32, in uniform
            return K.variable(np.random.uniform(low=-scale, high=scale, size=shape),
          File "mtrand.pyx", line 1255, in mtrand.RandomState.uniform (numpy/random/mtrand/mtrand.c:13575)
          File "mtrand.pyx", line 220, in mtrand.cont2_array_sc (numpy/random/mtrand/mtrand.c:2902)
        MemoryError

现在我无法理解为什么会这样。我的训练图像非常小,尺寸为150 * 150 * 3.

代码是 - :

        # import the necessary packages
        from keras.models import Sequential
        from keras.layers.convolutional import Convolution2D
        from keras.layers.core import Activation
        from keras.layers.core import Flatten
        from keras.layers.core import Dense

        class CNNModel:
          @staticmethod
          def build(width, height, depth):
            # initialize the model
            model = Sequential()
            # first set of CONV => RELU
            model.add(Convolution2D(50, 5, 5, border_mode="same", batch_input_shape=(None, depth, height, width)))
            model.add(Activation("relu"))

            # second set of CONV => RELU
            # model.add(Convolution2D(50, 5, 5, border_mode="same"))
            # model.add(Activation("relu"))

            # third set of CONV => RELU
            # model.add(Convolution2D(50, 5, 5, border_mode="same"))
            # model.add(Activation("relu"))

            model.add(Flatten())

            model.add(Dense(depth*height*width))

            # if weightsPath is not None:
            #   model.load_weights(weightsPath) 

            return model

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,我认为问题是Flattening层之前的数据点数超过了你的系统可以处理的数量点(我在差异系统中尝试过,因此一个ram工作得很好而且ram较少就会出现这个错误) 。只需添加更多CNN图层以减小尺寸,然后添加一个有效的展平图层。

这给了我错误:

model = Sequential()
model.add(Convolution2D(32, 3, 3,border_mode='same',input_shape=(1, 96, 96),activation='relu'))
model.add(Convolution2D(64, 3, 3,border_mode='same',activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Flatten())
model.add(Dense(1000,activation='relu'))
model.add(Dense(97,activation='softmax'))

这没有给出错误

model = Sequential()
model.add(Convolution2D(32, 3, 3,border_mode='same',input_shape=(1, 96, 96),activation='relu'))
model.add(Convolution2D(64, 3, 3,border_mode='same',activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Convolution2D(64, 3, 3,border_mode='same',activation='relu'))
model.add(Convolution2D(128, 3, 3,border_mode='same',activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Flatten())
model.add(Dense(1000,activation='relu'))
model.add(Dense(97,activation='softmax')

希望它有所帮助。