|
| 1 | +from PIL import Image |
| 2 | +from numpy import * |
| 3 | +from pylab import * |
| 4 | + |
| 5 | +from svmutil import * |
| 6 | +import os |
| 7 | + |
| 8 | +from PCV.classifiers import knn, bayes |
| 9 | +from PCV.tools import imtools |
| 10 | +from PCV.localdescriptors import dsift, sift |
| 11 | + |
| 12 | + |
| 13 | +""" |
| 14 | +This script collects the three classifiers applied to the hand gesture |
| 15 | +recognition test. |
| 16 | +
|
| 17 | +If you have created the data files with the dsift features, this will |
| 18 | +reproduce all confucion matrices in Chapter 8. |
| 19 | +
|
| 20 | +Use the if statements at the bottom to turn on/off different combinations. |
| 21 | +""" |
| 22 | + |
| 23 | + |
| 24 | +def read_gesture_features_labels(path): |
| 25 | + # create list of all files ending in .dsift |
| 26 | + featlist = [os.path.join(path,f) for f in os.listdir(path) if f.endswith('.dsift')] |
| 27 | + |
| 28 | + # read the features |
| 29 | + features = [] |
| 30 | + for featfile in featlist: |
| 31 | + l,d = sift.read_features_from_file(featfile) |
| 32 | + features.append(d.flatten()) |
| 33 | + features = array(features) |
| 34 | + |
| 35 | + # create labels |
| 36 | + labels = [featfile.split('/')[-1][0] for featfile in featlist] |
| 37 | + |
| 38 | + return features,array(labels) |
| 39 | + |
| 40 | + |
| 41 | +def convert_labels(labels,transl): |
| 42 | + """ Convert between strings and numbers. """ |
| 43 | + return [transl[l] for l in labels] |
| 44 | + |
| 45 | + |
| 46 | +def print_confusion(res,labels,classnames): |
| 47 | + |
| 48 | + n = len(classnames) |
| 49 | + |
| 50 | + # confusion matrix |
| 51 | + class_ind = dict([(classnames[i],i) for i in range(n)]) |
| 52 | + |
| 53 | + confuse = zeros((n,n)) |
| 54 | + for i in range(len(test_labels)): |
| 55 | + confuse[class_ind[res[i]],class_ind[test_labels[i]]] += 1 |
| 56 | + |
| 57 | + print 'Confusion matrix for' |
| 58 | + print classnames |
| 59 | + print confuse |
| 60 | + |
| 61 | + |
| 62 | +# read training data |
| 63 | +#################### |
| 64 | +features,labels = read_gesture_features_labels('../data/hand_gesture/train/') |
| 65 | +print 'training data is:', features.shape, len(labels) |
| 66 | + |
| 67 | +# read test data |
| 68 | +#################### |
| 69 | +test_features,test_labels = read_gesture_features_labels('../data/hand_gesture/test/') |
| 70 | +print 'test data is:', test_features.shape, len(test_labels) |
| 71 | + |
| 72 | +classnames = unique(labels) |
| 73 | +nbr_classes = len(classnames) |
| 74 | + |
| 75 | + |
| 76 | +if False: |
| 77 | + # reduce dimensions with PCA |
| 78 | + from PCV.tools import pca |
| 79 | + |
| 80 | + V,S,m = pca.pca(features) |
| 81 | + |
| 82 | + # keep most important dimensions |
| 83 | + V = V[:50] |
| 84 | + features = array([dot(V,f-m) for f in features]) |
| 85 | + test_features = array([dot(V,f-m) for f in test_features]) |
| 86 | + |
| 87 | + |
| 88 | +if True: |
| 89 | + # test kNN |
| 90 | + k = 1 |
| 91 | + knn_classifier = knn.KnnClassifier(labels,features) |
| 92 | + res = array([knn_classifier.classify(test_features[i],k) for i in range(len(test_labels))]) # TODO kan goras battre |
| 93 | + |
| 94 | + |
| 95 | +if False: |
| 96 | + # test Bayes |
| 97 | + bc = bayes.BayesClassifier() |
| 98 | + blist = [features[where(labels==c)[0]] for c in classnames] |
| 99 | + |
| 100 | + bc.train(blist,classnames) |
| 101 | + res = bc.classify(test_features)[0] |
| 102 | + |
| 103 | + |
| 104 | +if False: |
| 105 | + # test SVM |
| 106 | + # convert to lists for libsvm |
| 107 | + features = map(list,features) |
| 108 | + test_features = map(list,test_features) |
| 109 | + |
| 110 | + # create conversion function for the labels |
| 111 | + transl = {} |
| 112 | + for i,c in enumerate(classnames): |
| 113 | + transl[c],transl[i] = i,c |
| 114 | + |
| 115 | + # create SVM |
| 116 | + prob = svm_problem(convert_labels(labels,transl),features) |
| 117 | + param = svm_parameter('-t 0') |
| 118 | + |
| 119 | + # train SVM on data |
| 120 | + m = svm_train(prob,param) |
| 121 | + |
| 122 | + # how did the training do? |
| 123 | + res = svm_predict(convert_labels(labels,transl),features,m) |
| 124 | + |
| 125 | + # test the SVM |
| 126 | + res = svm_predict(convert_labels(test_labels,transl),test_features,m)[0] |
| 127 | + res = convert_labels(res,transl) |
| 128 | + |
| 129 | + |
| 130 | +# accuracy |
| 131 | +acc = sum(1.0*(res==test_labels)) / len(test_labels) |
| 132 | +print 'Accuracy:', acc |
| 133 | + |
| 134 | +print_confusion(res,test_labels,classnames) |
0 commit comments