# Deep Learning Environment Setup for Windows

Environment Setup | 05 February 2017

In this blog post, we will setup the environment for Deep Learning in Windows 10. At the end of this post, we will have a machine that is ready to run latest Deep Learning libraries such as Theano, TensorFlow and Keras. We will also learn how to enable GPU to speed up training Deep Neural Networks using CUDA. Let’s jump right in!

### Hardware

All the following steps are performed and checked with my hardware. My hardware specs are given below.

• CPU: Intel i7-7500U CPU @ 2.70GHz, 16 GB RAM
• OS:  Windows 10 (64-bit)
• GPU: NVIDIA GeForce 940MX, 4 GB RAM

### Directory structure

Let’s start with the directory structure which we will follow in this entire post. This is a mandatory step else it becomes more difficult later. Create a folder named “deeplearning” in C drive.

1
C:\deeplearning


This is the master folder inside which we will keep all the dependencies listed above except Visual Studio 2015 Community Edition.

### Environment variables

Before getting into installing dependencies, please become familiar with “Environment variables” and “path”. If you are not familiar with these two terms in Windows, it is highly recommended to read this. Else go to the next section.

• Go to “This PC” (or Computer) -> Right click it -> Select “Properties”.
• Select “Advanced System Settings”
• Select “Environment Variables”
• After that you will see a window having “User Variables” and “System Variables”.
• We will be highly using the “System Variables” for this post. Before getting into the next step, have a look at this System Variables and find “path” variable in that.
• We can create a new system variable, edit it, assign a value to it (normally a path) and delete it.
• We will be using “System Variables” a lot in this post. So, please become familiar with this before getting further.

The following are the software, libraries and tools needed to setup Deep Learning environment in Windows 10 (64-bit).

### Visual Studio 2015 Community Edition

• Go to this website and click on Download”. You will be taken to the page as shown below.

Update: At the time of writing this post, the above URL worked. In case, if it doesn't, check out this link.

• Check “vs_community.exe” and choose next. The download will start. After download finishes, run the setup and choose the config settings as shown below.

#### Adding environment variables

• Add “C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin” to “path” in System Variables.
• Create a system variable named “INCLUDE” with the value “C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt”
• Create a system variable named “LIB” with the value “C:\Program Files (x86)\Windows Kits\10\Lib\10.0.10240.0\um\x64;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.10240.0\ucrt\x64”

### Anaconda (64-bit)

I have installed Anaconda 64-bit installer to get Python 2.7. This worked without any error!

• Go to this website and select “Anaconda 64-bit installer” under Python 2.7.

Update (13/12/2017): If you need TensorFlow as the backend for Keras in Windows 10, you can now do that by installing Anaconda Python 3.6 (64-bit) installer. Choose Python 3.6 64-bit Graphical Installer from that link and download it.

• Run the setup and follow the installation.
• Choose the installation directory as -
1
>>> C:\deeplearning\anaconda

• In the next screen, check the two options to add anaconda and python 2.7 to “path”.

#### Adding environment variables

• Create a system variable named “PYTHON_HOME” with the value “C:\deeplearning\anaconda”
• Add the following in “path” under System Variables. (Add all these one by one by double clicking the value of “path” and clicking “New”)
• %PYTHON_HOME%
• %PYTHON_HOME%\Scripts
• %PYTHON_HOME%\Library\bin
• After adding the variables in path, open up a command prompt as administrator and execute the following command.
1
>>> conda install libpython


### CUDA 8.0 (64-bit)

To enable GPU to speed up training neural networks for Deep Learning, we need to install CUDA.

• Run the setup and choose the installation directory as -
1
>>> C:\deeplearning\cuda

• After completing the installation, the installer automatically creates “CUDA_PATH” in System Variables. Check this, else you need to add it for sure.
• In case if you don’t find CUDA related environment variables, follow the steps below -

#### Adding environment variables

• Create a system variable named “CUDA_PATH” with the value “C:\deeplearning\cuda”
• Add the following in “path” under System Variables.
• %CUDA_PATH%\libnvvp
• %CUDA_PATH%\bin

### MinGW-w64 (5.4.0)

• Go to this website and download the “mingw-w64-install.exe”.
• After downloading, run the setup and choose the installation directory as -
1
>>> C:\deeplearning\mingw-w64-5.4.0

