从零搭建深度学习环境Tensorflo+PyTorch

安装环境

Anaconda安装

首先安装python环境,推荐Anaconda+jupyter,而不是Pycharm

1.首先下载Anaconda:

image-20221221170834818
image-20221221170834818

https://www.anaconda.com/products/distribution#download-section

2.下载好打开Anaconda Prompt

image-20221221171359765
image-20221221171359765

3.配置一下镜像源,添加阿里镜像

conda config --set show_channel_urls yes

然后就在C:\Users\用户名 这里找到.condarc

记事本打开修改为:

channels:
  - defaults
show_channel_urls: true
default_channels:
  - http://mirrors.aliyun.com/anaconda/pkgs/main
  - http://mirrors.aliyun.com/anaconda/pkgs/r
  - http://mirrors.aliyun.com/anaconda/pkgs/msys2
custom_channels:
  conda-forge: http://mirrors.aliyun.com/anaconda/cloud
  msys2: http://mirrors.aliyun.com/anaconda/cloud
  bioconda: http://mirrors.aliyun.com/anaconda/cloud
  menpo: http://mirrors.aliyun.com/anaconda/cloud
  pytorch: http://mirrors.aliyun.com/anaconda/cloud
  simpleitk: http://mirrors.aliyun.com/anaconda/cloud
 

再清除索引缓存:

conda clean -i 

创建一个深度学习的环境(避免不同的包相互冲突,我目前设置了四个环境:geemap,绘图,地理库和深度学习)

# 1.查看有哪些可安装的python版本
conda search --full-name python
# 2.创建新环境DL
conda create --name DL python=3.8.12
# 如果想删除环境采用以下操作
# conda remove -n DL --all
# 激活环境
conda activate DL

如果不支持GPU环境,就可以直接安装Tensorflow了

pip install tensorflow
import tensorflow as tf

检测GPU环境

win下面搜索设备管理器

image-20230327105342675
image-20230327105342675

在显示适配器下面看到自己的显卡:

image-20230327105427088
image-20230327105427088

接下来查看电脑显卡型号是否支持CUDN,查看链接:https://developer.nvidia.com/zh-cn/cuda-gpus

可以看到我的显卡是在支持列表里的

image-20230327105542435
image-20230327105542435

接下来安装显卡驱动,官方驱动链接:https://www.nvidia.com/Download/index.aspx?lang=en-us

在这里找到你显卡的型号并Search:

image-20230327105948268
image-20230327105948268

之后会跳转到一个界面,点击下载即可:

image-20230327110100785
image-20230327110100785

下载后双击安装,根据程序默认一路点下去就好:

image-20230327110628110
image-20230327110628110

安装完后可以重启一下电脑。

版本选择

CUDA的版本依赖于显卡的驱动程序版本,首先查看GPU驱动版本,win搜索NVIDIA控制面板

image-20230327111217490
image-20230327111217490

可以看到我的版本号是531.41

官方参考链接:https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html

image-20230327111401973
image-20230327111401973

我的驱动版本是531.41,因此可以安装CUDA 12.1

cuDNN、TensorFlow 版本选择

官方参考链接:https://tensorflow.google.cn/install/source_windows#gpu

image-20230327112004633
image-20230327112004633

到官方查看。对应CUDA 12,向下兼容发现可以安装cuDNN 8和 tensorflow_gpu-2.6.0

安装CUDA、cuDNN

CUDA

下载:https://developer.nvidia.com/cuda-toolkit-archive

image-20230327112245488
image-20230327112245488

这里我选择exe(local)本地安装

下载到本地后双击exe文件安装:

image-20230327113037858
image-20230327113037858

安装完成后在环境变量中检查:

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1\lib\x64

我的已经自动添加了,若没有这两个路径则手动添加:

image-20230327114412157
image-20230327114412157

在CMD中输入:

nvcc -V

有消息提示则安装成功

image-20230327114535959
image-20230327114535959

cuDNN

cuDNN下载需要进行一个漫长的登陆

https://developer.nvidia.com/zh-cn/cudnn

image-20230327115609172
image-20230327115609172

下载之后解压有以下内容:

image-20230327115818898
image-20230327115818898

都复制到(除了LICENSE)CUDA的安装目录(C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1)下。

安装TensorFlow

最终我选择的环境(可以参考)

python3.8.12
cuda_11.6.1_511.6
cudnn_8.3.2.44
tensorflow-gpu 2.7.0
keras 2.7.0
pip install tensorflow-gpu==2.7.0
image-20230327120142306
image-20230327120142306

测试:

import tensorflow as tf
print(tf.config.list_physical_devices('GPU'))

