跳到主要内容

Using allow_growth memory option in Tensorflow and Keras

· 阅读需 2 分钟
Kobkrit Viriyayudhakorn
CEO, iApp Technology

1

We faced a problem when we have a GPU computer that shared with multiple users. Most users run their GPU process without the “allow_growth” option in their Tensorflow or Keras environments. It causes the memory of a graphics card will be fully allocated to that process. In reality, it is might need only the fraction of memory for operating. It prevents any new GPU process which consumes a GPU memory to be run on the same machine.

Example of three processes which can shared in two graphic cards enabled by “allow_growth” option.

To cove with this, They just enable the “allow_growth” setting in Tensorflow or Keras. The following code for setting allow_growth memory option in Tensorflow.

# Tensorflow  
import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.Session(config=config, ...)

And for Keras

#For Kerasfrom keras.callbacks import ModelCheckpoint  
from keras.models import Model, load_model, save_model, Sequential
from keras.layers import Dense, Activation, Dropout, Input, Masking, TimeDistributed, LSTM, Conv1D
from keras.layers import GRU, Bidirectional, BatchNormalization, Reshape
from keras.optimizers import Adamfrom keras.backend.tensorflow_backend import set_session
import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.allow_growth = True # dynamically grow the memory used on the GPU
config.log_device_placement = True # to log device placement (on which device the operation ran)sess = tf.Session(config=config)set_session(sess) # set this TensorFlow session as the default session for Keras

This increase the graphics cards utilization, not limited the number of process to the amount of card that host machine have. :)

For those whoever want to develop and get consult on creating your own AI model, please getting more information at our company website “iApp Technology” (https://iapp.co.th) and testing our AI demoes (https://ai.iapp.co.th). You can contact me directly at kobkrit@iapp.co.th. Thank you very much. #iApp #Ai

See more at https://iapp.co.th and https://ai.iapp.co.th