def showNearestMHIs(num, hv, MHIs, labels): # length = len(hv) # num = 5 test_m = [] test_l = [] train_m = hv train_l = labels for i in range(1): rand = num test_m.append(hv[rand,:]) test_l.append(labels[rand,0]) train_m = np.delete(train_m,rand, 0) train_l = np.delete(train_l,rand, 0) pred_labs = predictAction(test_m[0], train_m, np.ravel(train_l), debug = False, k = 4, return_all = True) print(pred_labs) plt.imshow(MHIs[num,:,:]) plt.title("original") fig = plt.figure() j = 1 for f in pred_labs: print('f', f) im = MHIs[f,:,:] a = fig.add_subplot(1, 4, j) imgplot = plt.imshow(im, cmap = cm.Greys_r) a.set_title(str(j)) j += 1
def predict_Leave1out(num,hv,labels): test_m = [] test_l = [] train_m = hv train_l = labels rand = num test_m.append(hv[rand,:]) test_l = labels[rand,0] train_m = np.delete(train_m,rand, 0) train_l = np.delete(train_l,rand, 0) pred_lab = predictAction(test_m[0], train_m, np.ravel(train_l), k = 1) return int(pred_lab), int(test_l)
allMHIs_counter += 1 allLabels = [i for i in xrange(1,6) for j in xrange(4)] allLabels = np.array(allLabels) #%% actions = ['botharms', 'crouch', 'leftarmup', 'punch', 'rightkick'] #actions = ['leftarmup'] #trainMHI = np.empty((480,640,20), dtype=np.float32) #trainMHI_counter = 0 np.save("huVectors", allMoments) np.save("allMHIs", allMHIs) allMoments = allMoments/np.linalg.norm(allMoments) confusionMatrix = np.zeros((5,5)) for actionnum in range(len(actions)): subdirname = basedir + actions[actionnum] + '/' subdir = os.listdir(subdirname) for seqnum in range(len(subdir)): # testMHI = computeMHI(subdirname + subdir[seqnum]) # testMoments = huMoments(testMHI) testMoments = allMoments[actionnum*4 + seqnum] trainMHI = np.delete(allMHIs, (actionnum*4 + seqnum), axis=2) trainMoments = np.delete(allMoments, (actionnum*4 + seqnum), axis=0) trainLabels = np.delete(allLabels, (actionnum*4 + seqnum), axis=0) print actions[actionnum], " and subdir is ", seqnum, " with complete ", subdirname + subdir[seqnum] # confusionMatrix[actionnum][predictAction(testMoments, trainMoments, trainLabels)] += 1 confusionMatrix[actionnum][predictAction(testMoments, allMoments, allLabels)-1] += 1 print confusionMatrix for i in range(5): print "Mean Recognition Rate for "+actions[i] + " class is ", confusionMatrix[i][i] * 25, "%"
#%% allHuMoments = np.empty((20,7), dtype=np.float32) for i in range(allMHIs.shape[2]): allHuMoments[i,:] = huMoments(allMHIs[:,:,i]) #%% #testMHI = computeMHI("./leftarmup/leftarm-up-p2-1") testMHI = computeMHI("./punch/punch-p1-1") testMoments = huMoments(testMHI) actions = ['botharms', 'crouch', 'leftarmup', 'punch', 'rightkick'] #actions = ['leftarmup'] trainMHI = np.empty((480,640,20), dtype=np.float32) trainMHI_counter = 0 for actionnum in range(len(actions)): subdirname = basedir + actions[actionnum] + '/' subdir = os.listdir(subdirname) for seqnum in range(len(subdir)): # cycle through all sequences for this action category trainMHI[:,:,trainMHI_counter] = computeMHI(subdirname + subdir[seqnum]) trainMHI_counter = trainMHI_counter + 1 trainMoments = np.empty((trainMHI.shape[2],7), dtype=np.float32) for i in range(trainMHI.shape[2]): trainMoments[i,:] = huMoments(trainMHI[:,:,i]) trainLabels = [i for i in xrange(1,6) for j in xrange(4)] trainLabels = np.array(trainLabels) print predictAction(testMoments, trainMoments, trainLabels)
if __name__ == '__main__': with open("huVectors.npy", "rb") as f: huVectors = pkl.load(f) num_seq = len(allLabels) num_actions = 5 confusion_matrix = np.zeros((num_actions, num_actions), dtype='int') for test_idx in range(num_seq): mask = np.arange(num_seq) != test_idx train_indices = np.arange(num_seq)[mask] test_moments = huVectors[test_idx] test_label = allLabels[test_idx] train_moments = huVectors[train_indices] train_labels = [allLabels[idx] for idx in train_indices] predicted_label = predictAction(test_moments, train_moments, train_labels)[0] confusion_matrix[test_label - 1][predicted_label - 1] += 1 print(confusion_matrix) orr = 0 for i in range(5): orr += confusion_matrix[i][i] orr = orr / len(allLabels) clr = [] for i in range(5): clr.append(confusion_matrix[i][i] / 4) print(orr, clr)
fmt = '.2f' if normalize else 'd' thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): plt.text(j, i, format(cm[i, j], fmt), horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") plt.ylabel('True label') plt.xlabel('Predicted label') plt.tight_layout() plt.show() # load data # each row is a hu hus = np.load('huVectors.npy') labels = [1] * 4 + [2] * 4 + [3] * 4 + [4] * 4 + [5] * 4 cm = np.zeros((5, 5)) actions = ['botharms', 'crouch', 'leftarmup', 'punch', 'rightkick'] for i in range(20): testVec = hus[i] trainData = np.delete(hus, i, axis=0) trainLabels = np.delete(labels, i, axis=0) prediction = predictAction(testVec, trainData, trainLabels) cm[i / 4, prediction - 1] = cm[i / 4, prediction - 1] + 1 plot_confusion_matrix(cm, actions, normalize=True)