mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-06 15:59:16 +08:00
Refactor occupancy
This commit is contained in:
1 parent
443fdef553
commit
97cb328468
5 files changed
+138
-25
No files matched your search
@@ -1,7 +1,6 @@
|
||||
"""
|
||||
The Graph module contains all trainable parameters.
|
||||
"""
|
||||
import logging
|
||||
from abc import abstractmethod
|
||||
from collections import defaultdict
|
||||
from typing import Dict, List, Union
|
||||
@@ -13,7 +12,6 @@ from torch.nn import Parameter
|
||||
from torchtyping import TensorType
|
||||
|
||||
from mattport.nerf.dataset.structs import SceneBounds
|
||||
from mattport.nerf.occupancy_grid import OccupancyGrid
|
||||
from mattport.nerf.ray_generator import RayGenerator
|
||||
from mattport.nerf.colliders import AABBBoxCollider
|
||||
from mattport.structures.cameras import get_camera_model
|
||||
@@ -65,7 +63,6 @@ class Graph(AbstractGraph):
|
||||
self.steps_per_occupancy_grid_update = steps_per_occupancy_grid_update
|
||||
self.kwargs = kwargs
|
||||
self.collider = None
|
||||
self.occupancy_grid = OccupancyGrid(aabb=self.scene_bounds.aabb)
|
||||
self.ray_generator = RayGenerator(self.intrinsics, self.camera_to_world)
|
||||
self.populate_collider()
|
||||
self.populate_fields()
|
||||
@@ -97,10 +94,6 @@ class Graph(AbstractGraph):
|
||||
batch: Union[str, Dict[str, torch.tensor]] = None,
|
||||
step: int = None,
|
||||
):
|
||||
# update the occupancy grid
|
||||
if step and step % self.steps_per_occupancy_grid_update == 0:
|
||||
logging.info("Updating occupancy grid.")
|
||||
self.occupancy_grid.update_occupancy_grid(density_fn=self.field_fine.density_fn)
|
||||
|
||||
# get the rays
|
||||
original_ray_bundle = self.ray_generator.forward(ray_indices) # RayBundle
|
||||
|
||||
@@ -15,6 +15,7 @@ from mattport.nerf.field_modules.encoding import NeRFEncoding
|
||||
from mattport.nerf.field_modules.field_heads import DensityFieldHead, RGBFieldHead
|
||||
from mattport.nerf.field_modules.mlp import MLP
|
||||
from mattport.nerf.fields.base import Field
|
||||
from mattport.nerf.fields.density_field import DensityField
|
||||
from mattport.nerf.graph.base import Graph
|
||||
from mattport.nerf.loss import MSELoss
|
||||
from mattport.nerf.renderers import AccumulationRenderer, DepthRenderer, RGBRenderer
|
||||
@@ -42,6 +43,9 @@ class NeRFField(Field):
|
||||
self.build_mlp_rgb()
|
||||
self.build_heads()
|
||||
|
||||
sequence = nn.Sequential(self.encoding_xyz, self.mlp_base)
|
||||
self.density_field = DensityField(sequence)
|
||||
|
||||
def build_encodings(self):
|
||||
"""Build the encodings."""
|
||||
self.encoding_xyz = NeRFEncoding(
|
||||
@@ -89,9 +93,8 @@ class NeRFField(Field):
|
||||
)
|
||||
if not valid_mask.any(): # empty mask
|
||||
return density, base_mlp_out
|
||||
encoded_xyz = self.encoding_xyz(positions[valid_mask])
|
||||
base_mlp_out[valid_mask] = self.mlp_base(encoded_xyz)
|
||||
density[valid_mask] = self.field_output_density(base_mlp_out[valid_mask])
|
||||
|
||||
density[valid_mask], base_mlp_out[valid_mask] = self.density_field(positions[valid_mask])
|
||||
return density, base_mlp_out
|
||||
|
||||
def get_outputs(self, point_samples: PointSamples, density_embedding=None, valid_mask=None):
|
||||
@@ -138,7 +141,6 @@ class NeRFGraph(Graph):
|
||||
self.field_fine = NeRFField()
|
||||
|
||||
def populate_misc_modules(self):
|
||||
|
||||
# samplers
|
||||
self.sampler_uniform = UniformSampler(num_samples=self.num_coarse_samples)
|
||||
self.sampler_pdf = PDFSampler(num_samples=self.num_importance_samples)
|
||||
@@ -162,7 +164,6 @@ class NeRFGraph(Graph):
|
||||
return param_groups
|
||||
|
||||
def get_outputs(self, ray_bundle: RayBundle):
|
||||
|
||||
# uniform sampling
|
||||
ray_samples_uniform = self.sampler_uniform(ray_bundle)
|
||||
|
||||
@@ -189,12 +190,6 @@ class NeRFGraph(Graph):
|
||||
accumulation_fine = self.renderer_accumulation(weights_fine)
|
||||
depth_fine = self.renderer_depth(weights_fine, ray_samples_pdf.ts)
|
||||
|
||||
# --- occupancy grid ---
|
||||
# TODO(ethan): move this elsewhere. currently here for debugging purposes
|
||||
density_occ = self.occupancy_grid.get_densities(ray_samples_pdf.positions)
|
||||
weights_occ = ray_samples_pdf.get_weights(density_occ)
|
||||
depth_occ = self.renderer_depth(weights_occ, ray_samples_pdf.ts)
|
||||
|
||||
outputs = {
|
||||
"rgb_coarse": rgb_coarse,
|
||||
"rgb_fine": rgb_fine,
|
||||
@@ -202,7 +197,6 @@ class NeRFGraph(Graph):
|
||||
"accumulation_fine": accumulation_fine,
|
||||
"depth_coarse": depth_coarse,
|
||||
"depth_fine": depth_fine,
|
||||
"depth_occ": depth_occ,
|
||||
}
|
||||
return outputs
|
||||
|
||||
@@ -241,9 +235,6 @@ class NeRFGraph(Graph):
|
||||
writer.write_image(name=f"image_idx_{image_idx}", image=combined_acc, step=step, group="accumulation")
|
||||
writer.write_image(name=f"image_idx_{image_idx}", image=combined_depth, step=step, group="depth")
|
||||
|
||||
depth_occ = visualization.apply_depth_colormap(outputs["depth_occ"])
|
||||
writer.write_image(name=f"image_idx_{image_idx}", image=depth_occ, step=step, group="depth_occupancy_grid")
|
||||
|
||||
# Switch images from [H, W, C] to [1, C, H, W] for metrics computations
|
||||
image = torch.moveaxis(image, -1, 0)[None, ...]
|
||||
rgb_coarse = torch.moveaxis(rgb_coarse, -1, 0)[None, ...]
|
||||
|
||||
Reference in new issue
Block a user