### Caffe

Caffe学习。

#@author:       gr
#@date:         2015-08-30
#@email:        forgerui@gmail.com   

1. Install

详细可以见官方文档博客1博客2

1.1 Prerequisites

  • CUDA is required for GPU mode.
    library version 7.0 and the latest driver version are recommended, but 6.* is fine too
    5.5, and 5.0 are compatible but considered legacy
  • BLAS via ATLAS, MKL, or OpenBLAS.
  • Boost >= 1.55
  • OpenCV >= 2.4 including 3.0
  • protobuf, glog, gflags
  • IO libraries hdf5, leveldb, snappy, lmdb

Caffe requires BLAS as the backend of its matrix and vector computations. There are several implementations of this library. The choice is yours:

  • ATLAS: free, open source, and so the default for Caffe.
  • Intel MKL: commercial and optimized for Intel CPUs, with a free trial and student licenses.
    Install MKL.
    Set BLAS := mkl in Makefile.config
  • OpenBLAS: free and open source; this optimized and parallel BLAS could require more effort to install, although it might offer a speedup.
    Install OpenBLAS
    Set BLAS := open in Makefile.config

我们这里使用atlas。

1.2 Compilation

  1. 拷贝配置文件

     cp Makefile.config.example Makefile.config
    
  2. 在Makefile.config文件中第73行LIBRARY_DIRS加上atlas库所在的位置,我的在/usr/lib64/atlas/,修改后:

     LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib64/atlas/  
    
  3. Makefile文件中在236行将boost_thread修改为boost_thread-mt,修改后:

     LIBRARIES += boost_thread-mt stdc++ 
    
  4. 编译

     make all -j 20            #多核编译,根据机子情况选定
    
  5. 编译matlab

    修改Makefile.config,MATLAB_DIR中加入matlab在机器中的位置:

     MATLAB_DIR := /usr/local/MATLAB/MATLAB_Production_Server/R2013a
    

    编译:

     make matcaffe -j 20
    
  6. 编译python

    修改Makefile.config,将PYTHON_INCLUDE, PYTHON_LIB修改为你机子正确的配置。

     PYTHON_INCLUDE := /usr/local/include/python2.7 \
     /usr/lib/python2.7/site-packages/numpy/core/include/numpy/
     
     PYTHON_LIB := /usr/local/lib 
    

    编译:

     make pycaffe -j 20
    

    注意:如果遇到如下问题,

     /usr/bin/ld: /usr/local/lib/libpython2.7.a(abstract.o): relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC
     /usr/local/lib/libpython2.7.a: could not read symbols: Bad value
     collect2: ld returned 1 exit status
    

    可以下载python,加上--enable-shared-fPIC选项重新编译安装,命令如下:

     ./configure --prefix=/usr/local/  --enable-shared CFLAGS=-fPIC  
     make  
     make install  
    

2. Usage

2.1 caffe中的例子

可以参见博客

2.1.1 mnist

mnist的网络框架在文件examples/mnist/lenet.prototxt中。分别运行如下命令,即可实现mnist:

sh data/mnist/get_mnist.sh
sh examples/mnist/create_mnist.sh
sh examples/mnist/train_lenet.sh

最后运行的结果,可以看到accuracy = 0.9907

I0830 21:56:59.506049 12371 solver.cpp:326] Iteration 10000, loss = 0.00290909
I0830 21:56:59.506080 12371 solver.cpp:346] Iteration 10000, Testing net (#0)
I0830 21:57:00.983238 12371 solver.cpp:414]     Test net output #0: accuracy = 0.9907
I0830 21:57:00.983290 12371 solver.cpp:414]     Test net output #1: loss = 0.0304467 (* 1 = 0.0304467 loss)
I0830 21:57:00.983304 12371 solver.cpp:331] Optimization Done.
I0830 21:57:00.983314 12371 caffe.cpp:214] Optimization Done.
2.1.2 cifair
sh data/cifar10/get_cifar10.sh
sh examples/cifar10/create_cifar10.sh
sh examples/cifar10/train_quick.sh

2.2 caffe 框架学习

2.2.1 框架

caffe的框架如下:

caffe framework

  1. 预处理图像的leveldb构建
    输入:一批图像和label (2和3)
    输出:leveldb (4)
    指令里包含如下信息:
    conver_imageset (构建leveldb的可运行程序)
    train/ (此目录放处理的jpg或者其他格式的图像)
    label.txt (图像文件名及其label信息)
    输出的leveldb文件夹的名字
    CPU/GPU (指定是在cpu上还是在gpu上运行code)

  2. CNN网络配置文件
    Imagenet_solver.prototxt (包含全局参数的配置的文件)
    Imagenet.prototxt (包含训练网络的配置的文件)
    Imagenet_val.prototxt (包含测试网络的配置文件)

2.2.2 Caffe层次

**Blob: **基础的数据结构,是用来保存学习到的参数以及网络传输过程中产生数据的类。
**Layer: **是网络的基本单元,由此派生出了各种层类。修改这部分的人主要是研究特征表达方向的。
**Net: **是网络的搭建,将Layer所派生出层类组合成网络。
**Solver: **是Net的求解,修改这部分人主要会是研究DL求解方向的。

2.3 RCNN

Training your own R-CNN detector on PASCAL VOC

!!! tvmonitor : 0.6483 0.6614
~~~~~~~~~~~~~~~~~~~~
Results:
    0.6428
    0.6963
    0.5016
    0.4191
    0.3191
    0.6251
    0.7087
    0.6036
    0.3266
    0.5852
    0.4627
    0.5616
    0.6037
    0.6684
    0.5414
    0.3157
    0.5285
    0.4889
    0.5772
    0.6483

    0.5412

~~~~~~~~~~~~~~~~~~~~

test_results = 

1x20 struct array with fields:

    recall
    prec
    ap
    ap_auc

Reference

1. http://caffe.berkeleyvision.org/installation.html
2. http://www.rthpc.com/plus/view.php?aid=351
3. http://www.cnblogs.com/platero/p/3993877.html
4. http://www.csdn.net/article/2015-01-22/2823663

posted @ 2015-10-30 19:33  bairuiworld  阅读(465)  评论(0编辑  收藏  举报