How To Implement .dat File For Handwritten Recognition Using Svm In Python
Solution 1:
I am going to assume that by "how to implement the model" you mean "how to predict the label for a new image".
First off, note that this does not have anything to do with the saved svm_data.dat
per se, unless you want to do this in a different script/session, in which case you can reload your trained svm
object from the file.
With that out of the way, making a prediction for new data requires three steps:
If your new data is somehow different from the training data, preprocess it so it matches the training data (see inverting and resizing below).
Extract features the same way as was done for the training data.
Use the trained classifier to predict the label.
For the example image you uploaded, this can be done as follows:
# Load the image
img_predict = cv2.imread('predict.png', 0)
# Preprocessing: this image is inverted compared to the training data# Here it is inverted back
img_predict = np.invert(img_predict)
# Preprocessing: it also has a completely different size# This resizes it to the same size as the training data
img_predict = cv2.resize(img_predict, (20, 20), interpolation=cv2.INTER_CUBIC)
# Extract the features
img_predict_ready = np.float32(hog(deskew(img_predict)))
# Reload the trained svm# Not necessary if predicting is done in the same session as training
svm = cv2.SVM()
svm.load("svm_data.dat")
# Run the prediction
prediction = svm.predict(img_predict_ready)
print int(prediction)
The output is 0
, as expected.
Note that it is very important to match the data you want to classify to the data you used for training. In this case, skipping the re-sizing step will lead to a mis-classification of the image as 2
.
Furthermore, a closer look at the image reveals that it is still a bit different from the training data (more background, different mean), so I would not be surprised if the classifier ends up performing a bit worse on images like this compared to the test data used in the original tutorial (which was just half of the training data). However, this depends on how sensitive the feature extraction is to the differences between the training images and your prediction images.
Post a Comment for "How To Implement .dat File For Handwritten Recognition Using Svm In Python"