-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kitti processing with hdbscan notebook
- Loading branch information
1 parent
eb703c8
commit 95f9512
Showing
1 changed file
with
342 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,342 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import copy\n", | ||
"import numpy as np\n", | ||
"import open3d as o3d\n", | ||
"import random\n", | ||
"\n", | ||
"def generate_random_colors(N):\n", | ||
" colors = [[0, 0, 0]]\n", | ||
" for _ in range(N):\n", | ||
" colors.append(\n", | ||
" [random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)]\n", | ||
" )\n", | ||
"\n", | ||
" colors = np.vstack(colors) / 255\n", | ||
" return colors\n", | ||
"\n", | ||
"def color_pcd_by_labels1(pcd, labels):\n", | ||
" colors = generate_random_colors(len(labels) + 1)\n", | ||
" pcd_colored = copy.deepcopy(pcd)\n", | ||
" pcd_colored.colors = o3d.utility.Vector3dVector(\n", | ||
" np.zeros(np.asarray(pcd.points).shape)\n", | ||
" )\n", | ||
"\n", | ||
" for i in range(len(pcd_colored.points)):\n", | ||
" if labels[i] == 0 or labels[i] == -1:\n", | ||
" continue\n", | ||
" pcd_colored.colors[i] = colors[labels[i]]\n", | ||
"\n", | ||
" return pcd_colored" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import pickle\n", | ||
"\n", | ||
"start_num = 20\n", | ||
"end_num = start_num + 4\n", | ||
"\n", | ||
"file_name = \"experiment_bin_0704_4_sem_offset0_T0l03/start{}_end{}.pickle\".format(start_num, end_num)\n", | ||
"\n", | ||
"with open(file_name, 'rb') as file:\n", | ||
" data = pickle.load(file)\n", | ||
"\n", | ||
"print(data[0]) # значения гиперпараметров запуска" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from sklearn.cluster import HDBSCAN\n", | ||
"\n", | ||
"clusterer = HDBSCAN()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy as np\n", | ||
"import open3d as o3d\n", | ||
"\n", | ||
"X = np.asarray(data[1]) # точки обработанного облака ДО вокселизации\n", | ||
"pcd_hdbscan = o3d.geometry.PointCloud()\n", | ||
"pcd_hdbscan.points = o3d.utility.Vector3dVector(X)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import copy\n", | ||
"\n", | ||
"# проведение точно такой же вокселизации, как и в моем алгоритме\n", | ||
"\n", | ||
"pcd_hdbscan_copy = copy.deepcopy(pcd_hdbscan)\n", | ||
"\n", | ||
"min_bound = pcd_hdbscan_copy.get_min_bound()\n", | ||
"max_bound = pcd_hdbscan_copy.get_max_bound()\n", | ||
"\n", | ||
"downpcd_trace = pcd_hdbscan_copy.voxel_down_sample_and_trace(\n", | ||
" 0.25, min_bound, max_bound, True\n", | ||
")\n", | ||
"\n", | ||
"downpcd = downpcd_trace[0]\n", | ||
"trace_hdbscan = downpcd_trace[2]\n", | ||
"\n", | ||
"print(\"voxel_down_pcd_size={}\".format(len(downpcd.points)))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# проверяем, что вокселизация привела облако в то состояние,\n", | ||
"# при котором проводилась сегментация моим алгоритмом\n", | ||
"\n", | ||
"trace_arrays = []\n", | ||
"for int_vector in trace_hdbscan:\n", | ||
" trace_arrays.append(np.asarray(int_vector))\n", | ||
"\n", | ||
"for ind, el in enumerate(trace_arrays):\n", | ||
" if (el != data[2][ind]).any():\n", | ||
" print(\"ERROR\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# сегментация HDBSCAN\n", | ||
"\n", | ||
"clusters = clusterer.fit_predict(np.asarray(downpcd.points))\n", | ||
"print(set(clusters))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import copy\n", | ||
"\n", | ||
"from src.utils.pcd_utils import visualize_pcd\n", | ||
"\n", | ||
"colored_pcd = color_pcd_by_labels1(copy.deepcopy(downpcd), clusters)\n", | ||
"visualize_pcd(colored_pcd)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# приводим результаты кластеризации к листу, элементы которого - массивы точек одного кластера\n", | ||
"\n", | ||
"clusters_list = []\n", | ||
"\n", | ||
"print(\"before set(clusters)={}\".format(set(clusters)))\n", | ||
"# метки 0 и -1 означают шум, они для нас равны\n", | ||
"for ind, label in enumerate(clusters):\n", | ||
" if label == -1:\n", | ||
" clusters[ind] = 0\n", | ||
"print(\"after set(clusters)={}\".format(set(clusters)))\n", | ||
"\n", | ||
"# инициализация листа\n", | ||
"for label in set(clusters):\n", | ||
" clusters_list.append([])\n", | ||
"\n", | ||
"for ind, label in enumerate(clusters):\n", | ||
" clusters_list[label].append(ind)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import copy\n", | ||
"\n", | ||
"from src.utils.pcd_utils import color_pcd_by_clusters_and_voxels\n", | ||
"from src.utils.pcd_utils import visualize_pcd\n", | ||
"\n", | ||
"colored_pcd = color_pcd_by_clusters_and_voxels(\n", | ||
" copy.deepcopy(pcd_hdbscan_copy), copy.deepcopy(trace_hdbscan), clusters_list\n", | ||
")\n", | ||
"visualize_pcd(colored_pcd)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# выборка из кластеризации только инстансов\n", | ||
"\n", | ||
"def find_num_in_inst_label_array(src_points, inst_label_array_for_clustering):\n", | ||
" for point in src_points:\n", | ||
" if inst_label_array_for_clustering[point] > 0:\n", | ||
" return inst_label_array_for_clustering[point]\n", | ||
" return -1\n", | ||
"\n", | ||
"\n", | ||
"def build_pred_inst_array(\n", | ||
" inst_label_array_for_clustering, clusters, trace, instance_threshold\n", | ||
"):\n", | ||
" pred_inst_array = np.zeros(len(inst_label_array_for_clustering), dtype=int)\n", | ||
" free_id = 1\n", | ||
" for cluster in clusters:\n", | ||
" voxel_not_in_gt_cluster_count = 0\n", | ||
" for voxel in cluster:\n", | ||
" src_points = trace[voxel]\n", | ||
" id = find_num_in_inst_label_array(\n", | ||
" src_points, inst_label_array_for_clustering\n", | ||
" )\n", | ||
" if id == -1:\n", | ||
" voxel_not_in_gt_cluster_count += 1\n", | ||
"\n", | ||
" cluster_in_gt_instance = (\n", | ||
" (len(cluster) - voxel_not_in_gt_cluster_count) / len(cluster)\n", | ||
" ) * 100\n", | ||
" if cluster_in_gt_instance >= instance_threshold:\n", | ||
" for voxel in cluster:\n", | ||
" src_points = trace[voxel]\n", | ||
" for src_point in src_points:\n", | ||
" pred_inst_array[src_point] = free_id\n", | ||
" free_id += 1\n", | ||
" return pred_inst_array" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"instance_threshold = 30\n", | ||
"inst_label_array_for_clustering = data[4] # из pickle\n", | ||
"\n", | ||
"pred_inst_array = build_pred_inst_array(\n", | ||
" copy.deepcopy(inst_label_array_for_clustering),\n", | ||
" clusters_list,\n", | ||
" copy.deepcopy(trace_hdbscan),\n", | ||
" instance_threshold,\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from evops.metrics import precision\n", | ||
"from evops.metrics import recall\n", | ||
"from evops.metrics import fScore\n", | ||
"\n", | ||
"pred_labels = pred_inst_array\n", | ||
"gt_labels = inst_label_array_for_clustering\n", | ||
"tp_condition = \"iou\"\n", | ||
"print(\"precision={}\".format(precision(pred_labels, gt_labels, tp_condition)))\n", | ||
"print(\"recall={}\".format(recall(pred_labels, gt_labels, tp_condition)))\n", | ||
"print(\"fScore={}\".format(fScore(pred_labels, gt_labels, tp_condition)))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import copy\n", | ||
"\n", | ||
"from src.utils.pcd_utils import color_pcd_by_labels\n", | ||
"from src.utils.pcd_utils import visualize_pcd\n", | ||
"\n", | ||
"colored_clusters_for_clustering = color_pcd_by_labels(\n", | ||
" copy.deepcopy(pcd_hdbscan_copy), pred_labels\n", | ||
")\n", | ||
"visualize_pcd(colored_clusters_for_clustering)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import copy\n", | ||
"\n", | ||
"from src.utils.pcd_utils import color_pcd_by_labels\n", | ||
"from src.utils.pcd_utils import visualize_pcd\n", | ||
"\n", | ||
"colored_clusters_for_clustering = color_pcd_by_labels(\n", | ||
" copy.deepcopy(pcd_hdbscan_copy), gt_labels\n", | ||
")\n", | ||
"visualize_pcd(colored_clusters_for_clustering)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.6" | ||
}, | ||
"orig_nbformat": 4 | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |