【解决】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/")
关键的设置为:
==================================================
posted on 2022-05-22 09:59 Angry_Panda 阅读(978) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
2021-05-22 ubuntu18.04系统环境下使用vs code安装pylint检查python的代码错误
2021-05-22 记录一次实验室显卡服务器崩溃事件(Ubuntu18.04 server系统,4块NVIDIA的特斯拉显卡)
2021-05-22 【转载】 nvidia-smi - Persistence-M (Persistence Mode)
2021-05-22 【转载】 arch linux下nvidia 驱动死机问题
2021-05-22 ubuntu18.04环境下如何为 vs code 实现ftp功能
2020-05-22 《Python数据可视化之matplotlib实践》 源码 第二篇 精进 第五章