本篇文章将会详细的介绍关于Ubuntu14.04系统,安装的Caffe(CPU版)的每个过程。一旦我们安装完成,我们可以通过使用caffe测试Generating images with Google’s “INCEPTIONISM” – deepdream(https://github.com/google/deepdream)是否成功.我们会使用一些公用的图片使用deepdream来泛化这些图片。废话不多说,那就让我们开始吧。
首先,让我们在$HOME下创建一个名为deep-learning的目录,我们将在这个在这个目录里下载所有所需要的包。
cd ~
mkdir deep-learning
cd deep-learning
1、建立依赖关系包
为了让caffe编译成功,我们需要首先安装一些依赖包。在终端里执行下面的命令.
提示:我在亲自安装的时候,发现有些包会安装不上。所以,更换了一些包的安装顺序就可以了。大家可以多试试。
注意:因为我们不使用GPU,所以我们无需安装cuda.
# General Dependencies
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
# BLAS -- for better CPU performance
sudo apt-get install libatlas-base-dev
# Python -- It comes preinstalled on Ubuntu 14.04
# Required if you want to use Python wrappers for Caffe sudo apt-get install the python-dev
# Remaining dependencies
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
安装依赖包的另一版本(上面的三条指令成功,则无需在试!):
sudo apt-get install libatlas-base-dev
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler
2、编译和安装Caffe
caffe的包被公布在GitHub上。通过在终端输入下面的命令来完成caffe包的克隆。
cd ~/deep-learning
git clone http://github.com/BVLC/caffe.git
cd caffe
Makefile.config.example文件的内容是验证文件的模板。我们使用他来重写Makefile。config文件。
cp Makefile.config.example Makefile.config
在Makefile.config文件中我们需要设置的是使用CPU还是GPU。打开你最喜欢的编辑器,取消注释CPU_ONLY:=1
CPU_ONLY := 1
(很有可能在第8行)像下面这样:
8 CPU_ONLY := 1
设置完验证文件后,接下的步骤就是编译caffe了。
make all
make test
make runtest
注意
如果你编译make all的时候返回OpenCV undefined reference error。
可能像下面所示:
.build_release/lib/libcaffe.so: undefined reference to `cv::imread(cv::String const&, int)'
.build_release/lib/libcaffe.so: undefined reference to `cv::imencode(cv::String const&, cv::_InputArray const&, std::vector<unsigned char, std::allocator<unsigned char> >&, std::vector<int, std::allocator<int> > const&)'
.build_release/lib/libcaffe.so: undefined reference to `cv::imdecode(cv::_InputArray const&, int)' collect2: error: ld returned 1 exit status
make: *** [.build_release/tools/caffe.bin] Error 1
点击https://github.com/BVLC/caffe/issues/2348可以看到更加详细的解答。
为了更正这个错误,我们需要在opencv_imgcodecs在176行(可能)添加下面的内容:
174 LIBRARIES += glog gflags protobuf leveldb snappy \
175 mdb boost_system hdf5_hl hdf5 m \
176 opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs
现在我们清楚build文件下的所有内容。这是一个很重要的步骤,不然会有其他的错误产生。
rm -rf ./build/*
现在,我们重新开始上面失败的指令。
make all
make test
make runtest
3、增加PYTHON支持
为了增加python的支持,我们需要在终端下执行下面的命令。我们需要python能够支持运行deepdream下的代码。
cd ~/deep-learning/caffe
make pycaffe
echo export PYTHONPATH=~/deep-learning/caffe/python:$PYTHONPATH >> ~/.bashrc
4、Running the INCEPTIONISM code
4.1. Download the GoogLeNet model and clone the deepdream GitHub repository.
cd ~/deep-learning
wget http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel mv bvlc_googlenet.caffemodel caffe/models/bvlc_googlenet/
git clone https://github.com/google/deepdream.git
4.2. Install the dependencies
sudo pip install "ipython[all]"
sudo pip install numpy
sudo pip install scipy
sudo pip install protobuf
sudo pip install skimage
4.3. Run the IPython Notebook
cd ~/deep=learning/deepdream
ipython notebook
Now the IPython notebook dashboard will open up in your default browser as shown below.
In the list of files, click on dream.ipynb and you will get an output as shown in the figure below.
So, this is it ! Use this IPython Notebook to play with the deepdream code to generate some awesome images. I hope you liked the post and THANK YOU :smile: for reading.