简体   繁体   English

尝试将训练数据拟合到 CNN model 时出现 Memory 错误

[英]Getting Memory Error while trying to fit training data into CNN model

I am trying to create model on the popular cats and dogs training data using CNN.我正在尝试使用 CNN 在流行的猫狗训练数据上创建 model。 When I try to fit the training data using fit_generator, I get Memory Error as - self.filepaths is dynamic, is better to call it once outside the loop.当我尝试使用 fit_generator 拟合训练数据时,我得到 Memory 错误,因为 self.filepaths 是动态的,最好在循环外调用它。 Below is my line of code:下面是我的代码行:

model.fit_generator(train_data, steps_per_epoch=10, 
                    validation_data=valid_data, validation_steps=2, epochs=10, verbose=2)

I read the training data using我使用

train_data = ImageDataGenerator().flow_from_directory(train_path, target_size=(224,224), classes=['dogs', 'cats'], batch_size=10)

Kindly suggest a solution.请提出解决方案。 Below is my model:下面是我的 model:

model = Sequential()
model.add(Conv2D(32, kernel_size=(5, 5),
                 activation='relu',
                  input_shape=(3,224, 224), data_format='channels_first'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=2))
model.add(Conv2D(64, (5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=2))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.4))

Be sure to use python 64-bit.请务必使用 python 64 位。 Last time I had a memory error I was using Python 32-bit and I didn't been aware of it.上次我遇到 memory 错误时,我使用的是 Python 32 位,但我不知道。

Try following techniques:尝试以下技术:

  1. Resize image to smaller height and width.将图像大小调整为更小的高度和宽度。 224 size generally works unless you have quite a lot number of images. 224 尺寸通常有效,除非您有大量图像。
  2. Use convolution layers and max/min pooling layers.使用卷积层和最大/最小池化层。 This will help reduce number of neurons.这将有助于减少神经元的数量。
  3. Try reducing the size of hidden layers and number of hidden layers.尝试减少隐藏层的大小和隐藏层的数量。
  4. Use sigmoid classifier since you are trying to build a binary classification (cat / dog).使用 sigmoid 分类器,因为您正在尝试构建二进制分类(猫/狗)。
  5. If you are running this on you laptop consider gc.collect() before running your model.如果您在笔记本电脑上运行此程序,请在运行 model 之前考虑 gc.collect()。 Also, try google colab to utilize gpu/tpu.另外,尝试使用 google colab 来利用 gpu/tpu。

There can be other possible issues too, if you like to explain a bit more about your model and machine you are using I can provide more specific answer.还有其他可能的问题,如果您想更多地解释您的 model 和您正在使用的机器,我可以提供更具体的答案。

I was able to resolve issue using below layers flow: In this flow, I made the data flat and passed the final output to Dense layer.我能够使用以下层流解决问题:在此流中,我使数据变平并将最终的 output 传递给密集层。 After doing this I got 100% correct prediction.这样做之后,我得到了 100% 正确的预测。 Please let me know if someone has a better solution.如果有人有更好的解决方案,请告诉我。

model.add(Conv2D(32, kernel_size=(5, 5), activation='relu', input_shape=(224,224,3)))
model.add(MaxPooling2D(pool_size=(2, 2), strides=2))
model.add(Conv2D(64, (5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=2))
model.add(Flatten())
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(2, activation='softmax'))

See below execution result:看下面的执行结果:

Epoch 1/10
 - 4s - loss: 222.1019 - accuracy: 0.5800 - val_loss: 23.9488 - val_accuracy: 0.5000
Epoch 2/10
 - 3s - loss: 12.1180 - accuracy: 0.5600 - val_loss: 3.5912 - val_accuracy: 0.5000
Epoch 3/10
 - 3s - loss: 1.3664 - accuracy: 0.7200 - val_loss: 0.5239 - val_accuracy: 0.6000
Epoch 4/10
 - 4s - loss: 1.0074 - accuracy: 0.7200 - val_loss: 0.1986 - val_accuracy: 0.9000
Epoch 5/10
 - 4s - loss: 0.1890 - accuracy: 0.9400 - val_loss: 0.0298 - val_accuracy: 1.0000
Epoch 6/10
 - 3s - loss: 0.1680 - accuracy: 0.9200 - val_loss: 0.1973 - val_accuracy: 0.9000
Epoch 7/10
 - 3s - loss: 0.9097 - accuracy: 0.9600 - val_loss: 0.0020 - val_accuracy: 1.0000
Epoch 8/10
 - 4s - loss: 0.0372 - accuracy: 0.9800 - val_loss: 0.0027 - val_accuracy: 1.0000
Epoch 9/10
 - 3s - loss: 0.0466 - accuracy: 1.0000 - val_loss: 4.7272e-04 - val_accuracy: 1.0000
Epoch 10/10
 - 3s - loss: 0.0172 - accuracy: 1.0000 - val_loss: 2.7418e-07 - val_accuracy: 1.0000

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 训练 CNN 模型时出错(tensorflow) - Getting error when training the CNN model(tensorflow) 尝试拟合模型时出错 - Tensorflow CNN - Error when trying to fit model - Tensorflow CNN 在 CNN 神经网络 model 中,我正在尝试将我的数据拟合到 fit.model () 但它显示错误 - In a CNN neural network model i am trying to fit my data to fit.model () but it's showing error 为什么在尝试为 CNN 添加训练集时显示以下错误? - Why is the following error is showing while trying to add training set for CNN? 用于训练 CNN model 的预处理数据 - Preprocessing Data for training CNN model 在 kaggle 上训练 model 时反复收到消息“清理调用...”。 我们怎样才能摆脱这个? (CNN model 使用 Keras) - Getting the message 'Cleanup called...' repeatedly while training a model on kaggle. How can we get rid of this? (CNN model using Keras) 训练临床 EHR pytorch 模型时出现未定义错误 - Getting not defined error while training clinical EHR pytorch model 在训练机器学习模型进行垃圾邮件检测时出现索引错误 - Getting an index error while training machine learning model for spam detection 使用keras构建CNN模型,以进行不均匀的训练和测试图像文件夹数据 - Build CNN model with keras for uneven training and testing data of folders of images 在Tensorflow和Caffe中实施的Faster-R-CNN训练模型中的错误 - Error in training model for Faster-R-CNN implemented in Tensorflow and Caffe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM