Retrieving training data
- Load dataset using this link . Unzip the zip file. There will be different folders with images for different mathematical symbols. For simplicity, use 0-9 digits, + ,? -? And, since the images are in our equation solver. Observing the dataset, we can see that it is biased for some numbers / characters as it contains 12000 images for one character and 3000 images for others. To correct this misalignment, reduce the number of images in each folder to approx. 4000.
- We can use outline extraction to get features.
- Invert the image and then convert it to binary image because extracting contours is best when the object is white and the environment is black.
- Use findContour to find contours. For objects, get the bounding rectangle-method/">rectangle of the path using the boundingRect function (the bounding rectangle-method/">rectangle — is the smallest horizontal rectangle-method/">rectangle that encloses the entire path).
- Since each image in our dataset contains only one character / digit, we only the bounding rectangle-method/">rectangle of the maximum size is needed. To do this, we calculate the area of the bounding rectangle-method/">rectangle of each path and select the rectangle-method/">rectangle with the maximum area.
- Now change the maximum size of the bounding rectangle-method/">rectangle to 28 by 28. Change it to 784 by 1. This will now have 784 pixel values or function. Now assign a label to it (for example, for 0-9 images, the same label as their digit, for — assign a label 10, for + assign a label 11, for a time stamp, assign a label 12). So now our dataset contains 784 feature columns and one label column. After extracting the functions, save the data to a CSV file.
Train the data using a convolutional neural network
- Since a convolutional neural network operates on 2D data, and our dataset has a shape of 785 to 1. So we need to change it. First, assign the y_train variable to the label column in our dataset. Then drop the label column from the dataset and then change it to 28 to 28. Our dataset is now ready for CNN.
- To create a CNN, import all required libraries.
|
|
- Use the following lines of code to fit the CNN to the data.
|
- Training our model will take about three hours with an accuracy of 98.46%. After training, we can save our model as a json file for future use so that we don’t have to train our model and wait three hours each time. To save our model, we can use the following line of codes.
|
Testing our model or solving an equation with it
- First, import to shu the saved model using the following line of codes.
|
figure>
- Download the complete code for solving handwritten equations here .
Shop
Latest questions