Ubuntu16+CUDA8+Caffe+Tensorflow+Pytorch+cuDNN6+Matlab2015b+Opencv3(一篇就够)

 本文旨在总结深度学习基础环境搭建中,一些基础工具软件包安装。因为在工作过程中,搭建环境不是天天遇到,但总是偶尔遇到,工具包安装不可谓不重要;由于每次搭建基础环境都要去找多个的博客,挺麻烦的,所以总结于此,方便自己也方便他人。
 本文将总结适应于Tensorflow,Pytorch,Caffe等深度学习框架通用基础环境搭建。

1 登陆方式

1.1 Ubuntu本机安装
  跳过该步

1.2 Ubuntu登陆另一台Ubuntu
  ssh -X usename@192.168.xx.xxx
  注意加上 -X :支持图像界面显示(当然也要登陆的Ubuntu有图像显示能力)

1.3 Windows登陆另一台Ubuntu
  个人推荐Xshell工具远程登陆
  下载XshellPlus6和Xmanager6两个工具包安装即可,会出现3个图标:

在这里插入图片描述

  Xshell是远程登陆工具,Xftp是文件传输工具,Xmanager是图像界面显示工具;有了这3个工具将简单方便得多(若有用Xshell5的童鞋,建议更新,Xshell6比5好用多了,更加稳定,更加方便)

2 基础工具

2.1 Python
 大部分ubuntu初始化都是有python的;若不存在,建议安装2.7 和 3.5两个版本,因为我们遇到的dl_demo可能两个版本都有

//python2.7
sudo apt-get install python2.7 python2.7-dev

//python3.5
sudo apt-get install python3.5 python3.5-dev

//很多pip安装的包都需要libssl和libevent编译环境
 sudo apt-get install build-essential libssl-dev libevent-dev libjpeg-dev libxml2-dev libxslt-dev
 
//安装pip
sudo apt-get install python-pip
sudo apt-get install python3-pip

 参考链接:ubuntu下Python的安装和使用

2.2 Cmake

sudo apt-get install git cmake build-essential
//cmake-gui  有界面 
sudo apt-get install cmake-qt-gui 

 安装好可以测试

cmake-gui

 看是否有cmake的界面出现(前提是环境要支持界面显示)

在这里插入图片描述

3 部分依赖包

值得注意:如果读者不用caffe,大可直接使用anaconda,来安装环境,简单快捷;但本人遇到过anaconda与caffe编译在protobuf上的冲突,折腾了3天没能解决,最终还是干掉了anaconda;读者可根据自己需求选择。

//这里大部分依赖包是caffe需要的
sudo apt-get install  libleveldb-dev libsnappy-dev  libhdf5-serial-dev libprotobuf-dev protobuf-compiler

sudo apt-get install --no-install-recommends libboost-all-dev

sudo apt-get install libopenblas-dev liblapack-dev libatlas-base-dev

sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev

 注:第一行中大多参考博客中都还有 libopencv-dev,我这里把它干掉了,因为后面会单独安装opencv;个人觉得一个系统中一个opencv版本就够了,以免混乱冲突

4 CUDA

4.1 查看显卡驱动和型号

//驱动
lsmod | grep -i nvidia
//型号
lspci | grep -i vga
lspci | grep -i nvidia

 默认情况下,是有驱动的,但可能不是CUDA需要的,可以用nvidia-smi查看下,若没有则需要安装驱动

4.2 安装驱动
  4.2.1 禁用第3方驱动nouveau(若存在)

lsmod | grep nouveau

 没有输出则跳过,若有则:

//1创建文件(若不存在)
cd /etc/modprobe.d
sudo touch blacklist-nouveau.conf
sudo gedit blacklist-nouveau.conf	

//2输入禁用信息
//文件中输入 并保存退出
blacklist nouveau  
options nouveau modeset=0 

//3更新 并重启ubuntu
sudo update-initramfs -u
reboot

//4再次查看(此时应该没有输出了)
 lsmod | grep nouveau

  4.2.2 安装驱动

