Caffe is a deep learning framework made with expression, speed, and modularity in mind. It is developed by the Berkeley Vision and Learning Center (BVLC) and by community contributors. Yangqing Jia created the project during his PhD at UC Berkeley. Caffe is released under the BSD 2-Clause license. In this tutorial I’ll show you how to compile Caffe with support from nVIDIA’s GPU computing capabilities, CUDA and CUDA Neural Network on a Windows 10-x64 machine.
In order to successfully build Caffe from source, you will need to meet the following prerequisites:
Microsoft Visual Studio 2013
You should have Microsoft Visual Stduio 2013 (which currently is the only version that CUDA 7.5 has compatibility with). Any edition (as long as it is VS2013 with MSVC12 compiler) will work, but if you do not have any of the professional/paid versions, you can simply get the free Community Edition from HERE. You need to have it installed before continuing with the rest of build process, because CUDA 7.5 at this moment only works with this version of Visual Studio and it DOES NOT work with Visual Studio 2015 or later (unless nVIDIA updates its NSIGHT package).
NVIDIA CUDA Toolkit 7.5
You need to install CUDA 7.5 from HERE. Please note that if you are a hardcore gamer ( like myself 😀 ) make sure that uncheck the suggested driver versions since they are really outdated. If you do not care about gaming, you can go ahead and install the drivers which are included with the CUDA installer, as it is necessary to allow the installation of GPU Deployment Kit (which is NOT necessary to build Caffe, just saying!)
After successful installation of CUDA 7.5, make sure that the following environment variables have been added to your system. Double check the correctness of the paths (just to be sure…for comparison, you can see the values which I have in my own system):
CUDA_PATH => C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5
CUDA_PATH_V7_5 => C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5
NVIDIA CUDA Neural Network Library 4
You will need nVIDIA’s CUDA Neural Network library version 4 which can be downloaded from HERE. The correct archive file is named cudnn-7.0-win-x64-v4.0-prod.zip. After extraction, this archive file yields a folder named cuda which is indeed quite confusing. But you only need to copy this folder and paste it in your actual CUDA installation folder. Again, in my case, I pasted this folder right into C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5
Now, pay a visit to your Environment Variables Settings and add the following enteries (replace the given path to the actual place you have installed the CUDA and copied cuda aka cudnn folder into):
CUDNN_PATH => C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5
CuDnnPath=> C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5
Somehow if you do not add these two options, the build process will fail, complaining about not being able to fine cudnn.h file. By now, this file should be in the following path if you have followed my guideline:
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\cuda\include\cudnn.h
NUGET Command-Line utility
You will need to download many dependencies of Caffe using nuget command line utility which can be downloaded from HERE. I am using version 3.X. Just make sure to put this single exe file somewhere which is known by your Path environment (e.g. make a directory named tools in your C: drive and add that directory to Path).
Do I really need to explain this?!
[nextpage title=”Preparing the sources”]Preparing the sources
After making sure that you have met the prerequisites, you can now begin downloading the Caffe source files. For this tutorial, I will use the following directory C:\projects
Cloning Caffe repository and merging it with fixes
Caffe was originally written for GNU/Linux systems, but recently a team from Microsoft has tried to maintain a Windows version. The Windows tree of Caffe can be watched as a branch of original Caffe repository on GitHub.
Open a command prompt (it maybe necessary to open the command prompt as Administrator, if you are facing problems e.g. creating directories in C: drive). To download the Caffe sources, enter the following command:
C:\>git clone https://github.com/BVLC/caffe.git c:\projects\caffe
Now goto the projects\caffe folder and pull and merge the fixes (Please note that you most likely need to check the README file of Caffe’s Windows tree (the build icon in the README) to check for the latest successful builds to see if you need to merge with any recent unoffical changes. The following merge with head #4481 does work as of 19 Jul 2016):
C:\>cd projects\caffe C:\projects\caffe>git fetch -q origin +refs/pull/4481/merge: C:\projects\caffe>git checkout -qf FETCH_HEAD
Now go to the windows directory inside the caffe folder and copy the example properties file (this is a Visual Studio solution properties file) by removing the .example from the file extension:
C:\projects\caffe>cd windows C:\projects\caffe\windows>copy CommonSettings.props.example CommonSettings.props
Downloading the required packages
It is now time to download all the necessary packages for compiling Caffe. Luckily they were kind enough (surprisingly!) to include a nuget configuration file that will download all the necessary packages:
C:\projects\caffe\windows>nuget restore Caffe.sln -PackagesDirectory ..\..\NugetPackages -ConfigFile nuget.config
This will download the dependency tree and to the C:\project\NugetPackages folder.
The only point is, the provided nuget configuration file only downloads the DEBUG build of dependency packages, hence the resulting Visual Studio 2013 solution can only build the DEBUG version sucessfully. If you need the Release version without the overhead of debug symbols, you will have to make changes in the nuget configuration file and tell it to download the Release version of the required packages as well!
[nextpage title=”Building the solution”]Building the solution
Now it is time to compile Caffe from source.
Loading the solution in Visual Studio 2013
Start your Visual Studio 2013 (you may need to do this as Administrator if you faced permission problems for accessing file in the C: drive). Now from File -> Open -> Project/Solutoin load the Caffe.sln file located in C:\projects\caffe\windows folder. After the solution has been loaded, the Solution Explorer window should look like the following image:
Editing compile options
To tell Visual Studio what to build for us (e.g. CPU, GPU, cuDNN, Matlab and Python support) you only need to edit the CommonSettings.props (highlighted in the above image) file. I personally do not care about the Matlab and Python wrappers, but if you would like to have them, follow the guide of the authors:
To build Caffe Python wrapper set
.\windows\CommonSettings.props. Download Miniconda 2.7 64-bit Windows installer from Miniconda website. Install for all users and add Python to PATH (through installer).
Run the following commands from elevated command prompt:
conda install --yes numpy scipy matplotlib scikit-image pip pip install protobuf
After you have built solution with Python support, in order to use it you have to either:
PythonPathenvironment variable to point to
- copy folder
To build Caffe Matlab wrapper set
MatlabDirto the root of your Matlab installation in
After you have built solution with Matlab support, in order to use it you have to:
- add the generated
matcaffefolder to Matlab search path, and
<caffe_root>\Build\x64\Releaseto your system path.
Back to our own journey, to compile Caffe with CUDA and cuDNN you need to make the following changes in the CommonSettings.props file:
- Set the <CpuOnlyBuild> to false
- Set the <UseCuDnn> to true
- Set the correct GPU Architecture by editing the <CudaArchitecture> tag. In my case, I am using a GTX 770 graphics card and I am good with these options: compute_30,sm_30 You can add pairs of compute _xx and sm_xx architectures separated by ‘;‘ characters. If you are not sure what shader model and compute capabilities your graphics card has, you can learn more at nVIDIA’s specification page for your graphics card.
- Give the following path for the <CuDnnPath> option: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5 because as you can see further more in this file, it always appends \cuda\ to this directory.
To make it short, your first few lines of CommonSettings.props file should look like this:
<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ImportGroup Label="PropertySheets" /> <PropertyGroup Label="UserMacros"> <BuildDir>$(SolutionDir)..\Build</BuildDir> <!--NOTE: CpuOnlyBuild and UseCuDNN flags can't be set at the same time.--> <CpuOnlyBuild>false</CpuOnlyBuild> <UseCuDNN>true</UseCuDNN> <CudaVersion>7.5</CudaVersion> <!-- NOTE: If Python support is enabled, PythonDir (below) needs to be set to the root of your Python installation. If your Python installation does not contain debug libraries, debug build will not work. --> <PythonSupport>false</PythonSupport> <!-- NOTE: If Matlab support is enabled, MatlabDir (below) needs to be set to the root of your Matlab installation. --> <MatlabSupport>false</MatlabSupport> <CudaDependencies></CudaDependencies> <!-- Set CUDA architecture suitable for your GPU. Setting proper architecture is important to mimize your run and compile time. --> <CudaArchitecture>compute_30,sm_30;compute_52,sm_52</CudaArchitecture> <!-- CuDNN 3 and 4 are supported --> <!-- EDITED BY SAEID 8======> you must copy cudnn folder here in "cuda" folder--> <CuDnnPath>C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5</CuDnnPath> <ScriptsDir>$(SolutionDir)\scripts</ScriptsDir> </PropertyGroup>
Now, make sure that your build mode is set to Debug and the target architecture is set to x64. Then just press F6 or from Build menu select the Build Solution option. After about 20 minutes (on a Core i5/4670) you should be done!
At the end of build process, the resulted executable and library files will be generated in C:\projects\caffe\Build\x64\Debug folder. Now you can start using Caffe! I also recommend to add this directory to your Path environment variable.
I am planning to run a simple image pattern recognition using Caffe. If I succeeded, I will add a tutorial here in my website! If you faced any problems, let me know in the comment section or in the dedicated forum topic.