8/10/2021

Overview

This is a digit recognition app to predict digits written by users.

User writes a digit using a mouse on the canvas located in the left-side, and it will make the prediction. The result also includes the probability of each label, so you can see how confidently it is predicted.

Model

The model is using the following simple 2-layer CNN.

library(keras)
source('src/model.R')  # run from app home directory

model <- keras_model_sequential() %>%
  layer_conv_2d(filters=16, kernel_size=c(3,3), activation='relu') %>%
  layer_max_pooling_2d(pool_size=c(2,2)) %>%
  layer_conv_2d(filters=16, kernel_size=c(3,3), activation='relu') %>%
  layer_max_pooling_2d(pool_size=c(2,2)) %>%
  layer_flatten() %>%
  layer_dense(units=128, activation='relu') %>%
  layer_dense(units=10, activation='softmax')

train_model(model)  # run with verbose=0
## 
## Final epoch (plot to see history):
##         loss: 0.01351
##     accuracy: 0.9956
##     val_loss: 0.05157
## val_accuracy: 0.9883

Brief summary of the result

As shown in the following image, it records the prediction result with success/failure counts. This makes it easy to see which number is hard to predict. (In this example, failed more often to predict \(6\), and it mistakenly predicted \(5\))

Update the model

This app provides a feature to download all images drawn by users and the labels associated with the images. (Top right button)

This helps to apply further model training, and more importantly, the model can adjust the users writing habit rather than public MNIST dataset. Thus, you can improve to predict some specific digits as shown in the previous page.

Demo: https://cds0123.shinyapps.io/handwritten-digit-recognition/ (shinyapp)

Source code: https://github.com/cds0123/handwritten-digit-recognition