diff --git a/.gitignore b/.gitignore index 181150a9068bd17d23aa75e0d346875ae43f996e..2bace18d74207d7f93466b1c31b253c6d731a79e 100644 --- a/.gitignore +++ b/.gitignore @@ -116,6 +116,7 @@ pip_egg_info/ *.t7 *.las *.png +*.h5 # Deep learning model.h5 @@ -125,4 +126,5 @@ logs/ tensorboard/ wandb/ data/ +shapenet_part_seg_hdf5_data/ ``` diff --git a/wandb_vis/dgcnn_from_scratch.ipynb b/jupyters/dgcnn_from_scratch.ipynb similarity index 100% rename from wandb_vis/dgcnn_from_scratch.ipynb rename to jupyters/dgcnn_from_scratch.ipynb diff --git a/jupyters/shapenet_visualization.ipynb b/jupyters/shapenet_visualization.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..273dc2364e187348874dc6105f8da232c4536d03 --- /dev/null +++ b/jupyters/shapenet_visualization.ipynb @@ -0,0 +1,175 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/home/nibio/.local/lib/python3.8/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", + " from .autonotebook import tqdm as notebook_tqdm\n", + "Failed to detect the name of this notebook, you can set it manually with the WANDB_NOTEBOOK_NAME environment variable to enable code saving.\n", + "\u001b[34m\u001b[1mwandb\u001b[0m: Currently logged in as: \u001b[33mmaciej-wielgosz-nibio\u001b[0m. Use \u001b[1m`wandb login --relogin`\u001b[0m to force relogin\n" + ] + }, + { + "data": { + "text/html": [ + "wandb version 0.15.3 is available! To upgrade, please run:\n", + " $ pip install wandb --upgrade" + ], + "text/plain": [ + "<IPython.core.display.HTML object>" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "Tracking run with wandb version 0.14.2" + ], + "text/plain": [ + "<IPython.core.display.HTML object>" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "Run data is saved locally in <code>/home/nibio/mutable-outside-world/code/nibio_graph_sem_seg/jupyters/wandb/run-20230525_133538-mpf5c532</code>" + ], + "text/plain": [ + "<IPython.core.display.HTML object>" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "Syncing run <strong><a href='https://wandb.ai/maciej-wielgosz-nibio/dgcnn/runs/mpf5c532' target=\"_blank\">pretty-river-363</a></strong> to <a href='https://wandb.ai/maciej-wielgosz-nibio/dgcnn' target=\"_blank\">Weights & Biases</a> (<a href='https://wandb.me/run' target=\"_blank\">docs</a>)<br/>" + ], + "text/plain": [ + "<IPython.core.display.HTML object>" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + " View project at <a href='https://wandb.ai/maciej-wielgosz-nibio/dgcnn' target=\"_blank\">https://wandb.ai/maciej-wielgosz-nibio/dgcnn</a>" + ], + "text/plain": [ + "<IPython.core.display.HTML object>" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + " View run at <a href='https://wandb.ai/maciej-wielgosz-nibio/dgcnn/runs/mpf5c532' target=\"_blank\">https://wandb.ai/maciej-wielgosz-nibio/dgcnn/runs/mpf5c532</a>" + ], + "text/plain": [ + "<IPython.core.display.HTML object>" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Logging point cloud\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/home/nibio/mutable-outside-world/code/nibio_graph_sem_seg/wandb_vis/log_point_cloud.py:30: UserWarning: Creating a tensor from a list of numpy.ndarrays is extremely slow. Please consider converting the list to a single numpy.ndarray with numpy.array() before converting to a tensor. (Triggered internally at ../torch/csrc/utils/tensor_new.cpp:230.)\n", + " label_gt = torch.tensor(label_gt)\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "table saved\n" + ] + } + ], + "source": [ + "# add the path to the src folder\n", + "import sys\n", + "import os\n", + "\n", + "\n", + "WORK_DIR = '/home/nibio/mutable-outside-world/code/nibio_graph_sem_seg'\n", + "sys.path.append(os.path.join(WORK_DIR))\n", + "import wandb\n", + "from wandb_vis.log_point_cloud import LogPointCloud\n", + "from src.shapenet_data import ShapeNetPart\n", + "# from src.shapenet_data_new import ShapeNetPart\n", + "\n", + "use_wandb = True\n", + "\n", + "dataset = ShapeNetPart(num_points=2048, partition='train', class_choice='chair')\n", + "# take 10 data items and log them into an array\n", + "data_point = []\n", + "data_label = []\n", + "\n", + "data = [item for item in dataset]\n", + "\n", + "data_point = [item[0] for item in data]\n", + "data_label = [item[2] for item in data]\n", + "\n", + "\n", + "# create a wandb run\n", + "if use_wandb:\n", + " wandb_run = wandb.init(project=\"dgcnn\", entity=\"maciej-wielgosz-nibio\")\n", + " # log the point cloud\n", + " print(\"Logging point cloud\")\n", + " log_point_cloud = LogPointCloud(log_metrics=True)\n", + " log_point_cloud(data_point[:20], data_label[:20], data_label[:20])\n", + " log_point_cloud.save_table()\n", + "\n", + "\n" + ] + } + ], + "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.8.10" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/jupyters/test_shapenet_data.ipynb b/jupyters/test_shapenet_data.ipynb deleted file mode 100644 index d07e8bbbc232267b1cd01e9daef6b27b569bbcb7..0000000000000000000000000000000000000000 --- a/jupyters/test_shapenet_data.ipynb +++ /dev/null @@ -1,194 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/home/nibio/.local/lib/python3.8/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", - " from .autonotebook import tqdm as notebook_tqdm\n", - "Failed to detect the name of this notebook, you can set it manually with the WANDB_NOTEBOOK_NAME environment variable to enable code saving.\n", - "\u001b[34m\u001b[1mwandb\u001b[0m: Currently logged in as: \u001b[33mmaciej-wielgosz-nibio\u001b[0m. Use \u001b[1m`wandb login --relogin`\u001b[0m to force relogin\n" - ] - }, - { - "data": { - "text/html": [ - "wandb version 0.15.0 is available! To upgrade, please run:\n", - " $ pip install wandb --upgrade" - ], - "text/plain": [ - "<IPython.core.display.HTML object>" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "Tracking run with wandb version 0.14.2" - ], - "text/plain": [ - "<IPython.core.display.HTML object>" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "Run data is saved locally in <code>/home/nibio/mutable-outside-world/code/nibio_graph_sem_seg/jupyters/wandb/run-20230428_100053-566cteoy</code>" - ], - "text/plain": [ - "<IPython.core.display.HTML object>" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "Syncing run <strong><a href='https://wandb.ai/maciej-wielgosz-nibio/dgcnn/runs/566cteoy' target=\"_blank\">happy-plasma-356</a></strong> to <a href='https://wandb.ai/maciej-wielgosz-nibio/dgcnn' target=\"_blank\">Weights & Biases</a> (<a href='https://wandb.me/run' target=\"_blank\">docs</a>)<br/>" - ], - "text/plain": [ - "<IPython.core.display.HTML object>" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - " View project at <a href='https://wandb.ai/maciej-wielgosz-nibio/dgcnn' target=\"_blank\">https://wandb.ai/maciej-wielgosz-nibio/dgcnn</a>" - ], - "text/plain": [ - "<IPython.core.display.HTML object>" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - " View run at <a href='https://wandb.ai/maciej-wielgosz-nibio/dgcnn/runs/566cteoy' target=\"_blank\">https://wandb.ai/maciej-wielgosz-nibio/dgcnn/runs/566cteoy</a>" - ], - "text/plain": [ - "<IPython.core.display.HTML object>" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Logging point cloud\n" - ] - }, - { - "ename": "IndexError", - "evalue": "list index out of range", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m/home/nibio/mutable-outside-world/code/nibio_graph_sem_seg/jupyters/test_shapenet_data.ipynb Cell 1\u001b[0m in \u001b[0;36m3\n\u001b[1;32m <a href='vscode-notebook-cell://ssh-remote%2Boracle_docker/home/nibio/mutable-outside-world/code/nibio_graph_sem_seg/jupyters/test_shapenet_data.ipynb#W0sdnNjb2RlLXJlbW90ZQ%3D%3D?line=28'>29</a>\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39mLogging point cloud\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[1;32m <a href='vscode-notebook-cell://ssh-remote%2Boracle_docker/home/nibio/mutable-outside-world/code/nibio_graph_sem_seg/jupyters/test_shapenet_data.ipynb#W0sdnNjb2RlLXJlbW90ZQ%3D%3D?line=29'>30</a>\u001b[0m log_point_cloud \u001b[39m=\u001b[39m LogPointCloud(log_metrics\u001b[39m=\u001b[39m\u001b[39mTrue\u001b[39;00m)\n\u001b[0;32m---> <a href='vscode-notebook-cell://ssh-remote%2Boracle_docker/home/nibio/mutable-outside-world/code/nibio_graph_sem_seg/jupyters/test_shapenet_data.ipynb#W0sdnNjb2RlLXJlbW90ZQ%3D%3D?line=30'>31</a>\u001b[0m log_point_cloud(data_point[:\u001b[39m10\u001b[39;49m], data_label[:\u001b[39m20\u001b[39;49m], data_label[:\u001b[39m20\u001b[39;49m])\n\u001b[1;32m <a href='vscode-notebook-cell://ssh-remote%2Boracle_docker/home/nibio/mutable-outside-world/code/nibio_graph_sem_seg/jupyters/test_shapenet_data.ipynb#W0sdnNjb2RlLXJlbW90ZQ%3D%3D?line=31'>32</a>\u001b[0m log_point_cloud\u001b[39m.\u001b[39msave_table()\n", - "File \u001b[0;32m~/mutable-outside-world/code/nibio_graph_sem_seg/wandb_vis/log_point_cloud.py:126\u001b[0m, in \u001b[0;36mLogPointCloud.__call__\u001b[0;34m(self, *args, **kwds)\u001b[0m\n\u001b[1;32m 125\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39m__call__\u001b[39m(\u001b[39mself\u001b[39m, \u001b[39m*\u001b[39margs: Any, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwds: Any) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m Any:\n\u001b[0;32m--> 126\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mlog(\u001b[39m*\u001b[39;49margs, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwds)\n", - "File \u001b[0;32m~/mutable-outside-world/code/nibio_graph_sem_seg/wandb_vis/log_point_cloud.py:112\u001b[0m, in \u001b[0;36mLogPointCloud.log\u001b[0;34m(self, points, label_gt, label_pred)\u001b[0m\n\u001b[1;32m 111\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mlog\u001b[39m(\u001b[39mself\u001b[39m, points, label_gt, label_pred): \n\u001b[0;32m--> 112\u001b[0m data_point_clouds, pred_point_clouds, diff_point_clouds \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mcreate_point_clouds(points, label_gt, label_pred)\n\u001b[1;32m 113\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mlog_metrics:\n\u001b[1;32m 114\u001b[0m acc, prec, rec, iou \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mcompute_metrics(label_gt, label_pred)\n", - "File \u001b[0;32m~/mutable-outside-world/code/nibio_graph_sem_seg/wandb_vis/log_point_cloud.py:62\u001b[0m, in \u001b[0;36mLogPointCloud.create_point_clouds\u001b[0;34m(self, points, label_gt, label_pred)\u001b[0m\n\u001b[1;32m 60\u001b[0m \u001b[39mfor\u001b[39;00m i \u001b[39min\u001b[39;00m \u001b[39mrange\u001b[39m(\u001b[39mlen\u001b[39m(label_pred)):\n\u001b[1;32m 61\u001b[0m colors_per_point \u001b[39m=\u001b[39m [\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mcolors[label_pred[i][j]] \u001b[39mfor\u001b[39;00m j \u001b[39min\u001b[39;00m \u001b[39mrange\u001b[39m(\u001b[39mlen\u001b[39m(label_pred[i]))]\n\u001b[0;32m---> 62\u001b[0m points_rgb \u001b[39m=\u001b[39m np\u001b[39m.\u001b[39marray([[p[\u001b[39m0\u001b[39m], p[\u001b[39m1\u001b[39m], p[\u001b[39m2\u001b[39m], c[\u001b[39m0\u001b[39m], c[\u001b[39m1\u001b[39m], c[\u001b[39m2\u001b[39m]] \u001b[39mfor\u001b[39;00m p, c \u001b[39min\u001b[39;00m \u001b[39mzip\u001b[39m(points[i], colors_per_point)])\n\u001b[1;32m 63\u001b[0m pred_point_clouds[i] \u001b[39m=\u001b[39m wandb\u001b[39m.\u001b[39mObject3D(\n\u001b[1;32m 64\u001b[0m {\n\u001b[1;32m 65\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mtype\u001b[39m\u001b[39m\"\u001b[39m: \u001b[39m\"\u001b[39m\u001b[39mlidar/beta\u001b[39m\u001b[39m\"\u001b[39m,\n\u001b[1;32m 66\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mpoints\u001b[39m\u001b[39m\"\u001b[39m: points_rgb\n\u001b[1;32m 67\u001b[0m }\n\u001b[1;32m 68\u001b[0m )\n\u001b[1;32m 70\u001b[0m \u001b[39mfor\u001b[39;00m i \u001b[39min\u001b[39;00m \u001b[39mrange\u001b[39m(\u001b[39mlen\u001b[39m(points)):\n\u001b[1;32m 71\u001b[0m \u001b[39m# check if label_gt is equal to label_pred\u001b[39;00m\n", - "\u001b[0;31mIndexError\u001b[0m: list index out of range" - ] - } - ], - "source": [ - "# add the path to the src folder\n", - "import sys\n", - "import os\n", - "\n", - "\n", - "WORK_DIR = '/home/nibio/mutable-outside-world/code/nibio_graph_sem_seg'\n", - "sys.path.append(os.path.join(WORK_DIR))\n", - "import wandb\n", - "from wandb_vis.log_point_cloud import LogPointCloud\n", - "from src.shapenet_data import ShapeNetPart\n", - "\n", - "use_wandb = True\n", - "\n", - "dataset = ShapeNetPart(num_points=2048, partition='train', class_choice='chair')\n", - "# take 10 data items and log them into an array\n", - "data_point = []\n", - "data_label = []\n", - "\n", - "data = [item for item in dataset]\n", - "\n", - "data_point = [item[0] for item in data]\n", - "data_label = [item[2] for item in data]\n", - "\n", - "\n", - "# create a wandb run\n", - "if use_wandb:\n", - " wandb_run = wandb.init(project=\"dgcnn\", entity=\"maciej-wielgosz-nibio\")\n", - " # log the point cloud\n", - " print(\"Logging point cloud\")\n", - " log_point_cloud = LogPointCloud(log_metrics=True)\n", - " log_point_cloud(data_point[:20], data_label[:20], data_label[:20])\n", - " log_point_cloud.save_table()\n", - "\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import json\n", - "\n", - "colors = json.load(open(\"/home/nibio/mutable-outside-world/code/nibio_graph_sem_seg/prepare_data/meta/partseg_colors.txt\", \"r\"))\n", - "\n", - "colors\n", - "\n", - "# color_array = []\n", - "\n", - "# for i in range(50):\n", - "# color_array.append(colors[i]['color'])\n", - "\n", - "\n", - "# color_array" - ] - } - ], - "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.8.10" - }, - "orig_nbformat": 4 - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/model.py b/model.py index 57582b483db9ef70888237ed93e4b088df8d65db..8cdf443161472f4368151e3f24a016526870a745 100644 --- a/model.py +++ b/model.py @@ -1,7 +1,3 @@ -import os -import sys -import copy -import math import numpy as np import torch import torch.nn as nn diff --git a/src/shapenet_data.py b/src/shapenet_data.py index 84742db244a934d14b34256911621db95a480c32..b1c408576b68da3337d87ef61cc19c73729ee828 100644 --- a/src/shapenet_data.py +++ b/src/shapenet_data.py @@ -1,23 +1,40 @@ import glob import json import os +import subprocess +import urllib.parse +from pathlib import Path + import cv2 -from torch.utils.data import Dataset -import numpy as np import h5py +import numpy as np +from torch.utils.data import Dataset -def download_shapenetpart(): - BASE_DIR = os.path.dirname(os.path.abspath(__file__)) - DATA_DIR = os.path.join(BASE_DIR, 'data') - if not os.path.exists(DATA_DIR): - os.mkdir(DATA_DIR) - if not os.path.exists(os.path.join(DATA_DIR, 'shapenet_part_seg_hdf5_data')): - www = 'https://shapenet.cs.stanford.edu/media/shapenet_part_seg_hdf5_data.zip' - zipfile = os.path.basename(www) - os.system('wget --no-check-certificate %s; unzip %s' % (www, zipfile)) - os.system('mv %s %s' % ('hdf5_data', os.path.join(DATA_DIR, 'shapenet_part_seg_hdf5_data'))) - os.system('rm %s' % (zipfile)) + + +def download_shapenetpart(download_location): + """Downloads the ShapeNet part dataset if it's not already in the download_location directory.""" + + DATASET_URL = 'https://shapenet.cs.stanford.edu/media/shapenet_part_seg_hdf5_data.zip' + DATASET_NAME = 'shapenet_part_seg_hdf5_data' + HDF5_NAME = 'hdf5_data' + + def download_dataset(url, dataset_name, hdf5_name): + """Downloads and unzips a file from a specified url.""" + file_name = urllib.parse.urlsplit(url).path.split("/")[-1] + subprocess.run(['wget', '--no-check-certificate', url], check=True) + subprocess.run(['unzip', file_name], check=True) + subprocess.run(['mv', hdf5_name, dataset_name], check=True) + subprocess.run(['rm', file_name], check=True) + + download_location = Path(download_location) + download_location.mkdir(parents=True, exist_ok=True) + + if not (download_location / DATASET_NAME).exists(): + print('Downloading ShapeNet part dataset...') + download_dataset(DATASET_URL, DATASET_NAME, HDF5_NAME) + def load_color_partseg(): colors = [] @@ -64,9 +81,9 @@ def load_color_partseg(): def load_data_partseg(partition): - download_shapenetpart() BASE_DIR = os.path.dirname(os.path.abspath(__file__)) DATA_DIR = os.path.join(BASE_DIR, 'data') + download_shapenetpart(DATA_DIR) all_data = [] all_label = [] all_seg = []