保姆级教程:深度学习环境配置指南!(Windows、Mac、Ubuntu全讲解)
小白学视觉
共 9264字,需浏览 19分钟
·
2022-04-12 22:24
点击上方“小白学视觉”,选择加"星标"或“置顶”
重磅干货,第一时间送达
一、Windows系统深度学习环境配置
1.1 打开Anaconda Prompt
![](https://filescdn.proginn.com/a2883b3843633605d33d1b4958302794/583f5c03f228657e54b14bb6c01c1d7c.webp)
![](https://filescdn.proginn.com/dddbcf54144413da293bf77b03a509b9/8f7fe7147a300e921dbe4c77f492acd6.webp)
![](https://filescdn.proginn.com/50dc6cd13104669bc683d90115d78e8f/a2efa1fd88200da92cb0ca3998e2642f.webp)
![](https://filescdn.proginn.com/69e8672773b202d1a9bb8a1fc4e5e9ef/ab9143c872078bcafffa2abec8c4488e.webp)
from future import print_function
import torch
x = torch.rand(5, 3)
print(x)
![](https://filescdn.proginn.com/4568731329478bac6a6287f28f45be2e/93294f3fc5309efa53b741158368222c.webp)
import torch
torch.cuda.is_available()
用下面命令创建虚拟环境报错
conda create -n py37_torch131 python=3.7
![](https://filescdn.proginn.com/e2075870953378ef6d223863e73f0113/40f6cf03ac7cae282cebcfa5a05aed4e.webp)
PackagesNotFoundError: The following packages are not available from current channels
![](https://filescdn.proginn.com/665dcbfe2583d28416ae2bfd427d629a/45b98ad9f825d0c020739580ddf34377.webp)
conda下载太慢问题
cuda安装
pytorch安装
conda install pytorch torchvision cudatoolkit=10.0 -c pytorch
jupyter note如何进入虚拟环境
python -m ipykernel install --user --name py27-caffe-notebook
jupyter kernelspec list
jupyter kernelspec remove kernelname
python -m ipykernel install --name kernelname
conda安装一半总失败
conda install --offline 包名
二、Mac深度学习环境配置
2.1 安装Anaconda
![](https://filescdn.proginn.com/4f3f9bd783e3d0b456194edabb1fbb28/cc67b869038d4f007141972116128d2f.webp)
2.2 确认下载情况
![](https://filescdn.proginn.com/5d58b1777cd8d813bc6a693284ea2d55/de647af6be2f230d09fae6f907a26f9e.webp)
python --version
jupyter notebook
![](https://filescdn.proginn.com/176633c7c8bb6041af1170595552719a/6dea55f86caa5c6bc27d302dcf44dce7.webp)
![](https://filescdn.proginn.com/48409cd4bc714609bff1fd034aa24302/900a9c6355621c49dd9fb63cd49f8523.webp)
2.2.1 常见问题
cd ~
cat .bash_profile
export PATH= ...
2.3 虚拟环境和包的下载
![](https://filescdn.proginn.com/64c46811b669500c852b8b0520b5fa8c/2376a13334a9c807cfe25de620c123db.webp)
![](https://filescdn.proginn.com/230e32101b61c62fa3058ff8defb9767/27e66139b7011c70e0ead463e81bf984.webp)
![](https://filescdn.proginn.com/dc3841f9f094a43e2026d91bd933fa38/c216be4a8ac346fb6078a2c456df83b2.webp)
![](https://filescdn.proginn.com/e50e76a1b8afc18f46c4e852b765038a/72df5d0ed9b1a1dfec77f941f31273da.webp)
conda create --name env_name
conda env list
conda activate env_name
conda deactivate
![](https://filescdn.proginn.com/9e8c610a43da08d12297e945dc245841/21195a4836adccd6fcda2e94dae0b209.webp)
conda install pytorch torchvision -c pytorch
python3
import torch
import torchvision
print(torch.__version__)
print(torchvision.__version__)
![](https://filescdn.proginn.com/5fd5ed88a321223dacf27f58f675a354/51159be9970094a645dc53d0e4e99050.webp)
quit()
三、Ubuntu深度学习环境配置
![](https://filescdn.proginn.com/2265f920f085d0cbd82a7563062f560e/df55bb10a1a4dd91fe612016627a408e.webp)
![](https://filescdn.proginn.com/4be3247ceac4058353fca04f2ab935d2/1fc4f1928eaaf6603f7d5cda14b5be69.webp)
![](https://filescdn.proginn.com/40b52352eaf23df0c14b4e7ef23d665e/8fae61caedb326b5c7b5105eea2d98ea.webp)
![](https://filescdn.proginn.com/1a59d01df781fe621c61efeee09563ab/ffeacf02ed82a5c4ed6eb130afed0bbf.webp)
![](https://filescdn.proginn.com/029b44b93f44a9ebc431223214759b81/c0ef9700c53a12b1230e8c260b113f03.webp)
![](https://filescdn.proginn.com/d4f7d12c3b33e8885a21b21248583cd8/90dd84d786c545acf70902d7e2e07eb2.webp)
# 区分anaconda python与系统内置python
alias python3="/usr/bin/python3.5"
alias python2="/usr/bin/python2.7"
. /home/cxd/anaconda3/etc/profile.d/conda.sh
![](https://filescdn.proginn.com/70558c5d30c42598b6bdbfeaf236b996/2ebf0eeb410e34c50a0d766e5c8325a0.webp)
![](https://filescdn.proginn.com/96ef9a086551ee026fb2928867a9454b/b7dd2441b7edf18a47d41a6dc9ce43ac.webp)
![](https://filescdn.proginn.com/4f5c9a4d38221b0611e35a401fba0197/bce0dc124154e5a2c62424534d42f23a.webp)
![](https://filescdn.proginn.com/c4bb47e99a6fc2f3b3d845a7e73d4feb/a1f45fb65996412a90d6ad718ba52e4e.webp)
![](https://filescdn.proginn.com/bd45ff4e6f50faf4015ce771ed5d219d/d81402cb4788692c3be08c053b6d65ce.webp)
![](https://filescdn.proginn.com/e5da71af42906dd5c22771d9f8452243/09aecd40445352cc0d57c919f1286cb6.webp)
![](https://filescdn.proginn.com/b639f8d2fd568fa94b34f034389f8f43/7d7f358e01f68558010220cd6f3ea3cb.webp)
![](https://filescdn.proginn.com/9af7c6cc26b73ffcfa3385df634bd468/5013265db6ad079a640077878c9c672b.webp)
3.2 pytorch cpu版本安装
![](https://filescdn.proginn.com/242e46929f3b4fae040e5923ae2fc272/a3e87559c2f25f5f6a146ac37e201ca7.webp)
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch
conda install pytorch torchvision cpuonly -c pytorch
![](https://filescdn.proginn.com/8443ed770933ac3f575e24d0d2f9daeb/822ddb130cdfc8a46077d5ffe673102e.webp)
import torch
print(torch.__version__)
![](https://filescdn.proginn.com/aec6bce5fcd9aa5d367bfb687f78bf68/5e816ee78a633144ad3c88986b86c500.webp)
3.3 pytorch-gpu安装
3.3.1 GPU驱动安装
检测显卡类型
== /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0 ==
modalias : pci:v000010DEd00001C8Dsv00001028sd0000086Fbc03sc02i00
vendor : NVIDIA Corporation
model : GP107M [GeForce GTX 1050 Mobile]
driver : nvidia-driver-390 - distro non-free
driver : nvidia-driver-435 - distro non-free
driver : nvidia-driver-440 - distro non-free recommended
driver : xserver-xorg-video-nouveau - distro free builtin
== /sys/devices/pci0000:00/0000:00:14.3 ==
modalias : pci:v00008086d0000A370sv00008086sd000042A4bc02sc80i00
vendor : Intel Corporation
model : Wireless-AC 9560 [Jefferson Peak]
manual_install: True
driver : backport-iwlwifi-dkms - distro free
安装驱动
sudo ubuntu-drivers autoinstall
sudo apt install nvidia-440
3.3.2 安装cuda
Table 1. CUDA Toolkit and Compatible Driver Versions
CUDA Toolkit Linux x86_64 Driver Version Windows x86_64 Driver Version
CUDA 10.2.89 >= 440.33 >= 441.22
CUDA 10.1 (10.1.105 general release, and updates) >= 418.39 >= 418.96
CUDA 10.0.130 >= 410.48 >= 411.31
CUDA 9.2 (9.2.148 Update 1) >= 396.37 >= 398.26
CUDA 9.2 (9.2.88) >= 396.26 >= 397.44
CUDA 9.1 (9.1.85) >= 390.46 >= 391.29
CUDA 9.0 (9.0.76) >= 384.81 >= 385.54
CUDA 8.0 (8.0.61 GA2) >= 375.26 >= 376.51
CUDA 8.0 (8.0.44) >= 367.48 >= 369.30
CUDA 7.5 (7.5.16) >= 352.31 >= 353.66
CUDA 7.0 (7.0.28) >= 346.46 >= 347.62
安装gcc
gcc --version
![](https://filescdn.proginn.com/e6d0be1c48f2d76985b2a01bfe5e0246/9d3f181847391024ad94d38e37a5f436.webp)
![](https://filescdn.proginn.com/c3e74a09e5e1d7f587357f0685ac5e02/ce519f487c269d9bf1bace2e4862502f.webp)
sudo apt-get install gcc-7.0
sudo apt-get install g++-7.0
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 50
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 50
sudo update-alternatives --config gcc
选择 路径 优先级 状态
------------------------------------------------------------
* 0 /usr/bin/gcc-9 50 自动模式
1 /usr/bin/g++-9 50 手动模式
2 /usr/bin/gcc-7 50 手动模式
安装cuda
sudo sh cuda_你的版本_linux.run
配置环境变量
sudo vim ~/.bashrc
export PATH=/usr/local/cuda-10.2/bin${PATH:+:$PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-10.2/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
nvcc -V
3.3.3 安装cudnn
![](https://filescdn.proginn.com/e054eb40bbf7c272191fe092b96e5b41/c1ad4fd0b3e97dab4aedc9fd67dcc083.webp)
sudo cp cuda/include/cudnn.h /usr/local/cuda-10.2/include #解压后的文件夹名字为cuda-10.2
sudo cp cuda/lib64/libcudnn* /usr/local/cuda-10.2/lib64
sudo chmod a+r /usr/local/cuda-10.2/include/cudnn.h /usr/local/cuda-10.2/lib64/libcudnn*
3.3.4 安装pytorch-gpu
![](https://filescdn.proginn.com/2d79b1d1b5e8667fe2b8769777b67283/518b30ed94c9fd605e995d5bb05cfa07.webp)
# 选择自己对应的cuda版本
conda install pytorch torchvision cudatoolkit=10.2
pip install torch torchvision -i https://pypi.mirrors.ustc.edu.cn/simple
小白团队出品:零基础精通语义分割↓↓↓
下载1:OpenCV-Contrib扩展模块中文版教程 在「小白学视觉」公众号后台回复:扩展模块中文教程,即可下载全网第一份OpenCV扩展模块教程中文版,涵盖扩展模块安装、SFM算法、立体视觉、目标跟踪、生物视觉、超分辨率处理等二十多章内容。 下载2:Python视觉实战项目52讲 在「小白学视觉」公众号后台回复:Python视觉实战项目,即可下载包括图像分割、口罩检测、车道线检测、车辆计数、添加眼线、车牌识别、字符识别、情绪检测、文本内容提取、面部识别等31个视觉实战项目,助力快速学校计算机视觉。 下载3:OpenCV实战项目20讲 在「小白学视觉」公众号后台回复:OpenCV实战项目20讲,即可下载含有20个基于OpenCV实现20个实战项目,实现OpenCV学习进阶。 交流群
欢迎加入公众号读者群一起和同行交流,目前有SLAM、三维视觉、传感器、自动驾驶、计算摄影、检测、分割、识别、医学影像、GAN、算法竞赛等微信群(以后会逐渐细分),请扫描下面微信号加群,备注:”昵称+学校/公司+研究方向“,例如:”张三 + 上海交大 + 视觉SLAM“。请按照格式备注,否则不予通过。添加成功后会根据研究方向邀请进入相关微信群。请勿在群内发送广告,否则会请出群,谢谢理解~
评论