使用 keras 时出现内存错误

使用 keras 时出现内存错误

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

        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

相关内容