深度学习Ubuntu16.04-pytoch+opencv+cuda+cudnn+caffe

4 篇文章 0 订阅
2 篇文章 0 订阅

==============================
目录
0.ubuntu分区
1.装chrome卸火狐
2.安装最新版wineQQ
3.CUDA+cudnn
4.pytorch
5.opencv
6.pycharm
7.tensorflow
8.caffe

=================================
现在caffe整合进pytorch啦,根据官网装就行了。
源码安装pytorch+caffe + cuda9.0 + cudnn7:

# [anaconda root directory]
export CMAKE_PREFIX_PATH="$(dirname $(which conda))/../" 
# Install basic dependencies
conda install numpy pyyaml mkl mkl-include setuptools cmake cffi typing
# Add LAPACK support for the GPU
conda install -c pytorch magma-cuda90
git clone --recursive https://github.com/pytorch/pytorch
cd pytorch
python setup.py install

cuda支持和cudnn7使用docker镜像安装。
安装Docker:

#卸载旧版本
sudo apt-get remove docker docker-engine docker.io
#使用apt安装
sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu \
(lsb_release -cs) stable"
#安装Docker CE
sudo apt-get update
sudo apt-get install docker-ce
#启动Docker CE
sudo systemctl enable docker
sudo systemctl start docker
#建立Docker用户组
sudo groupadd docker
sudo usermod -aG docker $USER
#换镜像加速,我使用的是DaoCloud
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://a6a62fc3.m.daocloud.io
#重启docker
sudo systemctl restart docker.service
#测试是否安装成功
docker run hello-world

没有出现connect错误旧就ok啦

最后cuda和cudnn:

docker build -t pytorch -f docker/pytorch/Dockerfile .

这里遇到的问题:
(1)
fatal: Not a git repository: /home/zhangjiaming/pytorch/.git/modules/aten/src/ATen/cpu/cpuinfo
Unable to find current revision in submodule path 'aten/src/ATen/cpu/cpuinfo'

这个据说是git版本的问题,把git换成2.9。
(2)第七步更新,not a git repository。我是直接在实际目录下更新后,注释掉dockerfile里面的这一行。
(3)后面又修了一万个bug,记不住了。。。。

还是用之前的方法实在(×_x):

    conda install pip pyyaml sympy h5py cython numpy scipy
    conda install -c menpo opencv3
    conda install -c soumith pytorch torchvision cuda80 
    pip install easydict

================================
我是2T机械,16G内存,GTX1066;
双系统,windows+Ubuntu16.04 + CUDA9.1;
先装CUDA,再装pytorch。
一般来说,双系统先装windows再装ubuntu。因为windows安装程序会覆盖MBR和自己所在扇区的boot sector(启动扇区)。没有选择机会,也没有选择菜单。而linux安装时,可以选择将boot loader加载MBR或某个boot sector,可设置菜单,可加入windows开机项。不过这好像对我的基本没啥用,因为bios里面可以直接设定默认进哪个系统,选择进哪个系统。ubuntu推荐不要装最新版的ubuntu17.10,而装最新的LTS(长期支持版),我现在的是16.04,至于为什么(-^-)#,无数次折腾的结论。windows我分了450个G,留下10G左右方便日后扩展,150G左右作为共享盘,存数据集之类的,剩下的全部分给Ubuntu。
0.ubuntu分区
好的分区易于备份,而且将那些频繁调用的分区独立出去,当出现问题时也避免扩大影响,而且还能提高空间利用效率。
1./swap 交换内存,一般为物理内存的1.5-2倍(20个G足以),使用内存时将暂时不用的内存存放在物理内存中。太小的话还可能导致系统无法进入休眠状态。这个不必挂载(我分的20G)
2./home 通常存放自己的文件(500G),当有多用户同时使用这台电脑时,分配的空间肯定要bigbia。
3./boot 存放kernel配置文件和grub目录,更新kernel配置文件时会用到,推荐500M以上(800M)
4./ 根目录,你没有挂载的文件都挂载在根目录上,相当于其它你没有单独挂载的文件共享这一块区间。我觉得保险起见还是50G以上。(80G)
5./usr 存放自己安装的软件(600G)
6.分一个150G左右的空间挂载到windows上,以后用来共享数据。安装好后,在你的根目录下会有一个windows的文件夹,就是你挂载的共享盘了。
6.剩下的作为预留空间,方便以后扩展。