//干掉之前的驱动(卸载显卡驱动)
sudo apt-get remove --purge nvidia-*

 到官网查看驱动型号

在这里插入图片描述

 点击搜索:

在这里插入图片描述

 可以下载离线安装,也可一键安装,如下:

//安装(小编的是1080ti 选择384,当然也可以是最新的410)
sudo apt-get install nvidia-384
//一路按住Enter  到100%即可

//验证
nvidia-smi

 出现下面的界面 就对了(若没有 重启试试)

在这里插入图片描述

  4.2.3 安装CUDA
 到CUDA官网下载需要的CUDA;根据自己的环境选择对应版本下载

在这里插入图片描述

  安装CUDA(小编下载的是CUDA8)

sudo sh cuda_8.0.61_375.26_linux.run
//单击回车,一路往下运行,直到提示“是否为NVIDIA安装驱动nvidia-375?”,选择否   因为已经安装好驱动程序了
//其他的全都是默认,需要选择y/n时   输入 y 即可
//不过要记住安装位置,默认是安装在/usr/local/cuda文件夹下

  配置环境变量,运行如下命令打开 ~/.bashrc文件

sudo gedit  ~/.bashrc

//打开文件后在文件末尾添加路径,也就是安装目录,命令如下:
export CUDA_HOME=/usr/local/cuda
export PATH=$PATH:$CUDA_HOME/bin
export LD_LIBRARY_PATH=/usr/local/cuda/lib64

//保存,然后重启电脑(非必需)
reboot

  测试CUDA的例子

cd  /usr/local/cuda-8.0/samples/1_Utilities/deviceQuery
sudo make
./deviceQuery

  如果显示的是关于GPU的信息,则说明安装成功了。

在这里插入图片描述

4.3 cuDNN安装
  如果要使用GPU来对DL网络进行加速,除了安装CUDA以外,cuDNN也需要安装的(也可不安装);据本人测试,大部分DL网络在有cuDNN加速下时可以提升速度的,尤其是在训练中,能够节约时间;但也有少部分DL网络,cuDNN加速不明显;但要注意若没有安装cuDNN,在编译caffe时要关闭cudnn编译选项。
  跟CUDA一样,去NVIDIA的官网下载cuDNN的安装包。不过这次没法直接下载,需要先注册,然后还要做个调查问卷什么的,稍微有点麻烦。我下的是cudnn-8.0-linux-x64-v6.0.tgz压缩包

//解压 
tar -xvf  cudnn-8.0-linux-x64-v6.0.tgz

  文件如下:

在这里插入图片描述

 安装

//安装(就是拷贝)
sudo cp lib64/lib* /usr/local/cuda/lib64/    
sudo cp include/cudnn.h /usr/local/cuda/include/ 

//更新软连接
cd /usr/local/cuda/lib64/  

//先干掉libcudnn.so  libcudnn.so.6
(小心点 根目录下rm的时候  一定要小心,不要干错了)
sudo rm -r  libcudnn.so  libcudnn.so.6  

//重建软连接
sudo ln -sf  libcudnn.so.6.0.21  libcudnn.so.6 
sudo ln -sf libcudnn.so.6 libcudnn.so  

//查看
ll libcudnn*

  输出:

在这里插入图片描述

 安装

//更新
sudo ldconfig  

5 Matlab

下载(密码 4gj3)Linux版本的Matlab,小编下载的是R2015b_Linux

在这里插入图片描述

 1 挂载

sudo mkdir /mnt/temp

//path :是指自己存放matlab安装包的路径
sudo mount -o loop  path/R2015b_glnxa64.iso  /mnt/temp

 2 安装

cd /mnt/temp/
sudo ./install

 根据提示安装

在这里插入图片描述

 输入密钥
在文件夹Matlab 2015b Linux64 Crack下的readme.txt中

1) standalone:
- Install choosing the option "Use a File Installation Key" and supply the following FIK
	09806-07443-53955-64350-21751-41297

 复制到安装界面里