• Choose the config options as shown below -

#### Adding environment variables

• Create a system variable named “MINGW_HOME” with the value “C:\deeplearning\mingw-w64-5.4.0”
• Add the following in “path” under System Variables.
• %MINGW_HOME%\mingw64\bin

### Theano

We will be installing Theano 0.8.2 using git from our command prompt.

• Create a folder named “theano-0.8.2” in “C:\deeplearning”.
1
>>> C:\deeplearning\theano-0.8.2

• Open command prompt as administrator and type the following -
1
2
>>> C:\deeplearning\
>>> git clone https://github.com/Theano/Theano.git theano-0.8.2 --branch rel-0.8.2

• Wait for the repository to get cloned and checked out.
• Enter into the theano folder and install it using the following commands
1
2
>>> cd C:\deeplearning\theano-0.8.2
>>> python setup.py install --record installed_files.txt


### OpenBLAS 0.2.14

• Go to this website to download “OpenBLAS” which is needed to perform parallel computation by running both CPU and GPU together. For example, data augmentation could be performed by CPU while the GPU could be used to speed up training the neural network.
• Download the .zip file and extract the files to the folder -
1
>>> C:\deeplearning\openblas-0.2.14-int32


#### Adding environment variables

• Create a system variable named “OPENBLAS_HOME” with the value “C:\deeplearning\openblas-0.2.14-int32”
• Add the following in “path” under System Variables.
• %OPENBLAS_HOME%\bin

### Enabling CPU or GPU

To switch between CPU and GPU, we can do a simple trick. Theano uses the processing unit by checking the variable “THEANO_FLAGS” in “path” under System Variables.

• To use CPU, copy-paste the following as the value to “THEANO_FLAGS” in “path”.
1
floatX=float32,device=cpu,lib.cnmem=0.8,blas.ldflags=-LC:/deeplearning/openblas-0.2.14-int32/bin -lopenblas

• To use GPU, copy-paste the following as the value to “THEANO_FLAGS” in “path”.
1
floatX=float32,device=gpu,dnn.enabled=False,lib.cnmem=0.8,blas.ldflags=-LC:/deeplearning/openblas-0.2.14-int32/bin -lopenblas


### OpenCV 3.1.0

For Python 2.7
Forget searching Google like “How to install OpenCV in Windows 10”. You will find more complicated procedures and techniques to install it, which fails the most. The simplest solution is to use Anaconda. This is the 100% working solution in my case. I tried a lot fighting with CMAKE, git, compiler issues etc.. and found this as the best solution. In case if you find any other method, kindly let me know.

• Open up a command prompt and enter the following command.
1
>>> conda install -c https://conda.anaconda.org/menpo opencv3


For Python 3.6

• Open up a command prompt and enter the following command.
1
2
>>> pip install opencv-python
>>> pip install opencv-contrib-python


After installing it, type the following command.

1
2
3
>>> python
>>> import cv2
>>> cv2.__version__


You should get something like this.

### Intermediate check

If you followed this post carefully till here, you should end up with your “System Variables” as shown below.

### Keras

Update (13/12/2017): You can now install Keras and TensorFlow using pip in Windows 10, if you have installed Python 3.6 from Anaconda website. It is as simple as two pip commands given below.

1
2
>>> pip install tensorflow
>>> pip install keras


For Python 2.7
We will be installing Keras 1.1.0 using git from our command prompt.

• Create a folder named “keras-1.1.0” in “C:\deeplearning”.
1
>>> C:\deeplearning\keras-1.1.0

• Open command prompt as administrator and type the following -
1
2
>>> C:\deeplearning\
>>> git clone https://github.com/fchollet/keras.git keras-1.1.0 --branch 1.1.0

• Wait for the repository to get cloned and checked out.
• Enter into the keras folder and install it using the following commands.
1
2
>>> cd C:\deeplearning\keras-1.1.0
>>> python setup.py install --record installed_files.txt


Since, we are using Theano, we don’t need TensorFlow as the backend for Keras. But Keras comes in default with TensorFlow as its backend. To change this, navigate to the folder -

1


Here, username is your name. You will find “keras.json” file. Open it and change it like this.

