【解决】ValueError: Memory growth cannot differ between GPU devices
在ubuntu系统下双显卡运行TensorFlow代码报错:
ValueError: Memory growth cannot differ between GPU devices
报错的代码位置为: tf.config.experimental.set_memory_growth(physical_devices[0], True)
全部代码:
import os
import tensorflow as tf
from tensorflow.keras.applications import resnet50 physical_devices = tf.config.list_physical_devices('GPU')
print(physical_devices) tf.config.experimental.set_memory_growth(physical_devices[0], True) #加载预训练模型
model = resnet50.ResNet50(weights='imagenet') #save_model方式保存模型
tf.saved_model.save(model, "resnet/1/")
报错:

===============================================
查了网上的各种说法,有的说是TensorFlow的版本问题,有的说是TensorFlow的bug问题,有的说的gpu的版本问题(只有rtx2070和rtx2080的双显卡才会报错)
于是我就换了在服务器上运行,服务器为4卡泰坦,运行结果与上面的相同。

通过这样的对比实验我们可以得出结论,那就是这个并不是TensorFlow版本问题或是显卡版本问题,而是对于多显卡的运行模式下上面的写法并不是很正确,这本身就是TensorFlow的预先设定。
=====================================================
如果代码修改下就可以不报错,也就是添加内容:os.environ['CUDA_VISIBLE_DEVICES']='0'
import os
import tensorflow as tf
from tensorflow.keras.applications import resnet50 os.environ['CUDA_VISIBLE_DEVICES']='0' physical_devices = tf.config.list_physical_devices('GPU')
print(physical_devices) tf.config.experimental.set_memory_growth(physical_devices[0], True) #加载预训练模型
model = resnet50.ResNet50(weights='imagenet') #save_model方式保存模型
tf.saved_model.save(model, "resnet/1/")
运行正常不报错:

如果修改成下面这样,依然报同样的错误。
import os
import tensorflow as tf
from tensorflow.keras.applications import resnet50 #os.environ['CUDA_VISIBLE_DEVICES']='0' physical_devices = tf.config.list_physical_devices('GPU')
print(physical_devices) #os.environ['CUDA_VISIBLE_DEVICES']='0'
tf.config.experimental.set_memory_growth(physical_devices[0], True) #加载预训练模型
model = resnet50.ResNet50(weights='imagenet') #save_model方式保存模型
tf.saved_model.save(model, "resnet/1/")
回过头我们再仔细研究下报错的信息:

其实之所以报错就是因为主机有多块显卡,我们默认进行计算的时候是使用所有的显卡的,但是我们设置set_memory_growth时只为了部分显卡做设定,这样就造成了所要调用的显卡中部分显卡为set_memory_growth模式而部分不是,这样就造成了显卡与显卡之间的模式不已配的问题,因此也就报错了:
ValueError: Memory growth cannot differ between GPU devices
解决这个问题也是简单只要我们把所有用到的显卡模式设置为统一,如果设置set_memory_growth模式就为所有显卡都进行设定,也或者我们指定只使用部分显卡然后对这部分指定的显卡设置set_memory_growth模式。而设定只使用部分显卡的写法为:
os.environ['CUDA_VISIBLE_DEVICES']='0'
os.environ['CUDA_VISIBLE_DEVICES']='0,1'
而且需要知道一点,那就是os.environ['CUDA_VISIBLE_DEVICES']='0'这样的设置必须在调用TensorFlow框架之前声明否则不生效,这样同样会报错,就像上面的对比实验一样。
如下面:

为了方便我们可以直接把os.environ['CUDA_VISIBLE_DEVICES']='0'写在文件的最前面。
===============================================
如果我们想使用多块显卡进行计算,并且想使用set_memory_growth设置,那么我们就需要对这些显卡都进行set_memory_growth模式设置,具体如下代码:
import os
import tensorflow as tf
from tensorflow.keras.applications import resnet50 os.environ['CUDA_VISIBLE_DEVICES']='0,1' physical_devices = tf.config.list_physical_devices('GPU')
print(physical_devices) tf.config.experimental.set_memory_growth(physical_devices[0], True)
tf.config.experimental.set_memory_growth(physical_devices[1], True) #加载预训练模型
model = resnet50.ResNet50(weights='imagenet') #save_model方式保存模型
tf.saved_model.save(model, "resnet/1/")
关键的设置为:


