Install Keras and PlaidML

install.packages(keras)
install_keras()

CPU-based Keras and TensorFlow was installed.
I installed PlaidML in r-tensorflow env (Site CPU-based Keras and TensorFlow was installed) according to the following site.
Installation Instructions of PlaidML (macOS) In PlaidML installation, Keras ver 2.0.8 might be install with PlaidML.
In this time, after install the PlaidML, Keras ver was updated to 2.2.0.

Next, setup PlaidML to use my computing device:

plaidml-setup (in terminal)

Reference (Japanese): Macでも高速に機械学習できるかもしれないPlaidMLを試してみた

plaidml-setup 

PlaidML Setup (0.3.4)

Thanks for using PlaidML!

Some Notes: * Bugs and other issues: https://github.com/plaidml/plaidml * Questions: https://stackoverflow.com/questions/tagged/plaidml * Say hello: https://groups.google.com/forum/#!forum/plaidml-dev * PlaidML is licensed under the GNU AGPLv3

Default Config Devices: No devices.

Experimental Config Devices: llvm_cpu.0 : CPU (LLVM) opencl_intel_intel(r)_hd_graphics_530.0 : Intel Inc. Intel(R) HD Graphics 530 (OpenCL) opencl_cpu.0 : Intel CPU (OpenCL) metal_amd_radeon_pro_450.0 : AMD Radeon Pro 450 (Metal) opencl_amd_amd_radeon_pro_450_compute_engine.0 : AMD AMD Radeon Pro 450 Compute Engine (OpenCL) metal_intel(r)_hd_graphics_530.0 : Intel(R) HD Graphics 530 (Metal)

Using experimental devices can cause poor performance, crashes, and other nastiness.

Enable experimental device support? (y,n)[n]:y

Multiple devices detected (You can override by setting PLAIDML_DEVICE_IDS). Please choose a default device:

1 : llvm_cpu.0 2 : opencl_intel_intel(r)_hd_graphics_530.0 3 : opencl_cpu.0 4 : metal_amd_radeon_pro_450.0 5 : opencl_amd_amd_radeon_pro_450_compute_engine.0 6 : metal_intel(r)_hd_graphics_530.0

Default device? (1,2,3,4,5,6)[1]:5

Selected device: opencl_amd_amd_radeon_pro_450_compute_engine.0

PlaidML sends anonymous usage statistics to help guide improvements. We’d love your help making it better.

Enable telemetry reporting? (y,n)[y]:y

Almost done. Multiplying some matrices… Tile code: function (B[X,Z], C[Z,Y]) -> (A) { A[x,y : X,Y] = +(B[x,z] * C[z,y]); } Whew. That worked.

Save settings to /Users/XX/.plaidml? (y,n)[y]:y

Success!

Load keras

library("keras"); library("sessioninfo")

Set PlaidML as backend

use_backend(backend = "plaidml")

mnist_cnn_PlaidML

Following is almost same as mnist_cnn exsample.

batch_size <- 128
num_classes <- 10
epochs <- 5
img_rows <- 28
img_cols <- 28
mnist <- dataset_mnist()
x_train <- mnist$train$x
y_train <- mnist$train$y
x_test <- mnist$test$x
y_test <- mnist$test$y
x_train <- array_reshape(x_train, c(nrow(x_train), img_rows, img_cols, 1))
x_test <- array_reshape(x_test, c(nrow(x_test), img_rows, img_cols, 1))
input_shape <- c(img_rows, img_cols, 1)

x_train <- x_train / 255
x_test <- x_test / 255

y_train <- to_categorical(y_train, 10)
y_test <- to_categorical(y_test, 10)

model <- keras_model_sequential() %>%
  layer_conv_2d(filters = 32, kernel_size = c(3,3), activation = 'relu',
                input_shape = input_shape) %>% 
  layer_conv_2d(filters = 64, kernel_size = c(3,3), activation = 'relu') %>% 
  layer_max_pooling_2d(pool_size = c(2, 2)) %>% 
  layer_dropout(rate = 0.25) %>% 
  layer_flatten() %>% 
  layer_dense(units = 128, activation = 'relu') %>% 
  layer_dropout(rate = 0.5) %>% 
  layer_dense(units = num_classes, activation = 'softmax')

