mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-01 21:39:06 +08:00
moving to depth nerfacto
This commit is contained in:
1 parent
9c3dcbbf5a
commit
a617dfa07b
6 files changed
+59
-9
No files matched your search
@@ -173,6 +173,7 @@ method_configs["depth-nerfacto"] = TrainerConfig(
|
|||||||
pipeline=VanillaPipelineConfig(
|
pipeline=VanillaPipelineConfig(
|
||||||
datamanager=VanillaDataManagerConfig(
|
datamanager=VanillaDataManagerConfig(
|
||||||
_target=VanillaDataManager[DepthDataset],
|
_target=VanillaDataManager[DepthDataset],
|
||||||
|
patch_size=2,
|
||||||
dataparser=NerfstudioDataParserConfig(),
|
dataparser=NerfstudioDataParserConfig(),
|
||||||
train_num_rays_per_batch=4096,
|
train_num_rays_per_batch=4096,
|
||||||
eval_num_rays_per_batch=4096,
|
eval_num_rays_per_batch=4096,
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
# Copyright 2022 the Regents of the University of California, Nerfstudio Team and contributors. All rights reserved.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
"""Config used for sampling rays."""
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from nerfstudio.configs.base_config import (
|
||||||
|
InstantiateConfig,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class SamplingConfig(InstantiateConfig):
|
||||||
|
"""Full config contents for defining a pixel sampler."""
|
||||||
|
|
||||||
|
patch_size: int = 1
|
||||||
|
"""Patch size for sampling rays."""
|
||||||
|
method_name: Optional[str] = None
|
||||||
|
"""Method name. Required to set in python or via cli"""
|
||||||
@@ -21,8 +21,11 @@ from typing import Dict
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
from nerfstudio.data.dataparsers.base_dataparser import DataparserOutputs
|
from nerfstudio.data.dataparsers.base_dataparser import DataparserOutputs
|
||||||
|
from nerfstudio.model_components import losses
|
||||||
from nerfstudio.data.datasets.base_dataset import InputDataset
|
from nerfstudio.data.datasets.base_dataset import InputDataset
|
||||||
from nerfstudio.data.utils.data_utils import get_depth_image_from_path
|
from nerfstudio.data.utils.data_utils import get_depth_image_from_path
|
||||||
|
from nerfstudio.utils.rich_utils import CONSOLE
|
||||||
|
|
||||||
from typing import Union
|
from typing import Union
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
import torch
|
import torch
|
||||||
@@ -32,7 +35,7 @@ import json
|
|||||||
|
|
||||||
|
|
||||||
class DepthDataset(InputDataset):
|
class DepthDataset(InputDataset):
|
||||||
"""Dataset that returns images and depths.
|
"""Dataset that returns images and depths. If no depths are found, then we generate them with Zoe Depth.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
dataparser_outputs: description of where and how to read input images.
|
dataparser_outputs: description of where and how to read input images.
|
||||||
@@ -47,8 +50,11 @@ class DepthDataset(InputDataset):
|
|||||||
"depth_filenames" not in dataparser_outputs.metadata.keys()
|
"depth_filenames" not in dataparser_outputs.metadata.keys()
|
||||||
or dataparser_outputs.metadata["depth_filenames"] is None
|
or dataparser_outputs.metadata["depth_filenames"] is None
|
||||||
):
|
):
|
||||||
|
losses.DEPTH_METRIC = 0
|
||||||
|
CONSOLE.print("[bold yellow] No depth data found!")
|
||||||
cache = dataparser_outputs.image_filenames[0].parent / "depths.npy"
|
cache = dataparser_outputs.image_filenames[0].parent / "depths.npy"
|
||||||
if cache.exists():
|
if cache.exists():
|
||||||
|
CONSOLE.print("[bold yellow] Loading cache!")
|
||||||
# load all the depths
|
# load all the depths
|
||||||
self.depths = np.load(cache)
|
self.depths = np.load(cache)
|
||||||
self.depths = torch.from_numpy(self.depths).to(device)
|
self.depths = torch.from_numpy(self.depths).to(device)
|
||||||
@@ -86,12 +92,14 @@ class DepthDataset(InputDataset):
|
|||||||
|
|
||||||
self.depths = torch.stack(depth_tensors)
|
self.depths = torch.stack(depth_tensors)
|
||||||
np.save(cache, self.depths.cpu().numpy())
|
np.save(cache, self.depths.cpu().numpy())
|
||||||
|
|
||||||
dataparser_outputs.metadata["depth_filenames"] = None
|
dataparser_outputs.metadata["depth_filenames"] = None
|
||||||
dataparser_outputs.metadata["depth_unit_scale_factor"] = 1.0
|
dataparser_outputs.metadata["depth_unit_scale_factor"] = 1.0
|
||||||
self.metadata["depth_filenames"] = None
|
self.metadata["depth_filenames"] = None
|
||||||
self.metadata["depth_unit_scale_factor"] = 1.0
|
self.metadata["depth_unit_scale_factor"] = 1.0
|
||||||
|
|
||||||
|
else:
|
||||||
|
losses.DEPTH_METRIC = 0
|
||||||
|
|
||||||
self.depth_filenames = self.metadata["depth_filenames"]
|
self.depth_filenames = self.metadata["depth_filenames"]
|
||||||
self.depth_unit_scale_factor = self.metadata["depth_unit_scale_factor"]
|
self.depth_unit_scale_factor = self.metadata["depth_unit_scale_factor"]
|
||||||
|
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ EPS = 1.0e-7
|
|||||||
|
|
||||||
# Sigma scale factor from Urban Radiance Fields (Rematas et al., 2022)
|
# Sigma scale factor from Urban Radiance Fields (Rematas et al., 2022)
|
||||||
URF_SIGMA_SCALE_FACTOR = 3.0
|
URF_SIGMA_SCALE_FACTOR = 3.0
|
||||||
|
DEPTH_METRIC = 1
|
||||||
|
|
||||||
|
|
||||||
class DepthLossType(Enum):
|
class DepthLossType(Enum):
|
||||||
|
|||||||
@@ -22,9 +22,11 @@ from dataclasses import dataclass, field
|
|||||||
from typing import Dict, Tuple, Type
|
from typing import Dict, Tuple, Type
|
||||||
|
|
||||||
import torch
|
import torch
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
from nerfstudio.cameras.rays import RayBundle
|
from nerfstudio.cameras.rays import RayBundle
|
||||||
from nerfstudio.model_components.losses import DepthLossType, depth_loss
|
from nerfstudio.model_components import losses
|
||||||
|
from nerfstudio.model_components.losses import DepthLossType, depth_loss, depth_ranking_loss
|
||||||
from nerfstudio.models.nerfacto import NerfactoModel, NerfactoModelConfig
|
from nerfstudio.models.nerfacto import NerfactoModel, NerfactoModelConfig
|
||||||
from nerfstudio.utils import colormaps
|
from nerfstudio.utils import colormaps
|
||||||
|
|
||||||
@@ -98,7 +100,14 @@ class DepthNerfactoModel(NerfactoModel):
|
|||||||
loss_dict = super().get_loss_dict(outputs, batch, metrics_dict)
|
loss_dict = super().get_loss_dict(outputs, batch, metrics_dict)
|
||||||
if self.training:
|
if self.training:
|
||||||
assert metrics_dict is not None and "depth_loss" in metrics_dict
|
assert metrics_dict is not None and "depth_loss" in metrics_dict
|
||||||
loss_dict["depth_loss"] = self.config.depth_loss_mult * metrics_dict["depth_loss"]
|
# If real depth used
|
||||||
|
if losses.DEPTH_METRIC:
|
||||||
|
loss_dict["depth_loss"] = self.config.depth_loss_mult * metrics_dict["depth_loss"]
|
||||||
|
else:
|
||||||
|
loss_dict["depth_ranking"] = np.interp(self.step, [0, 2000], [0, 0.2]) * depth_ranking_loss(
|
||||||
|
outputs["depth"], batch["depth_image"]
|
||||||
|
)
|
||||||
|
# Else use depth ranking loss
|
||||||
|
|
||||||
return loss_dict
|
return loss_dict
|
||||||
|
|
||||||
|
|||||||
@@ -301,8 +301,8 @@ class NerfactoModel(Model):
|
|||||||
outputs = {
|
outputs = {
|
||||||
"rgb": rgb,
|
"rgb": rgb,
|
||||||
"accumulation": accumulation,
|
"accumulation": accumulation,
|
||||||
"depth": depth,
|
"expected_depth": depth,
|
||||||
"depth_med": depth_med,
|
"depth": depth_med,
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.config.predict_normals:
|
if self.config.predict_normals:
|
||||||
@@ -359,9 +359,8 @@ class NerfactoModel(Model):
|
|||||||
loss_dict["pred_normal_loss"] = self.config.pred_normal_loss_mult * torch.mean(
|
loss_dict["pred_normal_loss"] = self.config.pred_normal_loss_mult * torch.mean(
|
||||||
outputs["rendered_pred_normal_loss"]
|
outputs["rendered_pred_normal_loss"]
|
||||||
)
|
)
|
||||||
|
if "depth_image" in batch:
|
||||||
if "depth_image" in batch and self.step >= 500:
|
loss_dict["depth_ranking"] = np.interp(self.step, [0, 2000], [0, 0.2]) * depth_ranking_loss(
|
||||||
loss_dict["depth_ranking"] = np.interp(self.step, [500, 1000], [0, 0.2]) * depth_ranking_loss(
|
|
||||||
outputs["depth"], batch["depth_image"]
|
outputs["depth"], batch["depth_image"]
|
||||||
)
|
)
|
||||||
return loss_dict
|
return loss_dict
|
||||||
|
|||||||
Reference in new issue
Block a user