Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allocate only one gpu to Keras (TF backend) script

I have a machine with 2 GPUs.

Quite often, one is used in production (i.e doing predictions with the already trained model), while the other is used for training and experimenting new models.

While I was using theano, I had no problem running my scripts on only one GPU by specifying a flag as follow

THEANO_FLAGS="device=cuda0" training_script.py THEANO_FLAGS="device=cuda1" prediction_script.py

Is there a simple way to do the same in Keras with a Tensorflow backend ? Default behavior seem to map all the memory of all the GPUs for one session

(Please note that I don't really care if each script maps a whole GPU separately, even if they could work using less memory)

like image 606
LoicM Avatar asked Jul 23 '18 14:07

LoicM


People also ask

Does Keras automatically use GPU?

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.

Will TensorFlow automatically use GPU?

Then, TensorFlow runs operations on your GPUs by default. You can control how TensorFlow uses CPUs and GPUs: Logging operations placement on specific CPUs or GPUs. Instructing TensorFlow to run certain operations in a specific “device context”—a CPU or a specific GPU, if there are multiple GPUs on the machine.

Can I run Keras without GPU?

allow_soft_placement allows for operations to be run on the CPU if any of the following criterion are met: there is no GPU implementation for the operation. there are no GPU devices known or registered.


1 Answers

You can easily choose one gpu. Just fill 0 or 1 on CUDA_VISIBLE_DEVICES

import os
os.environ["CUDA_VISIBLE_DEVICES"]="1"

Furthermore if you want to spesify a portion of gpu for the selected gpu above, add:

from keras import backend as K
import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.4 #what portion of gpu to use
session = tf.Session(config=config)
K.set_session(session)
like image 75
Ioannis Nasios Avatar answered Sep 19 '22 13:09

Ioannis Nasios