Download sfm_code/test_posnet.py from FelixOtt/industry_datasets: direct link, hf CLI and curl.
- Browser
- Download file 4.93 kB
-
https://huggingface.co/datasets/FelixOtt/industry_datasets/resolve/main/sfm_code/test_posnet.py
- Command line
-
hf download hf://datasets/FelixOtt/industry_datasets/sfm_code/test_posnet.py
-
curl -L -o test_posnet.py https://huggingface.co/datasets/FelixOtt/industry_datasets/resolve/main/sfm_code/test_posnet.py
4.93 kB
| import keras | |
| from tqdm import tqdm | |
| import numpy as np | |
| import argparse | |
| from keras import backend as K | |
| import os | |
| import glob | |
| import math | |
| import tensorflow as tf | |
| from keras.backend.tensorflow_backend import set_session | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument('--folder', help='name of the folder', required=True) | |
| parser.add_argument('--test', help='file of the test dataset', required=False) | |
| args = parser.parse_args() | |
| folder = args.folder | |
| test_set = args.test | |
| # Read in the parameter information from the current train folder | |
| f = open(folder + "parameters.txt", "r") | |
| lines = f.read() | |
| testdataset = test_set | |
| modelname = lines.split("\n")[0].split(" ")[-1] | |
| BETA = int(lines.split("\n")[4].split(" ")[-1]) | |
| weights = glob.glob(folder + "weights/*") | |
| startweight = weights[-1] | |
| errthresh = 3 | |
| config = tf.ConfigProto() | |
| config.gpu_options.allow_growth = True | |
| sess = tf.Session(config=config) | |
| set_session(sess) | |
| # Load and create the model | |
| import cnn_mTD_2048 as cnn | |
| model = cnn.create_cnn_lstm(startweight) | |
| import utilities_pos as utilities | |
| # Create the folder to save the results | |
| if not os.path.exists(folder + "/error/"): | |
| os.makedirs(folder + "/error/") | |
| # Create and open the files to save the results | |
| tests_error = open(folder + "/error/" + folder.split("/")[-2] + "_result_" + testdataset.split("/")[-1], "w") | |
| tests_error_pred= open(folder + "/error/" + folder.split("/")[-2] + "_prediction_" + testdataset.split("/")[-1], "w") | |
| def getError(posx, posq, actualx, actualq): | |
| """ Calculates the position and rotation error """ | |
| q1 = actualq / np.linalg.norm(actualq) | |
| q2 = posq / np.linalg.norm(posq) | |
| d = abs(np.sum(np.multiply(q1, q2))) | |
| theta = 2 * np.arccos(d) * 180 / np.pi | |
| errx = np.linalg.norm(actualx - posx) | |
| return errx, theta | |
| # Loss function (not relevant) | |
| def pose_loss3(y_true, y_pred): | |
| return K.sum(K.square(y_pred - y_true)) | |
| def rotation_loss3(y_true, y_pred): | |
| return BETA * K.sum(K.square(y_true-y_pred)) | |
| # compile the model | |
| sgd = keras.optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0) | |
| run_opts = tf.RunOptions(report_tensor_allocations_upon_oom = True) | |
| model.compile(optimizer=sgd, loss=[pose_loss3, rotation_loss3], options=run_opts) | |
| # Load the test data and create the data generator | |
| testdatasource, timestamps = utilities.get_training_data(testdataset) | |
| datagen = utilities.limited_gen_data(testdatasource) | |
| results = [] | |
| results2d = [] | |
| i = 0 | |
| for ims, xs, qs in tqdm(datagen): # predict over all test samples and calculate the error | |
| inputs = np.zeros([1, 3, 224, 224, 3]) | |
| inputs[0, :] = ims | |
| out = model.predict(inputs) | |
| pred_x = out[0][0][1] | |
| pred_q = out[1][0][1] | |
| label_x = xs[1] | |
| label_q = qs[1] | |
| _, theta = getError(pred_x, pred_q, label_x, label_q) | |
| label_x = np.squeeze(label_x) | |
| label_q = np.squeeze(label_q) | |
| pred_x = np.squeeze(pred_x) | |
| pred_q = np.squeeze(pred_q) | |
| if not math.isnan(theta): | |
| # Save the results in the above defined files | |
| error_x = np.linalg.norm(label_x - pred_x) | |
| error_x_2d = np.linalg.norm(label_x - [pred_x[0], pred_x[1], label_x[2]]) | |
| results.append([error_x, theta]) | |
| results2d.append([error_x_2d]) | |
| tests_error.write("\t Image %d: Error xyz: %fm, Error wpqr: %f degrees \n" % (int(i), error_x, theta)) | |
| tests_error.flush() | |
| tests_error_pred.write("Timestamp: " + timestamps[i] + ", Label: x: " + str(label_x[0]) + ", y: " + str(label_x[1]) + ", z: " + str(label_x[2]) + ", w0: " + str(label_q[0]) + ", w1: " + str(label_q[1]) + ", w2: " + str(label_q[2]) + ", w3: " + str(label_q[3]) + " Prediction: x: " + str(pred_x[0]) + ", y: " + str(pred_x[1]) + ", z: " + str(pred_x[2]) + ", w0: " + str(pred_q[0]) + ", w1: " + str(pred_q[1]) + ", w2: " + str(pred_q[2]) + ", w3: " + str(pred_q[3]) + "\n") | |
| tests_error_pred.flush() | |
| i = i + 1 | |
| # Calculate different values for an overview of the results | |
| mean_result = np.mean(results, axis=0) | |
| median_result = np.median(results, axis=0) | |
| percentile50_result_2d = np.percentile(results2d, axis=0, q=50) | |
| percentile95_result_2d = np.percentile(results2d, axis=0, q=95) | |
| percentile50_result = np.percentile(results, axis=0, q=50) | |
| percentile95_result = np.percentile(results, axis=0, q=95) | |
| # Print and save the overview | |
| print("\n Results: \n \t MAE 2D: %f m \n\t Median 2D: %f m \n\t CEP: %f m \n\t CE95: %f m \n\t MAE 2D: %f deg \n\t Median 2D: %f deg \n\t CE95: %f deg \n" % (mean_result[0], median_result[0], percentile50_result_2d[0], percentile95_result_2d[0], mean_result[1], median_result[1], percentile95_result[1])) | |
| tests_error.write("\n Results: \n \t MAE 2D: %f m \n\t Median 2D: %f m \n\t CEP: %f m \n\t CE95: %f m \n\t MAE 2D: %f deg \n\t Median 2D: %f deg \n\t CE95: %f deg \n" % (mean_result[0], median_result[0], percentile50_result_2d[0], percentile95_result_2d[0], mean_result[1], median_result[1], percentile95_result[1])) | |