搭建Pytorch 和 Tensorflow v1 兼容的环境

部署运行你感兴趣的模型镜像

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

然后是显卡相关的配置, cudatoolkitcudnn. 前面这个是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.jianshu.com/p/ca756da23619

最后把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

您可能感兴趣的与本文相关的镜像

PyTorch 2.5

PyTorch 2.5

PyTorch
Cuda

PyTorch 是一个开源的 Python 机器学习库,基于 Torch 库,底层由 C++ 实现,应用于人工智能领域,如计算机视觉和自然语言处理

<think>嗯,用户现在问的是如何为YOLOv1搭建虚拟环境。不过根据之前的对话,YOLOv11并不存在,用户可能混淆了版本号。需要先确认用户的需求,到底是YOLOv1还是其他版本。但用户明确提到YOLOv1,所以应该按照这个来处理。 首先,用户可能不太清楚YOLO各个版本的区别,尤其是早期的v1。YOLOv1是2016年提出的,基于Darknet框架,使用CCUDA,可能没有官方的PyTorch实现。所以搭建环境可能需要用到较旧的Python版本依赖库。 接下来,用户提到了虚拟环境配置,引用了之前关于YOLOv5、v8的环境搭建问题,但YOLOv1环境可能不同。需要考虑到YOLOv1环境依赖,比如Python 2.7或3.5,旧版的PyTorch或Darknet,以及CUDA版本是否兼容。 用户提供的引用中提到了使用conda创建虚拟环境,配置清华源,解决安装失败的问题。这些步骤可能在YOLOv1环境搭建中同样适用,但需要调整版本号。例如,用户可能需要安装PyTorch 0.3或更低版本,但PyTorch官方可能不再支持这些旧版本,这时候需要从源码编译或者寻找第三方源。 另外,用户可能遇到依赖冲突,比如numpy版本过高,导致与旧版PyTorch兼容。这时候需要指定特定版本的库。例如,使用pip install numpy==1.16.0来安装兼容的版本。 还需要考虑用户的操作系统,CUDA驱动是否支持旧版本的CUDA工具包。例如,YOLOv1可能需要CUDA 8.0,但新的显卡驱动可能不兼容,这时候可能需要使用Docker或者虚拟机来模拟旧环境。 最后,用户可能没有YOLOv1的官方代码,需要指导他们从正确的仓库获取源码,比如Joseph Redmon的GitHub,并确保代码与配置的环境兼容。如果用户遇到编译错误,可能需要修改代码以适应新的系统环境或库版本。 总结来说,回答需要分步骤指导用户创建虚拟环境,安装正确版本的PythonPyTorch/Darknet、CUDA,处理依赖关系,并解决常见错误。同时要提醒用户注意版本兼容性问题,建议使用Docker或虚拟机作为备选方案,并提供获取YOLOv1源码的途径。</think>根据您的需求引用内容中的有效信息,以下是针对**YOLOv1**的虚拟环境搭建指南。由于YOLOv1是2016年发布的初代模型,其环境配置与现代版本差异较大,需特别注意依赖版本控制: --- ### 一、环境搭建步骤(Python虚拟环境) #### 1. 创建独立虚拟环境 ```bash conda create -n yolov1 python=3.6 # YOLOv1建议使用Python 3.6或更低版本 conda activate yolov1 ``` #### 2. 安装基础依赖 ```bash pip install numpy==1.16.0 # 避免新版numpy兼容性问题 pip install opencv-python==3.4.2.17 # 兼容旧版图像处理库 pip install matplotlib==2.2.3 ``` #### 3. 框架选择与安装 YOLOv1原始实现基于**Darknet**,但社区有PyTorch复现代码。若使用PyTorch版本: ```bash # PyTorch需选择<=0.3.1版本(CUDA 8.0兼容) conda install pytorch=0.3.1 torchvision cuda80 -c pytorch ``` #### 4. 环境验证 ```python import torch print(torch.__version__) # 预期输出0.3.1 ``` --- ### 二、关键兼容性问题解决(引用[4]经验) 1. **CUDA版本匹配**: - YOLOv1原始代码仅支持CUDA 8.0/9.0 - 若使用清华源安装旧版工具包: ```bash conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/ conda install cudatoolkit=8.0 ``` 2. **依赖冲突处理**: ```bash pip install --no-deps tensorflow-gpu==1.4.0 # 若需混合框架 ``` --- ### 三、YOLOv1代码获取建议 1. **官方原始仓库**(已存档): ```bash git clone https://github.com/pjreddie/darknet.git cd darknet && git checkout tags/v1.0 ``` 2. **社区维护的PyTorch复现**: ```bash git clone https://github.com/zzzheng/pytorch-yolov1.git ``` --- ### 四、常见报错解决方案(引用[3]延伸) 1. **Conda环境创建失败**: - 清理缓存:`conda clean --all` - 手动指定通道优先级:`conda config --set channel_priority strict` 2. **CUDA内核编译错误**: ```bash # 修改Makefile中的ARCH参数 ARCH= -gencode arch=compute_30,code=sm_30 # 适配老显卡 ``` --- ### 五、虚拟环境管理建议(引用[2]优化) 1. **路径绑定技巧**: - 记录环境路径:`conda env list` 查看`yolov1`的存储位置 - PyCharm中设置解释器路径:`~/anaconda3/envs/yolov1/bin/python` 2. **环境导出与迁移**: ```bash conda env export > yolov1_env.yaml # 导出环境配置 conda env create -f yolov1_env.yaml # 在新机器重建 ``` ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

daimashiren

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值