转载:http://blog.csdn.net/u010402483/article/details/51506616
安装依赖包
1.安装protobuf,leveldb,snappy,opencv,hdf5, protobuf compiler andboost:
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
2.安装gflags,glogs ,lmdb andatlas.
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
sudo apt-get install libatlas-base-dev
下载Caffe
使用git直接下载Caffe非常简单,或者去https://github.com/BVLC/caffe下载
git clone https://github.com/BVLC/caffe
编译Caffe
1.切换到Caffe所在目录
cp Makefile.config.example Makefile.config
2.配置Makefile.config
1)CPU_ONLY := 1
2)配置一些引用文件(增加部分主要是解决新版本下,HDF5的路径问题)
1)INCLUDE_DIRS := $(PYTHON_INCLUDE)
/usr/local/include
/usr/lib/x86_64-linux-gnu/hdf5/serial/include
2)LIBRARY_DIRS := $(PYTHON_LIB)
/usr/local/lib
/usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial
3)BLAS := atlas
计算能力 mkl > openlas >atlas
3.Make Caffe
make all -j8
make test -j8
make runtest -j8
4.编译成功,否则执行 make clean 多执行以下,否则多google吧
发生错误可参考:http://blog.csdn.net/yhl_leo/article/details/51371936
测试参考:http://blog.csdn.net/zt_1995/article/details/56487162
首先下载MNIST数据集
$ cd data/mnist
$ ./get_mnist.sh
下载到四个文件
训练集(图片)train-images-idx3-ubyte
训练集(标签)train-labels-idx1-ubyte
测试集(图片)t10k-images-idx3-ubyte
测试集(标签)t10k-labels-idx1-ubyte
下载到的文件为二进制文件,可以执行下面命令来把文件转换为caffe能识别的文件(LEVELDB或LMDB)
$ ./examples/mnist/create_mnist.sh
因为是CPU运行,所以修改在examples文件下的Mnist下的lenet_solver.prototxt中的solver_mode:CPU
solver_mode: CPU
训练模型
./examples/mnist/train_lenet.sh
结果:
最后利用训练好的模型对数据进行预测
$ ./build/tools/caffe.bin test \
-model examples/mnist/lenet_train_test.prototxt \
-weights examples/mnist/lenet_iter_10000.caffemodel \
-iterations 100
结果为:
测试精度0.99
最终训练模型保存在 examples/mnist/lenet_iter_10000.caffemodel
训练状态保存在examples/mnist/lenet_iter_10000.solverstate