在这里插入图片描述

 默认安装路径:/usr/local/MATLAB/R2015b
 等待安装。。。

在这里插入图片描述

 3 卸载挂载

sudo umount /mnt/temp

 4 激活

cd /usr/local/MATLAB/R2015b/bin/
sudo  ./matlab

 找到Matlab 2015b Linux64 Crack下的license_standalone.lic (这里注意文件夹名最好不要有空格和汉字)

在这里插入图片描述

 若出现licenses写入错误,则需新建licenses文件夹并更改权限:

cd ..
sudo mkdir licenses
sudo chmod 777 licenses

 5 破解

//进入Matlab 2015b Linux64 Crack\R2015b\bin\glnxa64文件夹将libmwservices.so复制到/usr/local/MATLAB/R2015b/bin/glnxa64中:
sudo cp libmwservices.so /usr/local/MATLAB/R2015b/bin/glnxa64/

  6 运行

cd /usr/local/MATLAB/R2015b/bin/
./matlab

  7 软连接
  MATLAB启动程序默认会安装到/usr/local/MATLAB/R2015b/bin目录中,由于系统默认不会将该目录加入PATH环境变量,因此,直接 在终端输入matlab命令将不会启动,解决方法非常简单: 一种是将该路径加入到PATH中,另一种就是在/usr/loca/bin里建一个符号链接。 这里选择后者:

sudo ln -s /usr/local/MATLAB/R2015b/bin/matlab   /usr/local/bin/matlab

  之后在命令行中直接输入matlab即可启动Matlab应用。

  8 权限问题
  安装好后,若发现用sudo matlab 表现正常;而直接matlab启动出现权限问题:

在这里插入图片描述

 可以查看

cd ~/.matlab/
ll

 看到R2015b属于root账户

在这里插入图片描述

 更改所属账户

sudo chown -R $USER: ~/.matlab
ll

 就可以看到属于当前账户了 ;然后正常了

matlab

 9 其他问题
 若在安装过程出现激活错误操作等,先不要着急重新安装,只需要删除与 /usr/local/MATLAB/R2015b下的licenses文件夹,再激活即可

6 Opencv

 Opencv的编译安装比较重要,Python,C/C++等多种语言都会用到Opencv;当然本文只先关注Python和C/C++的需求。
 若只在Python中调用Opencv的朋友,可用pip安装即可(忽略后文Opencv安装)

//python2
sudo pip install opencv_python
//python3
sudo pip3 install opencv_python

 回到正题,事实上我们遇到的有些DL_Demo是需要C/C++支持Opencv的,继续
1 Opencv依赖包

sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev 
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev liblapacke-dev
sudo apt-get install libxvidcore-dev libx264-dev  # 处理视频所需的包
sudo apt-get install libatlas-base-dev gfortran   # 优化opencv功能
sudo apt-get install ffmpeg

 当然上面个别包已经安装过了,如cmake,git等,可跳过

2 Python依赖包

//python2
sudo pip2 install --upgrade pip
sudo pip install numpy scipy matplotlib scikit-image scikit-learn ipython

//python3
sudo pip3 install --upgrade pip
sudo pip3 install numpy scipy matplotlib scikit-image scikit-learn ipython

3 Opencv安装包
  Opencv安装包包括opencv包和contrib包两个,可以提前下载,也可git (本文用的方式1)
 (1)下载链接,选择sources版本,解压下载下来的zip包,也可wget

mkdir opencv34
cd opencv34
wget https://github.com/opencv/opencv/archive/3.4.0.zip
unzip 3.4.0.zip

mkdir contrib
cd contrib
wget -c https://github.com/opencv/opencv_contrib/archive/3.4.0.zip
unzip 3.4.0.zip

 完成后目录结构:

在这里插入图片描述

 (2)git方式

//根据自己喜好创建目录
mkdir opencv 
cd opencv

git clone https://github.com/opencv/opencv.git
cd opencv 
git checkout 3.4.0

