the image size from significantly altering the bag feature magnitude, the histogram should be normalized. Code: from sklearn import svm ##########--WRITE YOUR CODE HERE--########## # The following steps are just for your reference # You can write in your own way
IN PYTH0N JUPYT3R N0TEB00K
In order to represent our training and testing images as a bag of feature histograms, we must first create a visual language. By selecting a large number of local features (10s or 100s of thousands) from our training set and clustering them with k-means, we will create this vocabulary. The size of our vocabulary and the size of our features are measured by the number of k-means clusters. As an illustration, you might begin by grouping numerous SIFT descriptors into k=50 clusters. This divides the 128-dimensional continuous SIFT feature space into 50 regions. As long as we keep the centroids of our initial clusters, we can determine which region any new SIFT feature we find belongs to. These centroids represent our visual word wide range.
We display training and testing images as visual word histograms. Many SIFT descriptors will be densely sampled across each image. We simply count how many SIFT descriptors fit into each cluster in our visual word vocabulary rather than storing hundreds of SIFT descriptors. For each SIFT feature, the closest neighbor k-means centroid is located. Therefore, if we recognize 200 different SIFT features in an image and have a vocabulary of 50 visual words, our bag of SIFT representation will be a histogram with 50 dimensions, where each bin counts how many times a SIFT descriptor was to that cluster. There are 200 bin-counts in total. In order to prevent the image size from significantly altering the bag feature magnitude, the histogram should be normalized.
Code:
from sklearn import svm
##########--WRITE YOUR CODE HERE--##########
# The following steps are just for your reference
# You can write in your own way
#
# def train_SVM(X, Y):
# return clf
#
# def predict_SVM(clf, X):
# return predict
#
# clf = train_SVM(train_feat, train_label)
#
# # make predictions on test data
# prediction = [-1]*len(test_feat)
# for i in range(len(test_feat)):
# prediction[i] = predict_SVM(clf, np.reshape(test_feat[i],(1,-1)))
#
# test_label_pred = np.reshape(np.array(predictions),(-1,))
#
def train_SVM(X, Y):
return clf
def predict_SVM(clf, X):
return predict
clf = train_SVM(train_feat, train_label)
prediction = [-1]*len(test_feat)
for i inrange(len(test_feat)):
prediction[i] = predict_SVM(clf, np.reshape(test_feat[i],(1,-1)))
test_label_pred = np.reshape(np.array(predictions),(-1,))
test_label_pred =
##########-------END OF CODE-------##########
accuracy = sum(np.array(test_label_pred) == test_label) / float(len(test_label))
print("The accuracy of Bag of SIFT Representation + one-vs-all SVMs model is {:.2f}%".format(accuracy*100))
Step by step
Solved in 2 steps