[英]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:尝试以下技术:
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.