diff --git a/bash_helper_scripts/get_val_corrected_single_file.sh b/bash_helper_scripts/get_val_corrected_single_file.sh new file mode 100755 index 0000000000000000000000000000000000000000..1a3a2a84bc8ca22113a9991526bdf1b73ced855b --- /dev/null +++ b/bash_helper_scripts/get_val_corrected_single_file.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +TARGET_FOLDER=/home/nibio/mutable-outside-world/code/gitlab_fsct/instance_segmentation_classic/maciek +# clean the folder +rm -rf $TARGET_FOLDER/* + +# cp /home/nibio/mutable-outside-world/data/corrected/validation/Plot89_tile_-25_0.las $TARGET_FOLDER + +cp /home/nibio/mutable-outside-world/data/corrected/validation/plot85_tile_-25_0.las $TARGET_FOLDER + diff --git a/metrics/instance_segmentation_metrics_austrian.py b/metrics/instance_segmentation_metrics_austrian.py index a338ca0df00aa7b369716e9c89f3d39e37764f2b..0acfdc6796078e732d89d82b147ae2d251e1b2e6 100644 --- a/metrics/instance_segmentation_metrics_austrian.py +++ b/metrics/instance_segmentation_metrics_austrian.py @@ -328,10 +328,7 @@ class InstanceSegmentationMetrics: # do this if there is at least one label if metric_dict: for label in metric_dict.keys(): - print('label: ', label) for parameter in interesting_parameters: - print('parameter: ', parameter) - print('metric_dict[label][parameter]: ', metric_dict[label][parameter]) metric_dict_weighted_by_tree_hight[parameter] += metric_dict[label]['high_of_tree_gt'] * metric_dict[label][parameter] # divide by the sum of the hights of the trees for parameter in interesting_parameters: diff --git a/metrics/instance_segmentation_metrics_in_folder.py b/metrics/instance_segmentation_metrics_in_folder.py index ccc57c93702e3996b1c5281e5c8ae1d85d458ab8..e9b4eca42a34a3289e5396597dbe3b634cb4b1a5 100644 --- a/metrics/instance_segmentation_metrics_in_folder.py +++ b/metrics/instance_segmentation_metrics_in_folder.py @@ -6,7 +6,7 @@ from joblib import Parallel, delayed import laspy from metrics.instance_segmentation_metrics import InstanceSegmentationMetrics -from nibio_postprocessing.attach_labels_to_las_file import AttachLabelsToLasFile +from nibio_postprocessing.attach_labels_to_las_file_pred2gt import AttachLabelsToLasFilePred2Gt class InstanceSegmentationMetricsInFolder(): GT_LABEL_NAME = 'treeID' @@ -160,12 +160,12 @@ class InstanceSegmentationMetricsInFolder(): # create the output folder path save_to_csv_path = os.path.join(self.output_folder_path, gt_las_file_core_name + '.csv') # attach labels to the las file - AttachLabelsToLasFile( + AttachLabelsToLasFilePred2Gt( gt_las_file_path, target_las_file_path, update_las_file_path = os.path.join(self.output_folder_path, gt_las_file_core_name + '.las'), gt_label_name=self.GT_LABEL_NAME, - target_label_name=self.GT_LABEL_NAME, + target_label_name=self.TARGET_LABEL_NAME, verbose=self.verbose ).main() diff --git a/nibio_postprocessing/attach_labels_to_las_file.py b/nibio_postprocessing/attach_labels_to_las_file_gt2pred.py similarity index 97% rename from nibio_postprocessing/attach_labels_to_las_file.py rename to nibio_postprocessing/attach_labels_to_las_file_gt2pred.py index 26de21386d4445313d6bd4d2eaed002a9d61f04e..22191a964af179f74b1fc991465fc96a8d2e07bc 100644 --- a/nibio_postprocessing/attach_labels_to_las_file.py +++ b/nibio_postprocessing/attach_labels_to_las_file_gt2pred.py @@ -7,7 +7,7 @@ from sklearn.neighbors import KDTree import logging logging.basicConfig(level=logging.INFO) -class AttachLabelsToLasFile(): +class AttachLabelsToLasFileGt2Pred(): def __init__( self, gt_las_file_path, @@ -109,7 +109,7 @@ if __name__ == '__main__': args = parser.parse_args() # create an instance of AttachLabelsToLasFile class - attach_labels_to_las_file = AttachLabelsToLasFile( + attach_labels_to_las_file = AttachLabelsToLasFileGt2Pred( gt_las_file_path=args.gt_las_file_path, target_las_file_path=args.target_las_file_path, update_las_file_path=args.update_las_file_path, diff --git a/nibio_postprocessing/attach_labels_to_las_file_pred2gt.py b/nibio_postprocessing/attach_labels_to_las_file_pred2gt.py new file mode 100644 index 0000000000000000000000000000000000000000..5e23dc134a2e5aab5ec927c478dca1e460f24f69 --- /dev/null +++ b/nibio_postprocessing/attach_labels_to_las_file_pred2gt.py @@ -0,0 +1,131 @@ +import laspy +import argparse +import pandas as pd +import numpy as np +from sklearn.neighbors import KDTree + +import logging +logging.basicConfig(level=logging.INFO) + +class AttachLabelsToLasFilePred2Gt(): + def __init__( + self, + gt_las_file_path, + target_las_file_path, + update_las_file_path, + gt_label_name='gt_label', # 'gt_label' + target_label_name='target_label', # 'pred_label' + verbose=False + ): + + self.gt_las_file_path = gt_las_file_path + self.target_las_file_path = target_las_file_path + self.update_las_file_path = update_las_file_path + self.gt_label_name = gt_label_name + self.target_label_name = target_label_name + self.verbose = verbose + + + def attach_labels(self): + # read las file + gt_las = laspy.read(self.gt_las_file_path) + target_las = laspy.read(self.target_las_file_path) + + # read x, y, z and gt_label from gt las file to variables and set a size of sample_size + gt = gt_las.xyz + target = target_las.xyz + gt_label = gt_las[self.gt_label_name] + + # create a tree from target las file + tree = KDTree(gt, leaf_size=50, metric='euclidean') + # find the nearest neighbor for each point in target las file + ind = tree.query(target, k=1, return_distance=False) + + # # map target labels to gt labels + # target_labels = gt_label[ind] + + # get point format of target las file + point_format = gt_las.point_format + # get header of target las file + header = gt_las.header + + # create a new las file with the same header as target las file + new_header = laspy.LasHeader(point_format=point_format.id, version=header.version) + # add gt_label and target_label extra dimensions to the new las file + # get extra dimensions from target las file + gt_extra_dimensions = list(gt_las.point_format.extra_dimension_names) + + # add extra dimensions to new las file + for item in gt_extra_dimensions: + new_header.add_extra_dim(laspy.ExtraBytesParams(name=item, type=np.int32)) + + # add gt_label and target_label extra dimensions to the new las file + new_header.add_extra_dim(laspy.ExtraBytesParams(name=self.target_label_name, type=np.int32)) + + new_las = laspy.LasData(new_header) + + # copy x, y, z, gt_label and target_label from target las file to the new las file + new_las.x = gt_las.x + new_las.y = gt_las.y + new_las.z = gt_las.z + + # copy contents of extra dimensions from target las file to the new las file + for item in gt_extra_dimensions: + new_las[item] = gt_las[item] + + # prepare target labels + target_labels = np.empty((gt_label.shape), dtype=np.int32) + target_labels[:] = -1 # fill all with -1 (unlabeled), there are more gt labels than target labels + + target_labels[ind] = target_las[self.target_label_name].reshape(-1, 1) # fill with target labels + new_las[self.target_label_name] = target_labels + # write the new las file + new_las.write(self.update_las_file_path) + + def main(self): + self.attach_labels( + ) + if self.verbose: + # write a report using logging + logging.info('gt_las_file_path: {}'.format(self.gt_las_file_path)) + logging.info('target_las_file_path: {}'.format(self.target_las_file_path)) + logging.info('update_las_file_path: {}'.format(self.update_las_file_path)) + logging.info('gt_label_name: {}'.format(self.gt_label_name)) + logging.info('target_label_name: {}'.format(self.target_label_name)) + + # print the size of the las files + gt_las = laspy.read(self.gt_las_file_path) + target_las = laspy.read(self.target_las_file_path) + gt_las_size = gt_las.x.shape[0] + target_las_size = target_las.x.shape[0] + logging.info('gt_las_size: {}'.format(gt_las_size)) + logging.info('target_las_size: {}'.format(target_las_size)) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser('Attach labels to las file') + parser.add_argument('--gt_las_file_path', type=str, required=True) + parser.add_argument('--target_las_file_path', type=str, required=True) + parser.add_argument('--update_las_file_path', type=str, required=True) + parser.add_argument('--gt_label_name', type=str, default='gt_label') + parser.add_argument('--target_label_name', type=str, default='target_label') + parser.add_argument('--verbose', action='store_true', help="Print information about the process") + args = parser.parse_args() + + # create an instance of AttachLabelsToLasFile class + attach_labels_to_las_file = AttachLabelsToLasFilePred2Gt( + gt_las_file_path=args.gt_las_file_path, + target_las_file_path=args.target_las_file_path, + update_las_file_path=args.update_las_file_path, + gt_label_name=args.gt_label_name, + target_label_name=args.target_label_name, + verbose=args.verbose + + ) + + # call main function + attach_labels_to_las_file.main() + + + + diff --git a/nibio_preprocessing/pdal_subsampling_center_nn_folders.py b/nibio_preprocessing/pdal_subsampling_center_nn_folders.py index 01ee74b9ec8abb1d6081012c0bb83fc0d4feb63d..877df270ad38988620a394c676bc753cf75199d2 100644 --- a/nibio_preprocessing/pdal_subsampling_center_nn_folders.py +++ b/nibio_preprocessing/pdal_subsampling_center_nn_folders.py @@ -8,7 +8,7 @@ from tqdm import tqdm import laspy from nibio_preprocessing.pdal_subsampling_center_nn import PDALSubsamplingCenterNN -from nibio_postprocessing.attach_labels_to_las_file import AttachLabelsToLasFile +from nibio_postprocessing.attach_labels_to_las_file_gt2pred import AttachLabelsToLasFileGt2Pred class PDALSubsamplingCenterNNFolders: def __init__(self, input_folder, output_folder, voxel_size, verbose): @@ -62,7 +62,7 @@ class PDALSubsamplingCenterNNFolders: # read input files and output files one by one using laspy for input_file, output_file in tqdm(zip(input_files, output_files)): # use AttachLabelsToLasFile to transfer extra fields - transfer = AttachLabelsToLasFile( + transfer = AttachLabelsToLasFileGt2Pred( gt_las_file_path=input_file, target_las_file_path=output_file, update_las_file_path=output_file, @@ -73,7 +73,7 @@ class PDALSubsamplingCenterNNFolders: transfer.main() # transfer instance segmentation labels - transfer = AttachLabelsToLasFile( + transfer = AttachLabelsToLasFileGt2Pred( gt_las_file_path=input_file, target_las_file_path=output_file, update_las_file_path=output_file,