cd ..
git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout 3.4.0

4 Opencv预编译

cd opencv-3.4.0
mkdir build  //编译文件存放位置
cd build
cmake-gui

 (1)选择好source路径和build路径,点击Generate按钮,并勾选上Grouped和Advanced(也可不选,只是显示结构而已

在这里插入图片描述

 (2)在第1步后,找到OPENCV组下OPENCV_EXTRA_MODULES_PATH
 添加存放contrib的路径,目录直到modules级

在这里插入图片描述

 (3)不着急Configure,先检测
   a)检测其他关联是否正确,如Matlab,CUDA等一般不会出问题;但尤其要注意看python2和python3是否都正常,正常如下:

在这里插入图片描述

   b)生成栏是否存在Error或个别Warning,若存在可根据提示酌情处理,处理后再次Generate;多次处理后,感觉OK了,下一步

 (4)点击Configure
 没有错误,就可以关闭Cmake-gui了,回到终端,你就发现build下有很多东西了

5 Opencv编译

make -j8

  -j8表示多核编译,若CPU不是多核直接make就可以了,只是编译速度问题;接下来就是30分钟左右的等待时间。。。。

 编译好,opencv_python3和opencv_python2都编译好,如下:

在这里插入图片描述

6 install
 在上述操作后,opencv本地编译好了,但系统还不认识opencv

sudo make install

 大功告成

在这里插入图片描述

7 测试

//python2
python
import cv2
cv2.__version__

//python3
python3
import cv2
cv2.__version__

 OK

在这里插入图片描述

7 Caffe

  从github上git一个caffe工程试试环境

git clone https://github.com/BVLC/caffe.git
cd caffe
cp Makefile.config.example Makefile.config
vim Makefile.config

  //修改 cudnn opencv matlab hdf5

## Refer to http://caffe.berkeleyvision.org/installation.html
#Contributions simplifying and improving our build system are welcome!

#cuDNN acceleration switch (uncomment to build with cuDNN).
 USE_CUDNN := 1     #cuDNN测试

#CPU-only switch (uncomment to build without GPU support).
#CPU_ONLY := 1

#uncomment to disable IO dependencies and corresponding data layers
#USE_OPENCV := 0
#USE_LEVELDB := 0
#USE_LMDB := 0
#This code is taken from https://github.com/sh1r0/caffe-android-lib
#USE_HDF5 := 0

#uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)
#You should not set this flag if you will be reading LMDBs with any
#possibility of simultaneous read and write
#ALLOW_LMDB_NOLOCK := 1

#Uncomment if you're using OpenCV 3
 OPENCV_VERSION := 3    #opencv3测试

#To customize your choice of compiler, uncomment and set the following.
#N.B. the default for Linux is g++ and the default for OSX is clang++
#CUSTOM_CXX := g++

#CUDA directory contains bin/ and lib/ directories that we need.
CUDA_DIR := /usr/local/cuda
#On Ubuntu 14.04, if cuda tools are installed via
#"sudo apt-get install nvidia-cuda-toolkit" then use this instead:
#CUDA_DIR := /usr

#CUDA architecture setting: going with all of them.
#For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.
#For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.
#For CUDA >= 9.0, comment the *_20 and *_21 lines for compatibility.
CUDA_ARCH =	-gencode arch=compute_30,code=sm_30 \      #删除20  21两行  警告一大堆
		-gencode arch=compute_35,code=sm_35 \
		-gencode arch=compute_50,code=sm_50 \
		-gencode arch=compute_52,code=sm_52 \
		-gencode arch=compute_60,code=sm_60 \
		-gencode arch=compute_61,code=sm_61 \
		-gencode arch=compute_61,code=compute_61

#BLAS choice:
#atlas for ATLAS (default)
#mkl for MKL
#open for OpenBlas
BLAS := atlas
#Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
#Leave commented to accept the defaults for your choice of BLAS
#(which should work)!
#BLAS_INCLUDE := /path/to/your/blas
#BLAS_LIB := /path/to/your/blas

