Thumbnail article
How to handle memory leak keras predict

TensorFlow executes the entire graph whenever you (or Keras) call or tf.Tensor.eval(), so your models will become slower and slower to train, and you may also run out of memory. Use this code to freeing the memory.

import keras.backend as K 
import gc

# add this code after doing a prediction

gc provides an interface to the optional garbage collector. It provides the ability to disable the collector, tune the collection frequency, and set debugging options. It also provides access to unreachable objects that the collector found but cannot be free. Since the collector supplements the reference counting already used in Python, you can disable the collector if you are sure your program does not create reference cycles. Automatic collection can be disabled by calling gc.disable(). To debug a leaking program call gc.set_debug(gc.DEBUG_LEAK). Notice that this includes gc.DEBUG_SAVEALL, causing garbage-collected objects to be saved in gc.garbage for inspection. gc.collect() with no arguments will run a full collection. The optional argument generation may be an integer specifying which generation to collect (from 0 to 2). A ValueError is raised if the generation number is invalid. The number of unreachable objects found is returned. The free lists maintained for a number of built-in types are cleared whenever a full collection or collection of the highest generation (2) is run. Keras manages a global state, which it uses to implement the Functional model-building API and to uniquify autogenerated layer names. If you are creating many models in a loop, this global state will consume an increasing amount of memory over time, and you may want to clear it. Calling clear_session() releases the global state. This helps avoid clutter from old models and layers, especially when memory is limited. K.clear_session() is useful when you’re creating multiple models in succession, such as during hyperparameter search or cross-validation. Each model you train adds nodes (potentially numbering in the thousands) to the graph. Clearing the session removes all the nodes left over from previous models, and preventing slowdown.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *