Support Vector Machine for Handwritten Text Recognition


The objective of writing this article is to use a very crude approach of doing image classification, in this case, images of handwritten text. While we use Convolutional Neural Network models from scratch or use pre-trained models on MNIST data sets, it’s more preconditioned for this job. We use transfer learning, and in that process as a student myself, I miss out on the very basics. It’s like I am driving an automatic car, wherein I know what the drivetrain, the clutch, the accelerator, and brakes do, but I don’t know anything beyond these. We attempt to break down the problem of handwritten alphabet image recognition into a simple process rather than using heavy packages. This is an attempt to create the data and then build a model using Support Vector Machines for Classification.

Preparing the Data

We are going to manually prepare the data rather than download it from the web. This would allow us to understand our data from an initial stage. We are going to write a few alphabets manually on a white sheet and take images on our phone’s camera. We will then port it to our hard drive. Since it’s an experiment, I don’t want to spend a lot of time in the initial run. I would create data for maybe two or three distinct alphabets for demonstration. Its recommended that you try this mechanism for all the alphabets and check the efficiency. You may need to modify your code when you add more classes of alphabets, but that’s where the learning part will start. We are, right now. just in the training phase.

Structure of Data Storage

We can either write the alphabets on white paper and then extract it using a camera phone or directly use a graphics tool like paint to write using the pen tool. I have created two folders — train and test. In the train folder, we can save folders with the alphabet name, while in the test folder, we can dump the images for which we want to finally classify the instances. The training subfolders have been kept with an intention to have the subfolder name as the training tags. The Testing folder has not been kept in the same form, as we intend to do the classification here.

Figure 1: File Schema of Hand Written Images 

Alternatively, if you want to download the data I have used, right-click on this download data link and open it in a new tab or window. Unzip the folders and you should be able to see the same structure and data as above in your downloads folder. Later, you should create your own data and do the whole process again. This will show you the complete cycle. 

Download the Dependency Packages in RStudio

We will be using the jpeg package in R for Image handling and the Support Vector Machine implementation from the kernlab package. This would be a one-time install. Also, we have made sure the image data has dimensions of 200 x 200 pixels, with a horizontal and vertical resolution of 120dpi. You can try variations in the color channels and resolution later once you have implemented it in the current form.

Loading the Training Data Set

Feature Engineering

Since our intention is to not use a typical CNN approach, we are going to use the white, grey, and black pixel values for new feature creation. We intend to use the summation of all the pixel values of an image instance and save it in a feature called “sum”, the count of all pixels evaluating to zero as “zero”, the count of all pixels evaluating to “ones”, and the sum of all pixels evaluating to values other than zeroes and ones as “in_between”. The “label” feature is extracted from the folder name.

Build the Support Vector Machine Model

Loading the Testing Data Set

Implement for Prediction and Check Metrics

I would encourage you to the learn concepts of Support Vector Machines that couldn’t be explored completely in this article by clicking on this link for my free Data Science and Machine Learning video courses. While we have used the kernlab package and created the classifier, there is a lot of math involved, starting from vector space to kernel tricks. We have worked on the implementation of the classifier, but you should certainly learn the conceptual part of Support Vector Machines and other interesting algorithms.

This UrIoTNews article is syndicated fromDzone