7
$\begingroup$

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?

$\endgroup$
2
  • $\begingroup$ 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 .. $\endgroup$
    – Aditya
    Oct 1, 2018 at 10:38
  • $\begingroup$ I use CPU version of tensorflow not GPU $\endgroup$
    – Hunar
    Oct 1, 2018 at 11:04

2 Answers 2

4
$\begingroup$
  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.

$\endgroup$
2
  • $\begingroup$ 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? $\endgroup$
    – Hunar
    Oct 2, 2018 at 13:15
  • 1
    $\begingroup$ 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. $\endgroup$
    – MzdR
    Oct 7, 2018 at 21:44
4
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ 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. $\endgroup$
    – Hunar
    Oct 4, 2018 at 8:35
  • $\begingroup$ go through this medium post on using generators in keras. It might help you get an idea $\endgroup$
    – gavin
    Oct 4, 2018 at 9:09
  • $\begingroup$ it's much easier in keras, but my code is written in raw tensorflow and I can not change it to keras. $\endgroup$
    – Hunar
    Oct 4, 2018 at 12:47
  • $\begingroup$ this question from stackoverflow will help for sure $\endgroup$
    – gavin
    Oct 4, 2018 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.