By R Studio: details here
devtools::install_github("rstudio/keras")
For those using Conda
library(keras)
install_keras()
library(keras)
install_keras(tensorflow = "gpu")
Using your own python setup
pip install keras
print("Hello world!")
## [1] "Hello world!"
library(keras)
mnist <- dataset_mnist()
x_train <- mnist$train$x
y_train <- mnist$train$y
x_test <- mnist$test$x
y_test <- mnist$test$y
# reshape
x_train <- array_reshape(x_train, c(nrow(x_train), 784))
x_test <- array_reshape(x_test, c(nrow(x_test), 784))
# rescale
x_train <- x_train / 255
x_test <- x_test / 255
model <- keras_model_sequential() # Open an interface to tensorflow
# Set up the neural network
model %>%
layer_dense(units = 256, activation = 'relu', input_shape = c(784)) %>%
layer_dropout(rate = 0.4) %>%
layer_dense(units = 128, activation = 'relu') %>%
layer_dropout(rate = 0.3) %>%
layer_dense(units = 10, activation = 'softmax')
That’s it. Keras makes it easy.
summary(model)
## Model: "sequential_1"
## ___________________________________________________________________________
## Layer (type) Output Shape Param #
## ===========================================================================
## dense (Dense) (None, 256) 200960
## ___________________________________________________________________________
## dropout (Dropout) (None, 256) 0
## ___________________________________________________________________________
## dense_1 (Dense) (None, 128) 32896
## ___________________________________________________________________________
## dropout_1 (Dropout) (None, 128) 0
## ___________________________________________________________________________
## dense_2 (Dense) (None, 10) 1290
## ===========================================================================
## Total params: 235,146
## Trainable params: 235,146
## Non-trainable params: 0
## ___________________________________________________________________________
model %>% compile(
loss = 'sparse_categorical_crossentropy',
optimizer = optimizer_rmsprop(),
metrics = c('accuracy')
)
history <- model %>% fit(
x_train, y_train,
epochs = 30, batch_size = 128,
validation_split = 0.2
)
plot(history)
eval <- model %>% evaluate(x_test, y_test)
eval
## $loss
## [1] 0.1117176
##
## $accuracy
## [1] 0.9812
model %>% save_model_hdf5("../../Data/Session_11-mnist_model.h5")
model <- load_model_hdf5("../../Data/Session_11-mnist_model.h5")
Example output of AlexNet
The first (of 5) layers learned
Inputs:
Input and autoencoder
Generated celebrity images
Input
Output
Goal: Build a classifier based on the images’ content
summary(model)
## Model: "sequential"
## ___________________________________________________________________________
## Layer (type) Output Shape Param #
## ===========================================================================
## conv2d (Conv2D) (None, 254, 254, 32) 896
## ___________________________________________________________________________
## re_lu (ReLU) (None, 254, 254, 32) 0
## ___________________________________________________________________________
## conv2d_1 (Conv2D) (None, 252, 252, 16) 4624
## ___________________________________________________________________________
## leaky_re_lu (LeakyReLU) (None, 252, 252, 16) 0
## ___________________________________________________________________________
## batch_normalization (BatchNormal (None, 252, 252, 16) 64
## ___________________________________________________________________________
## max_pooling2d (MaxPooling2D) (None, 126, 126, 16) 0
## ___________________________________________________________________________
## dropout (Dropout) (None, 126, 126, 16) 0
## ___________________________________________________________________________
## flatten (Flatten) (None, 254016) 0
## ___________________________________________________________________________
## dense (Dense) (None, 20) 5080340
## ___________________________________________________________________________
## activation (Activation) (None, 20) 0
## ___________________________________________________________________________
## dropout_1 (Dropout) (None, 20) 0
## ___________________________________________________________________________
## dense_1 (Dense) (None, 2) 42
## ___________________________________________________________________________
## activation_1 (Activation) (None, 2) 0
## ===========================================================================
## Total params: 5,085,966
## Trainable params: 5,085,934
## Non-trainable params: 32
## ___________________________________________________________________________
history <- model %>% fit_generator(
img_train, # training data
epochs = 10, # epoch
steps_per_epoch =
as.integer(train_samples/batch_size),
# print progress
verbose = 2,
)
plot(history)
eval <- model %>%
evaluate_generator(img_test,
steps = as.integer(test_samples / batch_size),
workers = 4)
eval
## $loss
## [1] 0.7535837
##
## $accuracy
## [1] 0.6572581
history <- readRDS('../../Data/Session_11-tweet_history-30epoch.rds')
plot(history)
summary(model)
## Model: "sequential_2"
## ___________________________________________________________________________
## Layer (type) Output Shape Param #
## ===========================================================================
## conv2d_4 (Conv2D) (None, 62, 62, 96) 34944
## ___________________________________________________________________________
## re_lu_2 (ReLU) (None, 62, 62, 96) 0
## ___________________________________________________________________________
## max_pooling2d_2 (MaxPooling2D) (None, 31, 31, 96) 0
## ___________________________________________________________________________
## batch_normalization_2 (BatchNorm (None, 31, 31, 96) 384
## ___________________________________________________________________________
## conv2d_5 (Conv2D) (None, 21, 21, 256) 2973952
## ___________________________________________________________________________
## re_lu_3 (ReLU) (None, 21, 21, 256) 0
## ___________________________________________________________________________
## max_pooling2d_3 (MaxPooling2D) (None, 10, 10, 256) 0
## ___________________________________________________________________________
## batch_normalization_3 (BatchNorm (None, 10, 10, 256) 1024
## ___________________________________________________________________________
## conv2d_6 (Conv2D) (None, 8, 8, 384) 885120
## ___________________________________________________________________________
## re_lu_4 (ReLU) (None, 8, 8, 384) 0
## ___________________________________________________________________________
## batch_normalization_4 (BatchNorm (None, 8, 8, 384) 1536
## ___________________________________________________________________________
## conv2d_7 (Conv2D) (None, 6, 6, 384) 1327488
## ___________________________________________________________________________
## re_lu_5 (ReLU) (None, 6, 6, 384) 0
## ___________________________________________________________________________
## batch_normalization_5 (BatchNorm (None, 6, 6, 384) 1536
## ___________________________________________________________________________
## conv2d_8 (Conv2D) (None, 4, 4, 384) 1327488
## ___________________________________________________________________________
## re_lu_6 (ReLU) (None, 4, 4, 384) 0
## ___________________________________________________________________________
## max_pooling2d_4 (MaxPooling2D) (None, 2, 2, 384) 0
## ___________________________________________________________________________
## batch_normalization_6 (BatchNorm (None, 2, 2, 384) 1536
## ___________________________________________________________________________
## flatten_2 (Flatten) (None, 1536) 0
## ___________________________________________________________________________
## dense_4 (Dense) (None, 4096) 6295552
## ___________________________________________________________________________
## activation_4 (Activation) (None, 4096) 0
## ___________________________________________________________________________
## dropout_4 (Dropout) (None, 4096) 0
## ___________________________________________________________________________
## batch_normalization_7 (BatchNorm (None, 4096) 16384
## ___________________________________________________________________________
## dense_5 (Dense) (None, 4096) 16781312
## ___________________________________________________________________________
## activation_5 (Activation) (None, 4096) 0
## ___________________________________________________________________________
## dropout_5 (Dropout) (None, 4096) 0
## ___________________________________________________________________________
## batch_normalization_8 (BatchNorm (None, 4096) 16384
## ___________________________________________________________________________
## dense_6 (Dense) (None, 1000) 4097000
## ___________________________________________________________________________
## activation_6 (Activation) (None, 1000) 0
## ___________________________________________________________________________
## dropout_6 (Dropout) (None, 1000) 0
## ___________________________________________________________________________
## batch_normalization_9 (BatchNorm (None, 1000) 4000
## ___________________________________________________________________________
## dense_7 (Dense) (None, 2) 2002
## ___________________________________________________________________________
## activation_7 (Activation) (None, 2) 0
## ===========================================================================
## Total params: 33,767,642
## Trainable params: 33,746,250
## Non-trainable params: 21,392
## ___________________________________________________________________________
plot(history)
YOLOv3
Yolo model and graphing tool from lutzroeder/netron
Diagram from What’s new in YOLO v3 by Ayoosh Kathuria
Think about how facial recognition showed up everywhere for images over the past few years
What creative uses for the techniques discussed today do you expect to see become reality in accounting in the next 3-5 years?
Today, we: