mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-07 00:09:14 +08:00
network pass running
This commit is contained in:
1 parent
be6fbe799e
commit
7819f6da9d
11 files changed
+121
-128
No files matched your search
@@ -1,16 +1,13 @@
|
||||
"""
|
||||
The Graph module contains all trainable parameters.
|
||||
"""
|
||||
from abc import abstractmethod
|
||||
import importlib
|
||||
from abc import abstractmethod
|
||||
from dataclasses import dataclass
|
||||
from typing import Dict, List, Optional, Set
|
||||
from black import Priority
|
||||
from omegaconf import DictConfig
|
||||
|
||||
from torch import nn
|
||||
|
||||
from mattport.nerf.dataset.utils import DatasetInputs
|
||||
from torchtyping import TensorType
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -68,7 +65,6 @@ class Graph(nn.ModuleDict):
|
||||
@abstractmethod
|
||||
def populate_modules(self):
|
||||
"""Initializes the modules that are part of the network."""
|
||||
pass
|
||||
|
||||
def get_in_dim(self, curr_node: Node) -> None:
|
||||
"""Dynamically calculates and sets the input dimensions of the modules based on dependency graph
|
||||
@@ -152,11 +148,9 @@ class Graph(nn.ModuleDict):
|
||||
return ordering_stack[::-1]
|
||||
|
||||
@abstractmethod
|
||||
def forward(self):
|
||||
def forward(self, ray_indices: TensorType["num_rays", 3]):
|
||||
"""Forward function that needs to be overridden."""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_losses(self):
|
||||
def get_losses(self, batch, graph_outputs):
|
||||
"""Computes and returns the losses."""
|
||||
pass
|
||||
+24
-36
@@ -3,18 +3,9 @@ Implementation of vanilla nerf.
|
||||
"""
|
||||
|
||||
|
||||
import logging
|
||||
|
||||
import torch
|
||||
from omegaconf import DictConfig
|
||||
from torch import nn
|
||||
from torchtyping import TensorType
|
||||
|
||||
from mattport.nerf.dataset.utils import DatasetInputs, get_dataset_inputs
|
||||
from mattport.nerf.field.nerf import NeRFField
|
||||
from mattport.nerf.field_modules.encoding import NeRFEncoding
|
||||
from mattport.nerf.field_modules.field_outputs import DensityFieldOutput, RGBFieldOutput
|
||||
from mattport.nerf.field_modules.mlp import MLP
|
||||
from mattport.nerf.field_modules.ray_generator import RayGenerator
|
||||
from mattport.nerf.graph.base import Graph
|
||||
from mattport.nerf.loss import MSELoss
|
||||
@@ -23,6 +14,11 @@ from mattport.nerf.sampler import UniformSampler
|
||||
|
||||
|
||||
class NeRFGraph(Graph):
|
||||
"""_summary_
|
||||
|
||||
Args:
|
||||
Graph (_type_): _description_
|
||||
"""
|
||||
def __init__(self, intrinsics=None, camera_to_world=None) -> None:
|
||||
super().__init__(intrinsics=intrinsics, camera_to_world=camera_to_world)
|
||||
|
||||
@@ -46,36 +42,28 @@ class NeRFGraph(Graph):
|
||||
|
||||
def forward(self, ray_indices: TensorType["num_rays", 3]):
|
||||
"""Takes in the ray indices and renders out values."""
|
||||
|
||||
ray_bundle = self.ray_generator.forward(ray_indices)
|
||||
ray_samples = self.sampler_uniform(ray_bundle)
|
||||
|
||||
xyz = ray_samples.get_positions(ray_bundle) # (num_rays, num_samples, 3)
|
||||
num_rays, num_samples = xyz.shape[:2]
|
||||
xyz = xyz.view(-1, 3)
|
||||
dirs = (
|
||||
ray_bundle.directions.unsqueeze(1).repeat(1, num_samples, 1).view(num_rays * num_samples, 3)
|
||||
) # (num_rays, 3)
|
||||
|
||||
field_output_rgb, field_output_density = self.field_coarse(xyz=xyz, dirs=dirs)
|
||||
# move these back to the correct shape
|
||||
field_output_rgb = field_output_rgb.view(num_rays, num_samples, 3)
|
||||
field_output_density = field_output_density.view(num_rays, num_samples)
|
||||
|
||||
deltas = ray_samples.get_deltas()
|
||||
rgb_coarse = self.renderer_rgb(rgb=field_output_rgb, density=field_output_density, deltas=deltas)
|
||||
|
||||
print(field_output_rgb)
|
||||
print(field_output_density)
|
||||
outputs = {
|
||||
"rgb_coarse": rgb_coarse
|
||||
}
|
||||
# get the rays:
|
||||
ray_bundle = self.ray_generator.forward(ray_indices) # RayBundle
|
||||
# coarse network:
|
||||
uniform_ray_samples = self.sampler_uniform(ray_bundle) # RaySamples
|
||||
coarse_field_outputs = self.field_coarse(uniform_ray_samples) # FieldOutputs
|
||||
coarse_renderer_outputs = self.renderer_rgb(
|
||||
rgb=coarse_field_outputs.rgb, density=coarse_field_outputs.density, deltas=uniform_ray_samples.deltas
|
||||
) # RendererOutputs
|
||||
# fine network:
|
||||
pdf_ray_samples = self.sampler_uniform(ray_bundle) # RaySamples
|
||||
fine_field_outputs = self.field_fine(pdf_ray_samples) # FieldOutputs
|
||||
fine_renderer_outputs = self.renderer_rgb(
|
||||
rgb=fine_field_outputs.rgb, density=fine_field_outputs.density, deltas=pdf_ray_samples.deltas
|
||||
) # RendererOutputs
|
||||
# outputs:
|
||||
outputs = {"rgb_coarse": coarse_renderer_outputs.rgb, "rgb_fine": fine_renderer_outputs.rgb}
|
||||
return outputs
|
||||
|
||||
def get_losses(self, batch, graph_outputs):
|
||||
# batch.pixels # (num_rays, 3)
|
||||
losses = {}
|
||||
# rgb_loss_coarse = self.rgb_loss(batch.pixels, graph_outputs["rgb_coarse"])
|
||||
# rgb_loss_fine = self.rgb_loss(batch.pixels, graph_outputs["rgb_fine"])
|
||||
# losses = {"rgb_loss_coarse": rgb_loss_coarse, "rgb_loss_fine": rgb_loss_fine}
|
||||
rgb_loss_coarse = self.rgb_loss(batch.pixels, graph_outputs["rgb_coarse"])
|
||||
rgb_loss_fine = self.rgb_loss(batch.pixels, graph_outputs["rgb_fine"])
|
||||
losses = {"rgb_loss_coarse": rgb_loss_coarse, "rgb_loss_fine": rgb_loss_fine}
|
||||
return losses
|
||||
Reference in new issue
Block a user