def main(): global num_dense_sample description = 'This script is for testing posenet' parser = argparse.ArgumentParser(description=description) parser.add_argument('input_beacon_setting_file', action='store', nargs=None, const=None, \ default=None, type=str, choices=None, metavar=None, \ help='File path where beacon setting file is saved.') parser.add_argument('input_model_dir', action='store', nargs=None, const=None, \ default=None, type=str, choices=None, metavar=None, \ help='Directory path where input model is saved.') parser.add_argument('output_graph_file', action='store', nargs=None, const=None, \ default=None, type=str, choices=None, metavar=None, \ help='File path where exported graph def protobuf (.pb) file will be saved.') parser.add_argument('output_model_dir', action='store', nargs=None, const=None, \ default=None, type=str, choices=None, metavar=None, \ help='Directory path where output model will be saved.') parser.add_argument('-s', '--use_shrink_model', action='store_true', default=False, \ help='Use shrink model (default: False)') parser.add_argument('-l', '--lstm_model', action='store_true', default=False, \ help='Export LSTM model (default: False)') args = parser.parse_args() input_beacon_setting_file = args.input_beacon_setting_file input_model_dir = args.input_model_dir output_graph_file = args.output_graph_file output_model_dir = args.output_model_dir output_model_file = os.path.join(output_model_dir, "model.ckpt") use_shrink_model = args.use_shrink_model lstm_model = args.lstm_model print "use shrink model for training : " + str(use_shrink_model) # parse beacon setting file beaconmap = IBeaconUtils.parseBeaconSetting(input_beacon_setting_file) beacon_num = len(beaconmap.keys()) # convert hd5 file to ckpt # https://github.com/keras-team/keras/issues/9040 K.set_learning_phase(0) if use_shrink_model: if lstm_model: model = posenet_beacon_no_inception_shrink_lstm_keras.create_posenet(beacon_num, trainable=False) else: model = posenet_beacon_no_inception_shrink_keras.create_posenet(beacon_num, trainable=False) else: print "Do not shrink model is not supported" sys.exit() model.load_weights(os.path.join(input_model_dir, 'trained_weights.h5')) model.summary() #Save graph and checkpoint session = keras.backend.get_session() graph = session.graph graph_def = graph.as_graph_def() with gfile.GFile(output_graph_file, 'wb') as f: f.write(graph_def.SerializeToString()) saver = tf.train.Saver() saver.save(session, output_model_file)
def main(): description = 'This script is for testing posenet' parser = argparse.ArgumentParser(description=description) parser.add_argument('input_txt_file', action='store', nargs=None, const=None, \ default=None, type=str, choices=None, metavar=None, \ help='File path of input txt file in Cambridge Visual Landmark Dataset format.') parser.add_argument('input_beacon_setting_file', action='store', nargs=None, const=None, \ default=None, type=str, choices=None, metavar=None, \ help='File path where beacon setting file is saved.') parser.add_argument('output_model_dir', action='store', nargs=None, const=None, \ default=None, type=str, choices=None, metavar=None, \ help='Directory path where output models will be saved.') parser.add_argument('output_log_dir', action='store', nargs=None, const=None, \ default=None, type=str, choices=None, metavar=None, \ help='Directory path where log files will be saved.') parser.add_argument('-w', '--input_beacon_weight_file', action='store', type=str, default=None, \ help='File path beacon posenet weiht is stored in numpy format.') parser.add_argument('-s', '--use_shrink_model', action='store_true', default=False, \ help='Use shrink model (default: False)') parser.add_argument('-f', '--use_fixed_input_mean_std', action='store_true', default=False, \ help='Use fixed input mean and std (default: False)') parser.add_argument('-a', '--use_augmentation_beacon', action='store_true', default=False, \ help='Use data augmentation for beacon data (default: False)') parser.add_argument('-e', '--epochs', action='store', type=int, default=posenet_config.epochs, \ help='Epochs (Default : ' + str(posenet_config.epochs)) parser.add_argument('-b', '--batch_size', action='store', type=int, default=posenet_config.lstm_batch_size, \ help='Batch size (Default : ' + str(posenet_config.lstm_batch_size)) args = parser.parse_args() input_txt_file = args.input_txt_file input_beacon_setting_file = args.input_beacon_setting_file output_model_dir = args.output_model_dir output_log_dir = args.output_log_dir input_beacon_weight_file = args.input_beacon_weight_file use_shrink_model = args.use_shrink_model use_fixed_input_mean_std = args.use_fixed_input_mean_std use_augmentation_beacon = args.use_augmentation_beacon posenet_config.epochs = args.epochs posenet_config.lstm_batch_size = args.batch_size print "epochs : " + str(posenet_config.epochs) print "batch size : " + str(posenet_config.lstm_batch_size) print "use shrink model for training : " + str(use_shrink_model) print "use fixed input mean and std : " + str(use_fixed_input_mean_std) print "use beacon data augmentation : " + str(use_augmentation_beacon) if input_beacon_weight_file is None: print "please specify one of initial weight or restore directory" sys.exit() # parse beacon setting file beaconmap = IBeaconUtils.parseBeaconSetting(input_beacon_setting_file) beacon_num = len(beaconmap.keys()) output_numpy_mean_beacon_file = os.path.join(output_model_dir, "mean_beacon.npy") output_numpy_model_file = os.path.join(output_model_dir, "model.npy") output_model_file = os.path.join(output_model_dir, "model.ckpt") datasource, mean_beacon = posenet_data_utils.get_beacon_data( input_txt_file, beaconmap, beacon_num, use_fixed_input_mean_std, use_augmentation_beacon) if use_fixed_input_mean_std: print("Skip save mean beacon") else: with open(output_numpy_mean_beacon_file, 'wb') as fw: np.save(fw, mean_beacon) print("Save mean beacon at: " + output_numpy_mean_beacon_file) # Set GPU options gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.4) session = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) KTF.set_session(session) # Train model s_x = K.variable(value=0.0) s_q = K.variable(value=-3.0) if use_shrink_model: model = posenet_beacon_no_inception_shrink_lstm_keras.create_posenet( beacon_num, input_beacon_weight_file) else: print "Do not shrink model is not supported" sys.exit() adam = Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=0.00000001) euc_loss3x_s = posenet_homoscedastic_loss.euc_loss3x_s(s_x=s_x) euc_loss3q_s = posenet_homoscedastic_loss.euc_loss3q_s(s_q=s_q) model.compile(optimizer=adam, loss={ 'beacon_lstm_pose_xyz': euc_loss3x_s, 'beacon_lstm_pose_wpqr': euc_loss3q_s }) model.summary() # Setup checkpointing checkpointer = ModelCheckpoint(filepath=os.path.join( output_model_dir, "checkpoint_weights.h5"), verbose=1, save_weights_only=True, period=1) # Save Tensorboard log logger = TensorBoard(log_dir=output_log_dir, histogram_freq=0, write_graph=True) # Adjust Epoch size depending on beacon data augmentation if use_augmentation_beacon: posenet_config.epochs = posenet_config.epochs / posenet_config.num_beacon_augmentation steps_per_epoch = int( len(datasource.poses_index) / float(posenet_config.lstm_batch_size)) num_iterations = steps_per_epoch * posenet_config.epochs print("Number of epochs : " + str(posenet_config.epochs)) print("Number of training data : " + str(len(datasource.poses_index))) print("Number of iterations : " + str(num_iterations)) history = model.fit_generator( posenet_data_utils.gen_beacon_lstm_data_batch(datasource), steps_per_epoch=steps_per_epoch, epochs=posenet_config.epochs, callbacks=[checkpointer, logger]) model.save_weights(os.path.join(output_model_dir, "trained_weights.h5"))
def main(): global base_model description = 'This script is for testing posenet' parser = argparse.ArgumentParser(description=description) parser.add_argument('input_txt_file', action='store', nargs=None, const=None, \ default=None, type=str, choices=None, metavar=None, \ help='File path of input txt file in Cambridge Visual Landmark Dataset format.') parser.add_argument('input_beacon_setting_file', action='store', nargs=None, const=None, \ default=None, type=str, choices=None, metavar=None, \ help='File path where beacon setting file is saved.') parser.add_argument('input_model_dir', action='store', nargs=None, const=None, \ default=None, type=str, choices=None, metavar=None, \ help='Directory path where input model is saved.') parser.add_argument('result_log_dir', action='store', nargs=None, const=None, \ default=None, type=str, choices=None, metavar=None, \ help='Directory path where localization result files are saved.') parser.add_argument('-s', '--use_shrink_model', action='store_true', default=False, \ help='Use shrink model (default: False)') parser.add_argument('-f', '--use_fixed_input_mean_std', action='store_true', default=False, \ help='Use fixed input mean and std (default: False)') parser.add_argument('-m', '--base_model', action='store', type=str, default=base_model, \ help='Base model : inception-v1/inception-v3/mobilenet-v1 (Default : ' + str(base_model)) args = parser.parse_args() input_txt_file = args.input_txt_file input_beacon_setting_file = args.input_beacon_setting_file input_model_dir = args.input_model_dir result_log_dir = args.result_log_dir use_shrink_model = args.use_shrink_model use_fixed_input_mean_std = args.use_fixed_input_mean_std base_model = args.base_model print "base model : " + str(base_model) print "use shrink model for training : " + str(use_shrink_model) print "use fixed input mean and std : " + str(use_fixed_input_mean_std) if base_model != "inception-v1" and base_model != "inception-v3" and base_model != "mobilenet-v1": print "invalid base model : " + base_model sys.exit() input_image_dir = os.path.dirname(input_txt_file) input_numpy_mean_image_file = os.path.join(input_model_dir, "mean_image.npy") if use_fixed_input_mean_std: input_numpy_mean_image = None else: input_numpy_mean_image = np.load(input_numpy_mean_image_file) input_numpy_mean_beacon_file = os.path.join(input_model_dir, "mean_beacon.npy") if use_fixed_input_mean_std: input_numpy_mean_beacon = None else: input_numpy_mean_beacon = np.load(input_numpy_mean_beacon_file) output_localize_txt_file = os.path.join(result_log_dir, "localize-poses.txt") output_localize_json_file = os.path.join(result_log_dir, "localize-poses.json") output_summary_log_file = os.path.join(result_log_dir, "summary-log.txt") output_hist_log_file = os.path.join(result_log_dir, "hist-log.txt") output_detail_log_file = os.path.join(result_log_dir, "detail-log.txt") # parse beacon setting file beaconmap = IBeaconUtils.parseBeaconSetting(input_beacon_setting_file) beacon_num = len(beaconmap.keys()) if base_model == "inception-v1": image_size = 224 output_auxiliary = True elif base_model == "inception-v3": image_size = 299 output_auxiliary = False elif base_model == "mobilenet-v1": image_size = 224 output_auxiliary = False else: print "invalid base model : " + base_model sys.exit() datasource, image_filenames = get_data(input_txt_file, input_image_dir, input_numpy_mean_image, beaconmap, input_numpy_mean_beacon, use_fixed_input_mean_std) predicted_poses = np.zeros((len(datasource.beacons), 7)) groundtruth_poses = np.zeros((len(datasource.beacons), 7)) results = np.zeros((len(datasource.beacons), 2)) # Set GPU options gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.4) session = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) KTF.set_session(session) # load model if use_shrink_model: if base_model == "inception-v1": model = posenet_image_beacon_no_inception_shrink_keras.create_posenet_inception_v1( beacon_num) elif base_model == "inception-v3": model = posenet_image_beacon_no_inception_shrink_keras.create_posenet_inception_v3( beacon_num) elif base_model == "mobilenet-v1": model = posenet_image_beacon_no_inception_shrink_keras.create_posenet_mobilenet_v1( beacon_num) else: print "invalid base model : " + base_model sys.exit() else: print "Do not shrink model is not supported" sys.exit() model.load_weights(os.path.join(input_model_dir, 'trained_weights.h5')) model.summary() time_array = np.array([]) for i in range(len(datasource.image_filenames)): np_images = posenet_image_utils.preprocess_test_image( datasource.image_filenames[i], input_numpy_mean_image, use_fixed_input_mean_std, 1, False, image_size=image_size) np_beacon = datasource.beacons[i] pose_q = np.asarray(datasource.poses[i][3:7]) pose_x = np.asarray(datasource.poses[i][0:3]) start_time = time.time() sample_predicted = model.predict([np_images, np_beacon]) if output_auxiliary: # 0-3 are results for auxiliary outputs sample_predicted_x = sample_predicted[4] sample_predicted_q = sample_predicted[5] else: sample_predicted_x = sample_predicted[0] sample_predicted_q = sample_predicted[1] elapsed_time = time.time() - start_time time_array = np.append(time_array, elapsed_time) pose_q = np.squeeze(pose_q) pose_x = np.squeeze(pose_x) predicted_q = np.squeeze(sample_predicted_q) predicted_x = np.squeeze(sample_predicted_x) predicted_poses[i, 0] = predicted_x[0] predicted_poses[i, 1] = predicted_x[1] predicted_poses[i, 2] = predicted_x[2] predicted_poses[i, 3] = predicted_q[0] predicted_poses[i, 4] = predicted_q[1] predicted_poses[i, 5] = predicted_q[2] predicted_poses[i, 6] = predicted_q[3] groundtruth_poses[i, 0] = pose_x[0] groundtruth_poses[i, 1] = pose_x[1] groundtruth_poses[i, 2] = pose_x[2] groundtruth_poses[i, 3] = pose_q[0] groundtruth_poses[i, 4] = pose_q[1] groundtruth_poses[i, 5] = pose_q[2] groundtruth_poses[i, 6] = pose_q[3] # calculate error q1 = pose_q / np.linalg.norm(pose_q) q2 = predicted_q / np.linalg.norm(predicted_q) d = abs(np.sum(np.multiply(q1, q2))) # fix floating point inaccuracy if d < -1.0: d = -1.0 if d > 1.0: d = 1.0 theta = 2 * np.arccos(d) * 180 / math.pi error_x = np.linalg.norm(pose_x - predicted_x) results[i, :] = [error_x, theta] print 'Index=', i, ' , Pos Error(m)=', error_x, ', Rot Error(degrees)=', theta # write localize poses with open(output_localize_txt_file, "w") as fw: fw.write("Localization Data V1\n") fw.write("ImageFile, Camera Position [X Y Z W P Q R]\n") fw.write("\n") for idx in range(len(datasource.beacons)): fw.write( str(idx) + " " + ' '.join(['%f' % p for p in predicted_poses[idx, :]]) + "\n") locJsonObj = {} locJsonObj["locGlobal"] = [] for idx, pose in enumerate(predicted_poses): Rh = transformations.quaternion_matrix( [pose[3], pose[4], pose[5], pose[6]]) groundtruth_pose = groundtruth_poses[idx] groundtruth_Rh = transformations.quaternion_matrix([ groundtruth_pose[3], groundtruth_pose[4], groundtruth_pose[5], groundtruth_pose[6] ]) jsonLoc = {} jsonLoc["beacon_idx"] = idx jsonLoc["t"] = [pose[0], pose[1], pose[2]] jsonLoc["R"] = Rh[0:3, 0:3].tolist() jsonLoc["groundtruth"] = [ groundtruth_pose[0], groundtruth_pose[1], groundtruth_pose[2] ] jsonLoc["groundtruthR"] = groundtruth_Rh[0:3, 0:3].tolist() locJsonObj["locGlobal"].append(jsonLoc) with open(output_localize_json_file, "w") as fw: json.dump(locJsonObj, fw) # write histgram results bin_edge = [0.01 * float(x) for x in range(0, 1001)] dist_errors = results[:, 0] dist_hist, dist_hist_bins = np.histogram(dist_errors, bins=bin_edge) dist_hist_cum_ratio = np.cumsum(dist_hist) / float(len(datasource.beacons)) print "Histogram of error: " + str(dist_hist) print "Cumulative ratio of error: " + str(dist_hist_cum_ratio) print "Total loc err larger than " + str( np.max(bin_edge)) + " meters: " + str( len(datasource.beacons) - np.sum(dist_hist)) # write summary of results mean_result = np.mean(results, axis=0) std_result = np.std(results, axis=0) median_result = np.median(results, axis=0) max_result = np.max(results, axis=0) percentile_80_result = np.percentile(results, 80, axis=0) percentile_90_result = np.percentile(results, 90, axis=0) percentile_95_result = np.percentile(results, 95, axis=0) print 'Mean error ', mean_result[0], 'm and ', mean_result[1], 'degrees.' print 'StdDev error ', std_result[0], 'm and ', std_result[1], 'degrees.' print 'Median error ', median_result[0], 'm and ', median_result[ 1], 'degrees.' print 'Max error ', max_result[0], 'm and ', max_result[1], 'degrees.' print '80 percentile error ', percentile_80_result[ 0], 'm and ', percentile_80_result[1], 'degrees.' print '90 percentile error ', percentile_90_result[ 0], 'm and ', percentile_90_result[1], 'degrees.' print '95 percentile error ', percentile_95_result[ 0], 'm and ', percentile_95_result[1], 'degrees.' print 'Mean time ', str(np.average(time_array)) print 'StdDev time ', str(np.std(time_array)) print 'Median time ', str(np.median(time_array)) with open(output_summary_log_file, "w") as fw: fw.write("Number of test image = " + str(len(datasource.beacons)) + "\n") fw.write("Mean error = " + str(mean_result[0]) + " meters." + "\n") fw.write("StdDev error = " + str(std_result[0]) + " meters." + "\n") fw.write("Median error = " + str(median_result[0]) + " meters." + "\n") fw.write("Max error = " + str(max_result[0]) + " meters." + "\n") fw.write("80 percentile error = " + str(percentile_80_result[0]) + " meters." + "\n") fw.write("90 percentile error = " + str(percentile_90_result[0]) + " meters." + "\n") fw.write("95 percentile error = " + str(percentile_95_result[0]) + " meters." + "\n") fw.write("\n") fw.write("Mean error = " + str(mean_result[1]) + " degrees." + "\n") fw.write("StdDev error = " + str(std_result[1]) + " degrees." + "\n") fw.write("Median error = " + str(median_result[1]) + " degrees." + "\n") fw.write("Max error = " + str(max_result[1]) + " degrees." + "\n") fw.write("80 percentile error = " + str(percentile_80_result[1]) + " degrees." + "\n") fw.write("90 percentile error = " + str(percentile_90_result[1]) + " degrees." + "\n") fw.write("95 percentile error = " + str(percentile_95_result[1]) + " degrees." + "\n") fw.write("\n") fw.write("Histogram of error: " + str(dist_hist) + "\n") fw.write("Cumulative ratio: " + str( np.around( np.cumsum(dist_hist, dtype=float) / len(datasource.beacons), 2)) + "\n") fw.write("Total loc err larger than " + str(np.max(bin_edge)) + " meters: " + str(len(datasource.beacons) - np.sum(dist_hist)) + "\n") fw.write("\n") fw.write("Mean time = " + str(np.average(time_array)) + "\n") fw.write("StdDev time = " + str(np.std(time_array)) + "\n") fw.write("Median time = " + str(np.median(time_array)) + "\n") # write error histgram np.savetxt(output_hist_log_file, zip(dist_hist_bins, dist_hist_cum_ratio), delimiter=',') # write error histgram np.savetxt(output_hist_log_file, zip(dist_hist_bins, dist_hist_cum_ratio), delimiter=',') # write detail results with open(output_detail_log_file, "w") as fw: for idx in range(len(datasource.beacons)): fw.write( str(idx) + "," + str(results[idx, 0]) + "," + str(results[idx, 1]) + "\n")
def main(): global output_pos_layer_name global output_rot_layer_name description = 'This script is for testing posenet' parser = argparse.ArgumentParser(description=description) parser.add_argument('input_txt_file', action='store', nargs=None, const=None, \ default=None, type=str, choices=None, metavar=None, \ help='File path of input txt file in Cambridge Visual Landmark Dataset format.') parser.add_argument('input_beacon_setting_file', action='store', nargs=None, const=None, \ default=None, type=str, choices=None, metavar=None, \ help='File path where beacon setting file is saved.') parser.add_argument('input_pb_file', action='store', nargs=None, const=None, \ default=None, type=str, choices=None, metavar=None, \ help='File path of model pb file.') parser.add_argument('result_log_dir', action='store', nargs=None, const=None, \ default=None, type=str, choices=None, metavar=None, \ help='Directory path where localization result files are saved.') parser.add_argument('-f', '--use_fixed_input_mean_std', action='store_true', default=False, \ help='Use fixed input mean and std (default: False)') parser.add_argument('-m', '--base_model', action='store', type=str, default=posenet_config.base_model, \ help='Base model : inception-v1/inception-v3/mobilenet-v1 (Default : ' + str(posenet_config.base_model)) args = parser.parse_args() input_txt_file = args.input_txt_file input_beacon_setting_file = args.input_beacon_setting_file input_pb_file = args.input_pb_file result_log_dir = args.result_log_dir use_fixed_input_mean_std = args.use_fixed_input_mean_std posenet_config.base_model = args.base_model print "base model : " + str(posenet_config.base_model) # you can check output node name by tensorflow/tools/graph_transforms::summarize_graph # https://github.com/tensorflow/models/tree/master/research/slim#Export if posenet_config.base_model=="inception-v1": output_pos_layer_name = "image_beacon_cls3_fc_pose_xyz/BiasAdd" output_rot_layer_name = "image_beacon_cls3_fc_pose_wpqr/BiasAdd" elif posenet_config.base_model=="inception-v3" or posenet_config.base_model=="mobilenet-v1": output_pos_layer_name = "image_beacon_cls_fc_pose_xyz/BiasAdd" output_rot_layer_name = "image_beacon_cls_fc_pose_wpqr/BiasAdd" else: print "invalid base model : " + posenet_config.base_model sys.exit() input_image_dir = os.path.dirname(input_txt_file) input_model_dir = os.path.dirname(input_pb_file) input_numpy_mean_image_file = os.path.join(input_model_dir, "mean_image.npy") if use_fixed_input_mean_std: input_numpy_mean_image = None else: input_numpy_mean_image = np.load(input_numpy_mean_image_file) input_numpy_mean_beacon_file = os.path.join(input_model_dir, "mean_beacon.npy") if use_fixed_input_mean_std: input_numpy_mean_beacon = None else: input_numpy_mean_beacon = np.load(input_numpy_mean_beacon_file) output_localize_txt_file = os.path.join(result_log_dir, "localize-poses.txt") output_localize_json_file = os.path.join(result_log_dir, "localize-poses.json") output_summary_log_file = os.path.join(result_log_dir, "summary-log.txt") output_hist_log_file = os.path.join(result_log_dir, "hist-log.txt") output_detail_log_file = os.path.join(result_log_dir, "detail-log.txt") # parse beacon setting file beaconmap = IBeaconUtils.parseBeaconSetting(input_beacon_setting_file) beacon_num = len(beaconmap.keys()) image = tf.placeholder(tf.float32, [1, 224, 224, 3]) beacons = tf.placeholder(tf.float32, [1, beacon_num, 1, 1]) datasource, image_filenames = get_data(input_txt_file, input_image_dir, input_numpy_mean_image, beaconmap, input_numpy_mean_beacon, use_fixed_input_mean_std) predicted_poses = np.zeros((len(datasource.images),7)) groundtruth_poses = np.zeros((len(datasource.images),7)) results = np.zeros((len(datasource.images),2)) # Set GPU options gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.4) time_array = np.array([]) # Load model graph = load_graph(input_pb_file) input_image_name = "import/" + input_image_layer_name input_beacon_name = "import/" + input_beacon_layer_name output_pos_name = "import/" + output_pos_layer_name output_rot_name = "import/" + output_rot_layer_name input_image_operation = graph.get_operation_by_name(input_image_name) input_beacon_operation = graph.get_operation_by_name(input_beacon_name) output_pos_operation = graph.get_operation_by_name(output_pos_name) output_rot_operation = graph.get_operation_by_name(output_rot_name) with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options), graph=graph) as sess: for i in range(len(datasource.images)): np_image = datasource.images[i] np_beacon = datasource.beacons[i] feed = {input_image_operation.outputs[0]: np_image, input_beacon_operation.outputs[0]: np_beacon} pose_q= np.asarray(datasource.poses[i][3:7]) pose_x= np.asarray(datasource.poses[i][0:3]) start_time = time.time() predicted_x, predicted_q = sess.run([output_pos_operation.outputs[0], output_rot_operation.outputs[0]], feed_dict=feed) elapsed_time = time.time() - start_time time_array = np.append(time_array, elapsed_time) pose_q = np.squeeze(pose_q) pose_x = np.squeeze(pose_x) predicted_q = np.squeeze(predicted_q) predicted_x = np.squeeze(predicted_x) predicted_poses[i,0] = predicted_x[0] predicted_poses[i,1] = predicted_x[1] predicted_poses[i,2] = predicted_x[2] predicted_poses[i,3] = predicted_q[0] predicted_poses[i,4] = predicted_q[1] predicted_poses[i,5] = predicted_q[2] predicted_poses[i,6] = predicted_q[3] groundtruth_poses[i,0] = pose_x[0] groundtruth_poses[i,1] = pose_x[1] groundtruth_poses[i,2] = pose_x[2] groundtruth_poses[i,3] = pose_q[0] groundtruth_poses[i,4] = pose_q[1] groundtruth_poses[i,5] = pose_q[2] groundtruth_poses[i,6] = pose_q[3] # calculate error q1 = pose_q / np.linalg.norm(pose_q) q2 = predicted_q / np.linalg.norm(predicted_q) d = abs(np.sum(np.multiply(q1,q2))) # fix floating point inaccuracy if d<-1.0: d = -1.0 if d>1.0: d = 1.0 theta = 2 * np.arccos(d) * 180/math.pi error_x = np.linalg.norm(pose_x-predicted_x) results[i,:] = [error_x,theta] print 'Index=', i, ' , Pos Error(m)=', error_x, ', Rot Error(degrees)=', theta # write localize poses with open(output_localize_txt_file, "w") as fw: fw.write("Localization Data V1\n") fw.write("ImageFile, Camera Position [X Y Z W P Q R]\n") fw.write("\n") for idx, filename in enumerate(image_filenames): fw.write(os.path.basename(filename) + " " + ' '.join(['%f' % p for p in predicted_poses[idx,:]]) + "\n") locJsonObj = {} locJsonObj["locGlobal"] = [] for idx, pose in enumerate(predicted_poses): Rh = transformations.quaternion_matrix([pose[3], pose[4], pose[5], pose[6]]) groundtruth_pose = groundtruth_poses[idx] groundtruth_Rh = transformations.quaternion_matrix([groundtruth_pose[3], groundtruth_pose[4], groundtruth_pose[5], groundtruth_pose[6]]) jsonLoc = {} jsonLoc["filename"] = os.path.basename(image_filenames[idx]) jsonLoc["t"] = [pose[0], pose[1], pose[2]] jsonLoc["R"] = Rh[0:3,0:3].tolist() jsonLoc["groundtruth"] = [groundtruth_pose[0], groundtruth_pose[1], groundtruth_pose[2]] jsonLoc["groundtruthR"] = groundtruth_Rh[0:3,0:3].tolist() locJsonObj["locGlobal"].append(jsonLoc) with open(output_localize_json_file, "w") as fw: json.dump(locJsonObj, fw) # write histgram results bin_edge = [0.01*float(x) for x in range(0,1001)] dist_errors = results[:,0] dist_hist, dist_hist_bins = np.histogram(dist_errors, bins=bin_edge) dist_hist_cum_ratio = np.cumsum(dist_hist) / float(len(datasource.images)) print "Histogram of error: " + str(dist_hist) print "Cumulative ratio of error: " + str(dist_hist_cum_ratio) print "Total loc err larger than " + str(np.max(bin_edge)) + " meters: " + str(len(datasource.images)-np.sum(dist_hist)) # write summary of results mean_result = np.mean(results,axis=0) std_result = np.std(results,axis=0) median_result = np.median(results,axis=0) max_result = np.max(results,axis=0) percentile_80_result = np.percentile(results,80,axis=0) percentile_90_result = np.percentile(results,90,axis=0) percentile_95_result = np.percentile(results,95,axis=0) print 'Mean error ', mean_result[0], 'm and ', mean_result[1], 'degrees.' print 'StdDev error ', std_result[0], 'm and ', std_result[1], 'degrees.' print 'Median error ', median_result[0], 'm and ', median_result[1], 'degrees.' print 'Max error ', max_result[0], 'm and ', max_result[1], 'degrees.' print '80 percentile error ', percentile_80_result[0], 'm and ', percentile_80_result[1], 'degrees.' print '90 percentile error ', percentile_90_result[0], 'm and ', percentile_90_result[1], 'degrees.' print '95 percentile error ', percentile_95_result[0], 'm and ', percentile_95_result[1], 'degrees.' print 'Mean time ', str(np.average(time_array)) print 'StdDev time ', str(np.std(time_array)) print 'Median time ', str(np.median(time_array)) with open(output_summary_log_file, "w") as fw: fw.write("Number of test image = " + str(len(datasource.images)) + "\n") fw.write("Mean error = " + str(mean_result[0]) + " meters." + "\n") fw.write("StdDev error = " + str(std_result[0]) + " meters." + "\n") fw.write("Median error = " + str(median_result[0]) + " meters." + "\n") fw.write("Max error = " + str(max_result[0]) + " meters." + "\n") fw.write("80 percentile error = " + str(percentile_80_result[0]) + " meters." + "\n") fw.write("90 percentile error = " + str(percentile_90_result[0]) + " meters." + "\n") fw.write("95 percentile error = " + str(percentile_95_result[0]) + " meters." + "\n") fw.write("\n") fw.write("Mean error = " + str(mean_result[1]) + " degrees." + "\n") fw.write("StdDev error = " + str(std_result[1]) + " degrees." + "\n") fw.write("Median error = " + str(median_result[1]) + " degrees." + "\n") fw.write("Max error = " + str(max_result[1]) + " degrees." + "\n") fw.write("80 percentile error = " + str(percentile_80_result[1]) + " degrees." + "\n") fw.write("90 percentile error = " + str(percentile_90_result[1]) + " degrees." + "\n") fw.write("95 percentile error = " + str(percentile_95_result[1]) + " degrees." + "\n") fw.write("\n") fw.write("Histogram of error: " + str(dist_hist) + "\n") fw.write("Cumulative ratio: " + str(np.around(np.cumsum(dist_hist,dtype=float)/len(datasource.images),2)) + "\n") fw.write("Total loc err larger than " + str(np.max(bin_edge)) + " meters: " + str(len(datasource.images)-np.sum(dist_hist)) + "\n") fw.write("\n") fw.write("Mean time = " + str(np.average(time_array)) + "\n") fw.write("StdDev time = " + str(np.std(time_array)) + "\n") fw.write("Median time = " + str(np.median(time_array)) + "\n") # write error histgram np.savetxt(output_hist_log_file, zip(dist_hist_bins, dist_hist_cum_ratio), delimiter=',') # write error histgram np.savetxt(output_hist_log_file, zip(dist_hist_bins, dist_hist_cum_ratio), delimiter=',') # write detail results with open(output_detail_log_file, "w") as fw: for idx, filename in enumerate(image_filenames): fw.write(os.path.basename(filename) + "," + str(results[idx,0]) + "," + str(results[idx,1]) + "\n")
def main(): description = 'This script is for merging multiple SfM output models to one SfM model.' + \ 'Please prepare multiple OpenMVG projects which have output SfM models, and matrix to convert to global coordinate.' parser = argparse.ArgumentParser(description=description) parser.add_argument('input_csv', action='store', nargs=None, const=None, \ default=None, type=str, choices=None, metavar=None, \ help='Input CSV file which lists OpenMVG projects which will be merged.') parser.add_argument('output_dir', action='store', nargs=None, const=None, \ default=None, type=str, choices=None, metavar=None, \ help='Output directory path where merged model will be saved.') args = parser.parse_args() input_csv = args.input_csv output_dir = args.output_dir # load reconstruct parameters reconstructParam = ReconstructParam.ReconstructParam reconstructIBeaconParam = ReconstructIBeaconParam.ReconstructIBeaconParam # read projects list projectList = [] with open(input_csv, "r") as f: reader = csv.reader(f) for row in reader: project = {} project["dir"] = row[0] project["sfm_data"] = row[1] project["A"] = row[2] projectList.append(project) # copy source files to output directory for project in projectList: copyOriginalFiles(project["dir"], output_dir) # load beacon settings mergeBeaconmap = None for project in projectList: beacon_file = os.path.join(project["dir"], "Input", "listbeacon.txt") if os.path.exists(beacon_file): beaconmap = iBeaconUtils.parseBeaconSetting(beacon_file) if mergeBeaconmap is None: mergeBeaconmap = beaconmap else: if mergeBeaconmap!=beaconmap: print "invalid find listbeacon.txt for project data : " + project["dir"] print "listbeacon.txt should be same for all project data" sys.exit() else: print "valid listbeacon.txt for project data : " + project["dir"] # prepare output directory if not os.path.isdir(os.path.join(output_dir,"Ref")): FileUtils.makedir(os.path.join(output_dir,"Ref")) if not os.path.isdir(os.path.join(output_dir,"Ref","loc")): FileUtils.makedir(os.path.join(output_dir,"Ref","loc")) if not os.path.isdir(os.path.join(output_dir,"Output","SfM")): FileUtils.makedir(os.path.join(output_dir,"Output","SfM")) if not os.path.isdir(os.path.join(output_dir,"Output","SfM","reconstruction")): FileUtils.makedir(os.path.join(output_dir,"Output","SfM","reconstruction")) if not os.path.isdir(os.path.join(output_dir,"Output","SfM","reconstruction","global")): FileUtils.makedir(os.path.join(output_dir,"Output","SfM","reconstruction","global")) sfmDataList = [] for project in projectList: if not os.path.exists(project["sfm_data"]): print "cannot find sfm data : " + project["sfm_data"] sys.exit() with open(project["sfm_data"]) as jsonFile: sfmDataList.append(json.load(jsonFile)) AList = [] for project in projectList: AList.append(np.loadtxt(project["A"])) print "load mat : " + project["A"] print (np.loadtxt(project["A"])) print "Load 3D points" pointIdList = [] pointList = [] for sfmData in sfmDataList: pointId, point = mergeSfM.getAll3DPointloc(sfmData) pointn = np.asarray(point, dtype=np.float).T pointIdList.append(pointId) pointList.append(pointn) # merge models mergeSfmData = None mergePointId = None mergePointn = None for idx in range(0, len(sfmDataList)): if idx==0: mergeSfmData = sfmDataList[0] mergeSfM.transform_sfm_data(mergeSfmData, AList[0]) else: mergePointThres = mergeSfM.findMedianStructurePointsThres(mergeSfmData, reconstructParam.mergePointThresMul) print "thres to merge 3D points : " + str(mergePointThres) inlierMap = findInliersByKnownTransform(mergePointId, pointIdList[idx], mergePointn, pointList[idx], AList[idx], mergePointThres) print "number of points in base model : " + str(len(mergePointn[0])) print "number of points in model " + str(idx) + " : " + str(len(pointList[idx])) print "number of inliers : " + str(len(inlierMap)) mergeSfM.merge_sfm_data(mergeSfmData, sfmDataList[idx], AList[idx], {x[0]: x[1] for x in inlierMap}) mergePointId, mergePoint = mergeSfM.getAll3DPointloc(mergeSfmData) mergePointn = np.asarray(mergePoint, dtype=np.float).T # go back to coordinate of the first model _invA = np.linalg.inv(AList[0][0:3,0:3]) invA = np.c_[_invA, -np.dot(_invA,AList[0][:,3])] mergeSfM.transform_sfm_data(mergeSfmData, invA) mergeSfmData["root_path"] = os.path.join(output_dir,"Input","inputImg") resultSfMDataFile = os.path.join(output_dir,"Output","SfM","reconstruction","global","sfm_data.json") with open(resultSfMDataFile,"w") as jsonfile: json.dump(mergeSfmData, jsonfile) # write new beacon file if mergeBeaconmap is not None: iBeaconUtils.exportBeaconDataForSfmImageFrames(os.path.join(output_dir,"Input","csv"), resultSfMDataFile, os.path.join(output_dir,"Input","listbeacon.txt"), os.path.join(output_dir,"Output","SfM","reconstruction","global","beacon.txt"), reconstructIBeaconParam.normApproach) ''' os.system(reconstructParam.BUNDLE_ADJUSTMENT_PROJECT_PATH + " " + resultSfMDataFile + " " + resultSfMDataFile) ''' os.system(reconstructParam.BUNDLE_ADJUSTMENT_PROJECT_PATH + " " + resultSfMDataFile + " " + resultSfMDataFile + \ " -c=" + "rst,rsti" + " -r=" + "1") Amat = AList[0] with open(os.path.join(output_dir,"Ref","Amat.txt"),"w") as AmatFile: np.savetxt(AmatFile,Amat) FileUtils.convertNumpyMatTxt2OpenCvMatYml(os.path.join(output_dir,"Ref","Amat.txt"), os.path.join(output_dir,"Ref","Amat.yml"), "A") # To create same directory structure before merging, create sfm_data.json without structure information in matches directory with open(resultSfMDataFile) as fpr: sfmData = json.load(fpr) sfmData["extrinsics"] = [] sfmData["control_points"] = [] sfmData["structure"] = [] with open(os.path.join(output_dir,"Output","matches","sfm_data.json"),"w") as fpw: json.dump(sfmData, fpw) print "Execute : " + reconstructParam.WORKSPACE_DIR + "/TrainBoW/Release/TrainBoW " + os.path.join(output_dir,"Output") + " " + \ os.path.join(output_dir,"Output", "matches", "BOWfile.yml") + " -p=" + os.path.join(output_dir,"Output", "matches", "PCAfile.yml") os.system(reconstructParam.WORKSPACE_DIR + "/TrainBoW/Release/TrainBoW " + os.path.join(output_dir,"Output") + " " + \ os.path.join(output_dir,"Output", "matches", "BOWfile.yml") + " -p=" + os.path.join(output_dir,"Output", "matches", "PCAfile.yml")) os.system("openMVG_main_ComputeSfM_DataColor -i " + resultSfMDataFile + \ " -o " + os.path.join(output_dir,"Output","SfM","reconstruction","global","colorized.ply"))
def main(): description = 'This script is for testing posenet' parser = argparse.ArgumentParser(description=description) parser.add_argument('input_txt_file', action='store', nargs=None, const=None, \ default=None, type=str, choices=None, metavar=None, \ help='File path of input txt file in Cambridge Visual Landmark Dataset format.') parser.add_argument('input_beacon_setting_file', action='store', nargs=None, const=None, \ default=None, type=str, choices=None, metavar=None, \ help='File path where beacon setting file is saved.') parser.add_argument('input_pb_file', action='store', nargs=None, const=None, \ default=None, type=str, choices=None, metavar=None, \ help='File path of model pb file.') parser.add_argument('result_log_dir', action='store', nargs=None, const=None, \ default=None, type=str, choices=None, metavar=None, \ help='Directory path where localization result files are saved.') parser.add_argument('-f', '--use_fixed_input_mean_std', action='store_true', default=False, \ help='Use fixed input mean and std (default: False)') args = parser.parse_args() input_txt_file = args.input_txt_file input_beacon_setting_file = args.input_beacon_setting_file input_pb_file = args.input_pb_file result_log_dir = args.result_log_dir use_fixed_input_mean_std = args.use_fixed_input_mean_std input_model_dir = os.path.dirname(input_pb_file) input_numpy_mean_beacon_file = os.path.join(input_model_dir, "mean_beacon.npy") if use_fixed_input_mean_std: input_numpy_mean_beacon = None else: input_numpy_mean_beacon = np.load(input_numpy_mean_beacon_file) output_summary_log_file = os.path.join(result_log_dir, "summary-log.txt") output_hist_log_file = os.path.join(result_log_dir, "hist-log.txt") # parse beacon setting file beaconmap = IBeaconUtils.parseBeaconSetting(input_beacon_setting_file) beacon_num = len(beaconmap.keys()) beacons = tf.placeholder(tf.float32, [1, beacon_num, 1, 1]) datasource = get_data(input_txt_file, beaconmap, input_numpy_mean_beacon, use_fixed_input_mean_std) results = np.zeros((len(datasource.beacons), 2)) # Set GPU options gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.4) time_array = np.array([]) # Load model graph = load_graph(input_pb_file) input_beacon_name = "import/" + input_beacon_layer_name output_pos_name = "import/" + output_pos_layer_name output_rot_name = "import/" + output_rot_layer_name input_operation = graph.get_operation_by_name(input_beacon_name) output_pos_operation = graph.get_operation_by_name(output_pos_name) output_rot_operation = graph.get_operation_by_name(output_rot_name) with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options), graph=graph) as sess: for i in range(len(datasource.beacons)): np_beacon = datasource.beacons[i] feed = {input_operation.outputs[0]: np_beacon} pose_q = np.asarray(datasource.poses[i][3:7]) pose_x = np.asarray(datasource.poses[i][0:3]) start_time = time.time() predicted_x, predicted_q = sess.run([ output_pos_operation.outputs[0], output_rot_operation.outputs[0] ], feed_dict=feed) elapsed_time = time.time() - start_time time_array = np.append(time_array, elapsed_time) pose_q = np.squeeze(pose_q) pose_x = np.squeeze(pose_x) predicted_q = np.squeeze(predicted_q) predicted_x = np.squeeze(predicted_x) # calculate error q1 = pose_q / np.linalg.norm(pose_q) q2 = predicted_q / np.linalg.norm(predicted_q) d = abs(np.sum(np.multiply(q1, q2))) # fix floating point inaccuracy if d < -1.0: d = -1.0 if d > 1.0: d = 1.0 theta = 2 * np.arccos(d) * 180 / math.pi error_x = np.linalg.norm(pose_x - predicted_x) results[i, :] = [error_x, theta] print 'Index=', i, ' , Pos Error(m)=', error_x, ', Rot Error(degrees)=', theta # write histgram results bin_edge = [0.01 * float(x) for x in range(0, 1001)] dist_errors = results[:, 0] dist_hist, dist_hist_bins = np.histogram(dist_errors, bins=bin_edge) dist_hist_cum_ratio = np.cumsum(dist_hist) / float(len(datasource.beacons)) print "Histogram of error: " + str(dist_hist) print "Cumulative ratio of error: " + str(dist_hist_cum_ratio) print "Total loc err larger than " + str( np.max(bin_edge)) + " meters: " + str( len(datasource.beacons) - np.sum(dist_hist)) # write summary of results mean_result = np.mean(results, axis=0) std_result = np.std(results, axis=0) median_result = np.median(results, axis=0) max_result = np.max(results, axis=0) percentile_80_result = np.percentile(results, 80, axis=0) percentile_90_result = np.percentile(results, 90, axis=0) percentile_95_result = np.percentile(results, 95, axis=0) print 'Mean error ', mean_result[0], 'm and ', mean_result[1], 'degrees.' print 'StdDev error ', std_result[0], 'm and ', std_result[1], 'degrees.' print 'Median error ', median_result[0], 'm and ', median_result[ 1], 'degrees.' print 'Max error ', max_result[0], 'm and ', max_result[1], 'degrees.' print '80 percentile error ', percentile_80_result[ 0], 'm and ', percentile_80_result[1], 'degrees.' print '90 percentile error ', percentile_90_result[ 0], 'm and ', percentile_90_result[1], 'degrees.' print '95 percentile error ', percentile_95_result[ 0], 'm and ', percentile_95_result[1], 'degrees.' print 'Mean time ', str(np.average(time_array)) print 'StdDev time ', str(np.std(time_array)) print 'Median time ', str(np.median(time_array)) with open(output_summary_log_file, "w") as fw: fw.write("Number of test image = " + str(len(datasource.beacons)) + "\n") fw.write("Mean error = " + str(mean_result[0]) + " meters." + "\n") fw.write("StdDev error = " + str(std_result[0]) + " meters." + "\n") fw.write("Median error = " + str(median_result[0]) + " meters." + "\n") fw.write("Max error = " + str(max_result[0]) + " meters." + "\n") fw.write("80 percentile error = " + str(percentile_80_result[0]) + " meters." + "\n") fw.write("90 percentile error = " + str(percentile_90_result[0]) + " meters." + "\n") fw.write("95 percentile error = " + str(percentile_95_result[0]) + " meters." + "\n") fw.write("\n") fw.write("Mean error = " + str(mean_result[1]) + " degrees." + "\n") fw.write("StdDev error = " + str(std_result[1]) + " degrees." + "\n") fw.write("Median error = " + str(median_result[1]) + " degrees." + "\n") fw.write("Max error = " + str(max_result[1]) + " degrees." + "\n") fw.write("80 percentile error = " + str(percentile_80_result[1]) + " degrees." + "\n") fw.write("90 percentile error = " + str(percentile_90_result[1]) + " degrees." + "\n") fw.write("95 percentile error = " + str(percentile_95_result[1]) + " degrees." + "\n") fw.write("\n") fw.write("Histogram of error: " + str(dist_hist) + "\n") fw.write("Cumulative ratio: " + str( np.around( np.cumsum(dist_hist, dtype=float) / len(datasource.beacons), 2)) + "\n") fw.write("Total loc err larger than " + str(np.max(bin_edge)) + " meters: " + str(len(datasource.beacons) - np.sum(dist_hist)) + "\n") fw.write("\n") fw.write("Mean time = " + str(np.average(time_array)) + "\n") fw.write("StdDev time = " + str(np.std(time_array)) + "\n") fw.write("Median time = " + str(np.median(time_array)) + "\n") # write error histgram np.savetxt(output_hist_log_file, zip(dist_hist_bins, dist_hist_cum_ratio), delimiter=',') # write error histgram np.savetxt(output_hist_log_file, zip(dist_hist_bins, dist_hist_cum_ratio), delimiter=',')
def main(): description = 'This script is for testing posenet' parser = argparse.ArgumentParser(description=description) parser.add_argument('input_txt_file', action='store', nargs=None, const=None, \ default=None, type=str, choices=None, metavar=None, \ help='File path of input txt file in Cambridge Visual Landmark Dataset format.') parser.add_argument('input_beacon_setting_file', action='store', nargs=None, const=None, \ default=None, type=str, choices=None, metavar=None, \ help='File path where beacon setting file is saved.') parser.add_argument('output_model_dir', action='store', nargs=None, const=None, \ default=None, type=str, choices=None, metavar=None, \ help='Directory path where output models will be saved.') parser.add_argument('output_log_dir', action='store', nargs=None, const=None, \ default=None, type=str, choices=None, metavar=None, \ help='Directory path where log files will be saved.') parser.add_argument('-i', '--input_image_weight_file', action='store', type=str, default=None, \ help='File path image posenet weiht is stored in numpy format.') parser.add_argument('-w', '--input_beacon_weight_file', action='store', type=str, default=None, \ help='File path beacon posenet weiht is stored in numpy format.') parser.add_argument('-l', '--loss_beta', action='store', type=float, default=posenet_config.loss_beta, \ help='Beta for loss function (Default : ' + str(posenet_config.loss_beta)) parser.add_argument('-s', '--use_shrink_model', action='store_true', default=False, \ help='Use shrink model (default: False)') parser.add_argument('-f', '--use_fixed_input_mean_std', action='store_true', default=False, \ help='Use fixed image mean and std (default: False)') parser.add_argument('-a', '--use_augmentation_beacon', action='store_true', default=False, \ help='Use data augmentation for beacon data (default: False)') parser.add_argument('-m', '--base_model', action='store', type=str, default=posenet_config.base_model, \ help='Base model : inception-v1/inception-v3/mobilenet-v1 (Default : ' + str(posenet_config.base_model)) parser.add_argument('-e', '--epochs', action='store', type=int, default=posenet_config.epochs, \ help='Epochs (Default : ' + str(posenet_config.epochs)) parser.add_argument('-b', '--batch_size', action='store', type=int, default=posenet_config.batch_size, \ help='Batch size (Default : ' + str(posenet_config.batch_size)) args = parser.parse_args() input_txt_file = args.input_txt_file input_beacon_setting_file = args.input_beacon_setting_file output_model_dir = args.output_model_dir output_log_dir = args.output_log_dir input_image_weight_file = args.input_image_weight_file input_beacon_weight_file = args.input_beacon_weight_file posenet_config.loss_beta = args.loss_beta use_shrink_model = args.use_shrink_model use_fixed_input_mean_std = args.use_fixed_input_mean_std use_augmentation_beacon = args.use_augmentation_beacon posenet_config.base_model = args.base_model posenet_config.epochs = args.epochs posenet_config.batch_size = args.batch_size print "base model : " + str(posenet_config.base_model) print "epochs : " + str(posenet_config.epochs) print "batch size : " + str(posenet_config.batch_size) print "loss weight value beta : " + str(posenet_config.loss_beta) print "use shrink model for training : " + str(use_shrink_model) print "use fixed input mean and std : " + str(use_fixed_input_mean_std) print "use beacon data augmentation : " + str(use_augmentation_beacon) if posenet_config.base_model != "inception-v1" and posenet_config.base_model != "inception-v3" and posenet_config.base_model != "mobilenet-v1": print "invalid base model : " + posenet_config.base_model sys.exit() if input_image_weight_file is None or input_beacon_weight_file is None: print "please specify initial weight for image and beacon" sys.exit() # parse beacon setting file beaconmap = IBeaconUtils.parseBeaconSetting(input_beacon_setting_file) beacon_num = len(beaconmap.keys()) input_image_dir = os.path.dirname(input_txt_file) output_numpy_mean_image_file = os.path.join(output_model_dir, "mean_image.npy") output_numpy_mean_beacon_file = os.path.join(output_model_dir, "mean_beacon.npy") output_numpy_model_file = os.path.join(output_model_dir, "model.npy") output_model_file = os.path.join(output_model_dir, "model.ckpt") if posenet_config.base_model == "inception-v1": image_size = 224 output_auxiliary = True elif posenet_config.base_model == "inception-v3": image_size = 299 output_auxiliary = False elif posenet_config.base_model == "mobilenet-v1": image_size = 224 output_auxiliary = False else: print "invalid base model : " + posenet_config.base_model sys.exit() datasource, mean_image, mean_beacon = posenet_data_utils.get_image_beacon_data( input_txt_file, input_image_dir, beaconmap, beacon_num, use_fixed_input_mean_std, use_augmentation_beacon, image_size=image_size) if use_fixed_input_mean_std: print("Skip save mean image and beacon") else: with open(output_numpy_mean_image_file, 'wb') as fw: np.save(fw, mean_image) print("Save mean image at: " + output_numpy_mean_image_file) with open(output_numpy_mean_beacon_file, 'wb') as fw: np.save(fw, mean_beacon) print("Save mean beacon at: " + output_numpy_mean_beacon_file) # Set GPU options gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.4) session = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) KTF.set_session(session) # Train model adam = Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=0.00000001) if use_shrink_model: if posenet_config.base_model == "inception-v1": model = posenet_image_beacon_no_inception_shrink_keras.create_posenet_inception_v1( beacon_num, input_image_weight_file, input_beacon_weight_file) model.compile(optimizer=adam, loss={ 'image_beacon_cls1_fc_pose_xyz': posenet_loss.euc_loss1x, 'image_beacon_cls1_fc_pose_wpqr': posenet_loss.euc_loss1q, 'image_beacon_cls2_fc_pose_xyz': posenet_loss.euc_loss2x, 'image_beacon_cls2_fc_pose_wpqr': posenet_loss.euc_loss2q, 'image_beacon_cls3_fc_pose_xyz': posenet_loss.euc_loss3x, 'image_beacon_cls3_fc_pose_wpqr': posenet_loss.euc_loss3q }) elif posenet_config.base_model == "inception-v3": model = posenet_image_beacon_no_inception_shrink_keras.create_posenet_inception_v3( beacon_num, input_image_weight_file, input_beacon_weight_file) model.compile(optimizer=adam, loss={ 'image_beacon_cls3_fc_pose_xyz': posenet_loss.euc_loss3x, 'image_beacon_cls3_fc_pose_wpqr': posenet_loss.euc_loss3q }) elif posenet_config.base_model == "mobilenet-v1": model = posenet_image_beacon_no_inception_shrink_keras.create_posenet_mobilenet_v1( beacon_num, input_image_weight_file, input_beacon_weight_file) model.compile(optimizer=adam, loss={ 'image_beacon_cls_fc_pose_xyz': posenet_loss.euc_loss3x, 'image_beacon_cls_fc_pose_wpqr': posenet_loss.euc_loss3q }) else: print "invalid base model : " + posenet_config.base_model sys.exit() else: print "Do not shrink model is not supported" sys.exit() model.summary() # Setup checkpointing checkpointer = ModelCheckpoint(filepath=os.path.join( output_model_dir, "checkpoint_weights.h5"), verbose=1, save_weights_only=True, period=1) # Save Tensorboard log logger = TensorBoard(log_dir=output_log_dir, histogram_freq=0, write_graph=True) # Adjust Epoch size depending on beacon data augmentation if use_augmentation_beacon: posenet_config.epochs = posenet_config.epochs / posenet_config.num_beacon_augmentation steps_per_epoch = int( len(datasource.poses_index) / float(posenet_config.batch_size)) num_iterations = steps_per_epoch * posenet_config.epochs print("Number of epochs : " + str(posenet_config.epochs)) print("Number of training data : " + str(len(datasource.poses_index))) print("Number of iterations : " + str(num_iterations)) history = model.fit_generator( posenet_data_utils.gen_image_beacon_data_batch( datasource, output_auxiliary=output_auxiliary), steps_per_epoch=steps_per_epoch, epochs=posenet_config.epochs, callbacks=[checkpointer, logger]) model.save_weights(os.path.join(output_model_dir, "trained_weights.h5"))