How to unload a keras/tensorflow model from memory?

Viewed 3090

Here's the problem: My (Keras)model is listening to a task queue. If no task arrives in 10 min, I want to unload the model and free the memory.

But I never thought such a job would be so hard...

Here are some failed tries:

(1) Set model = None, hope GC collect the memory.

(2) del model

(3) Use K.clear_session(), tf.reset_defualt_graph().

(4) Any combination of above methods followed by calling gc.collect() manually.

Is it possible to unload a model from memory without exiting current process? Any other suggestions?

2 Answers

numba library

Assuming you are using device 0

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

What I do is:

import keras
keras.backend.clear_session()

As far as I know it clears the memory

Related