Pytorch 和 Tensorflow v1 兼容的环境搭建方法
作者:daimashiren 发布时间:2022-04-05 12:19:06
Github 上很多大牛的代码都是Tensorflow v1 写的,比较新的文章则喜欢用Pytorch,这导致我们复现实验或者对比实验的时候需要花费大量的时间在搭建不同的环境上。这篇文章是我经过反复实践总结出来的环境配置教程,亲测有效!
首先最基本的Python 环境配置如下:
conda create -n py37 python=3.7
python版本不要设置得太高也不要太低,3.6~3.7最佳,适用绝大部分代码库。(Tensorflow v1 最高支持的python 版本也只有3.7)
然后是Pytorch 环境 (因为最简单省力,哈哈哈)
# ROCM 5.1.1 (Linux only)
pip install torch==1.12.1+rocm5.1.1 torchvision==0.13.1+rocm5.1.1 torchaudio==0.12.1 --extra-index-url https://download.pytorch.org/whl/rocm5.1.1
# CUDA 11.6
pip install torch==1.12.1+cu116 torchvision==0.13.1+cu116 torchaudio==0.12.1 --extra-index-url https://download.pytorch.org/whl/cu116
# CUDA 11.3
pip install torch==1.12.1+cu113 torchvision==0.13.1+cu113 torchaudio==0.12.1 --extra-index-url https://download.pytorch.org/whl/cu113
# CUDA 10.2
pip install torch==1.12.1+cu102 torchvision==0.13.1+cu102 torchaudio==0.12.1 --extra-index-url https://download.pytorch.org/whl/cu102
# CPU only
pip install torch==1.12.1+cpu torchvision==0.13.1+cpu torchaudio==0.12.1 --extra-index-url https://download.pytorch.org/whl/cpu
推荐使用pip 安装,用conda 安装的有时候会出现torch 识别不到GPU 的问题....
官网教程链接
Previous PyTorch Versions | PyTorch
然后是显卡相关的配置, cudatoolkit 和 cudnn. 前面这个是pytorch 环境必须具备的包,后面这个则是tensorflow 要使用GPU所必需的。前面安装完pytorch 其实已经装好了一个cudatoolkit,我的电脑是Cuda 10.2 ,所以现在环境中已经有了一个cudatookit=10.2的包了,但是Tensorflow v1 最高只支持到 Cuda 10,所以得降低cudatoolkit的版本到10.0 (Cuda 环境是向下兼容的,我的Cuda 环境是10.2 但是cudatoolkit=10.0 也一样能用,这是Tensorflow v1 最高支持的版本,只能妥协......)
conda install cudatoolkit=10.0
然后装cudnn
conda install cudnn=7.6.5=cuda10.0_0
亦可使用如下命令搜索你所要的cudnn版本
conda search cudnn
如果conda 下载太慢请切换国内源
https://www.jb51.net/article/199913.htm
最后把Tensorflow v1装上
pip install tensorflow-gpu==1.15.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
推荐的Tensorflow v1 的版本是1.15.0 和1.14.0,其他版本尚未测试。
最后分别测试Pytorch 和Tensorflow 能否使用GPU如下:
import torch
print(torch.cuda.is_available()
Pytorch 检测GPU的方法相信大家都知道,不再赘述。Tensorflow v1 检测GPU的方法如下:
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
如果输出结果为:
TypeError: Descriptors cannot not be created directly.
If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0.
If you cannot immediately regenerate your protos, some other possible workarounds are:
1. Downgrade the protobuf package to 3.20.x or lower.
2. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).
则降低protobuf 的版本
pip install protobuf==3.19.6 -i https://pypi.tuna.tsinghua.edu.cn/simple
正确的输出为:
2022-10-30 21:46:59.982971: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2022-10-30 21:47:00.006072: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 3699850000 Hz
2022-10-30 21:47:00.006792: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x55d1633f2750 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2022-10-30 21:47:00.006808: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
2022-10-30 21:47:00.008473: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcuda.so.1
2022-10-30 21:47:00.105474: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-10-30 21:47:00.105762: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x55d1635c3f60 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
2022-10-30 21:47:00.105784: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): NVIDIA GeForce GTX 1080 Ti, Compute Capability 6.1
2022-10-30 21:47:00.105990: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-10-30 21:47:00.106166: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties:
name: NVIDIA GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.582
pciBusID: 0000:01:00.0
2022-10-30 21:47:00.106369: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.0
2022-10-30 21:47:00.107666: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10.0
2022-10-30 21:47:00.108687: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcufft.so.10.0
2022-10-30 21:47:00.108929: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcurand.so.10.0
2022-10-30 21:47:00.111721: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusolver.so.10.0
2022-10-30 21:47:00.112861: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusparse.so.10.0
2022-10-30 21:47:00.116688: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudnn.so.7
2022-10-30 21:47:00.116826: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-10-30 21:47:00.117018: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-10-30 21:47:00.117127: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
2022-10-30 21:47:00.117170: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.0
2022-10-30 21:47:00.117421: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1159] Device interconnect StreamExecutor with strength 1 edge matrix:
2022-10-30 21:47:00.117435: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1165] 0
2022-10-30 21:47:00.117446: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1178] 0: N
2022-10-30 21:47:00.117529: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-10-30 21:47:00.117678: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:983] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-10-30 21:47:00.117813: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1304] Created TensorFlow device (/device:GPU:0 with 10361 MB memory) -> physical GPU (device: 0, name: NVIDIA GeForce GTX 1080 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1)
[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 10409023728072267246
, name: "/device:XLA_CPU:0"
device_type: "XLA_CPU"
memory_limit: 17179869184
locality {
}
incarnation: 7385902535139826165
physical_device_desc: "device: XLA_CPU device"
, name: "/device:XLA_GPU:0"
device_type: "XLA_GPU"
memory_limit: 17179869184
locality {
}
incarnation: 7109357658802926795
physical_device_desc: "device: XLA_GPU device"
, name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 10864479437
locality {
bus_id: 1
links {
}
}
incarnation: 6537278509263123219
physical_device_desc: "device: 0, name: NVIDIA GeForce GTX 1080 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1"
]
最关键的地方是你得看到你的GPU 型号,我的是 GTX 1080Ti,检测成功!
以上环境适用绝大多数深度学习模型,希望能帮到你!喜欢请点赞!
完结!撒花!
参考文献
Could not load dynamic library 'libcudart.so.10.0' - 知乎
https://medium.com/analytics-vidhya/solution-to-tensorflow-2-not-using-gpu-119fb3e04daa
How to tell if tensorflow is using gpu acceleration from inside python shell? - Stack Overflow
来源:https://blog.csdn.net/daimashiren/article/details/127605221
猜你喜欢
- 本文实例讲述了Python数学形态学。分享给大家供大家参考,具体如下:一 原始随机图像1、代码import numpy as npimpor
- 在Python中可以通过几种方法调用C++的函数,这里介绍使用ctypes的方法。ctypes是Python自带的一个模块,能够直接载入C+
- 本文实例为大家分享了Python实现信息管理系统的具体代码,供大家参考,具体内容如下"""项目名称 =
- 序言这次玩次狠得。除了编译器使用yum安装,其他全部手动编译。哼~看似就Nginx、PHP、MySql三个东东,但是它们太尼玛依赖别人了。没
- 大家都知道一些论坛的标题有高亮显示功能,在这里我不讨论也不研究论坛普遍的实现方法,下面是我的实现方法:实现思路:把要高亮显示的标题加上特定标
- 如何用php实现APP消息推送现在有很多的消息推送厂商,比如阿里云的消息推送,极光推送,融云的消息推送。他们的原理都是把sdk内置在app里
- 没什么说的,就是生成随机数而已!!相关文章推荐:8个asp生成随机字符的函数<% Function gen_key(digi
- 你已经在上面取出w打头记录的例子中看到了LIKE的用法。LIKE判定词是一个非常有用的符号。不过,在很多情况下用了它可能会带给你太多的数据,
- 首先你得引入bootstrap与jquery推荐一个CDN:http://cdn.gbtags.com/index.html然后就是开始编写
- asp使用fso读取驱动器信息:<%vv=drive()response.write vv funct
- setTimeoutsetTimeout 语法例子用 setTimeout 来执行 function不断重复执行的 setTimeout设定
- 代码如下:--建立数据表createtable TestData ( ID int identity(1,1) primary key, D
- 随着网络的普及,基于网络的应用也越来越多。网络数据库就是其中之一。通过一台或几台服务器可以为很多客户提供服务,这种方式给人们带来了很多方便,
- 我见朋友可以把数据库的记录显示到列表框里去,挺实用,也想做一个。怎么做啊?这简单,代码和说明如下:dblist.asp<html>
- 本文的OCR当然不是自己从头开发的,是基于百度智能云提供的API(我感觉是百度在中国的人工智能领域值得称赞的一大贡献),其提供的API完全可
- jupyter因为其在python框架下具有一个较为人性化的设计,从而在数据分析,python教学,以及python的快速学习中大放光彩.j
- # coding:utf-8import urllibimport urllib2import cookielibfrom bs4 impo
- 多进程通信方法好多,不一而数。刚才试python封装好嘅多进程通信模块 multiprocessing.connection。简单测试咗一下
- 序言:刚结束公司的三个月试用期,意味着我即将正式步入社会成为广大从事IT行业的一员。作为一个编程小白,无论从技术层面还是知识层面都是比较薄弱
- 搞一个图形化界面还是挺酷的,是吧 安装库什么的应该不用多说了吧。。一般来说会让你把 designer.exe(编辑图形化界面的东西,跟vb差