Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to release the occupied GPU memory when calling keras model by Apache mod_wsgi django?

My server configuration is as follows:

  1. apache 2.4.23.
  2. Mod_wsgi 4.5.9

By using the Django framework and apache server, we call the Keras deep learning model. And after the successful calling of the model, the model has been always running in the GPU memory, which causes the GPU memory can not be released except by shutting down the apache server.

So, is there any way to control the release of GPU memory when calling a Keras model by Apache+Mod_wsgi+Django?

Thanks!

Runtime memory footprint screenshots

like image 966
xixixixi Avatar asked May 12 '17 06:05

xixixixi


People also ask

Does keras use GPU automatically?

If your system has an NVIDIA® GPU and you have the GPU version of TensorFlow installed then your Keras code will automatically run on the GPU.


2 Answers

For people who fail to make K.clear_session() work, there is an alternative solution:

from numba import cuda
cuda.select_device(0)
cuda.close()

Tensorflow is just allocating memory to the GPU, while CUDA is responsible for managing the GPU memory.

If CUDA somehow refuses to release the GPU memory after you have cleared all the graph with K.clear_session(), then you can use the cuda library to have a direct control on CUDA to clear GPU memory.

like image 174
Raven Cheuk Avatar answered Oct 06 '22 02:10

Raven Cheuk


from keras import backend as K
K.clear_session()

This will clear the current session (Graph) and so the stale model should be removed from GPU. If it didn't work, you might need to 'del model' and reload it again.

like image 40
orabis Avatar answered Oct 06 '22 04:10

orabis