Objective
The objective of this post is guide you use Keras with CUDA on your Windows 10 PC.
Install Dependencies
Hardware: A graphic card from NVIDIA that support CUDA, of course.
Driver: Download and install the latest driver from NVIDIA or your OEM website
CUDA: Download and install version 7.5 from https://developer.nvidia.com/cuda-75-downloads-archive DO NOT install outdated driver intergrated in installer. Visual Studio integration and GPU Deployment Kit is not needed.
cuDNN: Download and copy all folder (there should be three: “bin”, “include”, “lib”) in zipped cuda folder to your CUDA installation folder (there should be “bin”, “include”, “lib”) https://developer.nvidia.com/cudnn You may need to register as an NVIDIA Accelerated Computing Developer Program member to process to download.
Python: Download and install Anaconda 2 from https://www.continuum.io/downloads Run conda install mingw libpython
after installation.
Visual Studio: Install version 2010, 2012, or 2013. Newer version is not supported. The free community version is fine. We just need the compiler.
Microsoft Visual C++ Compiler for Python 2.7: Download and install from http://www.microsoft.com/en-us/download/details.aspx?id=44266
GCC: Download and install TDM-GCC from http://tdm-gcc.tdragon.net/
Install Theano and Keras
Keras support Theano or Tensor Flow as backend. However, Tensor Flow with GPU is not support in Windows. So just use Theano as backend.
Just install as a common package of python
pip install theano keras
Configuring Theano
Write a plain text file named .theanorc
(or .theanorc.txt
if previous one is hard to create)in your user folder(C:\Users\<Your username here>\)
Contents is as following
[global] floatX = float32 device = gpu [nvcc] flags=-LC:\Apps\Anaconda2 compiler_bindir=C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin [dnn] enabled = True [lib] cnmem=0.75
“device = gpu” means use gpu resource
Replace “C:\Apps\Anaconda2” to your installation path of your Anaconda 2. There is no space between “flags=-L” and the path.
Replace “C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin” to your installation path of Visual Studio. There should be a “cl.exe” at that path.
Replace “cnmem=0.75” to your free graphic card memory ratio. You may use GPU-Z from https://www.techpowerup.com/gpuz/ to determine how much memory can be allocated to Theano besides your normal usage (for screen display, etc.). Theano will eat that much memory (75% of memory in this configuration) when you initialize Theano every time.
Test Theano
Open python console and run import theano
It may be a little slow, but eventually it will print something like
Using gpu device 0: <Your GPU> (CNMeM is enabled with initial size: <Your cnmem ratio> of memory, cuDNN <numbers, whatever>)
Stop python console if it looks fine.
Configuring Keras
To manually assign Theano backend, change following lines in C:\Users\<Your username here>\.keras\keras.json
{ "image_dim_ordering": "tf", "epsilon": 1e-07, "floatx": "float32", "backend": "theano" }
Replace "image_dim_ordering": "tf",
to "image_dim_ordering": "th",
to use Theano’s image channel order(BGR).
Replace "backend": "tensorflow"
to "backend": "theano"
to assign Theano as backend.
Test Keras
Just run the example from official repositry https://github.com/fchollet/keras/blob/master/examples/imdb_cnn.py
Wait and you will know if it works well. You may also watch GPU-Z to know how many GPU resource is been used.
Conclusion
Now you get a fully workable Keras instance with CUDA acceleration.
Hi,
Thanks for the help. Really very helpful blog. However, ran into a small error:
“Theano nvcc.flags support only parameter/value pairs without space between them. e.g.: ‘–machine 64’ is not supported, but ‘–machine=64’ is supported. Please add the ‘=’ symbol.”
Even though I have followed exact instructions as you mentioned. Anything that comes to mind? may be regarding the editor I am using for the config file? is that possible?
have u solved it? can u tell me how to solve it ?
Never mind, sorted. It was very stupid.