1.装chrome卸载火狐
安装chorme:
百度上下载chrome 桌面版,选择linux 64bit。
在终端输入:sudo apt-update更新软件包列表(建议每隔一段时间执行一下)。否者使用apt install 时,会找不到安装包。
sudo apt-get install gdebi
安装从成功后,终端进入chrome安装包目录:
gdebi +文件名,安装chrome
进入usr/share/applications
找到chrome图标,拖到桌面来。
或者摁super键(windows键)打开应用框,搜索chrome,添加到收藏夹。

卸载火狐:
sudo apt-get purge firefox
如果有在firefox里面安装其他的插件支持之类的:
sudo apt-get purge firefox firefox-branding firefox-gnome-support ubufox

或者用一个简单的脚本完成上面的步骤:

#install chrome and remove firefox
#!/bin/bash
#History 2017/12/19 By Zhang Jiaming First release 
sudo apt-get update
echo -e "==========download chrome.deb=========="
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
echo -e "==========install gdebi=========="
sudo apt-get install gdebi
echo -e "==========install chrome=========="
sudo gdebi google-chrome-stable_current_amd64.deb
sudo rm google-chrome-stable_current_amd64.deb
sudo apt-get purge firefox
#清除firefox插件之类的,如果没有安装插件就不需要了
#sudo apt-get purge firefox-branding firefox-gnome-support ubufox

2.安装最新版wineQQ——-实践证明这东西一点也不好用,还经常崩溃
在ubuntu上使用QQ
Github地址:https://github.com/askme765cs/Wine-QQ
安装wine和winetricks:
(注意其中卸载包的操作,别让它把你有用的包给卸了)
apt install wine-development
apt install wine-stable
apt install winetricks
应用框里搜winetricks,创建一个默认wine容器
选择安装字体,fakechinese
选择安装windows dll或组件,msls31
终端输入wine.cfg
这里由于前面使用sudo,所以如果出现了wine is not owed by you 的错误,ctrl+D退出超级用户登陆,就可以了。

点击上方的“函数库”那一栏,并点击把光标移动到“新增库函数顶替”下方的方框里,分别输入*ntoskrnl.exe回车,*riched20回车,*txplatform.exe回车。然后在点击下方新出现的*ntoskrnl.exe和*txplatform.exe,点击编辑,选择“停用”。

说明:
如果不替换riched20的话则安装好QQ后无法输入用户名,
不停用txplatform.exe则WineQQ无法完整退出,导致关掉重开后提示QQ文件被占用,
禁用ntoskrnl.exe是为了规避可能出现QQ无法启动的bug