但是我这里报错了,先提示我降级protobuf包:

pip install protobuf==3.20.*

这里就成功了:

image-20230327185122580
image-20230327185122580

成功会显示下面的代码,否则只会显示[]

[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

若报错:

2023-03-27 12:06:54.443860: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2023-03-27 12:06:54.444235: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.

提示缺少ddl,把相应的ddl复制到

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1\bin

安装PyTorch

  • 除了pytorch,还有一个很好用的包是torchvision,用于图像相关的功能
  • torch和torchvision的版本对应如下表:(https://github.com/pytorch/vision实时更新)
torchtorchvisionpython
main / nightlymain / nightly>=3.8, <=3.11
2.0.00.15.1>=3.8, <=3.11
1.13.00.14.0>=3.7.2, <=3.10
1.12.00.13.0>=3.7, <=3.10
1.11.00.12.0>=3.7, <=3.10
1.10.20.11.3>=3.6, <=3.9
1.10.10.11.2>=3.6, <=3.9
1.10.00.11.1>=3.6, <=3.9
1.9.10.10.1>=3.6, <=3.9
1.9.00.10.0>=3.6, <=3.9
1.8.20.9.2>=3.6, <=3.9
1.8.10.9.1>=3.6, <=3.9
1.8.00.9.0>=3.6, <=3.9
1.7.10.8.2>=3.6, <=3.9
1.7.00.8.1>=3.6, <=3.8
1.7.00.8.0>=3.6, <=3.8
1.6.00.7.0>=3.6, <=3.8
1.5.10.6.1>=3.5, <=3.8
1.5.00.6.0>=3.5, <=3.8
1.4.00.5.0==2.7, >=3.5, <=3.8
1.3.10.4.2==2.7, >=3.5, <=3.7
1.3.00.4.1==2.7, >=3.5, <=3.7
1.2.00.4.0==2.7, >=3.5, <=3.7
1.1.00.3.0==2.7, >=3.5, <=3.7
<=1.0.10.2.2==2.7, >=3.5, <=3.7

首先确定pytorch的版本:

https://download.pytorch.org/whl/torch_stable.html

由于我是:

  • python3.8.12
  • cuda_11.6.1_511.6

故选择:

  • cp38
  • cu116
image-20230327185720504
image-20230327185720504

查表得torch 1.12.0 对应的 torchvision 0.13.0

image-20230327185844075
image-20230327185844075

建议不同的深度学习框架,换不同的envi

conda create --name torch python=3.8.12
conda activate torch

安装torch和torchvision

pip install torch-1.12.0+cu116-cp38-cp38-win_amd64.whl
pip install torchvision-0.13.0+cu116-cp38-cp38-win_amd64.whl

测试结果

TensorFlow

比较在CPU和GPU上的运行时间

import tensorflow as tf
import timeit


def cpu_run():
    with tf.device('/cpu:0'):
        cpu_a = tf.random.normal([10000, 1000])
        cpu_b = tf.random.normal([1000, 2000])
        c = tf.matmul(cpu_a, cpu_b)
    return c


def gpu_run():
    with tf.device('/gpu:0'):
        gpu_a = tf.random.normal([10000, 1000])
        gpu_b = tf.random.normal([1000, 2000])
        c = tf.matmul(gpu_a, gpu_b)
    return c


cpu_time = timeit.timeit(cpu_run, number=10)
gpu_time = timeit.timeit(gpu_run, number=10)
print("cpu:", cpu_time, "  gpu:", gpu_time)

image-20230327194838767
image-20230327194838767

可以看到差异明显

PyTorch

import torch
flag = torch.cuda.is_available()
print(flag)

ngpu= 1
# Decide which device we want to run on
device = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu > 0) else "cpu")
print(device)
print(torch.cuda.get_device_name(0))
print(torch.rand(3,3).cuda()) 

显示已经具有GPU环境

True
cuda:0
NVIDIA GeForce RTX 3080 Laptop GPU
tensor([[0.2823, 0.0544, 0.1159],
        [0.8368, 0.2139, 0.7360],
        [0.7613, 0.5881, 0.5153]], device='cuda:0')

教程推荐

深度学习基础

image-20230404133816701
image-20230404133816701

TensorFlow

image-20230327210918332
image-20230327210918332

Pytorch

image-20230327211311003
image-20230327211311003

都是入门的好书,提供了丰富的案例,对初学者比较友好。

免费分享PDF版本,后台回复【深度学习入门】

image-20230404135856203
image-20230404135856203

本文由 mdnice 多平台发布

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

地学万事屋

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

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

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

打赏作者

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

抵扣说明:

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

余额充值