当前位置:网站首页>GPU memory limit when using tensorflow or keras
GPU memory limit when using tensorflow or keras
2022-07-20 00:30:00 【Mein_ Augenstern】
Use Tensorflow or Keras When the GPU Memory limit
The original blog —— Use Tensorflow or Keras When the GPU Memory limit
run Keras perhaps Tensorflow By default, it takes up all GPU Memory , At this time, if you want to start another process , Or others can't squeeze in when they want to start a process , therefore must Limit GPU Memory
The best information is Official documents
visible_device_list
Specify which graphics card to useper_process_gpu_memory_fraction
The proportion of allocated memory in the total memoryallow_growth
Allocate according to the needs of the runtime GPU Memory , Allocate very little memory at first , With Session Start running and need more GPU Memory will expand automatically , But then Don't release Memoryper_process_gpu_memory_fraction
Andallow_growth
Choose one of the two methods . It's usually usedallow_growth
that will do , If you can accurately estimate how much video memory your program needs , recommendper_process_gpu_memory_fraction
# Add the following code to .py Just start the file
import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.visible_device_list = '0'
# config.gpu_options.per_process_gpu_memory_fraction = 0.2
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
# from keras import backend as K
# K.set_session(sess)
In terms of personal habits , Each project has a configuration file config.py, Write the basic configuration function inside , Such as GPU To configure , Path configuration, etc .
set_gpu() function , Automatically identify whether there is a graphics card , If there is a graphics card, specify more free memory or GPU Use a low graphics card , And set the distribution proportion
The code is for two graphics cards , It's too lazy to change. , Many graphics cards are similar ~
def set_gpu(ratio=0, target='memory'):
"""
To configure GPU,0 It means adaptive ,(0, 1] Indicates the proportion of video memory
:param ratio:
:param target: Choose a card with large video memory or GPU Cards with low utilization
:return:
"""
command1 = "nvidia-smi -q -d Memory | grep -A4 GPU | grep Free | awk '{print $3}'"
command2 = "nvidia-smi -q | grep Gpu | awk '{print $3}'"
memory = list(map(int, os.popen(command1).readlines()))
gpu = list(map(int, os.popen(command2).readlines()))
if memory and gpu: # If you don't have a graphics card ,memory,gpu Are all []
if target == 'memory':
num = (1, 0)[memory[0] > memory[1]]
else:
num = (0, 1)[gpu[0] > gpu[1]]
print('>>> Free Memory : GPU0 %6d MiB | GPU1 %6d MiB' % (memory[0], memory[1]))
print('>>> Volatile GPU-Util : GPU0 %6d %% | GPU1 %6d %% ' % (gpu[0], gpu[1]))
print('>>> Using GPU%d' % num)
import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.visible_device_list = str(num) # choice GPU
if ratio == 0:
config.gpu_options.allow_growth = True
else:
config.gpu_options.per_process_gpu_memory_fraction = ratio
sess = tf.Session(config=config)
from keras import backend as K
K.set_session(sess)
else:
print('>>> Could not find the GPU')
There are two other ways to specify a graphics card
- Add environment variables
export CUDA_VISIBLE_DEVICES=0
- Use os
import os
os.environ["CUDA_VISIBLE_DEVICES"] = '0'
see GPU usage
watch -n 1 nvidia-smi
Refresh per secondnvidia-smi
边栏推荐
- Technical writing guide for programmers to leave work early
- pycaret在钻石数据集上的使用 - 回归问题
- CRM实施的三大误区:缺乏战略,缺乏认同感,唯技术至上
- How to build and analyze the learning environment of skywalking series
- HTB-Apocalyst
- R语言ggplot2可视化:使用ggpubr包的ggarrange函数将多幅图像组合起来、使用common.legend参数指定组合图的图例
- 打包tricks
- Leetcode notes: Weekly contest 302
- anaconda下载与spyder的报错解决
- 777. 在LR字符串中交换相邻字符
猜你喜欢
随机推荐
创原会丨携手50+云原生企业,共探跨越数字化鸿沟新模式
MySQL learning -where
[songhongkang MySQL database] [advanced chapter] [09] data storage structure of InnoDB
【宋紅康 MySQL數據庫 】【高級篇】【09】InnoDB的數據存儲結構
1该方法用于获取两个整数之间的随机整数;2获得一组数的平均值
Flask database Sqlalchemy
Quatre exemples simples pour améliorer l'expérience utilisateur grâce à l'enregistrement du comportement de l'utilisateur
A complete set of source code of information management (his) system of large-scale third-class hospitals [share the source code for free]
MySQL different table field sorting index failure?
Markov chain
动手实践丨手把手教你用STM32做一个智能鱼缸
Find the day of the year on which the date is located
Technical writing guide for programmers to leave work early
FIR 滤波器设计
相似度計算公式
Violent recursion Huawei topic
【VisDrone数据集】YOLOV7训练VisDrone数据集与结果
Eolink 和 JMeter 接口测试优势分析
【宋红康 MySQL数据库 】【高级篇】【09】InnoDB的数据存储结构
R语言ggplot2可视化:使用ggpubr包的ggarrange函数将多幅图像组合起来、使用common.legend参数指定组合图的图例