从今天开始,打算正式用Caffe做点事情了。这主要是由于Caffe有C++接口,而且设计之初就是专门针对计算机视觉的。况且它的作者贾扬清是中国人,怎么着都得follow一下。Caffe是在Linux上开发的,但由于工作原因,我更多地在Windows上进行开发。好在GibHub上,Caffe有一个Windows的branch,想必Windows上支持起来也不会有那么困难。话就不多说了,先从在Windows上安装Caffe开始吧。
需要使用的工具:
- Git: 可直接使用GitHub提供的工具GitHubDesktop
- Visual Studio 2015: 可以使用免费的社区版
- Python: 下载64位版本
- CUDA 8.0
- cuDNN v5
- CMake
下载源码
从GitHub上下载源码。
> cd F:\OpenSource\caffe
> git clone https://github.com/BVLC/caffe.git .
Cloning into '.'...
remote: Counting objects: 37253, done.
remote: Compressing objects: 100% (42/42), done.
remote: Total 37253 (delta 43), reused 22 (delta 22), pack-reused 37189
Receiving objects: 100% (37253/37253), 47.91 MiB | 1012.00 KiB/s, done.
Resolving deltas: 100% (24879/24879), done.
F:\OpenSource\caffe [master ≡]>
此时,我们需要切换到Windows分支
F:\OpenSource\caffe [master ≡≡]> git checkout windows
Branch windows set up to track remote branch windows from origin.
Switched to a new branch 'windows'
F:\OpenSource\caffe [windows ≡]>
构建工程
接下来要用到的是scripts文件夹中的build_win.cmd文件。这个文件的作用是对cmake进行一些配置,然后运行cmake来构建整个caffe工程。这个文件有个大的if语句。判断是否DEFINE了APPVEYOR,在我的机子上是没有DEFINE的。因此,可以直接看else部分。我的设置如下:
:: Change the settings here to match your setup
:: Change MSVC_VERSION to 12 to use VS 2013
if NOT DEFINED MSVC_VERSION set MSVC_VERSION=14
:: Change to 1 to use Ninja generator (builds much faster)
if NOT DEFINED WITH_NINJA set WITH_NINJA=0
:: Change to 1 to build caffe without CUDA support
if NOT DEFINED CPU_ONLY set CPU_ONLY=0
:: Change to Debug to build Debug. This is only relevant for the Ninja generator the Visual Studio generator will generate both Debug and Release configs
if NOT DEFINED CMAKE_CONFIG set CMAKE_CONFIG=Release
:: Set to 1 to use NCCL
if NOT DEFINED USE_NCCL set USE_NCCL=0
:: Change to 1 to build a caffe.dll
if NOT DEFINED CMAKE_BUILD_SHARED_LIBS set CMAKE_BUILD_SHARED_LIBS=0
:: Change to 3 if using python 3.5 (only 2.7 and 3.5 are supported)
if NOT DEFINED PYTHON_VERSION set PYTHON_VERSION=2
:: Change these options for your needs.
if NOT DEFINED BUILD_PYTHON set BUILD_PYTHON=1
if NOT DEFINED BUILD_PYTHON_LAYER set BUILD_PYTHON_LAYER=1
if NOT DEFINED BUILD_MATLAB set BUILD_MATLAB=0
:: If python is on your path leave this alone
if NOT DEFINED PYTHON_EXE set PYTHON_EXE=python
:: Run the tests
if NOT DEFINED RUN_TESTS set RUN_TESTS=0
:: Run lint
if NOT DEFINED RUN_LINT set RUN_LINT=0
:: Build the install target
if NOT DEFINED RUN_INSTALL set RUN_INSTALL=0
155行开始:
cmake -G"!CMAKE_GENERATOR!" ^
-DBLAS=Open ^
-DCMAKE_BUILD_TYPE:STRING=%CMAKE_CONFIG% ^
-DBUILD_SHARED_LIBS:BOOL=%CMAKE_BUILD_SHARED_LIBS% ^
-DBUILD_python:BOOL=%BUILD_PYTHON% ^
-DBUILD_python_layer:BOOL=%BUILD_PYTHON_LAYER% ^
-DBUILD_matlab:BOOL=%BUILD_MATLAB% ^
-DCPU_ONLY:BOOL=%CPU_ONLY% ^
-DCOPY_PREREQUISITES:BOOL=1 ^
-DCUDNN_ROOT=D:/Program Files/NVIDIA_GPU_Computing_Toolkit/CUDA ^
-DINSTALL_PREREQUISITES:BOOL=1 ^
-DUSE_NCCL:BOOL=!USE_NCCL! ^
"%~dp0\.."
注意这一行:
-DCUDNN_ROOT=D:/Program Files/NVIDIA_GPU_Computing_Toolkit/CUDA ^
如果安装了CUDNN的话,则需要将此安装目录包含进来。
OK,现在到caffe目录,执行
> scripts\build_win.cmd
然后发现,工程在向我的C盘里面下东西。
-- Downloading prebuilt dependencies to C:/Users/zhongc/.caffe/dependencies/down
load/libraries_v140_x64_py27_1.1.0.tar.bz2
如果不想要它下在C盘(我就担心我的C盘不够用),需要修改一下cmake文件。将cmake\WindowsDownloadPrebuiltDependencies.cmake文件中的
set(CAFFE_DEPENDENCIES_ROOT_DIR ${USERPROFILE_DIR}/.caffe/dependencies CACHE PATH "Prebuild depdendencies root directory")
修改为
set(CAFFE_DEPENDENCIES_ROOT_DIR "the-dir-you-define" CACHE PATH "Prebuild depdendencies root directory")
保存,然后将caffe下的build目录删掉,并重新运行
> scripts\build_win.cmd
出错。
CMake Error at D:/Program Files/CMake/share/cmake-3.8/Modules/ExternalProject.cmake:2503 (add_custom_target):
add_custom_target cannot create target "glog" because an imported target
with the same name already exists.
Call Stack (most recent call first):
cmake/External/glog.cmake:33 (ExternalProject_Add)
cmake/Dependencies.cmake:38 (include)
CMakeLists.txt:79 (include)
修改 caffe\cmake\External\glog.cmake
将
find_package(Glog)
修改为
find_package(glog CONFIG)
再次cmake,通过。
小技巧:第一次使用scripts\build_win.cmd,以后就可以直接用cmake GUI界面来进行cmake了。
编译程序
双击caffe\build\Caffe.sln
。
先生成caffe项目。经过漫长的等待……虽然有很多警告,但最终是成功了。
再把tools和examples生成一遍。经过有点长的等待……虽然有很多警告,但是也都成功了。
好了,转到Release下再尝试一遍吧…
OK,也全部成功。
MNIST测试
接下来,为了保持完整性,我们来跑一个深度学习界的Hello world程序。参考官网上教程。但官网上的教程是Linux版的,用到很多shell命令,因此我们需要下载安装cygwin才可以运行。
安装完毕之后,来运行
cd $CAFFE_ROOT
./data/mnist/get_mnist.sh
./examples/mnist/create_mnist.sh
不过,似乎情况并不那么妙:
$ ./data/mnist/get_mnist.sh
./data/mnist/get_mnist.sh:行3: $'\r': 未找到命令
./data/mnist/get_mnist.sh: 第 5 行:cd: $'/cygdrive/f/OpenSource/caffe/data/mnist\r\r': No such file or directory
./data/mnist/get_mnist.sh:行6: $'\r': 未找到命令
Downloading...
./data/mnist/get_mnist.sh:行8: $'\r': 未找到命令
./data/mnist/get_mnist.sh:行10: 未预期的符号 `$'do\r'' 附近有语法错误
'/data/mnist/get_mnist.sh:行10: `do
似乎出了点问题。不过,一看这’\r’的问题,我们似乎就猜到了是Windows文件的换行符与Linux文件的换行符的不同导致的。可以这样来解决:
- 用notepad++打开data/mnist/get_mnist.sh和examples/mnist/create_mnist.sh。
- 选择”编辑”->”文档格式转换”->”转换为UNIX格式”。
- 保存
- 修改create_mnist.sh为
#!/usr/bin/env sh
# This script converts the mnist data into lmdb/leveldb format,
# depending on the value assigned to $BACKEND.
set -e
EXAMPLE=examples/mnist
DATA=data/mnist
BUILD=build/examples/mnist/Release
BACKEND="lmdb"
echo "Creating ${BACKEND}..."
rm -rf $EXAMPLE/mnist_train_${BACKEND}
rm -rf $EXAMPLE/mnist_test_${BACKEND}
$BUILD/convert_mnist_data $DATA/train-images-idx3-ubyte \
$DATA/train-labels-idx1-ubyte $EXAMPLE/mnist_train_${BACKEND} --backend=${BACKEND}
$BUILD/convert_mnist_data $DATA/t10k-images-idx3-ubyte \
$DATA/t10k-labels-idx1-ubyte $EXAMPLE/mnist_test_${BACKEND} --backend=${BACKEND}
echo "Done."
注意第8,17,19行
重新运行命令。
这时,examples\mnist中多了两个文件夹:mnist_train_lmdb
和mnist_test_lmdb
。
最后,打开./examples/mnist/train_lenet.sh,修改为:
#!/usr/bin/env sh
set -e
./build/tools/Release/caffe train --solver=examples/mnist/lenet_solver.prototxt $@
并保存为UNIX格式。
运行:
cd $CAFFE_ROOT
./examples/mnist/train_lenet.sh
最后的几行为:
I0413 09:13:29.063158 10764 solver.cpp:398] Test net output #0: accuracy = 0.9905
I0413 09:13:29.063158 10764 solver.cpp:398] Test net output #1: loss = 0.0293514 (* 1 = 0.0293514 loss)
I0413 09:13:29.063158 10764 solver.cpp:316] Optimization Done.
I0413 09:13:29.063158 10764 caffe.cpp:260] Optimization Done.
达到了99.05%的正确率。