Keras: document how to make it work with plaidML

Created on 30 Nov 2017  路  10Comments  路  Source: rstudio/keras

I am trying to run this on a mac with a AMD Radeon R9 M370X, so I wanted to use the plaidML back-end (https://github.com/plaidml/plaidml) , but I was not able to make it work: it complains it cannot find tensorflow

Most helpful comment

I have it working on Linux using a conda environment. Install process:

  1. Install Anaconda / Miniconda.
  2. Create and activate a virtual environment.
  3. Install the plaidml wheel with pip. That will install the PlaidML version of Keras but will not install TensorFlow.
  4. Install TensorFlow with conda.
  5. Execute plaidml-setup.

At run time, activate the virtual environment on the command line. Then start R or RStudio and type

keras::is_keras_available()

On my machine it returns "TRUE".

Here's the script that installs and tests PlaidML on my system. clpeak and clinfo are open-source utilitlies to look at your OpenCL installation. They should compile on a Mac easily.

#! /bin/bash

export DEST=plaidml-testing
export PYTHON_VERSION="3.6"
export HERE=`pwd`

# conda
conda env remove --yes --name $DEST
conda create --name $DEST --yes python=$PYTHON_VERSION numpy tensorflow
source activate $DEST
conda update --yes --all

# plaidml-keras
pip install -U plaidml-keras
plaidml-setup

# test
pushd /data/Installers
if [ ! -e plaidbench ]
then
  git clone https://github.com/plaidml/plaidbench.git --recursive
fi
cd plaidbench
git pull
pip install -r requirements.txt
clinfo 2>&1 | tee $HERE/clinfo.log
clpeak 2>&1 | tee $HERE/clpeak.log
python plaidbench.py inception_v3 2>&1 | tee $HERE/inception_v3.log
python plaidbench.py mobilenet 2>&1 | tee $HERE/mobilenet.log
python plaidbench.py resnet50 2>&1 | tee $HERE/resnet50.log
python plaidbench.py vgg16 2>&1 | tee $HERE/vgg16.log
python plaidbench.py vgg19 2>&1 | tee $HERE/vgg19.log
python plaidbench.py xception 2>&1 | tee $HERE/xception.log
popd

This was on my to-do list - thanks for insipring me to test it out!

All 10 comments

I have it working on Linux using a conda environment. Install process:

  1. Install Anaconda / Miniconda.
  2. Create and activate a virtual environment.
  3. Install the plaidml wheel with pip. That will install the PlaidML version of Keras but will not install TensorFlow.
  4. Install TensorFlow with conda.
  5. Execute plaidml-setup.

At run time, activate the virtual environment on the command line. Then start R or RStudio and type

keras::is_keras_available()

On my machine it returns "TRUE".

Here's the script that installs and tests PlaidML on my system. clpeak and clinfo are open-source utilitlies to look at your OpenCL installation. They should compile on a Mac easily.

#! /bin/bash

export DEST=plaidml-testing
export PYTHON_VERSION="3.6"
export HERE=`pwd`

# conda
conda env remove --yes --name $DEST
conda create --name $DEST --yes python=$PYTHON_VERSION numpy tensorflow
source activate $DEST
conda update --yes --all

# plaidml-keras
pip install -U plaidml-keras
plaidml-setup

# test
pushd /data/Installers
if [ ! -e plaidbench ]
then
  git clone https://github.com/plaidml/plaidbench.git --recursive
fi
cd plaidbench
git pull
pip install -r requirements.txt
clinfo 2>&1 | tee $HERE/clinfo.log
clpeak 2>&1 | tee $HERE/clpeak.log
python plaidbench.py inception_v3 2>&1 | tee $HERE/inception_v3.log
python plaidbench.py mobilenet 2>&1 | tee $HERE/mobilenet.log
python plaidbench.py resnet50 2>&1 | tee $HERE/resnet50.log
python plaidbench.py vgg16 2>&1 | tee $HERE/vgg16.log
python plaidbench.py vgg19 2>&1 | tee $HERE/vgg19.log
python plaidbench.py xception 2>&1 | tee $HERE/xception.log
popd

This was on my to-do list - thanks for insipring me to test it out!

In my testing this works. Here is what you need to do:

1) Install plaidml for OS X as described on their website: https://github.com/plaidml/plaidml/blob/master/BUILDING.md (note that the bazel in Homebrew is v0.8 and this is currently not compatible with plaidml so you need to install v0.7 of bazel from the Bazel GitHub releases page: https://github.com/bazelbuild/bazel/releases/tag/0.7.0)

2) Instruct Keras to use whatever version of Python plaidml was installed within. For me this was the root Anaconda python at /anaconda3/bin/python:

```r
library(keras)
use_python("/anaconda3/bin/python")
```

3) Execute the code required to install PlaidML as a Keras backend (this was probably not obvious and we can certainly automate this part):

```r
library(reticulate)
pml_keras <- import("plaidml.keras")
pml_keras$install_backend()
```

4) Use Keras as normal:

```r
k_backend()
## [1] "plaidml"
```

I added support for PlaidML to the use_backend() function. Writeup is here: https://keras.rstudio.com/articles/faq.html#how-can-i-use-the-plaidml-backend

many thanks for the quick response!
I was able to run it on my machine. Awesome. Some feedback:
1) I am using use_virtualenv("~/plaidml") instead of use_python
2) some of the example use tensorflow instead of k_XXX, for example https://keras.rstudio.com/articles/examples/deep_dream.html usestf$Variable(0.0), which fails

Thanks! Just added a note to the docs on use_virtualenv / use_condaenv and changed tf$Variable to k_variable in the deep dream example.

Thanks. Now in running deep_dream I get this error that I was not able to figure out by myself:
```> for(layer_name in names(settings$features)){



    • # Add the L2 norm of the features of a layer to the loss

  • coeff <- settings$features[[layer .... [TRUNCATED]
    Error in x[, 4:(out_shape[2] - 1), 4:(out_shape[3] - 1), ] :
    object of type 'environment' is not subsettable
    `` x contains_Op(relu)and it seems you can't apply the[operator on it, i.e.x[1,1]` gives the same error

This error usually happens if you run part of a script that saves some Keras objects then in another session try to use those same objects. I would try clearing your environment and then re running the script from the beginning.

thanks. I tried restarting the r session a couple of time, but the problem persists.

I don't know what's going on then. PlaidML is documented only as an "experimental" back end so unfortunately can't justify/afford the time to run down every odd error message that occurs in examples. Anything you discover here please let us know and we are happy to make any changes you identify that would remedy problems.

totally understand that. I will try to see whether I can reproduce the error in python and report as an issue on the plaidML project.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MaximilianPi picture MaximilianPi  路  5Comments

systats picture systats  路  4Comments

Mrugankakarte picture Mrugankakarte  路  5Comments

fderyckel picture fderyckel  路  4Comments

leonjessen picture leonjessen  路  4Comments