Skip to content

Instantly share code, notes, and snippets.

@jlmelville
Last active January 24, 2021 19:43
Show Gist options
  • Save jlmelville/5edf9d44396b2aa43ac9813b370faeff to your computer and use it in GitHub Desktop.
Save jlmelville/5edf9d44396b2aa43ac9813b370faeff to your computer and use it in GitHub Desktop.
R, keras, tensorflow, windows 10, GPUs

I'm on Windows 10, and also in possession of an NVidia GTX 1080, which I lied to myself was for getting to grips with deep learning and not for playing video games. I was having trouble installing the keras package with GPU support from the instructions. The CPU version installed fine, but when I tried to install the GPU-aware version I got the dreaded "Error: Python module was not found" error. Oh, and I also repeatedly got an error about the wrong version of NumPy being found.

Somehow I managed to fix this. I think the main thing was to manually install a conda environment, rather than leaving it to the install_keras command in R. I don't have much advice on installing CUDA and cuDNN. I definitely installed CUDA 10 (and was using tensorflow 1.13 that is supposed to use that), but I may well have attempted to put previous version on my path during attempts to get this to work, or maybe the conda stuff below took care of all that.

First, I followed Harveen Singh's instructions on installing tensorflow with GPU support:

conda create --name tf_gpu tensorflow-gpu
conda activate tf_gpu
python

Check if tensorflow is using your GPU:

import tensorflow as tf
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))

It should be pretty obvious if tensorflow has found a GPU, because the word "gpu" will show up as well as "cpu", and it should mention the model of the card.

For some reason, after exiting python, I had to close and reopen my Anaconda window to get it to recognize any commands, but you might not have that problem. At any rate, the next step was to install keras with GPU support:

conda activate tf_gpu
conda install keras-gpu
python

and test that keras could see the GPU (similarly, it should mention seeing a GPU as well as a CPU)

from keras import backend as K
K.get_session().list_devices()

Over in R:

library(keras)
use_condaenv("tf_gpu")
backend()$get_session()$list_devices()

You should see the same message as in Python. I was able to get through the tutorial on MNIST without errors. When calling the fit function, I saw a message:

I tensorflow/stream_executor/dso_loader.cc:152] successfully opened CUDA library cublas64_100.dll locally

which seems like a good sign.

Other Useful Python Packages

To use keras from Jupyter Lab and to do useful visualizations, install:

conda install -c conda-forge jupyterlab
conda install nb_conda_kernels
conda install pillow
conda install matplotlib
conda install -c conda-forge opencv

and run jupyter lab from inside the activated tf_gpu environment. nb_conda_kernels seems to be needed so Jupyter can use a different environment as the kernel. Pillow is the Python 3 alternative to PIL, needed to use ImageDataGenerators (which pass image files from disk directly to a model during training). Installing opencv lets you import cv2 to generate class activation maps.

If you forget to install some packages before running Jupyter Lab, according to jakevdp, it's safe to install them like this:

import sys
!conda install --yes --prefix {sys.prefix} matplotlib
@jlmelville
Copy link
Author

Unfortunately, these instructions were for using keras with Tensorflow 1.x. I suspect all of this has changed dramatically with new major versions of Tensorflow (and R) and I haven't tried installing them on Windows since then. Sorry. If I was doing this again, I would probably try getting this to work with WSL2 before attempting the native Windows way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment