Deep Learning in R (Keras)

August 21, 2017

Image Classification in R (Using Keras)

Recently, I found this amazing tutorial on using Keras on Building powerful image classification models using very little data by Francois Chollet. This is hands-down the best tutorial that I have found on Image Classifcation with Deep learning; however, it’s in Python. Here’s my R implementation with a few tweaks.

Setup

Download train.zip Image Dataset from Kaggle’s Dog’s vs Cats Competition

On your Terminal (Mac Terminal):

## For demonstration purposes I unzip the file from the Downloads to Documents

unzip ~/Downloads/train.zip -d ~/Documents/

For Keras to Evaluate, Data must be in this structure(Terminal)

mkdir -p ~/Documents/classification_problem/{train/{cats,dogs},validation/{cats,dogs}}
## Training 1000 images each
cp cat.{0..999}.jpg ~/Documents/classification_problem/train/cats
cp dog.{0..999}.jpg ~/Documents/classification_problem/train/dogs

## Validation 400 images each
cp cat.{1000..1400}.jpg ~/Documents/classification_problem/validation/cats
cp dog.{1000..1400}.jpg ~/Documents/classification_problem/validation/dogs

Setup Keras in R

install.packages("keras")
library(keras)
install_tensorflow()

Pre Training Setup

img_dimensions <- c(150,150)
input_shape <- c(150,150,3)

epochs <- 50
batch_size <- 16

train_data_dir <- '~/Documents/classification_problem/train/'
validation_data_dir <- '~/Documents/classification_problem/validation/'

Keras Model

model <- keras_model_sequential()

model %>% layer_conv_2d(filter = 32, kernel_size = c(3,3), input_shape = input_shape) %>%
  layer_activation("relu") %>%
  layer_max_pooling_2d(pool_size = c(2,2)) %>%
  layer_conv_2d(filter = 32, kernel_size = c(3,3)) %>%
  layer_activation("relu") %>%
  layer_max_pooling_2d(pool_size = c(2,2)) %>%
  layer_conv_2d(filter=64, kernel_size = c(3,3)) %>%
  layer_activation("relu") %>%
  layer_max_pooling_2d(pool_size = c(2,2)) %>%
  layer_flatten() %>%
  layer_dense(64) %>%
  layer_activation("relu") %>%
  layer_dropout(0.5) %>%
  layer_dense(1) %>% 
  layer_activation("sigmoid")

model%>% compile(
  loss = "binary_crossentropy",
  optimizer = "rmsprop",
  metrics = "accuracy"
)

Reading Images and Augmentation

train_datagen <- image_data_generator(
  rescale=1. / 255,
  shear_range=0.2,
  zoom_range=0.2,
  horizontal_flip=TRUE)

test_datagen <- image_data_generator(rescale=1. / 255)

train_generator <- flow_images_from_directory(
  directory = train_data_dir,
  generator = train_datagen,
  target_size = img_dimensions,
  batch_size=batch_size,
  class_mode='binary')


validation_generator <- flow_images_from_directory(
    directory = validation_data_dir,
    generator = test_datagen,
    target_size= img_dimensions,
    batch_size=batch_size,
    class_mode='binary')


nb_train_samples <- length(train_generator$filenames)
nb_validation_samples <- length(validation_generator$filenames)

Fit Model

model %>% fit_generator(generator = train_generator, steps_per_epoch = nb_train_samples %/% batch_size,
  epochs = epochs,
  validation_data = validation_generator,
  validation_steps = nb_validation_samples %/% batch_size
)


model %>% save_model_weights_hdf5('first_try.h5')

If you would like to learn more, I reccomend looking at the python example linked above and reading Deep Learning With Python by Francois Chollet. The first chapter is free!

comments powered by Disqus