def main(): svg_parser = SvgParser.SvgParser( "ground-truth/locations/", "images/", "task/train.txt", "task/valid.txt", "task/test.txt", "ground-truth/transcription.txt", False) svg_parser.get_cropped_images() print_timer("Cropping") training_samples, validation_samples, test_samples, transcriptions = svg_parser.get_binarized_word_images(50) output_lines = [] print("Keyword spotting...") # Iterate trough test_keywords and for each keyword, find matching training sample, search for it in test samples. with open("test_keywords.txt", 'r') as f: for line in f.readlines(): line = line.rstrip() keyword_and_id = line.split(',') keyword = svg_parser.binarize( "cropped/train/%s.png" % keyword_and_id[1], 50) dtw = DTW(keyword) output_line = [keyword_and_id[0]] print("searching %s" % keyword_and_id[0]) for sample in test_samples: print("comparing to %s" % sample[1]) output_line.append(sample[1]) output_line.append( dtw.calculate_cost_and_matrix(sample[0])[0]) output_lines.append(output_line) print(output_line) with open("output.csv", "w") as f: writer = csv.writer(f) writer.writerows(output_lines)
def apply_dtw(template): dtw = DTW(template) results = [ dtw.calculate_cost_and_matrix(enr) for enr in enrollment if enr.get_user() == template.get_user() ] template.cost = min(results)
def classify(self, seq_test): ''' Classify a sequence based in its 1-nearest neighbor ''' id_class = -1 min_distance = float('inf') for seq in self.training_data: dtw = DTW(seq[1], seq_test) tmp_distance = dtw.iterative_dtw() #recursive_dtw(len(seq[1])-1, len(seq_test)-1) if tmp_distance < min_distance: id_class = seq[0] min_distance = tmp_distance return id_class
def KNN(test, trainSet): k = 5 content = [] ids = [] counter=0 results = [] for item in trainSet: item = item[1] x = DTW(test, item) #print x content.append(x) ids.append(counter) counter += 1 max1 = max(set(content)) min = max1 for j in range(0, k, 1): for i in range(0, len(content), 1): if i in results: continue if content[i] < min: min = content[i] minPos = i results.append(minPos) print minPos, " - ", content[minPos] min = max1 return results
''' spin1_2 = fingers_spin[0] spin2_2 = fingers_spin[1] spin3_2 = fingers_spin[2] spin4_2 = fingers_spin[3] spin5_2 = fingers_spin[4] roll1_2 = fingers_roll[0] roll2_2 = fingers_roll[1] roll3_2 = fingers_roll[2] roll4_2 = fingers_roll[3] roll5_2 = fingers_roll[4] f.close() print(DTW(finger1_1, finger1_2, 0)) print(DTW(finger2_1, finger2_2, 0)) print(DTW(finger3_1, finger3_2, 0)) print(DTW(finger4_1, finger4_2, 0)) print(DTW(finger5_1, finger5_2, 0)) print('/n') print(DTW(spin1_1, spin1_2, 1)) print(DTW(spin2_1, spin2_2, 1)) print(DTW(spin3_1, spin3_2, 1)) print(DTW(spin4_1, spin4_2, 1)) print(DTW(spin5_1, spin5_2, 1)) print('/n') print(DTW(roll1_1, roll1_2, 2)) print(DTW(roll2_1, roll2_2, 2)) print(DTW(roll3_1, roll3_2, 2)) print(DTW(roll4_1, roll4_2, 2))
def print_timer(purpose_message=""): global start_time print("%s timer: %ss" % (purpose_message, int(time.clock()-start_time))) start_time = time.clock() """ Reading Data """ svg_parser = SvgParser.SvgParser("ground-truth/locations/", "images/", "task/train.txt", "task/valid.txt") svg_parser.get_cropped_images() print_timer("Cropping") training_samples, validation_samples = svg_parser.get_binarized_word_images(50) print("binarized training sample size: %s" % len(training_samples)) print_timer("Binarizing") """ Calculating Features """ features = features.calculate_features(training_samples) print("Features of trainingset[0]: %s" % features[0]) print_timer("Feature computation") """ Searching for Keyword """ keyword = svg_parser.binarize("cropped/train/270_32_out.png", 50) dtw = DTW(keyword) results = [dtw.calculate_cost_and_matrix(sample) for sample in training_samples] print_timer("Computing String Distance") print([costs[0] for costs in sorted(results)][:30]) print("Word found in image numbers: &s" % [i for i, x in enumerate(results[:, 0]) if x < 6000])
index_col='tripId') testSet = pd.read_csv( 'test_dataset/test_set_a1.csv', # replace with the correct path sep="\t", converters={"Trajectory": literal_eval}) path = 'Test_Trip/' if not os.path.exists(path): os.makedirs(path) #trainSet = trainSet[:200] for i in range(len(testSet)): start_time = time.time() origin = testSet['Trajectory'].iloc[i] knn = KNN(5, DTW(Harvesine), origin, False) for j in range(len(trainSet)): knn.calculate_neighbor(trainSet['Trajectory'].iloc[j], j) results = knn.results() elapsed_time = time.time() - start_time path = 'Test_Trip/Test_Trip_' + str(i + 1) if not os.path.exists(path): os.makedirs(path) file = open(path + '/results', 'w') file.write("Test_Trip_" + str(i + 1) + "\n") print_map(origin, 'Test_Trip_' + str(i + 1), path) count = 1 for k in results: print_map(trainSet['Trajectory'].iloc[k[0]], 'Neighbor_' + str(count), path) file.write('Neighbor_' + str(count) + '\nJP_ID: ' +
def apply_dtw(template): dtw = DTW(template) results = [dtw.calculate_cost_and_matrix(enr) for enr in enrollment if enr.get_user() == template.get_user()] template.cost = min(results)
def trainTask(self): def stopper(self): raw_input('Press Enter to stop demonstration.') self.stopperBool = False def recordData(self): dataLeft = np.zeros((6, 0)) dataRight = np.zeros((6, 0)) timeArray = np.array([[0.1]]) thread.start_new_thread(stopper, (self, )) while self.stopperBool: startTime = time.time() resultLeft = self.motionProxy.getPosition('LArm', 0, True) resultLeft = np.reshape(resultLeft, (6, 1)) resultRight = self.motionProxy.getPosition('RArm', 0, True) resultRight = np.reshape(resultRight, (6, 1)) dataLeft = np.hstack((dataLeft, resultLeft)) dataRight = np.hstack((dataRight, resultRight)) elapsed = time.time() - startTime + 0.005 timeArray = np.hstack( (timeArray, np.array([[timeArray[0, -1] + elapsed]]))) timeArray = timeArray[:, :(timeArray.size - 1)] self.stopperBool = True return np.vstack((timeArray, dataLeft)), np.vstack( (timeArray, dataRight)) self.motionProxy.setStiffnesses([ 'LShoulderPitch', 'LShoulderRoll', 'LElbowYaw', 'LElbowRoll', 'LWristYaw', 'LHand', 'RShoulderPitch', 'RShoulderRoll', 'RElbowYaw', 'RElbowRoll', 'RWristYaw', 'RHand' ], 0.0) DataLeft = [] DataRight = [] while True: startDemo = raw_input( 'Do you want to start new demonstration? (Y/N): ') if startDemo == 'Y': left, right = recordData(self) DataLeft.append(left) DataRight.append(right) elif startDemo == 'N': print 'Data recording is finished.' break else: print 'Incorrect input given.' self.motionProxy.setStiffnesses([ 'LShoulderPitch', 'LShoulderRoll', 'LElbowYaw', 'LElbowRoll', 'LWristYaw', 'LHand', 'RShoulderPitch', 'RShoulderRoll', 'RElbowYaw', 'RElbowRoll', 'RWristYaw', 'RHand' ], 1.0) inputMat = DataLeft[0][0, :] DataLeft = DTW(DataLeft) DataRight = DTW(DataRight) leftGMR = GMM_GMR(self.numberOfStates) rightGMR = GMM_GMR(self.numberOfStates) leftGMR.fit(DataLeft) rightGMR.fit(DataRight) leftGMR.predict(inputMat) rightGMR.predict(inputMat) fig = plt.figure() ax1 = fig.add_subplot(231) plt.title('Left Hand x-axis trajectory') leftGMR.plot(ax=ax1, plotType="Data") leftGMR.plot(ax=ax1, plotType="Regression") ax2 = fig.add_subplot(232) plt.title('Left Hand y-axis trajectory') leftGMR.plot(yAxis=2, ax=ax2, plotType="Data") leftGMR.plot(yAxis=2, ax=ax2, plotType="Regression") ax3 = fig.add_subplot(233) plt.title('Left Hand z-axis trajectory') leftGMR.plot(yAxis=3, ax=ax3, plotType="Data") leftGMR.plot(yAxis=3, ax=ax3, plotType="Regression") ax4 = fig.add_subplot(234) plt.title('Left Hand x-axis orientation') leftGMR.plot(yAxis=4, ax=ax4, plotType="Data") leftGMR.plot(yAxis=4, ax=ax4, plotType="Regression") ax5 = fig.add_subplot(235) plt.title('Left Hand y-axis orientation') leftGMR.plot(yAxis=5, ax=ax5, plotType="Data") leftGMR.plot(yAxis=5, ax=ax5, plotType="Regression") ax6 = fig.add_subplot(236) plt.title('Left Hand z-axis orientation') leftGMR.plot(yAxis=6, ax=ax6, plotType="Data") leftGMR.plot(yAxis=6, ax=ax6, plotType="Regression") fig = plt.figure() ax1 = fig.add_subplot(231) plt.title('Right Hand x-axis trajectory') rightGMR.plot(ax=ax1, plotType="Data") rightGMR.plot(ax=ax1, plotType="Regression") ax2 = fig.add_subplot(232) plt.title('Right Hand y-axis trajectory') rightGMR.plot(yAxis=2, ax=ax2, plotType="Data") rightGMR.plot(yAxis=2, ax=ax2, plotType="Regression") ax3 = fig.add_subplot(233) plt.title('Right Hand z-axis trajectory') rightGMR.plot(yAxis=3, ax=ax3, plotType="Data") rightGMR.plot(yAxis=3, ax=ax3, plotType="Regression") ax4 = fig.add_subplot(234) plt.title('Right Hand x-axis orientation') rightGMR.plot(yAxis=4, ax=ax4, plotType="Data") rightGMR.plot(yAxis=4, ax=ax4, plotType="Regression") ax5 = fig.add_subplot(235) plt.title('Right Hand y-axis orientation') rightGMR.plot(yAxis=5, ax=ax5, plotType="Data") rightGMR.plot(yAxis=5, ax=ax5, plotType="Regression") ax6 = fig.add_subplot(236) plt.title('Right Hand z-axis orientation') rightGMR.plot(yAxis=6, ax=ax6, plotType="Data") rightGMR.plot(yAxis=6, ax=ax6, plotType="Regression") plt.show() self.trainedTasks.append( (leftGMR.getPredictedMatrix(), rightGMR.getPredictedMatrix()))
import csv from KNN import KNN from sklearn import preprocessing from DTW import DTW from Harvesine import Harvesine from sklearn.cross_validation import train_test_split trainSet = pd.read_csv( '../train_set.csv', # replace with the correct path converters={"Trajectory": literal_eval}, index_col='tripId') train,_ = train_test_split(trainSet,test_size=0.9) #Initialize Encoder le = preprocessing.LabelEncoder() le.fit(train["journeyPatternId"]) y = le.transform(train["journeyPatternId"]) X = train['Trajectory'] knn = KNN(5,DTW(Harvesine)) scoring = ['accuracy'] classifier_pipeline = make_pipeline(knn) scores = cross_validate(classifier_pipeline, X, y, cv=10,scoring=scoring) print mean(scores['test_accuracy'])
def findDistance(self, x, y): return DTW(x, y)