Skip to content
Snippets Groups Projects
Commit 78b8d4ec authored by Maciej Wielgosz's avatar Maciej Wielgosz
Browse files

visualization updated for instance seg. pred -> gt mapping is now done

parent 3c7a478f
Branches
No related tags found
No related merge requests found
#!/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
...@@ -328,10 +328,7 @@ class InstanceSegmentationMetrics: ...@@ -328,10 +328,7 @@ class InstanceSegmentationMetrics:
# do this if there is at least one label # do this if there is at least one label
if metric_dict: if metric_dict:
for label in metric_dict.keys(): for label in metric_dict.keys():
print('label: ', label)
for parameter in interesting_parameters: 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] 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 # divide by the sum of the hights of the trees
for parameter in interesting_parameters: for parameter in interesting_parameters:
......
...@@ -6,7 +6,7 @@ from joblib import Parallel, delayed ...@@ -6,7 +6,7 @@ from joblib import Parallel, delayed
import laspy import laspy
from metrics.instance_segmentation_metrics import InstanceSegmentationMetrics 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(): class InstanceSegmentationMetricsInFolder():
GT_LABEL_NAME = 'treeID' GT_LABEL_NAME = 'treeID'
...@@ -160,12 +160,12 @@ class InstanceSegmentationMetricsInFolder(): ...@@ -160,12 +160,12 @@ class InstanceSegmentationMetricsInFolder():
# create the output folder path # create the output folder path
save_to_csv_path = os.path.join(self.output_folder_path, gt_las_file_core_name + '.csv') save_to_csv_path = os.path.join(self.output_folder_path, gt_las_file_core_name + '.csv')
# attach labels to the las file # attach labels to the las file
AttachLabelsToLasFile( AttachLabelsToLasFilePred2Gt(
gt_las_file_path, gt_las_file_path,
target_las_file_path, target_las_file_path,
update_las_file_path = os.path.join(self.output_folder_path, gt_las_file_core_name + '.las'), update_las_file_path = os.path.join(self.output_folder_path, gt_las_file_core_name + '.las'),
gt_label_name=self.GT_LABEL_NAME, gt_label_name=self.GT_LABEL_NAME,
target_label_name=self.GT_LABEL_NAME, target_label_name=self.TARGET_LABEL_NAME,
verbose=self.verbose verbose=self.verbose
).main() ).main()
......
...@@ -7,7 +7,7 @@ from sklearn.neighbors import KDTree ...@@ -7,7 +7,7 @@ from sklearn.neighbors import KDTree
import logging import logging
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
class AttachLabelsToLasFile(): class AttachLabelsToLasFileGt2Pred():
def __init__( def __init__(
self, self,
gt_las_file_path, gt_las_file_path,
...@@ -109,7 +109,7 @@ if __name__ == '__main__': ...@@ -109,7 +109,7 @@ if __name__ == '__main__':
args = parser.parse_args() args = parser.parse_args()
# create an instance of AttachLabelsToLasFile class # 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, gt_las_file_path=args.gt_las_file_path,
target_las_file_path=args.target_las_file_path, target_las_file_path=args.target_las_file_path,
update_las_file_path=args.update_las_file_path, update_las_file_path=args.update_las_file_path,
......
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()
...@@ -8,7 +8,7 @@ from tqdm import tqdm ...@@ -8,7 +8,7 @@ from tqdm import tqdm
import laspy import laspy
from nibio_preprocessing.pdal_subsampling_center_nn import PDALSubsamplingCenterNN 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: class PDALSubsamplingCenterNNFolders:
def __init__(self, input_folder, output_folder, voxel_size, verbose): def __init__(self, input_folder, output_folder, voxel_size, verbose):
...@@ -62,7 +62,7 @@ class PDALSubsamplingCenterNNFolders: ...@@ -62,7 +62,7 @@ class PDALSubsamplingCenterNNFolders:
# read input files and output files one by one using laspy # read input files and output files one by one using laspy
for input_file, output_file in tqdm(zip(input_files, output_files)): for input_file, output_file in tqdm(zip(input_files, output_files)):
# use AttachLabelsToLasFile to transfer extra fields # use AttachLabelsToLasFile to transfer extra fields
transfer = AttachLabelsToLasFile( transfer = AttachLabelsToLasFileGt2Pred(
gt_las_file_path=input_file, gt_las_file_path=input_file,
target_las_file_path=output_file, target_las_file_path=output_file,
update_las_file_path=output_file, update_las_file_path=output_file,
...@@ -73,7 +73,7 @@ class PDALSubsamplingCenterNNFolders: ...@@ -73,7 +73,7 @@ class PDALSubsamplingCenterNNFolders:
transfer.main() transfer.main()
# transfer instance segmentation labels # transfer instance segmentation labels
transfer = AttachLabelsToLasFile( transfer = AttachLabelsToLasFileGt2Pred(
gt_las_file_path=input_file, gt_las_file_path=input_file,
target_las_file_path=output_file, target_las_file_path=output_file,
update_las_file_path=output_file, update_las_file_path=output_file,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment