Ubuntu 14.04安装CPU版Caffe

最近在笔记本上配置了Ubuntu 14.04,并配置了Caffe,整个过程大概花了2个小时。希望在安装时能给大家一个启发,这里配置的是无GPU版的,因为我的笔记本时核心显卡,配置GPU版的要编译CUDA和cuDNN。

好了,下面开始。

1.安装各种依赖项:

sudo apt-get install -y libatlas-base-dev libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler

2.安装python依赖项,在编译pycaffe时需要用到:

sudo apt-get install -y python-numpy python-scipy python-matplotlib python-sklearn python-skimage python-h5py python-protobuf python-leveldb python-networkx python-nose python-pandas python-gflags Cython ipython

3.下载caffe-master:

sudo git clone https://github.com/BVLC/caffe.git

如果网速比较慢,也可以自己去github上下载,然后解压即可,记得解压后讲caffe-master重命名为caffe。

4.安装opencv:

cd caffe
sudo git clone https://github.com/jayrambhia/Install-OpenCV
cd Install-OpenCV/Ubuntu
sudo ./dependencies.sh
cd 2.4
sudo sh opencv2_4_10.sh

这是直接使用命令行安装,在我这里安装时速度很慢,主要是下载的速度,只几kb/s,所以最后我直接从github网上下载的,速度达到几mb/s(WTF?),然后修改了下opencv2_4_10.sh的代码,就是把下载那几行代码删去,然后修改一下路径就可以。

如果直接用上面代码安装的,下面这部分就不用看了,觉得速度比较慢的,可以自己修改一下代码,这里是我最后的opencv2_4_10.sh代码:

arch=$(uname -m)
if [ "$arch" == "i686" -o "$arch" == "i386" -o "$arch" == "i486" -o "$arch" == "i586" ]; then
flag=1
else
flag=0
fi
echo "Installing OpenCV 2.4.10"
sudo mkdir OpenCV
cd OpenCV
sudo mv ../opencv-2.4.10.zip . 
echo "Removing any pre-installed ffmpeg and x264"
sudo apt-get -y remove ffmpeg x264 libx264-dev
echo "Installing Dependenices"
sudo apt-get -y install libopencv-dev
sudo apt-get -y install build-essential checkinstall cmake pkg-config yasm
sudo apt-get -y install libtiff4-dev libjpeg-dev libjasper-dev
sudo apt-get -y install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev
sudo apt-get -y install python-dev python-numpy
sudo apt-get -y install libtbb-dev
sudo apt-get -y install libqt4-dev libgtk2.0-dev
sudo apt-get -y install libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev
sudo apt-get -y install x264 v4l-utils ffmpeg
echo "Installing OpenCV 2.4.10"
if ! [ -d "opencv-2.4.10" ]; then
  unzip opencv-2.4.10.zip
fi
rm opencv-2.4.10.zip
cd opencv-2.4.10
rm -rf build
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D BUILD_TIFF=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..
make -j
sudo make install
sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
echo "OpenCV 2.4.10 ready to be used"

要注意,需要把下载下来的opencv-2.4.10.zip放到和opencv2_4_10.sh同一个文件夹下。有的在安装时会出现卡机或死机的情况,需要把"make -j"这一行代码改为“make",即去掉-j即可。

其实大部分时间都花在安装opencv上了。

5.安装caffe:

cd ~/caffe
sudo cp Makefile.config.example Makefile.config
sudo make all

然后静静的等着他安装玩就可以了。到此caffe安装完毕。

6.安装pycaffe:

先加入环境变量:

sudo vi ~/.bashrc

习惯gedit的用户可以把vi换成gedit,然后在文件末尾添加一行:

export PYTHONPATH="/home/xxx/caffe/python:$PYTHONPATH"

保存退出,然后执行:

sudo ldconfig
sudo make pycaffe

ok,打开python窗口,试试import caffe,看是否成功,有的需要重启命令行窗口才可以。如果能import,这安装成功。

相关推荐