==================================================
【解决】ValueError: Memory growth cannot differ between GPU devices的更多相关文章
- Allowing GPU memory growth
By default, TensorFlow maps nearly all of the GPU memory of all GPUs (subject to CUDA_VISIBLE_DEVICE ...
- 【转】简单内存泄漏检测方法 解决 Detected memory leaks! 问题
我的环境是: XP SP2 . VS2003 最近在一个项目中,程序退出后都出现内存泄漏: Detected memory leaks! Dumping objects -> {98500} n ...
- _CrtSetBreakAlloc简单内存泄漏检测方法,解决Detected memory leaks!问题
我的环境是: XP SP2 . VS2003 最近在一个项目中,程序退出后都出现内存泄漏: Detected memory leaks! Dumping objects -> {98500} n ...
- 【Visual Studio】简单内存泄漏检测方法 解决 Detected memory leaks! 问题(转)
原文转自 http://blog.csdn.net/u011430225/article/details/47840647 我的环境是: XP SP2.VS2003 最近在一个项目中, 程序退出后都出 ...
- [已解决]ValueError: row index was 65536, not allowed by .xls format
报错: ValueError: row index was 65536, not allowed by .xls format 解决方案: xlrd和xlwt处理的是xls文件,单个sheet最大行数 ...
- 吴裕雄--天生自然TensorFlow高层封装:解决ValueError: Invalid backend. Missing required entry : placeholder
找到对应的keras配置文件keras.json 将里面的内容修改为以下就可以了
- cmd 【已解决】windows连接手机,运行adb devices提示“unauthorized”
报错截图如下: 问题原因:电脑连接手机.手机未授权 解决方式: 设置----开发者选项-----打开USB调试,出现如下弹框,点击"确定"即可解决问题.
- Tensorflow2对GPU内存的分配策略
一.问题源起 从以下的异常堆栈可以看到是BLAS程序集初始化失败,可以看到是执行MatMul的时候发生的异常,基本可以断定可能数据集太大导致memory不够用了. 2021-08-10 16:38:0 ...
- 解决TensorFlow程序无限制占用GPU
今天遇到一个奇怪的现象,使用tensorflow-gpu的时候,出现内存超额~~如果我训练什么大型数据也就算了,关键我就写了一个y=W*x.......显示如下图所示: 程序如下: import te ...
- 【Udacity并行计算课程笔记】- Lesson 3 Fundamental GPU Algorithms (Reduce, Scan, Histogram)
本周主要内容如下: 如何分析GPU算法的速度和效率 3个新的基本算法:归约.扫描和直方图(Reduce.Scan.Histogram) 一.评估标准 首先介绍用于评估GPU计算的两个标准: ste ...
随机推荐
- Spring扩展———自定义bean组件注解
引言 Java 注解(Annotation)又称 Java 标注,是 JDK5.0 引入的一种注释机制. Java 语言中的类.方法.变量.参数和包等都可以被标注.和 Javadoc 不同,Java ...
- 函数式表达式基本类型及固定类型 demo
1.常见类型及使用 import java.util.function.BiConsumer; import java.util.function.BiFunction; import java.ut ...
- maven依赖关系及打包及阿里云仓库地址
https://developer.aliyun.com/mvn/search 阿里云仓库地址,可以搜索 <repositories> <repository> <id& ...
- Thread.sleep 延时查询或延时查询前更新es缓存数据
Thread.sleep 延时查询或延时查询前更新es缓存数据 MQ消息的顺序性,或发送MQ的发送端未严格事务处理,可能存在数据未落库的情况,而导致接收端处理MQ消息的时候,查询为空. //demo1 ...
- Nivdia向量数据库图检索最新标杆——CAGRA
本文连接:https://wanger-sjtu.github.io/CARGA/ CAGRA 是 N社在RAFT项目中 最新的 ANN 向量索引.这是一种高性能的. GPU 加速的.基于图的方法,尤 ...
- Imdeploy笔记
Smiling & Weeping ---- 天气不好的时候,我会小心地把自己心上的裂缝补起来.为什么?... LMDeploy 的量化和部署 1 环境配置 2 服务部署 2.1 模型转换 2 ...
- Vector | Graph:蚂蚁首个开源Graph RAG框架设计解读
检索增强生成(RAG:Retrieval Augmented Generation)技术旨在把信息检索与大模型结合,以缓解大模型推理"幻觉"的问题.近来关于RAG的研究如火如荼,支 ...
- Mybatis.xml文件中 大于小于等于
Mybatis中 大于小于等于的转义写法第一种写法:符号 转义字符< <<= <=> >>= >=& &am ...
- .NET 文件上传服务设计
.NET文件上传服务设计 前言 在b站学习了一个后端小项目,然后发现这个文件上传设计还挺不错,来实现讲解一下. 项目结构如下: 基于策略+工厂模式实现文件上传服务 枚举 在Model层创建即可 pub ...
- Docker Harbor的安装配置
1.先安装docker-compose curl -L http://github.com/docker/compose/releases/download/1.21.2/docker-compose ...