Keras in the use of the GPU when the feature is that the default is full of video memory. That way, if you have multiple models that need to run with a GPU, the restrictions are huge and a waste to the GPU. So when using Keras, you need to consciously set how much capacity you need to use the video card when you run it.
There are generally three situations in this setting:
1. Specify the video card
2. Limit GPU Usage
3. That is, specify the graphics card and limit the GPU Usage view GPU Usage statement (Linux)
# 1 seconds to refresh once
watch-n 1 nvidia-smi
first, the designated video card
Import os
os.environ["cuda_visible_devices"] = "2"
This specifies the use of the GPU number 2, you can specify the GPU used according to the needs and actual situation , limit GPU usage 1, set the percentage of use GPU
Import TensorFlow as TF
import keras.backend.tensorflow_backend as KTF
#进行配置, using 30% gpu
config = tf. Configproto ()
config.gpu_options.per_process_gpu_memory_fraction = 0.3
session = TF. Session (Config=config)
# Set Session
Ktf.set_session (session)
It is important to note that although the code or configuration level sets a percentage threshold for memory consumption, the threshold is exceeded if the program needs to do so if the threshold is reached in the actual operation. In other words, if you run on a big data set, you will still use more memory. The above video memory limit is just to avoid wasting memory when running a small data set.
2, GPU on-demand use
Import TensorFlow as TF
import keras.backend.tensorflow_backend as KTF
config = tf. Configproto ()
config.gpu_options.allow_growth=true #不全部占满显存, ON demand
session = TF. Session (Config=config)
# Set Session
Ktf.set_session (Sess)
Iii. specifying the GPU and limiting GPU usage
This is relatively simple, that is to say the above two cases can be connected ...
Import OS
import TensorFlow as tf
import keras.backend.tensorflow_backend as KTF
# Specifies that the first GPU is available
os.environ["cuda_visible_devices"] = "0"
config = tf. Configproto ()
config.gpu_options.allow_growth=true #不全部占满显存, on demand
sess = tf. Session (config=config)
ktf.set_session (Sess)