mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-05 23:38:42 +08:00
Refactoring (data loading, semantic-nerf/nerf-w fixes, ...) (#8)
* in progress. camera_indices broken * faster dataloader. starting on pixel sampler logic * imagesampler and pixelsampler * lint and black * continuing merge * pytest working * fixed docs * docs * minor changes
This commit is contained in:
1 parent
1f189b3e54
commit
146332cfb6
50 files changed
+914
-831
No files matched your search
@@ -20,15 +20,17 @@
|
||||
"import torch\n",
|
||||
"from torch.utils.data import DataLoader\n",
|
||||
"\n",
|
||||
"from pyrad.nerf.dataset.image_dataset import ImageDataset, collate_batch\n",
|
||||
"from pyrad.nerf.dataset.collate import CollateIterDataset, collate_batch_size_one\n",
|
||||
"from pyrad.nerf.dataset.image_dataset import ImageDataset, PanopticImageDataset\n",
|
||||
"from pyrad.nerf.image_sampler import CacheImageSampler\n",
|
||||
"from pyrad.nerf.pixel_sampler import PixelSampler\n",
|
||||
"from pyrad.nerf.dataset.utils import get_dataset_inputs\n",
|
||||
"from pyrad.nerf.field_modules.ray_generator import RayGenerator\n",
|
||||
"from pyrad.nerf.field_modules.scene_bounds_collider import SceneBoundsCollider\n",
|
||||
"from pyrad.nerf.ray_generator import RayGenerator\n",
|
||||
"from pyrad.nerf.colliders import SceneBoundsCollider, AABBBoxCollider\n",
|
||||
"from pyrad.structures.rays import RayBundle\n",
|
||||
"from pyrad.utils.io import get_absolute_path\n",
|
||||
"from pyrad.viewer.plotly import get_line_segments_from_lines\n",
|
||||
"from pyrad.structures.cameras import get_camera_model\n",
|
||||
"from pyrad.utils.misc import get_dict_to_torch, instantiate_from_dict_config\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"from hydra import compose, initialize\n",
|
||||
@@ -38,28 +40,6 @@
|
||||
"import random"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "012bf657",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import goat\n",
|
||||
"\n",
|
||||
"vis = goat.vis_utils.get_vis()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "71f46c79",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"goat.vis_utils.show_box_test(vis)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
@@ -67,70 +47,9 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"with initialize(config_path=\"../configs\"):\n",
|
||||
" cfg = compose(config_name=\"default.yaml\")\n",
|
||||
" # cfg = compose(config_name=\"default.yaml\", overrides=[\"data/dataset=friends_TBBT-big_living_room\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "0b010534",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"dataset_inputs = get_dataset_inputs(**cfg.data.dataset, split=\"train\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e95073a9",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# import goat.threedvis.meshcat.geometry as g\n",
|
||||
"# import numpy as np\n",
|
||||
"# point_cloud = dataset_inputs.point_cloud\n",
|
||||
"# size = 0.1\n",
|
||||
"# vis[\"point_cloud\"].set_object(\n",
|
||||
"# g.PointCloud(position=np.transpose(np.array(point_cloud.xyz)),\n",
|
||||
"# color=np.transpose(np.array(point_cloud.rgb)) / 255.0, size=size)\n",
|
||||
"# )"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "aac97d4a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# show dataset inputs with three.js\n",
|
||||
"# goat.vis_utils.show_box_test(vis)\n",
|
||||
"num_images_to_show = 200\n",
|
||||
"for idx in tqdm(list(range(len(dataset_inputs.image_filenames)))[:num_images_to_show]):\n",
|
||||
" image = imageio.imread(dataset_inputs.image_filenames[idx])[:, :, :3]\n",
|
||||
"\n",
|
||||
" num_intrinsics_params = len(dataset_inputs.intrinsics[idx])\n",
|
||||
" camera_class = get_camera_model(num_intrinsics_params)\n",
|
||||
" camera_to_world = dataset_inputs.camera_to_world[idx]\n",
|
||||
" camera = camera_class(*dataset_inputs.intrinsics[idx].tolist(), camera_to_world=camera_to_world)\n",
|
||||
" K = camera.get_intrinsics_matrix()\n",
|
||||
"\n",
|
||||
" name = \"train/{:06d}\".format(idx)\n",
|
||||
" # height = 0.5\n",
|
||||
" displayed_focal_length = 0.5\n",
|
||||
" goat.vis_utils.draw_camera_frustum(\n",
|
||||
" vis,\n",
|
||||
" name=name,\n",
|
||||
" image=image,\n",
|
||||
" K=K.numpy(),\n",
|
||||
" pose=camera_to_world.double().numpy(),\n",
|
||||
" height=3.0,\n",
|
||||
" realistic=False,\n",
|
||||
" displayed_focal_length=0.0,\n",
|
||||
" )"
|
||||
"with initialize(version_base=None, config_path=\"../configs\"):\n",
|
||||
" config = compose(config_name=\"default_setup.yaml\")\n",
|
||||
"dataset_inputs = get_dataset_inputs(**config.data.dataset, split=\"train\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -140,31 +59,34 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"image_dataset = ImageDataset(\n",
|
||||
" image_filenames=dataset_inputs.image_filenames, downscale_factor=dataset_inputs.downscale_factor\n",
|
||||
")\n",
|
||||
"iter_dataset = CollateIterDataset(\n",
|
||||
" image_dataset,\n",
|
||||
" collate_fn=lambda batch_list: collate_batch(\n",
|
||||
" batch_list, cfg.data.dataloader.num_rays_per_batch, keep_full_image=True\n",
|
||||
" ),\n",
|
||||
" num_samples_to_collate=cfg.data.dataloader.num_images_to_sample_from,\n",
|
||||
" num_times_to_repeat=cfg.data.dataloader.num_times_to_repeat_images,\n",
|
||||
")\n",
|
||||
"dataloader = DataLoader(\n",
|
||||
" iter_dataset,\n",
|
||||
" batch_size=1,\n",
|
||||
" num_workers=cfg.data.dataloader.num_workers,\n",
|
||||
" collate_fn=collate_batch_size_one,\n",
|
||||
" pin_memory=True,\n",
|
||||
")\n",
|
||||
"dataloader_iter = iter(dataloader)\n",
|
||||
"device = \"cpu\"\n",
|
||||
"\n",
|
||||
"train_image_dataset = instantiate_from_dict_config(\n",
|
||||
" config.data.image_dataset,\n",
|
||||
" image_filenames=dataset_inputs.image_filenames,\n",
|
||||
" downscale_factor=dataset_inputs.downscale_factor,\n",
|
||||
" semantics=dataset_inputs.semantics,\n",
|
||||
" alpha_color=dataset_inputs.alpha_color,\n",
|
||||
") # ImageDataset\n",
|
||||
"train_image_sampler = CacheImageSampler(\n",
|
||||
" train_image_dataset,\n",
|
||||
" num_samples_to_collate=len(train_image_dataset)\n",
|
||||
" if config.data.image_sampler.num_images_to_sample_from == 0\n",
|
||||
" else config.data.image_sampler.num_images_to_sample_from,\n",
|
||||
" num_times_to_repeat_images=config.data.image_sampler.num_times_to_repeat_images,\n",
|
||||
" device=device if config.data.image_sampler.move_to_graph_device else \"cpu\",\n",
|
||||
") # ImageSampler\n",
|
||||
"train_pixel_sampler = PixelSampler(\n",
|
||||
" num_rays_per_batch=config.data.pixel_sampler.num_rays_per_batch, keep_full_image=True\n",
|
||||
") # PixelSampler\n",
|
||||
"\n",
|
||||
"ray_generator = RayGenerator(dataset_inputs.intrinsics, dataset_inputs.camera_to_world)\n",
|
||||
"\n",
|
||||
"iter_train_image_sampler = iter(train_image_sampler)\n",
|
||||
"num_batches = 10\n",
|
||||
"for _ in tqdm(range(num_batches)):\n",
|
||||
" batch = next(dataloader_iter)\n",
|
||||
" image_batch = next(iter_train_image_sampler)\n",
|
||||
" batch = train_pixel_sampler.sample(image_batch)\n",
|
||||
" ray_bundle = ray_generator.forward(batch[\"indices\"])\n",
|
||||
" break"
|
||||
]
|
||||
@@ -176,7 +98,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"media.show_image(image_dataset.get_image(10))"
|
||||
"media.show_image(train_image_dataset.get_image(10), height=200)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -188,20 +110,22 @@
|
||||
"source": [
|
||||
"def show_batch(batch):\n",
|
||||
" # set the color of the sampled rays\n",
|
||||
" print(batch.keys())\n",
|
||||
" c, y, x = [i.flatten() for i in torch.split(batch[\"local_indices\"], 1, dim=-1)]\n",
|
||||
" batch[\"image\"][c, y, x] = 1.0\n",
|
||||
" batch[\"image\"][c, y, x] = 0.0\n",
|
||||
"\n",
|
||||
" # batch[\"image\"] is num_images, h, w, 3\n",
|
||||
" images = torch.split(batch[\"image\"], 1, dim=0)\n",
|
||||
" image_list = [image[0] for image in images]\n",
|
||||
" image = torch.cat(image_list, dim=1) # cat along the width dimension\n",
|
||||
"\n",
|
||||
" # the white pixels are rays\n",
|
||||
" # the black pixels are rays\n",
|
||||
" media.show_image((image * 255).to(torch.uint8))\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def sample_and_show_batch():\n",
|
||||
" batch = next(iter(dataloader))\n",
|
||||
" image_batch = next(iter_train_image_sampler)\n",
|
||||
" batch = train_pixel_sampler.sample(image_batch)\n",
|
||||
" show_batch(batch)\n",
|
||||
" return batch"
|
||||
]
|
||||
@@ -256,20 +180,10 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"scene_bounds_collider = SceneBoundsCollider(dataset_inputs.scene_bounds)\n",
|
||||
"scene_bounds_collider = AABBBoxCollider(dataset_inputs.scene_bounds)\n",
|
||||
"intersected_ray_bundle = scene_bounds_collider(ray_bundle)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "9ca1b3c3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# intersected_ray_bundle.nears"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
@@ -277,12 +191,10 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# near_plane = cfg.graph.network.near_plane\n",
|
||||
"# far_plane = cfg.graph.network.far_plane\n",
|
||||
"lines = torch.stack(\n",
|
||||
" [\n",
|
||||
" intersected_ray_bundle.origins + intersected_ray_bundle.directions * intersected_ray_bundle.nears,\n",
|
||||
" intersected_ray_bundle.origins + intersected_ray_bundle.directions * intersected_ray_bundle.fars,\n",
|
||||
" intersected_ray_bundle.origins + intersected_ray_bundle.directions * intersected_ray_bundle.nears[..., None],\n",
|
||||
" intersected_ray_bundle.origins + intersected_ray_bundle.directions * intersected_ray_bundle.fars[..., None],\n",
|
||||
" ],\n",
|
||||
" dim=1,\n",
|
||||
").tolist() # (num_rays, 2, 3)\n",
|
||||
@@ -323,34 +235,7 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "7aa506fe-66a1-4284-ade4-8c90fde5476a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "f259edb0-4126-4930-99f4-b5574dbb2eea",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"fig.write_image(\"temp.png\")\n",
|
||||
"plotly_image = imageio.imread(\"temp.png\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "d1a11772-4692-4800-b446-cfdbe4ab92fa",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "722f5370-3b0f-4ad1-87a1-df479ae85b60",
|
||||
"id": "fd04703a-aa50-45e4-95fd-4d881ff18d61",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
@@ -361,9 +246,9 @@
|
||||
"hash": "4a749acc7d255f078aee52e0584cc77b3cb5aaed1b3a7407ec4262c1bf6cb526"
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "pyrad",
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "pyrad"
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
|
||||
Reference in new issue
Block a user