在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的更多相关文章

  1. Allowing GPU memory growth

    By default, TensorFlow maps nearly all of the GPU memory of all GPUs (subject to CUDA_VISIBLE_DEVICE ...

  2. 【转】简单内存泄漏检测方法 解决 Detected memory leaks! 问题

    我的环境是: XP SP2 . VS2003 最近在一个项目中,程序退出后都出现内存泄漏: Detected memory leaks! Dumping objects -> {98500} n ...

  3. _CrtSetBreakAlloc简单内存泄漏检测方法,解决Detected memory leaks!问题

    我的环境是: XP SP2 . VS2003 最近在一个项目中,程序退出后都出现内存泄漏: Detected memory leaks! Dumping objects -> {98500} n ...

  4. 【Visual Studio】简单内存泄漏检测方法 解决 Detected memory leaks! 问题(转)

    原文转自 http://blog.csdn.net/u011430225/article/details/47840647 我的环境是: XP SP2.VS2003 最近在一个项目中, 程序退出后都出 ...

  5. [已解决]ValueError: row index was 65536, not allowed by .xls format

    报错: ValueError: row index was 65536, not allowed by .xls format 解决方案: xlrd和xlwt处理的是xls文件,单个sheet最大行数 ...

  6. 吴裕雄--天生自然TensorFlow高层封装:解决ValueError: Invalid backend. Missing required entry : placeholder

    找到对应的keras配置文件keras.json 将里面的内容修改为以下就可以了

  7. cmd 【已解决】windows连接手机,运行adb devices提示“unauthorized”

    报错截图如下: 问题原因:电脑连接手机.手机未授权 解决方式: 设置----开发者选项-----打开USB调试,出现如下弹框,点击"确定"即可解决问题.

  8. Tensorflow2对GPU内存的分配策略

    一.问题源起 从以下的异常堆栈可以看到是BLAS程序集初始化失败,可以看到是执行MatMul的时候发生的异常,基本可以断定可能数据集太大导致memory不够用了. 2021-08-10 16:38:0 ...

  9. 解决TensorFlow程序无限制占用GPU

    今天遇到一个奇怪的现象,使用tensorflow-gpu的时候,出现内存超额~~如果我训练什么大型数据也就算了,关键我就写了一个y=W*x.......显示如下图所示: 程序如下: import te ...

  10. 【Udacity并行计算课程笔记】- Lesson 3 Fundamental GPU Algorithms (Reduce, Scan, Histogram)

    本周主要内容如下: 如何分析GPU算法的速度和效率 ​​3个新的基本算法:归约.扫描和直方图(Reduce.Scan.Histogram) 一.评估标准 首先介绍用于评估GPU计算的两个标准: ste ...

随机推荐

  1. work09

    day10作业: 第一题: 定义一个接口Animal,包含: 1.抽象方法: run() 2.默认方法: breathe(),输出"动物会呼吸",同时调用静态方法 eat(),私有 ...

  2. work08

    day09作业: 必做题:========================================第一题: 1.定义一个Animal类,包含如下行为: eat() 打印"要吃饭&qu ...

  3. Pycharm import faker 和 colorlog提示“No module name faker/colorlog”

    问题: Pycharm import faker / colorlog,下划线爆红,提示"No module name faker/colorlog" 排查,检查pycharm 该 ...

  4. SpringBoot动态数据源配置

    SpringBoot动态数据源配置 序:数据源动态切换流程图如下: 1:pom.xml文件依赖声明 <dependency>   <groupId>org.springfram ...

  5. vim 中代码的折叠和打开

    # vim 中代码的折叠和打开 reference: vim中代码的折叠和打开(有删改) https://www.cnblogs.com/xuxm2007/archive/2011/11/10/224 ...

  6. 嵌入式必读!瑞芯微RK3568J/RK3568B2开发板规格书

    评估板简介 创龙科技TL3568-EVM是一款基于瑞芯微RK3568J/RK3568B2处理器设计的四核ARM Cortex-A55国产工业评估板,每核主频高达1.8GHz/2.0GHz,由核心板和评 ...

  7. Log4Net配置详解及输出自定义消息类示例

    1.简单使用实例 1.1 添加log4net.dll的引用.   在NuGet程序包中搜索log4net并添加,此次我所用版本为2.0.17.如下图: 1.2 添加配置文件   右键项目,添加新建项, ...

  8. c 语言学习第六天

    数组 语法: 类型 数组名[元素个数]; int a[6]; // 4*6 字节 char b[24]; // 1*24 字节 double c[2]; // 2*8 字节 访问数组中的元素 语法: ...

  9. Java-用户登录验证案例

    用户登录验证 1.案例需求: 1.访问带有验证码的登录页面login.jsp 2.用户输入用户名,密码以及验证码 * 如果用户名和密码输入有误,跳转登录页面,提示:用户名或密码错误 * 如果验证码输入 ...

  10. VirtualBox中Ubuntu 22.04 Server支持kvm

    kvm简介 KVM 是 Kernel-based Virtual Machine 的缩写,是一种用于虚拟化的开源硬件虚拟化技术. 使用 Linux 内核的虚拟化模块,将物理服务器划分为多个虚拟机. K ...