# Memory error when using more layers in CNN model

On my dell core i7 - 16GB RAM - 4gb 960m GPU laptop, I am working on a project to the classify lung CT images using 3d CNN. I'm using the CPU version of tensorflow. The images are prepared as numpy array size (25,50,50).

My CNN model had 2 conv layers, two maxpool layer, one FC layer and output layer. With this architecture I could train the model with approximately (5000 to 6000) samples. After adding more layers my model now has 6 conv layers, 3 max-pool layers, FC and output layer. My problem is after changing the architecture with just more than 1000 samples my memory gets filled and I get memory error. I tried to make smaller batches, but every time getting same error. I have two questions:

1. Why by adding more layers the model needs more memory?

2. Is there any way to deal with this type of problem?

• It's probably because you are having a lot of Parameters and by default your GPU is being used for computations and 4gb might not fit .. Oct 1, 2018 at 10:38
• I use CPU version of tensorflow not GPU Oct 1, 2018 at 11:04

1. More layers mean more parameters for your network, which in turn means more required space in memory to store those parameters.

2. The only solution (besides increasing the memory of your computer) is reducing the size of your network. A few pointers on this: Firstly, 3-D CNNs require much more space than 2-D ones. One idea could be to shift to a 2-D one. Other than that, the layers with the most parameters are the FC ones. These are sometimes redundant. I'd suggest reducing the size of those first.

• I think that 3d CNN is giving better accuracy than 2d, for that I used 3d CNN. the number of nodes in my FC layer is 1024, do you mean reducing that? is this does not affect the accuracy? Oct 2, 2018 at 13:15
• The number of neurons (or outputs) your FC layer has is $1024$. If let's say its inputs are another $2048$, then the total number of parameters would be $2048 \cdot 1024 + 1024$ or approximately $2.1$ million parameters. This is a large number of parameters for a single layer. For another example you could look at the VGG19 architecture, which has $140m$ parameters, $100m$ of which belong to a single FC layer.
– MzdR
Oct 7, 2018 at 21:44

Adding to the point made by MzdR, you could also try using generators instead. As you're model won't really be needing to have all your images in memory while training, I think a generator should serve well.

Check this question out from stackoverflow. Generators are pretty cool when you're on a memory bargain. But if this fails as well, try reducing the size of your FC layers.

• I think the generator is an interesting idea for my case and I will try it, but my problem is I don't good info about generators cuz I am new in python. Oct 4, 2018 at 8:35
• go through this medium post on using generators in keras. It might help you get an idea Oct 4, 2018 at 9:09
• it's much easier in keras, but my code is written in raw tensorflow and I can not change it to keras. Oct 4, 2018 at 12:47
• this question from stackoverflow will help for sure Oct 4, 2018 at 13:53