summary(model)
## ___________________________________________________________________________
## Layer (type)                     Output Shape                  Param #     
## ===========================================================================
## conv2d_1 (Conv2D)                (None, 26, 26, 32)            320         
## ___________________________________________________________________________
## conv2d_2 (Conv2D)                (None, 24, 24, 64)            18496       
## ___________________________________________________________________________
## max_pooling2d_1 (MaxPooling2D)   (None, 12, 12, 64)            0           
## ___________________________________________________________________________
## dropout_1 (Dropout)              (None, 12, 12, 64)            0           
## ___________________________________________________________________________
## flatten_1 (Flatten)              (None, 9216)                  0           
## ___________________________________________________________________________
## dense_1 (Dense)                  (None, 128)                   1179776     
## ___________________________________________________________________________
## dropout_2 (Dropout)              (None, 128)                   0           
## ___________________________________________________________________________
## dense_2 (Dense)                  (None, 10)                    1290        
## ===========================================================================
## Total params: 1,199,882
## Trainable params: 1,199,882
## Non-trainable params: 0
## ___________________________________________________________________________
model %>% compile(
  loss = loss_categorical_crossentropy,
  optimizer = optimizer_adadelta(),
  metrics = c('accuracy')
)

Train model using plaidml (GPU)

model %>% fit(
  x_train, y_train,
  batch_size = batch_size,
  epochs = epochs,
  validation_split = 0.2
)
GPU was worked

GPU was worked

Keras-PlaidML GPU was about 3-4 times fater than Keras-TensorFlow-CPU in my environment.
Finally I got motivated deep learning with R installed on macOS (AMD GPU) !!

session_info()
## ─ Session info ──────────────────────────────────────────────────────────
##  setting  value                       
##  version  R version 3.5.0 (2018-04-23)
##  os       macOS High Sierra 10.13.5   
##  system   x86_64, darwin15.6.0        
##  ui       X11                         
##  language (EN)                        
##  collate  ja_JP.UTF-8                 
##  tz       Asia/Tokyo                  
##  date     2018-06-25                  
## 
## ─ Packages ──────────────────────────────────────────────────────────────
##  package     * version    date       source                             
##  backports     1.1.2      2017-12-13 CRAN (R 3.5.0)                     
##  base64enc     0.1-3      2015-07-28 CRAN (R 3.5.0)                     
##  clisymbols    1.2.0      2017-05-21 CRAN (R 3.5.0)                     
##  digest        0.6.15     2018-01-28 CRAN (R 3.5.0)                     
##  evaluate      0.10.1     2017-06-24 CRAN (R 3.5.0)                     
##  htmltools     0.3.6      2017-04-28 CRAN (R 3.5.0)                     
##  jsonlite      1.5        2017-06-01 CRAN (R 3.5.0)                     
##  keras       * 2.1.6      2018-04-29 CRAN (R 3.5.0)                     
##  knitr         1.20       2018-02-20 CRAN (R 3.5.0)                     
##  lattice       0.20-35    2017-03-25 CRAN (R 3.5.0)                     
##  magrittr      1.5        2014-11-22 CRAN (R 3.5.0)                     
##  Matrix        1.2-14     2018-04-13 CRAN (R 3.5.0)                     
##  R6            2.2.2      2017-06-17 CRAN (R 3.5.0)                     
##  Rcpp          0.12.17    2018-05-18 CRAN (R 3.5.0)                     
##  reticulate    1.8.0.9000 2018-06-24 Github (rstudio/reticulate@ea5e9b5)
##  rmarkdown     1.10       2018-06-11 CRAN (R 3.5.0)                     
##  rprojroot     1.3-2      2018-01-03 CRAN (R 3.5.0)                     
##  sessioninfo * 1.0.0      2017-06-21 CRAN (R 3.5.0)                     
##  stringi       1.2.3      2018-06-12 CRAN (R 3.5.0)                     
##  stringr       1.3.1      2018-05-10 cran (@1.3.1)                      
##  tensorflow    1.8        2018-06-14 CRAN (R 3.5.0)                     
##  tfruns        1.3        2018-02-18 CRAN (R 3.5.0)                     
##  whisker       0.3-2      2013-04-28 CRAN (R 3.5.0)                     
##  withr         2.1.2      2018-03-15 CRAN (R 3.5.0)                     
##  yaml          2.1.19     2018-05-01 cran (@2.1.19)                     
##  zeallot       0.1.0      2018-01-28 CRAN (R 3.5.0)