Compiling OpenCV on Windows 8.1

I’m setting up the latest OpenCV version from source since the Caffe dependency version installed doesn’t have some tools I want to add to my vision processing pipeline.

I started with grabbing the latest version from github
git clone

Then I used the CMake GUI. Visual Studios 12 appears to be the latest buildchain supported

If you haven’t used the GUI version of CMake before, the most import feature you’ll use while setting everything up is the File – Delete Cache command. CMake needs the cache cleared after you fail a configuration attempt or a bad environment generation.

I was really lazy after grabbing the source and didn’t check any of the dependencies involved. CMake found my Cuda installation without any extra config, but there’s a python requirement for both versions 2.7 and 3.4 which I can’t get both to open correctly. %PYTHONHOME% is a serious PITA. Hitting the generate button a few times eventually yielded a successful config regardless.

Once you get the CMake to generate a configuration you can hit the Open Project button to open the project in Visual Studios.
From there, hit Build – Build Solution to start compiling.

Everything appeared able to compile with 6955 warnings and 1 error related to a python27_d.lib linker error that hopefully only broke the python interface.
error LNK1104: cannot open file 'python27_d.lib'

An optimization that’s probably worth is exploring is explicitly picking the CUDA architecture as I think it built it for all possible versions. My suspicion stems from these warnings:
warning : The 'compute_20', 'sm_20', and 'sm_21' architectures are deprecated, and may be removed in a future release (Use -Wno-deprecated-gpu-targets to suppress warning).
And the fact that the opencv directory is now over 8GB which seems on the large side.

Setting Up Caffe on Windows 8.1

It took a fair bit of fussing with CMake, but I’ve successfully compiled Caffe from the Berkeley Vision and Learning Center. There does appear to still be an issue with a psudorandom generator in one of the linked libraries that will probably impair training new nets, but I can currently run existing models and compare images against them for classification.

I’ll outline some of the issues I ran into along the way.

First of all, always check the installation requirements before starting. Per the installation guide:
CUDA 7+ is required for GPU mode
BLAS (Basic Linear Algebra System) – OpenBLAS libraries are downloaded with the dependency install, don’t need to install separate
BOOST – this is a C++ cross compilation libary, don’t install – python dependency script will get it
protobuf, glog, gflags, hdf5 – Don’t install any of these either!
OpenCV – Script will fetch this too
leveldb, lmdb – Script gets one of these

cuDNN – This one you do need to download for an nVidea GPU setup. Ensures fastest operation

Why are these listed in the requirements if you don’t need to install them!? Well if you were compiling from scratch in linux you’d need to get all these installed first.

Python is also listed as optional, but you’ll need it to run the dependency install scripts. Get Anaconda 2.7. 3.5 doesn’t appear to be supported at the moment. Also download CMake 3.4+. I used 3.6.

OK, got CUDA 8, cuDNN 5, and Anaconda 2.7, and CMake 3.4+ installed? Now start following these windows install instructions.

First issue I ran into was a conflicting python install trying to install dependencies since I have probably at least 5 python installs on my machine.
C:\Programming\caffe>py -2.7
File "C:\tools\python\lib\", line 176
SyntaxError: invalid syntax

Double check your %PATH%, user PATH, PYTHONPATH, PYTHONHOME variables. Why do they have so many variables? After scrubbing that I used the Visual Studio 2015 dependency install from the Caffe root.
> python scripts\ --msvc_version=v140

I initially tried using the Ninja generator since it supposedly will build faster. Eventually gave up. One note on that process was that the command prompt needs to run in Admin Mode to use conda install commands at least on Windows8.1

I used the “Visual Studio 14 2015 Win64” CMAKE_GENERATOR. The CMAKE generation command I eventually got to work was
cmake -G%CMAKE_GENERATOR% -DBLAS=Open -DCMAKE_BUILD_TYPE=%CMAKE_CONFIGURATION% -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX="C:/Programming/caffe" -DCUDNN_ROOT="D:/Programming/cudnn-8.0-windows7-x64-v5.1/cuda" -DBoost_COMPILER=-vc140 -DBoost_DEBUG=ON -DBoost_USE_STATIC_LIBS=OFF -DBUILD_python_layer=1 -DBUILD_python=1 -DCUDA_gpu_detect_output=FALSE -DCPU_ONLY=0 -DCUDA_VERBOSE_BUILD=ON -DUSE_OPENCV=ON -C %CAFFE_DEPENDENCIES%\caffe-builder-config.cmake ..\

Had a problem where HDF5 library wasn’t found.

CMake Error at C:/Program Files/Anaconda2/Library/cmake/FindHDF5.cmake:85 (include)
include could not find load file:

Call Stack (most recent call first):
cmake/Dependencies.cmake:39 (find_package)
CMakeLists.txt:68 (include)
Delete everything in your CMAKE build directory and try rebuilding. Fixed it for me anyway.

Once I finally got the exe’s built, I also had to move a bunch of DLL’s to the binary directory that Caffe lives in. Not sure if that’s expected or if that’s an incorrect build flag, but Caffe runs after moving them in.

To setup the pycaffe directory you also need to copy the %CAFFE_ROOT%\python\caffe into your site_packages.

After all that I can use the caffe package in IPython and am able to load different caffe models from the community model zoo .

This guide on using pycaffe is very helpful.

Here’s some example out from the InceptionBN-21K-for-Caffe model.

In [128]: im ='examples/images/sandwich.jpg')

In [129]: net.blobs[‘data’].data[…] = transformer.preprocess(‘data’, im)

In [130]: out = net.forward()

In [131]: top_k = net.blobs[‘softmax’].data[0].flatten().argsort()[-1:-6:-1]

In [132]: print labels[top_k]
[‘n07698401 Bacon-lettuce-tomato sandwich, BLT’
‘n07697825 Bomber, grinder, hero, hero sandwich, hoagie, hoagy, Cuban sandwich,
Italian sandwich, poor boy, sub, submarine, submarine sandwich, torpedo, wedge,
‘n07696403 Sandwich plate’ ‘n07696625 Ham sandwich’
‘n07696839 Club sandwich, three-decker, triple-decker’]

No bacon or ham in there, but pretty good for top 5 results.