官网上下载QQ.exe,在对应目录下执行命令:wine QQ.exe
或者在输入绝对路径安装。(当文件路径有空格时,需用”\ “隔开;当有‘(“括号时可能会报语法错误,最简单的方法还是直接用双引号将路径包起来)

创建快捷启动方式:
1.写脚本。终端进入QQ的文件夹里 gedit qq.sh
里面写入:#!/bin/bash
wine QQ.exe的路径/QQ.exe
2.创建快捷方式。gedit QQ.desktop.这个比较简单,之前记录过了。
启动快捷方式时可能会提示:应用程序启动器”QQ.desktop“不被信任
点击QQ.desktop的属性,在权限里选择,允许程序作为可执行文件。

现在的问题是虽然可以登录qq,但是,一旦最小化后就找不到窗口了.

3.CUDA9.1 + Cudnn7
装CUDA会自带显卡驱动,就不用单独再装显卡驱动了。
官方说明的CUDA9.1支持的gcc版本为:5.3.1,但实际ubuntu16.04自带的gcc-5.4.0现在也支持了。
kenel要求为4.4但我4.10也依然可以支持。(uname -r查询)
官方安装指南:https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html
官网下载ubuntu16.04的CUDA包,我选择官方建议的deb安装包。建议使用wget -c 断点下载。按照官方操作来基本没有问题。
安装:
sudo apt-get update
wget -c https://developer.nvidia.com/compute/cuda/9.1/Prod/local_installers/cuda-repo-ubuntu1604-9-1-local_9.1.85-1_amd64
sudo apt-key add /var/cuda-repo-/7fa2af80.pub(先执行下一步,会提示你的,把它复制过来即可)
sudo dpkg -i cuda-repo-ubuntu1604-9-1-local_9.1.85-1_amd64
sudo apt-get update
sudo apt-get install cuda
安装成功后(或许需要重启),可以现在设置->软件更新->附加驱动,里看看驱动有没有装上。或则用:
nvidia-smi查看,只要识别到你的显卡就成功了。
gedit ~/.bashrc
文件末尾加上环境变量:
export PATH=/usr/local/cuda-9.1/bin PATH:+:$PATHrunfiledebexportLDLIBRARYPATH=/usr/local/cuda9.1/lib64  P A T H : + : $ P A T H 这 里 如 果 你 当 初 下 的 是 r u n f i l e 而 不 是 d e b 的 话 , 还 需 要 加 上 : e x p o r t L D L I B R A R Y P A T H = / u s r / l o c a l / c u d a − 9.1 / l i b 64   {LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
然后:
source ~/.bashrc
nvcc -V
出现CUDA版本信息就安装成功了
如果nvcc提示没有安装,看看你环境变量路径有没有写错,或者到文件夹下搜索nvcc的位置。

接下来安装cudnn。
首先需要进行注册,如果在注册的时候发现在第二步的时候老是提交不上去,一般是你的cookie没有设置好,在浏览器设置里允许的第三方网站读取cookie里面加入invidia的网站。
列表里:for linux的版本是二进制版本,我下载的是三个deb文件(runtime,developer, code samples).进入命令行分别用安装三个deb包就行了。

sudo dpkg -i libcudnn7_7.0.3.11-1+cuda9.0_amd64.deb
sudo dpkg -i libcudnn7-dev_7.0.3.11-1+cuda9.0_amd64.deb
sudo dpkg -i libcudnn7-doc_7.0.3.11-1+cuda9.0_amd64.deb

下面检查cudnn是否安装成功。
你可以像官网说的那样把usr/src/code samples 复制出来,在测试。
我是直接到目录下的mnistCUDNN文件夹下测试的:

#编译
sudo make
执行
./mnistCUDNN

没有报错就ok了
官网教程

4.pytorch安装
(1)python使用anaconda自带版本即可。如果要改python默认版本:http://www.jb51.net/article/100407.htm
(2)安装anaconda
下载对应的anaconda包。
解压后:
sh Anaconda3-5.0.1-Linux-x86_64.sh安装anaconda
按照默认设置装就行了。特别是不要改变安装路径!
期间会问你是否将anaconda加入环境变量,如果你不小心选了否:
gedit ~/.bashrc
在最后一行加上:
export PATH=”/home/*/anaconda3/bin:$PATH”
export LD_LIBRARY_PATH=/home/**/anaconda3/lib:$LD_LIBRARY_PATH
source ~/.bashrc
输入conda,没有提示找不到conda就安装成功了
(3)安装pytorch
没有翻墙打话是看不到官网给的安装命令的。
使用anaconda安装:conda install pytorch torchvision cuda90 -c pytorch
不过一般网很慢,会下载失败。于是我将原来打命令拆成了两条:

conda install pytorch cuda90 -c pytorch
conda install torchvison cuda90 -c pytorch
#再检查一下
conda install pytorch torchvision cuda90 -c pytorch

这里拆开命令要注意cuda90作为限制版本条件,否则可能会装到cuda80的pytorch版本。
在python里面试一试:
import torch,torchvision
没有报错就成功了。
如果每次数据移到GPU上都要花很长时间(我是400多秒)
例如这种简单的命令:

t = Variable(torch.randn(5))
t =t.cuda()
print(t)

这是pytorch 版本和CUDA不匹配造成这种长时间的挂起的(@……@),就应该是你前面没有加cuda90限制,下载啦cuda80对应打pytorch版本了,升级一下就好。

如果还是下载不了:
http://pytorch.org/previous-versions/

5.安装opencv
新的安装方式..:

conda install -c menpo opencv3

之前的方法:

wget -c https://sourceforge.net/projects/opencvlibrary/files/latest/download
sudo apt install cmake
安装必要的包和工具:
sudo apt-get install build-essential cmake git pkg-config
sudo apt-get install libjpeg8-dev libtiff4-dev libjasper-dev libpng12-dev(图像I/O工具包)
sudo apt-get install libgtk2.0-dev(GTK用来显示GUI)
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev(处理视频流和单个帧)

将压缩包解压到你想放的地方,进入opencv-3.4.0目录:
mkdir build
cd build
cmake -DBUILD_TIFF=ON -DBUILD_opencv_java=OFF -DWITH_CUDA=OFF -DWITH_OPENGL=ON -DWITH_OPENCL=ON -DWITH_IPP=ON -DWITH_TBB=ON -DWITH_EIGEN=ON -DWITH_V4L=ON -DWITH_VTK=OFF -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX= (pythonc"importsys;print(sys.prefix)")DPYTHON3EXECUTABLE= ( p y t h o n − c " i m p o r t s y s ; p r i n t ( s y s . p r e f i x ) " ) − D P Y T H O N 3 E X E C U T A B L E = (which python) -DPYTHON3_INCLUDE_DIR= (pythonc"fromdistutils.sysconfigimportgetpythoninc;print(getpythoninc())")DPYTHON3PACKAGESPATH= ( p y t h o n − c " f r o m d i s t u t i l s . s y s c o n f i g i m p o r t g e t p y t h o n i n c ; p r i n t ( g e t p y t h o n i n c ( ) ) " ) − D P Y T H O N 3 P A C K A G E S P A T H = (python -c “from distutils.sysconfig import get_python_lib; print(get_python_lib())”) ..

在这里,多半会卡在ippicv这里,在这里找到自己需要的版本:
https://github.com/opencv/opencv/tree/master/3rdparty/ippicv
我的自己的下载是:

wget -c https://github.com/opencv/opencv_3rdparty/raw/ippicv/master_20170822/ippicv/ippicv_2017u3_lnx_intel64_general_20170822.tgz

下载好后,将压缩包move到.cache文件里,就在opencv-3.4.0的隐藏目录下。然后再到build目录中编译:
make -j4
make install

最后在python中检验:import cv2
没有报错就成功了。


**6.安装配置pycharm**
http://blog.csdn.net/qq_38290417/article/details/78704801
之前自作主张,在安装anaconda时改变了anaconda的安装路径,现在的ide基本都是在Home/Anacaonda下寻找conda,所以pycharm会找不到anaconda......哇,玩基吧......
后面安装的东西基本都在anaconda里面,直接把这个文件夹remove了。重新再来一遍吧。

**7.tensorflow安装**
conda create -n tensorflow python=3.6
source activate tensorflow
 #上面执行完后,目录前会出现‘(tensorflow)’,和django的虚拟环境差不多
 pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.4.0-cp36-cp36m-linux_x86_64.whl

后面的下载网址是python3.6的,其它版本自己找,链接

在python命令框里try 一 try:

import tensorflow 

结束了记得source deactivate关掉虚拟环境。用之前,conda activate tensorflow.

8.caffe安装
依赖安装:

#conda install glog
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev

git clone https://github.com/BVLC/caffe.git
cd caffe
cp Makefile.config.example Makefile.config
Makefile.config修改后为:

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

#add this in 2018.3.12
LINKFLAGS := -Wl,-rpath,$(HOME)/anaconda3/lib

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

# 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

# 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

# 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 \
        -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_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)/anaconda3
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
        $(ANACONDA_HOME)/include/python3.6m \
        $(ANACONDA_HOME)/lib/python3.6/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.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/lib/x86_64-linux-gnu/hdf5/serial/include 
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /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 ?= @

遇到的问题:
(1)找不到pyconfig.h,一般是配置的时候没有PYTHON_INCLUDE没有包含pyconfig.h所在的‘include’
(2)libpng16.so.16找不到,加入:
LINKFLAGS := -Wl,-rpath,$(HOME)/anaconda3/lib
(3)出现protobuf,文件由先前版本生成的警告。一般为anaconda和用apt-get 安装了相同依赖,使用conda uninstall 卸载即可。这里应该也可以卸载apt-get 安装的包,这样就不会出现(5)中的问题
(4) raise ValueError, “Can’t create weekday with n == 0”

sudo pip3 install --upgrade python-util
pip install python-dateutil --upgrade

(5) 编译成功后在(anaconda)python中import caffe,又会出现:
No module named ‘google.protobuf.internal
这是因为前面把conda中的protobuf给卸载了,现在再装回来:

conda install protobuf

(6)在后面make runtest时,会出现:
BatchReindexLayerTest/3.TestGradient, where TypeParam = caffe::GPUDevice
貌似是cuda9.1的问题,可以先不管
(7)cannot find -lboost_python3:
在/usr/lib/x86_64-linux-gnu下建立个软连接即可:
sudo ln -s libboost_python-py35.so libboost_python3.so
这里是在/usr/lib/x86_64-linux-gnu下建立软连接而不是报错的位置:/usr/bin

make all -j8
make pycaffe
make test
make runtest

在环境变量中:

export PYTHONPATH= /path/to/caffe/python: $PYTHON
PATH
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值