Installing Keras with Theano on Windows for Practical Deep Learning For Coders, Part 1

Posted on Posted in AI, CUDA

The below instructions should have you set up with both Keras 1.2.2 and theano 0.9 on Windows 8.1 in 30 minutes or less, depending on the speed of your internet connection. I can confirm that this set up is suitable for all the lessons in the fantastic Practical Deep Learning For Coders, Part 1, course.

The only requirements are:

Providing you have a suitable Nvidia GPU and you have installed both Visual Studio Community 2013 and the CUDA 8.0 Toolkit you are ready to proceed onto the next steps.

Install Anaconda 2 and bcolz
  1. Install Anaconda 2 to your user directory. If you have spaces in your user directory name, then I would suggest installing it somewhere with no spaces in the path, for example C:\Anaconda2\
  2. Start the Anaconda Prompt, install bcolz (for use in the course) and then update all packages.
    conda install -y bcolz
    conda upgrade -y --all

Install Nvidia cuDNN
  1. Extract the contents of the archive to its current directory.
  2. This should extract to a cudnn-8.0-windows7-x64-v5.1 directory inside the current directory. Copy the contents (bin, include and lib directories) from
    C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\.
    This will copy all the required cuDNN files into the bin, include and lib directories, inside your CUDA 8.0 install.

Install theano 0.9 and verify that it is using the GPU
  1. Install dependencies
    conda install -y numpy scipy mkl-service libpython m2w64-toolchain nose nose-parameterized sphinx pydot-ng
  2. Install theano and pygpu
    conda install -y theano pygpu
  3. Create a .theanorc file in your user directory (e.g. C:\Users\USER_FOLDER_WITHOUT_SPACES\.thanorc) and add the following to
    device = gpu
    floatX = float32
    compiler_bindir=C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin
    enabled = True
    include_path=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include
    library_path=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\lib\x64

    The easiest way to do this, because windows explorer won’t let you create a .theanorc file, is to open up notepad, copy the above text and save with “Save as type” set to “All Files (*.*)”, alternatively you can download from here.
  4. Test your install is using the GPU by running the code provided by the theano team. The output when run through the anaconda prompt should resemble the below, to verify that theano is using the GPU you want to see the text “Used the gpu”.

Install and test Keras 1.2.2

The instructions below will install an older version of Keras, the current version at present is 2.0.6. 1.2.2 is the version which the course was constructed to use, and hence the most widely tested. If you want to install the latest version instead then skip this section and go to Installing the current version of Keras.

  1. Install Keras 1.2.2
    pip install keras==1.2.2
  2. Create a .keras directory inside your user directory (e.g. C:\Users\USER_FOLDER_WITHOUT_SPACES\.keras). The easiest way to do this, because windows explorer won’t let you create a folder starting with a “.” is from the command prompt:
    • Press the windows key + r and type in cmd.
    • This will land you in your user directory so all you need to do is create the directory by typing
      md .keras
  3. Create a keras.json file inside the newly created .keras folder and add the following.
    "image_dim_ordering": "th",
    "epsilon": 1e-07,
    "floatx": "float32",
    "backend": "theano"

    Alternatively you can download the .keras folder from here.
  4. Test your Keras install with the training example shown below.
    from keras.models import Sequential
    from keras.layers import Dense, Activation
    import numpy as np# For a single-input model with 2 classes (binary classification):
    model = Sequential()
    model.add(Dense(32, activation='relu', input_dim=100))
    model.add(Dense(1, activation='sigmoid'))

    # Generate dummy data
    data = np.random.random((1000, 100))
    labels = np.random.randint(2, size=(1000, 1))

    # Train the model, iterating on the data in batches of 32 samples, labels, epochs=10, batch_size=32)

    If everything is installed correctly the output from the above code should be similar to the below.

    Start the Practical Deep Learning For Coders, Part 1 course
    1. Download or clone the course material to your user directory.
    2. Type jupyter notebook in the Anaconda prompt and enjoy the course.

    Installing the current version of Keras

    The latest version of Keras is not guaranteed to work with all the lessons from the Practical Deep Learning For Coders, Part 1 course, however with the changes below I have not had any errors, only warnings regarding API changes.

    1. Install the latest version of Keras.
      pip install keras
    2. Change the following in
      • from keras.regularizers import l2, activity_l2, l1, activity_l1
        from keras.regularizers import l2, l1
      • from keras.utils.layer_utils import layer_from_config
        from keras.layers import deserialize as layer_from_config
      • return np.concatenate([ for i in range(batches.nb_sample)])
        return np.concatenate([ for i in range(batches.n)])
    3. Change the below in your keras.json file
      "image_dim_ordering": "th"
      "image_data_format": "channels_first"
    4. The number of steps in calls to fit_generator, predict_generator and evaluate_generator has changed from the number of images to the number of batches. For example in lesson 3 where you previously had
      trn_features = conv_model.predict_generator(batches, batches.nb_sample)
      you would now put
      trn_features = conv_model.predict_generator(batches, batches.n/batches.batch_size)

    If you have any comments and/or problems then please let me know.

    Leave a Reply

    Your email address will not be published. Required fields are marked *