Update (13/12/2017): If you want to use TensorFlow as the backend for Keras, you need to change "image_dim_ordering" to "tf" and "backend" to "tensorflow".

### Final check

If you followed all the steps correctly till here, you can execute the following final check to verify the installation of all the dependencies.

Note: I have installed CUDA 8.0 in a different folder. But it still worked. Make sure you assign "CUDA_PATH" variable in System Variables to the correct folder where it got installed. If it was automatically mapped to the correct path, leave it as it is.

• Open up a text editor and copy-paste the following code.
• Save it in “Desktop” as “test.py”.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from theano import function, config, shared, sandbox
import theano.tensor as T
import numpy
import time

vlen = 10 * 30 * 768  # 10 x #cores x # threads per core
iters = 1000

rng = numpy.random.RandomState(22)
x = shared(numpy.asarray(rng.rand(vlen), config.floatX))
f = function([], T.exp(x))
print(f.maker.fgraph.toposort())
t0 = time.time()
for i in range(iters):
r = f()
t1 = time.time()
print("Looping %d times took %f seconds" % (iters, t1 - t0))
print("Result is %s" % (r,))
if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):
print('Used the cpu')
else:
print('Used the gpu')


If you want to use CPU -

• Open the “Environment Variables” and go to “System Variables” -> “THEANO_FLAGS”
• Double click the value of “THEANO_FLAGS” and change it to -
1
floatX=float32,device=cpu,lib.cnmem=0.8,blas.ldflags=-LC:/deeplearning/openblas-0.2.14-int32/bin -lopenblas


If you want to use GPU -

• Open the “Environment Variables” and go to “System Variables” -> “THEANO_FLAGS”
• Double click the value of “THEANO_FLAGS” and change it to -
1
floatX=float32,device=gpu,dnn.enabled=False,lib.cnmem=0.8,blas.ldflags=-LC:/deeplearning/openblas-0.2.14-int32/bin -lopenblas

• Open the command prompt and navigate to the “Desktop”. * Run the following command.
1
2
>>> python test.py

• I have enabled GPU and I got the following result.
• In case if you meet any error, go to -
1

• Select all folders, delete the contents and try again. It will work. Else, restart the system once and try again.

### 11. Installing additional libraries

If you installed pip for windows, it is much simpler to install some additional packages needed for development. Some are listed below. Open up command prompt as administrator and enter the commands listed.

1
2
3
4
5
>>> pip install scikit-learn
>>> pip install mahotas
>>> pip install jupyter
>>> pip install imutils
>>> pip install librosa


### Installing cuDNN

• Go to this website, register and download cuDNN for CUDA 8.0 and Windows 10.
• Copy the contents from each of the folder (bin, include, lib) inside cuDNN folder and paste it in CUDA path having (bin, include, lib) respectively. That is -
• Copy the contents inside “C:\Users<username>\Downloads\cuDNN\bin” folder to “C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\bin”
• Copy the contents inside “C:\Users<username>\Downloads\cuDNN\include” folder to “C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include”
• Copy the contents inside “C:\Users<username>\Downloads\cuDNN\lib\x64” folder to “C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\lib\x64”
• After performing the above steps, if you want to use cuDNN to speed up your Deep Learning process, you must change the THEANO_FLAGS as shown below.

If you want to use cuDNN and GPU-

• Open the “Environment Variables” and go to “System Variables” -> “THEANO_FLAGS”
• Double click the value of “THEANO_FLAGS” and change it to -
1
floatX=float32,device=gpu,optimizer_including=cudnn,lib.cnmem=0.8,dnn.conv.algo_bwd_filter=deterministic,dnn.conv.algo_bwd_data=deterministic,blas.ldflags=-LC:/deeplearning/openblas-0.2.14-int32/bin -lopenblas


Once you have updated the path, open up command prompt and type the following.

1
2
>>> python
>>> import theano


You should get something like this.

That’s it. You are all set to begin your journey in Deep Learning using Windows 10. Feel free to share this link to someone who is struggling to setup environment for Deep Learning and Computer Vision in Windows 10.

In case if you found something useful to add to this article or you found a bug in the code or would like to improve some points mentioned, feel free to write it down in the comments. Hope you found something useful here.

Happy learning!