## Introduction
Image classification is one important field in Computer Vision, not only because so many applications are associated with it, but also a lot of Computer Vision problems can be effectively reduced to image classification. The state of art tool in image classification is Convolutional Neural Network (CNN). In this article, I am going to write a simple Neural Network with 2 layers (fully connected). First, I will train it to classify a set of 4-class 2D data and visualize the decision boundary. Second, I am going to train my NN with the famous MNIST data (you can download it here: https://www.kaggle.com/c/digit-recognizer/download/train.csv) and see its performance. The first part is inspired by CS 231n course offered by Stanford: http://cs231n.github.io/, which is taught in Python.
## Data set generation
First, let’s create a spiral dataset with 4 classes and 200 examples each.
X, y are 800 by 2 and 800 by 1 data frames respectively, and they are created in a way such that a linear classifier cannot separate them. Since the data is 2D, we can easily visualize it on a plot. They are roughly evenly spaced and indeed a line is not a good decision boundary.
## Neural network construction
Now, let’s construct a NN with 2 layers. But before that, we need to convert X into a matrix (for matrix operation later on). For labels in y, a new matrix Y (800 by 4) is created such that for each example (each row in Y), the entry with index==label is 1 (and 0 otherwise).
Next, let’s build a function ‘nnet’ that takes two matrices X and Y and returns a list of 4 with W, b and W2, b2 (weight and bias for each layer). I can specify step_size (learning rate) and regularization strength (reg, sometimes symbolized as lambda).
For the choice of activation and loss (cost) function, ReLU and softmax are selected respectively. If you have taken the ML class by Andrew Ng (strongly recommended), sigmoid and logistic cost function are chosen in the course notes and assignment. They look slightly different, but can be implemented fairly easily just by modifying the following code. Also note that the implementation below uses vectorized operation that may seem hard to follow. If so, you can write down dimensions of each matrix and check multiplications and so on. By doing so, you also know what’s under the hood for a neural network. ## Prediction function and model training
Next, create a prediction function, which takes X (same col as training X but may have different rows) and layer parameters as input. The output is the column index of max score in each row. In this example, the output is simply the label of each class. Now we can print out the training accuracy.
```
## [1] "training accuracy: 0.96375"
```
## Decision boundary
Next, let’s plot the decision boundary. We can also use the caret package and train different classifiers with the data and visualize the decision boundaries. It is very interesting to see how different algorithms make decisions. This is going to be another post.
## MNIST data and preprocessing
The famous MNIST (“Modified National Institute of Standards and Technology”) dataset is a classic within the Machine Learning community that has been extensively studied. It is a collection of handwritten digits that are decomposed into a csv file, with each row representing one example, and the column values are grey scale from 0-255 of each pixel. First, let’s display an image.
Now, let’s preprocess the data by removing near zero variance columns and scaling by max(X). The data is also splitted into two for cross validation. Once again, we need to creat a Y matrix with dimension N by K. This time the non-zero index in each row is offset by 1: label 0 will have entry 1 at index 1, label 1 will have entry 1 at index 2, and so on. In the end, we need to convert it back. (Another way is put 0 at index 10 and no offset for the rest labels.)
## Model training and CV accuracy
Now we can train the model with the training set. Note even after removing nzv columns, the data is still huge, so it may take a while for result to converge. Here I am only training the model for 3500 interations. You can vary the iterations, learning rate and regularization strength and plot the learning curve for optimal fitting.
nnet.mnist <- nnet(X.proc, Y, step_size = 0.3, reg = 0.0001, niteration = 3500) ```
## [1] "training set accuracy: 0.93089140563888"
```
```
## [1] "CV accuracy: 0.912360085734699"
```
## Prediction of a random image
Finally, let’s randomly select an image and predict the label.
```
## [1] "The predicted digit is: 3"
```
displayDigit(Xtest) ## Conclusion
It is rare nowadays for us to write our own machine learning algorithm from ground up. There are tons of packages available and they most likey outperform this one. However, by doing so, I really gained a deep understanding how neural network works. And at the end of the day, seeing your own model produces a pretty good accuracy is a huge satisfaction.
26 Comments
STATWORX
2/7/2016 04:32:46 am
like!
Reply
brian piercy
2/7/2016 04:17:07 pm
Nice!
Reply
2/8/2016 10:53:14 am
In your article you defined:
Reply
BW
2/8/2016 11:23:33 am
createDataPartition is part of the caret library. This is included in the first code chuck.
Reply
2/8/2016 11:09:38 am
Excellent! Tested it with R 3.2.3 and it worked great. A couple suggestions
Reply
2/8/2016 01:33:29 pm
I finally got it to work. I was using the wrong train.csv. Yes folks, there is more than one floating around out there. You must use the one on the Kaggle web site: https://www.kaggle.com/c/digit-recognizer/data?train.csv
Reply
Mai Dang
2/9/2016 02:11:57 pm
I could not "caret" into R 3.2.3 as it always ended up with error as below. how did you load "caret" ?
Reply
Mai
2/9/2016 04:29:38 pm
yes i did as below
Jun
2/9/2016 05:28:11 pm
Do you have the library stats attached?
Reply
Mai
2/9/2016 08:32:22 pm
it's fixed now, thanks Jun
Jun
2/10/2016 06:52:16 am
Cool, thanks for sharing your solution!
Reply
Paolo
2/11/2016 12:42:59 am
Great tips, thanks for sharing.
Reply
Parinya.hi
2/12/2016 12:24:49 am
Really good sharing. Thanks Jun.
Reply
joe4k
2/14/2016 09:15:24 pm
Nice tutorial, thanks for sharing
Reply
Yoshi
2/21/2016 06:05:32 pm
So great. Thanks for sharing.
Reply
Jun
3/1/2016 11:08:55 am
Hi, I just tried training the dataset without removing the NZV rows. It is slower (my code is not optimized I guess) but after 1500 iterations, I can get an accuracy of 93.5%.
Reply
debbie
12/18/2016 08:35:32 pm
Hi - Thank you very much for this detailed tutorial. I tried the digital recognition, but when I tried to fit the model using:
Reply
debbie
12/20/2016 10:04:42 pm
Apologize for the dump question. I should've follow it from beginning. I mistaken the nnet() function was from nnet package. Now I got it!!
Reply
## Leave a Reply. |
## AuthorA mechanical engineer who also loves data. ## Archives
October 2018
## Categories## Blogs I enjoy reading |