Hello guys, welcome to this new project named "Hand Signs using KNN". There are different types of Hand Signs, but here we use only hand signs of Zero, One, Two, Three, Four & Five. But wait.! we are not using any Deep Learning or Neural Networks in this project. Instead we are using KNN a.k.a K-Nearest Neighbor which is one the Machine Learning Algorithms. We will talk about this later in this post. But how can we give directly images as input to the model, if we use a Machine Learning algorithm? Don't worry about it there are two files (train.h5, test.h5) that consists of features of the images (here features is nothing but pixel values of the image). You can find these files and also full code here.
Now lets talk about K Nearest Neighbor.
Hand Signs |
K Nearest Neighbor(A Supervised Learning Algorithm)
Introduction
KNN algorithm is the laziest algorithm, we can also say KNN as instance based learning. It is called as the laziest algorithm because it does nothing in the training. When we give training data, it just stores the data and does nothing with it. It starts working when we provide testing data. From the figure below red and blue points are the training data and green point is our test data.
Now we will learn how this lazy algorithm works!
Working
We already know that it does nothing in training data. Now, when we pass the test point (green point from the above figure) it works as the following steps
KNN algorithm (K = 5)
- First it finds the distance between the test point and the nearest neighbors to that test point. Now this K is the hyperparameter, that says that how many nearest neighbor should we consider.
- Suppose assume that K=5, as shown in the figure it considers only 5 nearest points to predict the test point.
- Then within that 5 points the majority of them have blue points, so this algorithm concludes that the test point (black point) belongs to blue category.
- Make sure that K value must be an Odd number, if we take even number then we get equal number of points from both categories (here both categories are red and blue).
- This algorithm uses Euclidian distance to calculate the distance between two points.
- In classification problem (discrete value or 1/0) it predicts the majority classes from nearest neighbors.
- In regression problem (continuous value) it predicts the average values of the nearest neighbors.
Now lets start coding!!!
Import necessary modules
Here,
- h5py is used to load train and test files(.h5 files)
- pickle is used to dump our model and use it later
- numpy is used to perform array operations
- matplotlib is used to plot the output images
- sklearn is the machine learning library which contains lots of inbuilt modules.
import h5py
import os
import pickle
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from sklearn import neighbors
Load the dataset
You can load the .h5 files as shown below
filename_train = 'Train_signs.h5'
filename_test = 'Test_signs.h5'
f_train = h5py.File(filename_train, 'r')
f_test = h5py.File(filename_test, 'r')
train_images, train_labels = (list(f_train[list(f_train.keys())[1]]), list(f_train[list(f_train.keys())[2]]))
test_images, test_labels = (list(f_test[list(f_test.keys())[1]]), list(f_test[list(f_test.keys())[2]]))
class_names = list(f_train[list(f_train.keys())[0]])
Preprocess the data
Here,
- we convert pixel values(matrices) to numpy arrays
- here there are 1080 training images, 120 testing images and number of classes is 6 (0 - 5)
train_images=np.array(train_images)
test_images=np.array(test_images)
train_labels=np.array(train_labels)
test_labels=np.array(test_labels)
import keras
train_labels=keras.utils.to_categorical(train_labels,num_classes=6)
test_labels=keras.utils.to_categorical(test_labels,num_classes=6)
train_images=train_images.reshape(1080,-1)
test_images=test_images.reshape(120,-1)
Build the model
Here I used K = 5, because it works well for this project and then fit out training images and training labels to our model and find the accuracy of the model or how well it performs on test data.
knn = neighbors.KNeighborsClassifier(n_neighbors=5)
knn_model=knn.fit(train_images,train_labels)
accuracy=knn_model.score(test_images,test_labels)
print(accuracy)
This gives us accuracy = 0.77. This means out of 10 images it predicts 7 correctly.
Save our model
You can save/dump our model using pickle and you can use it later using pickle.loads
saved_model = pickle.dumps(knn_model)
knn_from_pickle = pickle.loads(saved_model)
Testing
img = mpimg.imread("image_2.jpg")
im = Image.fromarray(img, 'RGB')
im = im.resize((64,64))
img_array = np.array(im).reshape(1,-1)
pred = knn_from_pickle.predict(img_array)
output = "predicted: " + str(np.argmax(pred))
plt.imshow(img)
plt.axis("off")
plt.title(output)
plt.show()
It gives us output as the following image.
OK guys you came the end of this post. I hope you got some information from this post. You can find full code of this project here and also you can find the MNIST handwritten digits recognition project here.
Thank you!
Contacts:
ph.No: +91 9182530027
gmail: hunnurjirao2000@gmail.com
github: github.com/hunnurjirao
Comments
Post a Comment
If you have any doubts please leave it in a comment box