#Homebrew puts openblas in a directory that is not on the standard search path
#BLAS_INCLUDE := $(shell brew --prefix openblas)/include
#BLAS_LIB := $(shell brew --prefix openblas)/lib

#This is required only if you will compile the matlab interface.
#MATLAB directory should contain the mex binary in /bin.
 MATLAB_DIR := /usr/local/MATLAB/R2015b     #MATLAB测试
#MATLAB_DIR := /Applications/MATLAB_R2012b.app

#NOTE: this is required only if you will compile the python interface.
#We need to be able to find Python.h and numpy/arrayobject.h.
PYTHON_INCLUDE := /usr/include/python2.7 \
		/usr/lib/python2.7/dist-packages/numpy/core/include
#Anaconda Python distribution is quite popular. Include path:
#Verify anaconda location, sometimes it's in root.
#ANACONDA_HOME := $(HOME)/anaconda
#PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
		# $(ANACONDA_HOME)/include/python2.7 \
		# $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include

#Uncomment to use Python 3 (default is Python 2)
#PYTHON_LIBRARIES := boost_python3 python3.5m
#PYTHON_INCLUDE := /usr/include/python3.5m \
#/usr/lib/python3.5/dist-packages/numpy/core/include

#We need to be able to find libpythonX.X.so or .dylib.
PYTHON_LIB := /usr/lib
#PYTHON_LIB := $(ANACONDA_HOME)/lib

#Homebrew installs numpy in a non standard path (keg only)
#PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include
#PYTHON_LIB += $(shell brew --prefix numpy)/lib

#Uncomment to support layers written in Python (will link against Python libs)
#WITH_PYTHON_LAYER := 1

#Whatever else you find you need goes here. #额外添加hdf5路径
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial

#If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies
#INCLUDE_DIRS += $(shell brew --prefix)/include
#LIBRARY_DIRS += $(shell brew --prefix)/lib

#NCCL acceleration switch (uncomment to build with NCCL)
#https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0)
#USE_NCCL := 1

#Uncomment to use `pkg-config` to specify OpenCV library paths.
#(Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)
#USE_PKG_CONFIG := 1

#N.B. both build and distribute dirs are cleared on `make clean`
BUILD_DIR := build
DISTRIBUTE_DIR := distribute

#Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171
#DEBUG := 1

#The ID of the GPU that 'make runtest' will use to run unit tests.
TEST_GPUID := 0

#enable pretty build (comment to see full commands)
Q ?= @

  在修改后,保存,编译,表现正常

make -j8 
make matcaffe
make pycaffe

8 Tensorflow

  Tensorflow安装是基于python的,最好指定版本,默认最新可能不支持CUDA8(根据自己CUDA版本选择

//python2
sudo pip install tensorflow-gpu==1.4 	

//python3 
sudo pip3 install tensorflow-gpu==1.4 

9 Pytorch

  Pytorch安装也是基于python,进入Pytroch官网根据个人情况选择,然后复制命令行安装即可(记得加sudo)
  注意torch和torchvision都要安装

在这里插入图片描述

 测试

在这里插入图片描述

10 其他

 至此基础环境就安装好了,caffe,tensorflow,pytorch等深度学习框架随便切换;当然在有些demo下可能要求一些特殊包,按照提示安装即可。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,我无法回答关于"pytorch .mdb"的问题,因为在提供的引用内容中没有提到与".mdb"相关的信息。请提供更多上下文或详细信息,以便我能够更好地回答您的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [pytorch读取lmdb文件报错,lmdb.InvalidParameterError:解决](https://blog.csdn.net/lxb206/article/details/125646064)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Ubuntu16+CUDA8+Caffe+Tensorflow+Pytorch+cuDNN6+Matlab2015b+Opencv3(一篇就够)](https://blog.csdn.net/u011681952/article/details/84579954)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [1MDB_commnets_classification_:IMDB影评类基于pytorch框架acc 97%](https://download.csdn.net/download/weixin_42175971/16126935)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值