mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-26 02:49:57 +08:00
[wip] dynamic batch size for instant ngp (#501)
* starting dynamic batch size branch * dynamic batch pipeline code
This commit is contained in:
1 parent
0c43e7c4b2
commit
7b5a251a30
19 files changed
+351
-221
No files matched your search
@@ -20,14 +20,16 @@ from __future__ import annotations
|
||||
|
||||
from abc import abstractmethod
|
||||
from collections import defaultdict
|
||||
from typing import Any, Dict, List, Optional, Tuple
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Any, Dict, List, Optional, Tuple, Type
|
||||
|
||||
import torch
|
||||
from torch import nn
|
||||
from torch.nn import Parameter
|
||||
|
||||
from nerfactory.cameras.rays import RayBundle
|
||||
from nerfactory.configs import base as cfg
|
||||
from nerfactory.configs.base import InstantiateConfig
|
||||
from nerfactory.configs.utils import to_immutable_dict
|
||||
from nerfactory.datamanagers.structs import SceneBounds
|
||||
from nerfactory.fields.density_fields.density_grid import DensityGrid
|
||||
from nerfactory.models.modules.scene_colliders import NearFarCollider
|
||||
@@ -35,6 +37,35 @@ from nerfactory.utils.callbacks import TrainingCallback, TrainingCallbackAttribu
|
||||
from nerfactory.utils.misc import get_masked_dict
|
||||
|
||||
|
||||
# Model related configs
|
||||
@dataclass
|
||||
class ModelConfig(InstantiateConfig):
|
||||
"""Configuration for model instantiation"""
|
||||
|
||||
_target: Type = field(default_factory=lambda: Model)
|
||||
"""target class to instantiate"""
|
||||
enable_collider: bool = True
|
||||
"""Whether to create a scene collider to filter rays."""
|
||||
collider_params: Optional[Dict[str, float]] = to_immutable_dict({"near_plane": 2.0, "far_plane": 6.0})
|
||||
"""parameters to instantiate scene collider with"""
|
||||
loss_coefficients: Dict[str, float] = to_immutable_dict({"rgb_loss_coarse": 1.0, "rgb_loss_fine": 1.0})
|
||||
"""Loss specific weights."""
|
||||
enable_density_field: bool = False
|
||||
"""Whether to create a density field to filter samples."""
|
||||
density_field_params: Dict[str, Any] = to_immutable_dict(
|
||||
{
|
||||
"center": 0.0, # simply set it as the center of the scene bbox
|
||||
"base_scale": 3.0, # simply set it as the scale of the scene bbox
|
||||
"num_cascades": 1, # if using more than 1 cascade, the `base_scale` can be smaller than scene scale.
|
||||
"resolution": 128,
|
||||
"update_every_num_iters": 16,
|
||||
}
|
||||
)
|
||||
"""parameters to instantiate density field with"""
|
||||
eval_num_rays_per_chunk: int = 4096
|
||||
"""specifies number of rays per chunk during eval"""
|
||||
|
||||
|
||||
class Model(nn.Module):
|
||||
"""Model class
|
||||
Where everything (Fields, Optimizers, Samplers, Visualization, etc) is linked together. This should be
|
||||
@@ -45,11 +76,11 @@ class Model(nn.Module):
|
||||
scene_bounds: dataset scene bounds
|
||||
"""
|
||||
|
||||
config: cfg.ModelConfig
|
||||
config: ModelConfig
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config: cfg.ModelConfig,
|
||||
config: ModelConfig,
|
||||
scene_bounds: SceneBounds,
|
||||
num_train_data: int,
|
||||
**kwargs,
|
||||
@@ -216,3 +247,13 @@ class Model(nn.Module):
|
||||
"""
|
||||
state = {key.replace("module.", ""): value for key, value in loaded_state["model"].items()}
|
||||
self.load_state_dict(state) # type: ignore
|
||||
|
||||
|
||||
@dataclass
|
||||
class VanillaModelConfig(ModelConfig):
|
||||
"""Vanilla Model Config"""
|
||||
|
||||
num_coarse_samples: int = 64
|
||||
"""Number of samples in coarse field evaluation"""
|
||||
num_importance_samples: int = 128
|
||||
"""Number of samples in fine field evaluation"""
|
||||
@@ -39,10 +39,9 @@ from torchmetrics.functional import structural_similarity_index_measure
|
||||
from torchmetrics.image.lpip import LearnedPerceptualImagePatchSimilarity
|
||||
|
||||
from nerfactory.cameras.rays import RayBundle
|
||||
from nerfactory.configs import base as cfg
|
||||
from nerfactory.fields.compound_field import field_implementation_to_class
|
||||
from nerfactory.fields.modules.field_heads import FieldHeadNames
|
||||
from nerfactory.models.base import Model
|
||||
from nerfactory.models.base import Model, ModelConfig
|
||||
from nerfactory.models.modules.ray_sampler import VolumetricSampler
|
||||
from nerfactory.optimizers.loss import MSELoss
|
||||
from nerfactory.renderers.renderers import (
|
||||
@@ -59,7 +58,7 @@ from nerfactory.utils.callbacks import (
|
||||
|
||||
|
||||
@dataclass
|
||||
class CompoundModelConfig(cfg.ModelConfig):
|
||||
class CompoundModelConfig(ModelConfig):
|
||||
"""Compound Model Config"""
|
||||
|
||||
_target: Type = field(
|
||||
|
||||
@@ -29,10 +29,9 @@ from torchmetrics.functional import structural_similarity_index_measure
|
||||
from torchmetrics.image.lpip import LearnedPerceptualImagePatchSimilarity
|
||||
|
||||
from nerfactory.cameras.rays import RayBundle
|
||||
from nerfactory.configs import base as cfg
|
||||
from nerfactory.fields.instant_ngp_field import field_implementation_to_class
|
||||
from nerfactory.fields.modules.field_heads import FieldHeadNames
|
||||
from nerfactory.models.base import Model
|
||||
from nerfactory.models.base import Model, ModelConfig
|
||||
from nerfactory.models.modules.ray_sampler import VolumetricSampler
|
||||
from nerfactory.optimizers.loss import MSELoss
|
||||
from nerfactory.renderers.renderers import (
|
||||
@@ -49,7 +48,7 @@ from nerfactory.utils.callbacks import (
|
||||
|
||||
|
||||
@dataclass
|
||||
class InstantNGPModelConfig(cfg.ModelConfig):
|
||||
class InstantNGPModelConfig(ModelConfig):
|
||||
"""Instant NGP Model Config"""
|
||||
|
||||
_target: Type = field(
|
||||
@@ -62,7 +61,7 @@ class InstantNGPModelConfig(cfg.ModelConfig):
|
||||
"""Instant NGP doesn't use a collider."""
|
||||
field_implementation: Literal["torch", "tcnn"] = "tcnn" # torch, tcnn, ...
|
||||
"""one of "torch" or "tcnn", or other fields in 'field_implementation_to_class'"""
|
||||
num_samples: int = 1024
|
||||
max_num_samples_per_ray: int = 1024
|
||||
"""Number of samples in field evaluation."""
|
||||
grid_resolution: int = 128
|
||||
"""Resolution of the grid used for the field."""
|
||||
@@ -102,7 +101,7 @@ class NGPModel(Model):
|
||||
aabb=self.scene_aabb,
|
||||
density_fn=self.field.density_fn,
|
||||
grid_resolution=self.config.grid_resolution,
|
||||
num_samples=self.config.num_samples,
|
||||
num_samples=self.config.max_num_samples_per_ray,
|
||||
)
|
||||
|
||||
# renderers
|
||||
@@ -174,6 +173,7 @@ class NGPModel(Model):
|
||||
"accumulation": accumulation,
|
||||
"depth": depth,
|
||||
"alive_ray_mask": alive_ray_mask, # the rays we kept from sampler
|
||||
"num_samples_per_ray": packed_info[:, 1],
|
||||
}
|
||||
return outputs
|
||||
|
||||
@@ -181,6 +181,7 @@ class NGPModel(Model):
|
||||
image = batch["image"].to(self.device)
|
||||
metrics_dict = {}
|
||||
metrics_dict["psnr"] = self.psnr(outputs["rgb"], image)
|
||||
metrics_dict["num_samples_per_batch"] = outputs["num_samples_per_ray"].sum()
|
||||
return metrics_dict
|
||||
|
||||
def get_loss_dict(self, outputs, batch, metrics_dict=None):
|
||||
|
||||
@@ -26,11 +26,10 @@ from torchmetrics.functional import structural_similarity_index_measure
|
||||
from torchmetrics.image.lpip import LearnedPerceptualImagePatchSimilarity
|
||||
|
||||
from nerfactory.cameras.rays import RayBundle
|
||||
from nerfactory.configs import base as cfg
|
||||
from nerfactory.fields.modules.encoding import NeRFEncoding
|
||||
from nerfactory.fields.modules.field_heads import FieldHeadNames
|
||||
from nerfactory.fields.nerf_field import NeRFField
|
||||
from nerfactory.models.base import Model
|
||||
from nerfactory.models.base import Model, ModelConfig
|
||||
from nerfactory.models.modules.ray_sampler import PDFSampler, UniformSampler
|
||||
from nerfactory.optimizers.loss import MSELoss
|
||||
from nerfactory.renderers.renderers import (
|
||||
@@ -50,7 +49,7 @@ class MipNerfModel(Model):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config: cfg.ModelConfig,
|
||||
config: ModelConfig,
|
||||
**kwargs,
|
||||
) -> None:
|
||||
self.field = None
|
||||
|
||||
@@ -27,12 +27,11 @@ from torchmetrics.functional import structural_similarity_index_measure
|
||||
from torchmetrics.image.lpip import LearnedPerceptualImagePatchSimilarity
|
||||
|
||||
from nerfactory.cameras.rays import RayBundle
|
||||
from nerfactory.configs import base as cfg
|
||||
from nerfactory.fields.modules.encoding import NeRFEncoding
|
||||
from nerfactory.fields.modules.field_heads import FieldHeadNames
|
||||
from nerfactory.fields.modules.spatial_distortions import SceneContraction
|
||||
from nerfactory.fields.nerf_field import NeRFField
|
||||
from nerfactory.models.base import Model
|
||||
from nerfactory.models.base import Model, ModelConfig
|
||||
from nerfactory.models.modules.ray_losses import distortion_loss
|
||||
from nerfactory.models.modules.ray_sampler import PDFSampler, UniformSampler
|
||||
from nerfactory.optimizers.loss import MSELoss
|
||||
@@ -53,7 +52,7 @@ class MipNerf360Model(Model):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config: cfg.ModelConfig,
|
||||
config: ModelConfig,
|
||||
**kwargs,
|
||||
) -> None:
|
||||
self.field = None
|
||||
|
||||
@@ -18,7 +18,8 @@ NeRF-W (NeRF in the wild) implementation.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Dict, Tuple
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Dict, Tuple, Type
|
||||
|
||||
import torch
|
||||
from torchmetrics import PeakSignalNoiseRatio
|
||||
@@ -26,12 +27,12 @@ from torchmetrics.functional import structural_similarity_index_measure
|
||||
from torchmetrics.image.lpip import LearnedPerceptualImagePatchSimilarity
|
||||
|
||||
from nerfactory.cameras.rays import RayBundle
|
||||
from nerfactory.configs import base as cfg
|
||||
from nerfactory.configs.utils import to_immutable_dict
|
||||
from nerfactory.fields.modules.encoding import NeRFEncoding
|
||||
from nerfactory.fields.modules.field_heads import FieldHeadNames
|
||||
from nerfactory.fields.nerf_field import NeRFField
|
||||
from nerfactory.fields.nerfw_field import VanillaNerfWField
|
||||
from nerfactory.models.base import Model
|
||||
from nerfactory.models.base import Model, VanillaModelConfig
|
||||
from nerfactory.models.modules.ray_sampler import PDFSampler, UniformSampler
|
||||
from nerfactory.models.modules.scene_colliders import AABBBoxCollider
|
||||
from nerfactory.optimizers.loss import MSELoss
|
||||
@@ -44,6 +45,33 @@ from nerfactory.renderers.renderers import (
|
||||
from nerfactory.utils import colors, misc, visualization
|
||||
|
||||
|
||||
@dataclass
|
||||
class NerfWModelConfig(VanillaModelConfig):
|
||||
"""NerfW model config"""
|
||||
|
||||
_target: Type = field(default_factory=lambda: NerfWModel)
|
||||
"""target class to instantiate"""
|
||||
loss_coefficients: Dict[str, float] = to_immutable_dict(
|
||||
{"rgb_loss_coarse": 1.0, "rgb_loss_fine": 1.0, "uncertainty_loss": 1.0, "density_loss": 0.01}
|
||||
)
|
||||
"""Loss specific weights."""
|
||||
num_coarse_samples: int = 64
|
||||
"""Number of samples in coarse field evaluation."""
|
||||
num_importance_samples: int = 64
|
||||
"""Number of samples in fine field evaluation."""
|
||||
uncertainty_min: float = 0.03
|
||||
"""This is added to the end of the uncertainty
|
||||
rendering operation. It's called 'beta_min' in other repos.
|
||||
This avoids calling torch.log() on a zero value, which would be undefined.
|
||||
"""
|
||||
num_images: int = 10000 # TODO: don't hardcode this
|
||||
"""How many images exist in the dataset."""
|
||||
appearance_embedding_dim: int = 48
|
||||
"""Dimension of appearance embedding."""
|
||||
transient_embedding_dim: int = 16
|
||||
"""Dimension of transient embedding."""
|
||||
|
||||
|
||||
class NerfWModel(Model):
|
||||
"""NeRF-W model
|
||||
|
||||
@@ -51,11 +79,11 @@ class NerfWModel(Model):
|
||||
config: NerfW configuration to instantiate model
|
||||
"""
|
||||
|
||||
config: cfg.NerfWModelConfig
|
||||
config: NerfWModelConfig
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config: cfg.NerfWModelConfig,
|
||||
config: NerfWModelConfig,
|
||||
**kwargs,
|
||||
) -> None:
|
||||
"""A NeRF-W model.
|
||||
|
||||
@@ -23,12 +23,12 @@ import torch
|
||||
from torch import nn
|
||||
|
||||
from nerfactory.cameras.rays import RayBundle
|
||||
from nerfactory.configs import base as cfg
|
||||
from nerfactory.datamanagers.structs import Semantics
|
||||
from nerfactory.fields.modules.encoding import NeRFEncoding
|
||||
from nerfactory.fields.modules.field_heads import FieldHeadNames
|
||||
from nerfactory.fields.nerf_field import NeRFField
|
||||
from nerfactory.fields.semantic_nerf_field import SemanticNerfField
|
||||
from nerfactory.models.base import ModelConfig
|
||||
from nerfactory.models.modules.scene_colliders import AABBBoxCollider
|
||||
from nerfactory.models.vanilla_nerf import NeRFModel
|
||||
from nerfactory.renderers.renderers import SemanticRenderer
|
||||
@@ -43,7 +43,7 @@ class SemanticNerfModel(NeRFModel):
|
||||
semantics: additional semantics data info
|
||||
"""
|
||||
|
||||
def __init__(self, config: cfg.ModelConfig, semantics: Semantics, **kwargs) -> None:
|
||||
def __init__(self, config: ModelConfig, semantics: Semantics, **kwargs) -> None:
|
||||
self.stuff_classes = semantics.stuff_classes
|
||||
self.stuff_colors = semantics.stuff_colors
|
||||
super().__init__(config=config, **kwargs)
|
||||
|
||||
@@ -18,7 +18,8 @@ TensorRF implementation.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Dict, List, Tuple
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Dict, List, Tuple, Type
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
@@ -28,11 +29,11 @@ from torchmetrics.functional import structural_similarity_index_measure
|
||||
from torchmetrics.image.lpip import LearnedPerceptualImagePatchSimilarity
|
||||
|
||||
from nerfactory.cameras.rays import RayBundle
|
||||
from nerfactory.configs import base as cfg
|
||||
from nerfactory.configs.utils import to_immutable_dict
|
||||
from nerfactory.fields.modules.encoding import TensorVMEncoding
|
||||
from nerfactory.fields.modules.field_heads import FieldHeadNames
|
||||
from nerfactory.fields.nerf_field import NeRFField
|
||||
from nerfactory.models.base import Model
|
||||
from nerfactory.models.base import Model, VanillaModelConfig
|
||||
from nerfactory.models.modules.ray_sampler import PDFSampler, UniformSampler
|
||||
from nerfactory.optimizers.loss import L1Loss, MSELoss
|
||||
from nerfactory.optimizers.optimizers import Optimizers
|
||||
@@ -49,6 +50,22 @@ from nerfactory.utils.callbacks import (
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class TensoRFModelConfig(VanillaModelConfig):
|
||||
"""TensoRF model config"""
|
||||
|
||||
_target: Type = field(default_factory=lambda: TensoRFModel)
|
||||
"""target class to instantiate"""
|
||||
init_resolution: int = 128
|
||||
"""initial render resolution"""
|
||||
final_resolution: int = 200
|
||||
"""final render resolution"""
|
||||
upsampling_iters: Tuple[int, ...] = (5000, 5500, 7000)
|
||||
"""specifies a list of iteration step numbers to perform upsampling"""
|
||||
loss_coefficients: Dict[str, float] = to_immutable_dict({"rgb_loss_coarse": 1.0, "feature_loss": 8e-5})
|
||||
"""Loss specific weights."""
|
||||
|
||||
|
||||
class TensoRFModel(Model):
|
||||
"""TensoRF Model
|
||||
|
||||
@@ -58,7 +75,7 @@ class TensoRFModel(Model):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config: cfg.TensoRFModelConfig,
|
||||
config: TensoRFModelConfig,
|
||||
**kwargs,
|
||||
) -> None:
|
||||
self.init_resolution = config.init_resolution
|
||||
|
||||
@@ -27,11 +27,10 @@ from torchmetrics.functional import structural_similarity_index_measure
|
||||
from torchmetrics.image.lpip import LearnedPerceptualImagePatchSimilarity
|
||||
|
||||
from nerfactory.cameras.rays import RayBundle
|
||||
from nerfactory.configs import base as cfg
|
||||
from nerfactory.fields.modules.encoding import NeRFEncoding
|
||||
from nerfactory.fields.modules.field_heads import FieldHeadNames
|
||||
from nerfactory.fields.nerf_field import NeRFField
|
||||
from nerfactory.models.base import Model
|
||||
from nerfactory.models.base import Model, ModelConfig
|
||||
from nerfactory.models.modules.ray_sampler import PDFSampler, UniformSampler
|
||||
from nerfactory.optimizers.loss import MSELoss
|
||||
from nerfactory.renderers.renderers import (
|
||||
@@ -56,7 +55,7 @@ class NeRFModel(Model):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config: cfg.ModelConfig,
|
||||
config: ModelConfig,
|
||||
**kwargs,
|
||||
) -> None:
|
||||
self.field_coarse = None
|
||||
|
||||
Reference in new issue
Block a user