Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limiting GPU memory usage by Keras / TF 2019?

I have read answers like:

import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.2
set_session(tf.Session(config=config))

But it just doesn't work. There seems to be so much update in both keras and TF that almost anything written in 2017 doesn't work! So, how to limit memory usage?

like image 645
Alex Deft Avatar asked Jan 01 '23 01:01

Alex Deft


2 Answers

One way to restrict reserving all GPU RAM in tensorflow is to grow the amount of reservation. This method will allow you to train multiple NN using same GPU but you cannot set a threshold on the amount of memory you want to reserve.

Using the following snippet before importing keras or just use tf.keras instead.

import tensorflow as tf
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
    try:
        for gpu in gpus:
            tf.config.experimental.set_memory_growth(gpu, True)

    except RuntimeError as e:
        print(e)
like image 65
Coderji Avatar answered Jan 08 '23 00:01

Coderji


Seems that I have the same problem. Have you tried this?

import tensorflow as tf
from keras.backend.tensorflow_backend import set_session

config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
set_session(sess)

This method will make the application allocate only as much GPU memory based on runtime allocation.

like image 23
Yustina Ivanova Avatar answered Jan 08 '23 00:01

Yustina Ivanova