Caffe学习之一caffe安装

来源:互联网 发布:陈华伟 知乎 编辑:程序博客网 时间:2024/05/31 18:37

mac下安装caffe

首先直接github下载caffe源代码:
https://github.com/BVLC/caffe
克隆到本地
接下来开始编译caffe.
先安装矩阵运算库blas:

brew install homebrew/science/openblas

编译caffe之前,需要先安装相关的依赖库:

for x in snappy leveldb gflags glog szip hdf5 lmdb homebrew/science/opencv;do    brew uninstall $x;    brew install --fresh -vd $x;donebrew uninstall --force protobuf; brew install --with-python --fresh -vd protobufbrew uninstall boost boost-python; brew install --fresh -vd boost boost-python

接下来准备编译,先进入caffe源代码目录。

cp Makefile.config.example Makefile.config

编辑Makefile.config,由于我的电脑不支持cuda,需要将里面的

  CPU_ONLY := 1

前面的注释去掉。同时注释掉所有和cuda相关的选项。
同时mac中需要配置下python,不然后面的pycaffe没办法用,具体配置如下:

 PYTHON_INCLUDE := /usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/include/python2.7\                  /usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/include
 PYTHON_LIB := /usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib

保证python的版本一致,因为发现mac中还有个2.7.10的python,所以导致运行pycaffe的时候直接报错。
编译:

make allmake test make runtest

再编译python wrapper

make pycaffe

编译完成后,将python的路径写入环境变量,此路径在源代码python下。写入~/.bash_profile
或者可以按照cmake的方式编译安装:

cmake . -DCMAKE_BUILD_TYPE=Debug  # Debug模式make -j 4 && make install           cmake . -DCMAKE_BUILD_TYPE=Release # Release模式make -j 4 && make install
export  PYTHONPATH=$PYTHONPATH:~/code_space/deeplearning/caffe/caffe/python

caffe处理过程

caffe一般需要经过:

Created with Raphaël 2.1.0数据格式处理编写网络结构文件*.prototxt网络求解文件solver.prototxt训练得到caffemodel文件
I0622 23:37:30.107743 2793825216 solver.cpp:447] Snapshotting to binary proto file examples/mnist/lenet_iter_10000.caffemodelI0622 23:37:30.121928 2793825216 sgd_solver.cpp:273] Snapshotting solver state to binary proto file examples/mnist/lenet_iter_10000.solverstate

以上训练过程测试方法:

./build/tools/caffe train --solver=examples/mnist/lenet_solver.prototxt 

上面的调用过程对应流程:

Created with Raphaël 2.1.0caffe调用求解lenet_solver.prototxt 调用网络结构文件lenet_train_test.prototxtlenet_train_test.prototxt文件里面调用输入的训练图片等数据训练caffemodel文件

caffe数据格式生成

caffe输入训练图片数据一般使用lmdb格式,在源代码目录下example/imagenet文件夹下面的一些脚本文件可以快速生成相关caffe所需的数据。
create_imagenet.sh可以快速的生成lmdb的数据格式文件,只需要把这个脚本稍作修改,就可以对训练图片、标注文件进行打包为lmdb格式文件。

#!/usr/bin/env sh  # Create the imagenet lmdb inputs  # N.B. set the path to the imagenet train + val data dirs  EXAMPLE=.          # 生成模型训练数据文化夹  TOOLS=../../build/tools                              # caffe的工具库,不用变  DATA=.                  # python脚步处理后数据路径  TRAIN_DATA_ROOT=train/  #待处理的训练数据图片路径  VAL_DATA_ROOT=val/      # 带处理的验证数据图片路径  # Set RESIZE=true to resize the images to 256x256. Leave as false if images have  # already been resized using another tool.  RESIZE=true   #图片缩放  if $RESIZE; then    RESIZE_HEIGHT=256    RESIZE_WIDTH=256  else    RESIZE_HEIGHT=0    RESIZE_WIDTH=0  fi  if [ ! -d "$TRAIN_DATA_ROOT" ]; then    echo "Error: TRAIN_DATA_ROOT is not a path to a directory: $TRAIN_DATA_ROOT"    echo "Set the TRAIN_DATA_ROOT variable in create_imagenet.sh to the path" \         "where the ImageNet training data is stored."    exit 1  fi  if [ ! -d "$VAL_DATA_ROOT" ]; then    echo "Error: VAL_DATA_ROOT is not a path to a directory: $VAL_DATA_ROOT"    echo "Set the VAL_DATA_ROOT variable in create_imagenet.sh to the path" \         "where the ImageNet validation data is stored."    exit 1  fi  echo "Creating train lmdb..."  GLOG_logtostderr=1 $TOOLS/convert_imageset \      --resize_height=$RESIZE_HEIGHT \      --resize_width=$RESIZE_WIDTH \      --shuffle \      $TRAIN_DATA_ROOT \      $DATA/train.txt \     #标签训练数据文件      $EXAMPLE/train_lmdb  echo "Creating val lmdb..."  GLOG_logtostderr=1 $TOOLS/convert_imageset \      --resize_height=$RESIZE_HEIGHT \      --resize_width=$RESIZE_WIDTH \      --shuffle \      $VAL_DATA_ROOT \      $DATA/val.txt \    #验证集标签数据      $EXAMPLE/val_lmdb  echo "Done."

今天先学习到这里,后面学习怎么利用图片进行训练。

原创粉丝点击