By R Studio: details here
devtools::install_github("rstudio/keras")
For those using Conda
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.
units = 10
maps to the number of categories in the datasummary()
on the model to see what we builtsummary(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')
)
$loss
[1] 0.1117176
$accuracy
[1] 0.9812
98% accurate! Random chance would only be 10%
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
Goal: Build a classifier based on the images’ content
summary(model)
Model: "sequential"
________________________________________________________________________________
Layer (type) Output Shape Param # Trainable
================================================================================
conv2d (Conv2D) (None, 254, 254, 32) 896 Y
re_lu (ReLU) (None, 254, 254, 32) 0 Y
conv2d_1 (Conv2D) (None, 252, 252, 16) 4624 Y
leaky_re_lu (LeakyReLU) (None, 252, 252, 16) 0 Y
batch_normalization (BatchNor (None, 252, 252, 16) 64 Y
malization)
max_pooling2d (MaxPooling2D) (None, 126, 126, 16) 0 Y
dropout (Dropout) (None, 126, 126, 16) 0 Y
flatten (Flatten) (None, 254016) 0 Y
dense (Dense) (None, 20) 5080340 Y
activation (Activation) (None, 20) 0 Y
dropout_1 (Dropout) (None, 20) 0 Y
dense_1 (Dense) (None, 2) 42 Y
activation_1 (Activation) (None, 2) 0 Y
================================================================================
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
YOLOv3
You Only Look Once: Because the algorithm only does one pass (looks once) to classify any and all objects
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
From Redmon and Farhadi (2018) [The YOLO v3 paper]
These work by embedding images and text into the same embedding space
“A photo of the Singapore skyline including Marina Bay Sands”
“Singapore Management University”
“Lithograph of a camel eating a pear”
“A cartoon icon of a dog getting a hair cut.”
“Sustainability data”
“A cavapoo enjoying a nice warm cup of tea”
“Tiny cute isometric living room in a cutaway box, soft smooth lighting, soft colors, purple and blue color scheme, soft colors, 100mm lens, 3d blender render”
Today, we: