mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-01 21:39:06 +08:00
reducing proposal network / hashgrid sizes, merging with main
This commit is contained in:
1 parent
785a935668
commit
a8262ce46a
8 files changed
+79
-132
No files matched your search
@@ -27,7 +27,6 @@ from nerfstudio.cameras.camera_optimizers import CameraOptimizerConfig
|
|||||||
from nerfstudio.configs.base_config import ViewerConfig
|
from nerfstudio.configs.base_config import ViewerConfig
|
||||||
from nerfstudio.data.datamanagers.base_datamanager import VanillaDataManagerConfig
|
from nerfstudio.data.datamanagers.base_datamanager import VanillaDataManagerConfig
|
||||||
from nerfstudio.data.datamanagers.depth_datamanager import DepthDataManagerConfig
|
from nerfstudio.data.datamanagers.depth_datamanager import DepthDataManagerConfig
|
||||||
from nerfstudio.data.datamanagers.sdf_datamanager import SDFDataManagerConfig
|
|
||||||
from nerfstudio.data.datamanagers.dreamfusion_datamanager import (
|
from nerfstudio.data.datamanagers.dreamfusion_datamanager import (
|
||||||
DreamFusionDataManagerConfig,
|
DreamFusionDataManagerConfig,
|
||||||
)
|
)
|
||||||
@@ -448,24 +447,22 @@ method_configs["dreamembed"] = TrainerConfig(
|
|||||||
pipeline=VanillaPipelineConfig(
|
pipeline=VanillaPipelineConfig(
|
||||||
generative=True,
|
generative=True,
|
||||||
datamanager=DreamFusionDataManagerConfig(
|
datamanager=DreamFusionDataManagerConfig(
|
||||||
horizontal_rotation_warmup=4000,
|
horizontal_rotation_warmup=0,
|
||||||
),
|
),
|
||||||
model=DreamEmbeddingModelConfig(
|
model=DreamEmbeddingModelConfig(
|
||||||
eval_num_rays_per_chunk=1 << 15,
|
eval_num_rays_per_chunk=1 << 15,
|
||||||
distortion_loss_mult=0.1,
|
distortion_loss_mult=0.1,
|
||||||
interlevel_loss_mult=100.0,
|
interlevel_loss_mult=100.0,
|
||||||
orientation_loss_mult=1.,
|
orientation_loss_mult=1.0,
|
||||||
max_res=256,
|
max_res=128,
|
||||||
sphere_collider=True,
|
sphere_collider=True,
|
||||||
initialize_density=False,
|
initialize_density=False,
|
||||||
random_background=True,
|
|
||||||
proposal_warmup=500,
|
proposal_warmup=500,
|
||||||
proposal_update_every=0,
|
proposal_update_every=0,
|
||||||
proposal_weights_anneal_max_num_iters=100,
|
proposal_weights_anneal_max_num_iters=100,
|
||||||
start_normals_training=1000,
|
|
||||||
opacity_loss_mult=1,
|
opacity_loss_mult=1,
|
||||||
positional_prompting="discrete",
|
positional_prompting="discrete",
|
||||||
guidance_scale=100,
|
guidance_scale=50,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
optimizers={
|
optimizers={
|
||||||
@@ -476,7 +473,7 @@ method_configs["dreamembed"] = TrainerConfig(
|
|||||||
"fields": {
|
"fields": {
|
||||||
"optimizer": AdamOptimizerConfig(lr=1e-3, eps=1e-15),
|
"optimizer": AdamOptimizerConfig(lr=1e-3, eps=1e-15),
|
||||||
"scheduler": ExponentialDecaySchedulerConfig(
|
"scheduler": ExponentialDecaySchedulerConfig(
|
||||||
warmup_steps=2000, lr_final=1e-6, max_steps=20000, ramp="linear"
|
warmup_steps=1000, lr_final=1e-6, max_steps=20000, ramp="linear"
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -55,9 +55,7 @@ class TrivialDataset(InputDataset):
|
|||||||
|
|
||||||
def __getitem__(self, index: int) -> Dict:
|
def __getitem__(self, index: int) -> Dict:
|
||||||
return {
|
return {
|
||||||
"image": torch.cat(
|
"image": torch.cat([torch.ones(128, 256, 3), torch.zeros(128, 256, 3)], dim=0),
|
||||||
[torch.ones(128, 256, 3), torch.zeros(128, 256, 3)], dim=0
|
|
||||||
),
|
|
||||||
"image_idx": index,
|
"image_idx": index,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,13 +93,11 @@ def random_train_pose(
|
|||||||
)
|
)
|
||||||
# This is the uniform sample on the part of the sphere we care about where 0 = 0 degrees and 1 = 360 degrees
|
# This is the uniform sample on the part of the sphere we care about where 0 = 0 degrees and 1 = 360 degrees
|
||||||
sampled_uniform = (
|
sampled_uniform = (
|
||||||
torch.rand(size) * (vertical_rotation_range[1] - vertical_rotation_range[0])
|
torch.rand(size) * (vertical_rotation_range[1] - vertical_rotation_range[0]) + vertical_rotation_range[0]
|
||||||
+ vertical_rotation_range[0]
|
|
||||||
) / 180
|
) / 180
|
||||||
vertical_rotation = torch.arccos(1 - 2 * sampled_uniform)
|
vertical_rotation = torch.arccos(1 - 2 * sampled_uniform)
|
||||||
central_rotation = torch.deg2rad(
|
central_rotation = torch.deg2rad(
|
||||||
torch.rand(size) * (central_rotation_range[1] - central_rotation_range[0])
|
torch.rand(size) * (central_rotation_range[1] - central_rotation_range[0]) + central_rotation_range[0]
|
||||||
+ central_rotation_range[0]
|
|
||||||
)
|
)
|
||||||
|
|
||||||
c_cos = torch.cos(central_rotation)
|
c_cos = torch.cos(central_rotation)
|
||||||
@@ -131,9 +127,7 @@ def random_train_pose(
|
|||||||
|
|
||||||
# Default directions are facing in the -z direction, so origins should face opposite way
|
# Default directions are facing in the -z direction, so origins should face opposite way
|
||||||
origins = torch.stack([torch.tensor([0, 0, 1])] * size, dim=0)
|
origins = torch.stack([torch.tensor([0, 0, 1])] * size, dim=0)
|
||||||
origins = (origins * radius_mean) + (
|
origins = (origins * radius_mean) + (origins * (torch.randn((origins.shape)) * radius_std))
|
||||||
origins * (torch.randn((origins.shape)) * radius_std)
|
|
||||||
)
|
|
||||||
R = torch.bmm(rot_z, rot_x) # Want to have Rx @ Ry @ origin
|
R = torch.bmm(rot_z, rot_x) # Want to have Rx @ Ry @ origin
|
||||||
t = (
|
t = (
|
||||||
torch.bmm(R, origins.unsqueeze(-1))
|
torch.bmm(R, origins.unsqueeze(-1))
|
||||||
@@ -220,9 +214,7 @@ class DreamFusionDataManager(DataManager): # pylint: disable=abstract-method
|
|||||||
self.test_split = "test" if test_mode in ["test", "inference"] else "val"
|
self.test_split = "test" if test_mode in ["test", "inference"] else "val"
|
||||||
|
|
||||||
if self.config.data is not None:
|
if self.config.data is not None:
|
||||||
CONSOLE.print(
|
CONSOLE.print("[red] --data should not be used with the DreamFusionDataManager[/red]")
|
||||||
"[red] --data should not be used with the DreamFusionDataManager[/red]"
|
|
||||||
)
|
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
cameras, _, _ = random_train_pose(
|
cameras, _, _ = random_train_pose(
|
||||||
@@ -266,7 +258,7 @@ class DreamFusionDataManager(DataManager): # pylint: disable=abstract-method
|
|||||||
# ).flatten()
|
# ).flatten()
|
||||||
# return ray_bundle, {"initialization": False}
|
# return ray_bundle, {"initialization": False}
|
||||||
|
|
||||||
horizontal_range = min((step / self.config.horizontal_rotation_warmup), 1) * 180
|
horizontal_range = min((step / max(1, self.config.horizontal_rotation_warmup)), 1) * 180
|
||||||
|
|
||||||
cameras, vertical_rotation, central_rotation = random_train_pose(
|
cameras, vertical_rotation, central_rotation = random_train_pose(
|
||||||
self.config.train_images_per_batch,
|
self.config.train_images_per_batch,
|
||||||
@@ -280,9 +272,7 @@ class DreamFusionDataManager(DataManager): # pylint: disable=abstract-method
|
|||||||
center=self.config.center,
|
center=self.config.center,
|
||||||
central_rotation_range=(-horizontal_range, horizontal_range),
|
central_rotation_range=(-horizontal_range, horizontal_range),
|
||||||
)
|
)
|
||||||
ray_bundle = cameras.generate_rays(
|
ray_bundle = cameras.generate_rays(torch.tensor(list(range(self.config.train_images_per_batch)))).flatten()
|
||||||
torch.tensor(list(range(self.config.train_images_per_batch)))
|
|
||||||
).flatten()
|
|
||||||
|
|
||||||
# camera_idx = torch.randint(0, self.eval_cameras.shape[0], [1], dtype=torch.long, device=self.device)
|
# camera_idx = torch.randint(0, self.eval_cameras.shape[0], [1], dtype=torch.long, device=self.device)
|
||||||
# ray_bundle = self.eval_cameras.generate_rays(camera_idx).flatten()
|
# ray_bundle = self.eval_cameras.generate_rays(camera_idx).flatten()
|
||||||
|
|||||||
@@ -42,9 +42,6 @@ class FieldHeadNames(Enum):
|
|||||||
SDF = "sdf"
|
SDF = "sdf"
|
||||||
ALPHA = "alpha"
|
ALPHA = "alpha"
|
||||||
GRADIENT = "gradient"
|
GRADIENT = "gradient"
|
||||||
SDF = "sdf"
|
|
||||||
ALPHA = "alpha"
|
|
||||||
GRADIENT = "gradient"
|
|
||||||
|
|
||||||
|
|
||||||
class FieldHead(FieldComponent):
|
class FieldHead(FieldComponent):
|
||||||
|
|||||||
@@ -1,3 +1,17 @@
|
|||||||
|
# Copyright 2022 The Nerfstudio Team. 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.
|
||||||
|
|
||||||
|
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
@@ -18,6 +32,7 @@ except ImportError:
|
|||||||
# tinycudann module doesn't exist
|
# tinycudann module doesn't exist
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def get_normalized_directions(directions: TensorType["bs":..., 3]):
|
def get_normalized_directions(directions: TensorType["bs":..., 3]):
|
||||||
"""SH encoding must be in the range [0, 1]
|
"""SH encoding must be in the range [0, 1]
|
||||||
|
|
||||||
@@ -26,6 +41,7 @@ def get_normalized_directions(directions: TensorType["bs":..., 3]):
|
|||||||
"""
|
"""
|
||||||
return (directions + 1.0) / 2.0
|
return (directions + 1.0) / 2.0
|
||||||
|
|
||||||
|
|
||||||
class DreamEmbeddingField(Field):
|
class DreamEmbeddingField(Field):
|
||||||
"""DreamEmbedding Field that uses TCNN
|
"""DreamEmbedding Field that uses TCNN
|
||||||
|
|
||||||
@@ -132,7 +148,7 @@ class DreamEmbeddingField(Field):
|
|||||||
# from smaller internal (float16) parameters.
|
# from smaller internal (float16) parameters.
|
||||||
density = trunc_exp(density_before_activation.to(positions))
|
density = trunc_exp(density_before_activation.to(positions))
|
||||||
return density, base_mlp_out
|
return density, base_mlp_out
|
||||||
|
|
||||||
def get_background_feature(self, ray_bundle: RayBundle):
|
def get_background_feature(self, ray_bundle: RayBundle):
|
||||||
"""Predicts background colors at infinity."""
|
"""Predicts background colors at infinity."""
|
||||||
directions = get_normalized_directions(ray_bundle.directions)
|
directions = get_normalized_directions(ray_bundle.directions)
|
||||||
@@ -142,7 +158,7 @@ class DreamEmbeddingField(Field):
|
|||||||
background_feature = self.mlp_background_feature(directions_flat).view(*outputs_shape, -1).to(directions)
|
background_feature = self.mlp_background_feature(directions_flat).view(*outputs_shape, -1).to(directions)
|
||||||
|
|
||||||
return background_feature
|
return background_feature
|
||||||
|
|
||||||
def get_outputs(self, ray_samples: RaySamples, density_embedding: Optional[TensorType] = None):
|
def get_outputs(self, ray_samples: RaySamples, density_embedding: Optional[TensorType] = None):
|
||||||
assert density_embedding is not None
|
assert density_embedding is not None
|
||||||
outputs = {}
|
outputs = {}
|
||||||
@@ -156,4 +172,4 @@ class DreamEmbeddingField(Field):
|
|||||||
feature = self.mlp_head(h).view(*outputs_shape, -1).to(directions)
|
feature = self.mlp_head(h).view(*outputs_shape, -1).to(directions)
|
||||||
outputs.update({FieldHeadNames.FEATURE: feature})
|
outputs.update({FieldHeadNames.FEATURE: feature})
|
||||||
|
|
||||||
return outputs
|
return outputs
|
||||||
@@ -16,9 +16,9 @@
|
|||||||
Stable diffusion utils
|
Stable diffusion utils
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import torch
|
||||||
from torchtyping import TensorType
|
from torchtyping import TensorType
|
||||||
from typing_extensions import Literal
|
from typing_extensions import Literal
|
||||||
import torch
|
|
||||||
|
|
||||||
from nerfstudio.generative.stable_diffusion import StableDiffusion
|
from nerfstudio.generative.stable_diffusion import StableDiffusion
|
||||||
|
|
||||||
|
|||||||
@@ -342,15 +342,16 @@ class NormalsRenderer(nn.Module):
|
|||||||
if normalize:
|
if normalize:
|
||||||
n = safe_normalize(n)
|
n = safe_normalize(n)
|
||||||
return n
|
return n
|
||||||
|
|
||||||
|
|
||||||
class FeatureRenderer(nn.Module):
|
class FeatureRenderer(nn.Module):
|
||||||
"""Calculate features along the ray."""
|
"""Calculate features along the ray."""
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def forward(
|
def forward(
|
||||||
cls,
|
cls,
|
||||||
features: TensorType["bs":..., "num_samples", 4],
|
features: TensorType["bs":..., "num_samples", 4],
|
||||||
weights: TensorType["bs":..., "num_samples", 1],
|
weights: TensorType["bs":..., "num_samples", 1],
|
||||||
) -> TensorType["bs":..., 4]:
|
) -> TensorType["bs":..., 4]:
|
||||||
feat = torch.sum(weights * features, dim=-2)
|
feat = torch.sum(weights * features, dim=-2)
|
||||||
return feat
|
return feat
|
||||||
@@ -44,13 +44,9 @@ class ModelConfig(InstantiateConfig):
|
|||||||
"""target class to instantiate"""
|
"""target class to instantiate"""
|
||||||
enable_collider: bool = True
|
enable_collider: bool = True
|
||||||
"""Whether to create a scene collider to filter rays."""
|
"""Whether to create a scene collider to filter rays."""
|
||||||
collider_params: Optional[Dict[str, float]] = to_immutable_dict(
|
collider_params: Optional[Dict[str, float]] = to_immutable_dict({"near_plane": 2.0, "far_plane": 6.0})
|
||||||
{"near_plane": 2.0, "far_plane": 6.0}
|
|
||||||
)
|
|
||||||
"""parameters to instantiate scene collider with"""
|
"""parameters to instantiate scene collider with"""
|
||||||
loss_coefficients: Dict[str, float] = to_immutable_dict(
|
loss_coefficients: Dict[str, float] = to_immutable_dict({"rgb_loss_coarse": 1.0, "rgb_loss_fine": 1.0})
|
||||||
{"rgb_loss_coarse": 1.0, "rgb_loss_fine": 1.0}
|
|
||||||
)
|
|
||||||
"""parameters to instantiate density field with"""
|
"""parameters to instantiate density field with"""
|
||||||
eval_num_rays_per_chunk: int = 4096
|
eval_num_rays_per_chunk: int = 4096
|
||||||
"""specifies number of rays per chunk during eval"""
|
"""specifies number of rays per chunk during eval"""
|
||||||
@@ -80,9 +76,7 @@ class Model(nn.Module):
|
|||||||
super().__init__()
|
super().__init__()
|
||||||
self.config = config
|
self.config = config
|
||||||
self.scene_box = scene_box
|
self.scene_box = scene_box
|
||||||
self.render_aabb = (
|
self.render_aabb = None # the box that we want to render - should be a subset of scene_box
|
||||||
None # the box that we want to render - should be a subset of scene_box
|
|
||||||
)
|
|
||||||
self.num_train_data = num_train_data
|
self.num_train_data = num_train_data
|
||||||
self.kwargs = kwargs
|
self.kwargs = kwargs
|
||||||
self.collider = None
|
self.collider = None
|
||||||
@@ -160,9 +154,7 @@ class Model(nn.Module):
|
|||||||
return {}
|
return {}
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def get_loss_dict(
|
def get_loss_dict(self, outputs, batch, metrics_dict=None) -> Dict[str, torch.Tensor]:
|
||||||
self, outputs, batch, metrics_dict=None
|
|
||||||
) -> Dict[str, torch.Tensor]:
|
|
||||||
"""Computes and returns the losses dict.
|
"""Computes and returns the losses dict.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -172,9 +164,7 @@ class Model(nn.Module):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
@torch.no_grad()
|
@torch.no_grad()
|
||||||
def get_outputs_for_camera_ray_bundle(
|
def get_outputs_for_camera_ray_bundle(self, camera_ray_bundle: RayBundle) -> Dict[str, torch.Tensor]:
|
||||||
self, camera_ray_bundle: RayBundle
|
|
||||||
) -> Dict[str, torch.Tensor]:
|
|
||||||
"""Takes in camera parameters and computes the output of the model.
|
"""Takes in camera parameters and computes the output of the model.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -187,9 +177,7 @@ class Model(nn.Module):
|
|||||||
for i in range(0, num_rays, num_rays_per_chunk):
|
for i in range(0, num_rays, num_rays_per_chunk):
|
||||||
start_idx = i
|
start_idx = i
|
||||||
end_idx = i + num_rays_per_chunk
|
end_idx = i + num_rays_per_chunk
|
||||||
ray_bundle = camera_ray_bundle.get_row_major_sliced_ray_bundle(
|
ray_bundle = camera_ray_bundle.get_row_major_sliced_ray_bundle(start_idx, end_idx)
|
||||||
start_idx, end_idx
|
|
||||||
)
|
|
||||||
outputs = self.forward(ray_bundle=ray_bundle)
|
outputs = self.forward(ray_bundle=ray_bundle)
|
||||||
for output_name, output in outputs.items(): # type: ignore
|
for output_name, output in outputs.items(): # type: ignore
|
||||||
outputs_lists[output_name].append(output)
|
outputs_lists[output_name].append(output)
|
||||||
@@ -224,10 +212,7 @@ class Model(nn.Module):
|
|||||||
Args:
|
Args:
|
||||||
loaded_state: dictionary of pre-trained model states
|
loaded_state: dictionary of pre-trained model states
|
||||||
"""
|
"""
|
||||||
state = {
|
state = {key.replace("module.", ""): value for key, value in loaded_state["model"].items()}
|
||||||
key.replace("module.", ""): value
|
|
||||||
for key, value in loaded_state["model"].items()
|
|
||||||
}
|
|
||||||
self.load_state_dict(state) # type: ignore
|
self.load_state_dict(state) # type: ignore
|
||||||
|
|
||||||
def update_to_step(self, step: int) -> None:
|
def update_to_step(self, step: int) -> None:
|
||||||
|
|||||||
@@ -1,14 +1,26 @@
|
|||||||
|
# Copyright 2022 The Nerfstudio Team. 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.
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from typing import Dict, List, Optional, Tuple, Type
|
from typing import Dict, List, Optional, Tuple, Type
|
||||||
|
|
||||||
|
|
||||||
import torch
|
|
||||||
import imageio
|
import imageio
|
||||||
|
import torch
|
||||||
import torch.nn.functional as F
|
import torch.nn.functional as F
|
||||||
|
|
||||||
from typing_extensions import Literal
|
from typing_extensions import Literal
|
||||||
|
|
||||||
from nerfstudio.cameras.rays import RayBundle
|
from nerfstudio.cameras.rays import RayBundle
|
||||||
@@ -28,7 +40,6 @@ from nerfstudio.model_components.losses import (
|
|||||||
interlevel_loss,
|
interlevel_loss,
|
||||||
orientation_loss,
|
orientation_loss,
|
||||||
)
|
)
|
||||||
|
|
||||||
from nerfstudio.model_components.renderers import FeatureRenderer
|
from nerfstudio.model_components.renderers import FeatureRenderer
|
||||||
from nerfstudio.models.dreamfusion import DreamFusionModel, DreamFusionModelConfig
|
from nerfstudio.models.dreamfusion import DreamFusionModel, DreamFusionModelConfig
|
||||||
from nerfstudio.utils import colormaps, colors, math, misc
|
from nerfstudio.utils import colormaps, colors, math, misc
|
||||||
@@ -57,8 +68,6 @@ class DreamEmbeddingModelConfig(DreamFusionModelConfig):
|
|||||||
"""Strength schedule of center density"""
|
"""Strength schedule of center density"""
|
||||||
sphere_collider: bool = True
|
sphere_collider: bool = True
|
||||||
"""Use spherical collider instead of box"""
|
"""Use spherical collider instead of box"""
|
||||||
random_background: bool = True
|
|
||||||
"""Randomly choose between using background mlp and random color for background"""
|
|
||||||
target_transmittance_start: float = 0.4
|
target_transmittance_start: float = 0.4
|
||||||
"""target transmittance for opacity penalty. This is the percent of the scene that is
|
"""target transmittance for opacity penalty. This is the percent of the scene that is
|
||||||
background when rendered at the start of training"""
|
background when rendered at the start of training"""
|
||||||
@@ -67,7 +76,7 @@ class DreamEmbeddingModelConfig(DreamFusionModelConfig):
|
|||||||
background when rendered at the end of training"""
|
background when rendered at the end of training"""
|
||||||
transmittance_end_schedule: int = 1500
|
transmittance_end_schedule: int = 1500
|
||||||
"""number of iterations to reach target_transmittance_end"""
|
"""number of iterations to reach target_transmittance_end"""
|
||||||
num_proposal_samples_per_ray: Tuple[int, ...] = (256, 96)
|
num_proposal_samples_per_ray: Tuple[int, ...] = (96, 48)
|
||||||
"""Number of samples per ray for each proposal network."""
|
"""Number of samples per ray for each proposal network."""
|
||||||
num_nerf_samples_per_ray: int = 48
|
num_nerf_samples_per_ray: int = 48
|
||||||
"""Number of samples per ray for the nerf network."""
|
"""Number of samples per ray for the nerf network."""
|
||||||
@@ -85,13 +94,13 @@ class DreamEmbeddingModelConfig(DreamFusionModelConfig):
|
|||||||
"hidden_dim": 16,
|
"hidden_dim": 16,
|
||||||
"log2_hashmap_size": 17,
|
"log2_hashmap_size": 17,
|
||||||
"num_levels": 5,
|
"num_levels": 5,
|
||||||
"max_res": 128,
|
"max_res": 64,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"hidden_dim": 16,
|
"hidden_dim": 16,
|
||||||
"log2_hashmap_size": 17,
|
"log2_hashmap_size": 17,
|
||||||
"num_levels": 5,
|
"num_levels": 5,
|
||||||
"max_res": 256,
|
"max_res": 128,
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
@@ -106,8 +115,6 @@ class DreamEmbeddingModelConfig(DreamFusionModelConfig):
|
|||||||
"""Proposal loss multiplier."""
|
"""Proposal loss multiplier."""
|
||||||
distortion_loss_mult: float = 1.0
|
distortion_loss_mult: float = 1.0
|
||||||
"""Distortion loss multiplier."""
|
"""Distortion loss multiplier."""
|
||||||
start_normals_training: int = 1000
|
|
||||||
"""Start training normals after this many iterations"""
|
|
||||||
start_lambertian_training: int = 1000
|
start_lambertian_training: int = 1000
|
||||||
"""start training with lambertian shading after this many iterations"""
|
"""start training with lambertian shading after this many iterations"""
|
||||||
opacity_penalty: bool = True
|
opacity_penalty: bool = True
|
||||||
@@ -159,34 +166,19 @@ class DreamEmbeddingModel(DreamFusionModel):
|
|||||||
super().populate_modules()
|
super().populate_modules()
|
||||||
|
|
||||||
# setting up fields
|
# setting up fields
|
||||||
self.field = DreamEmbeddingField(
|
self.field = DreamEmbeddingField(self.scene_box.aabb, max_res=self.config.max_res)
|
||||||
self.scene_box.aabb, max_res=self.config.max_res
|
|
||||||
)
|
|
||||||
image = imageio.imread('/home/terrance/nerfstudio/data/generative/redfrogcrop.png')
|
|
||||||
image = (torch.from_numpy(image).to(self.sd_device).half() / 255.0).unsqueeze(0).permute(0, 3, 1, 2)
|
|
||||||
self.input_image = F.interpolate(image, (512, 512), mode="bilinear")
|
|
||||||
|
|
||||||
input_latent = self.sd.imgs_to_latent(self.input_image)
|
|
||||||
self.input_latent = input_latent.permute(0, 2, 3, 1).flatten(end_dim=-2)
|
|
||||||
|
|
||||||
self.renderer_feature = FeatureRenderer()
|
self.renderer_feature = FeatureRenderer()
|
||||||
self.l2_loss = MSELoss()
|
|
||||||
|
|
||||||
def get_outputs(self, ray_bundle: RayBundle, ray_dims: Tuple):
|
def get_outputs(self, ray_bundle: RayBundle, ray_dims: Tuple):
|
||||||
# uniform sampling
|
# uniform sampling
|
||||||
background_feature = self.field.get_background_feature(ray_bundle)
|
background_feature = self.field.get_background_feature(ray_bundle)
|
||||||
ray_samples, weights_list, ray_samples_list = self.proposal_sampler(
|
ray_samples, weights_list, ray_samples_list = self.proposal_sampler(ray_bundle, density_fns=self.density_fns)
|
||||||
ray_bundle, density_fns=self.density_fns
|
|
||||||
)
|
|
||||||
field_outputs = self.field(ray_samples, compute_normals=True)
|
field_outputs = self.field(ray_samples, compute_normals=True)
|
||||||
density = field_outputs[FieldHeadNames.DENSITY]
|
density = field_outputs[FieldHeadNames.DENSITY]
|
||||||
|
|
||||||
if self.initialize_density:
|
if self.initialize_density:
|
||||||
pos = ray_samples.frustums.get_positions()
|
pos = ray_samples.frustums.get_positions()
|
||||||
density_blob = (
|
density_blob = self.density_strength * torch.exp(-torch.norm(pos, dim=-1) / (2 * 0.04))[..., None]
|
||||||
self.density_strength
|
|
||||||
* torch.exp(-torch.norm(pos, dim=-1) / (2 * 0.04))[..., None]
|
|
||||||
)
|
|
||||||
density = density + density_blob
|
density = density + density_blob
|
||||||
|
|
||||||
weights = ray_samples.get_weights(density)
|
weights = ray_samples.get_weights(density)
|
||||||
@@ -195,13 +187,9 @@ class DreamEmbeddingModel(DreamFusionModel):
|
|||||||
|
|
||||||
accumulation = self.renderer_accumulation(weights)
|
accumulation = self.renderer_accumulation(weights)
|
||||||
depth = self.renderer_depth(weights, ray_samples)
|
depth = self.renderer_depth(weights, ray_samples)
|
||||||
features = self.renderer_feature(
|
features = self.renderer_feature(features=field_outputs[FieldHeadNames.FEATURE], weights=weights)
|
||||||
features=field_outputs[FieldHeadNames.FEATURE], weights=weights
|
|
||||||
)
|
|
||||||
|
|
||||||
accum_mask = torch.clamp(
|
accum_mask = torch.clamp((torch.nan_to_num(accumulation, nan=0.0)), min=0.0, max=1.0)
|
||||||
(torch.nan_to_num(accumulation, nan=0.0)), min=0.0, max=1.0
|
|
||||||
)
|
|
||||||
accum_mask_inv = 1.0 - accum_mask
|
accum_mask_inv = 1.0 - accum_mask
|
||||||
|
|
||||||
background = accum_mask_inv * background_feature
|
background = accum_mask_inv * background_feature
|
||||||
@@ -236,9 +224,7 @@ class DreamEmbeddingModel(DreamFusionModel):
|
|||||||
|
|
||||||
a_mask = accum_mask.reshape(1, 1, height, width)
|
a_mask = accum_mask.reshape(1, 1, height, width)
|
||||||
|
|
||||||
a_mask = torch.nn.functional.interpolate(
|
a_mask = torch.nn.functional.interpolate(a_mask, size=(height * 8, width * 8), mode="bilinear")
|
||||||
a_mask, size=(height * 8, width * 8), mode="bilinear"
|
|
||||||
)
|
|
||||||
|
|
||||||
a_mask = torch.flatten(a_mask.permute(0, 2, 3, 1), end_dim=-2)
|
a_mask = torch.flatten(a_mask.permute(0, 2, 3, 1), end_dim=-2)
|
||||||
a_mask_inv = 1 - a_mask
|
a_mask_inv = 1 - a_mask
|
||||||
@@ -258,13 +244,9 @@ class DreamEmbeddingModel(DreamFusionModel):
|
|||||||
outputs["ray_samples_list"] = ray_samples_list
|
outputs["ray_samples_list"] = ray_samples_list
|
||||||
|
|
||||||
for i in range(self.config.num_proposal_iterations):
|
for i in range(self.config.num_proposal_iterations):
|
||||||
outputs[f"prop_depth_{i}"] = self.renderer_depth(
|
outputs[f"prop_depth_{i}"] = self.renderer_depth(weights=weights_list[i], ray_samples=ray_samples_list[i])
|
||||||
weights=weights_list[i], ray_samples=ray_samples_list[i]
|
|
||||||
)
|
|
||||||
|
|
||||||
normals = self.renderer_normals(
|
normals = self.renderer_normals(normals=field_outputs[FieldHeadNames.NORMALS], weights=weights)
|
||||||
normals=field_outputs[FieldHeadNames.NORMALS], weights=weights
|
|
||||||
)
|
|
||||||
outputs["normals"] = self.shader_normals(normals, weights=accum_mask)
|
outputs["normals"] = self.shader_normals(normals, weights=accum_mask)
|
||||||
|
|
||||||
outputs["rendered_orientation_loss"] = orientation_loss(
|
outputs["rendered_orientation_loss"] = orientation_loss(
|
||||||
@@ -275,34 +257,25 @@ class DreamEmbeddingModel(DreamFusionModel):
|
|||||||
|
|
||||||
assert weights.shape[-1] == 1
|
assert weights.shape[-1] == 1
|
||||||
if self.config.opacity_penalty:
|
if self.config.opacity_penalty:
|
||||||
outputs["opacity_loss"] = (
|
outputs["opacity_loss"] = torch.sqrt(torch.sum(weights, dim=-2) ** 2 + 0.01) * self.config.opacity_loss_mult
|
||||||
torch.sqrt(torch.sum(weights, dim=-2) ** 2 + 0.01)
|
|
||||||
* self.config.opacity_loss_mult
|
|
||||||
)
|
|
||||||
|
|
||||||
return outputs
|
return outputs
|
||||||
|
|
||||||
def get_loss_dict(
|
def get_loss_dict(self, outputs, batch, metrics_dict=None) -> Dict[str, torch.Tensor]:
|
||||||
self, outputs, batch, metrics_dict=None
|
|
||||||
) -> Dict[str, torch.Tensor]:
|
|
||||||
# Scaling metrics by coefficients to create the losses.
|
# Scaling metrics by coefficients to create the losses.
|
||||||
|
|
||||||
loss_dict = {}
|
loss_dict = {}
|
||||||
loss_dict = misc.scale_dict(loss_dict, self.config.loss_coefficients)
|
loss_dict = misc.scale_dict(loss_dict, self.config.loss_coefficients)
|
||||||
if self.train_normals:
|
if self.train_normals:
|
||||||
# orientation loss for computed normals
|
# orientation loss for computed normals
|
||||||
loss_dict[
|
loss_dict["orientation_loss"] = self.config.orientation_loss_mult * torch.mean(
|
||||||
"orientation_loss"
|
|
||||||
] = self.config.orientation_loss_mult * torch.mean(
|
|
||||||
outputs["rendered_orientation_loss"]
|
outputs["rendered_orientation_loss"]
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
loss_dict["orientation_loss"] = 0
|
loss_dict["orientation_loss"] = 0
|
||||||
|
|
||||||
if self.config.opacity_penalty:
|
if self.config.opacity_penalty:
|
||||||
loss_dict["opacity_loss"] = (
|
loss_dict["opacity_loss"] = self.config.opacity_loss_mult * outputs["opacity_loss"].mean()
|
||||||
self.config.opacity_loss_mult * outputs["opacity_loss"].mean()
|
|
||||||
)
|
|
||||||
|
|
||||||
if self.prompt != self.cur_prompt:
|
if self.prompt != self.cur_prompt:
|
||||||
self.cur_prompt = self.prompt
|
self.cur_prompt = self.prompt
|
||||||
@@ -339,23 +312,17 @@ class DreamEmbeddingModel(DreamFusionModel):
|
|||||||
loss_dict["sds_loss"] = sds_loss.to(self.device)
|
loss_dict["sds_loss"] = sds_loss.to(self.device)
|
||||||
|
|
||||||
if self.training:
|
if self.training:
|
||||||
loss_dict[
|
loss_dict["distortion_loss"] = self.config.distortion_loss_mult * distortion_loss(
|
||||||
"distortion_loss"
|
|
||||||
] = self.config.distortion_loss_mult * distortion_loss(
|
|
||||||
outputs["weights_list"], outputs["ray_samples_list"]
|
outputs["weights_list"], outputs["ray_samples_list"]
|
||||||
)
|
)
|
||||||
loss_dict[
|
loss_dict["interlevel_loss"] = self.config.interlevel_loss_mult * interlevel_loss(
|
||||||
"interlevel_loss"
|
|
||||||
] = self.config.interlevel_loss_mult * interlevel_loss(
|
|
||||||
outputs["weights_list"], outputs["ray_samples_list"]
|
outputs["weights_list"], outputs["ray_samples_list"]
|
||||||
)
|
)
|
||||||
# if 45 < batch["central"] <= 135:
|
# if 45 < batch["central"] <= 135:
|
||||||
# loss_dict["input_latent_loss"] = self.l2_loss(self.input_latent.detach(), outputs["latents"])
|
# loss_dict["input_latent_loss"] = self.l2_loss(self.input_latent.detach(), outputs["latents"])
|
||||||
return loss_dict
|
return loss_dict
|
||||||
|
|
||||||
def forward(
|
def forward(self, ray_bundle: RayBundle, ray_dims: Tuple = ()) -> Dict[str, torch.Tensor]:
|
||||||
self, ray_bundle: RayBundle, ray_dims: Tuple = ()
|
|
||||||
) -> Dict[str, torch.Tensor]:
|
|
||||||
"""Run forward starting with a ray bundle. This outputs different things depending on the configuration
|
"""Run forward starting with a ray bundle. This outputs different things depending on the configuration
|
||||||
of the model and whether or not the batch is provided (whether or not we are training basically)
|
of the model and whether or not the batch is provided (whether or not we are training basically)
|
||||||
|
|
||||||
@@ -369,9 +336,7 @@ class DreamEmbeddingModel(DreamFusionModel):
|
|||||||
return self.get_outputs(ray_bundle, ray_dims)
|
return self.get_outputs(ray_bundle, ray_dims)
|
||||||
|
|
||||||
@torch.no_grad()
|
@torch.no_grad()
|
||||||
def get_outputs_for_camera_ray_bundle(
|
def get_outputs_for_camera_ray_bundle(self, camera_ray_bundle: RayBundle) -> Dict[str, torch.Tensor]:
|
||||||
self, camera_ray_bundle: RayBundle
|
|
||||||
) -> Dict[str, torch.Tensor]:
|
|
||||||
"""Takes in camera parameters and computes the output of the model.
|
"""Takes in camera parameters and computes the output of the model.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -384,12 +349,8 @@ class DreamEmbeddingModel(DreamFusionModel):
|
|||||||
for i in range(0, num_rays, num_rays_per_chunk):
|
for i in range(0, num_rays, num_rays_per_chunk):
|
||||||
start_idx = i
|
start_idx = i
|
||||||
end_idx = i + num_rays_per_chunk
|
end_idx = i + num_rays_per_chunk
|
||||||
ray_bundle = camera_ray_bundle.get_row_major_sliced_ray_bundle(
|
ray_bundle = camera_ray_bundle.get_row_major_sliced_ray_bundle(start_idx, end_idx)
|
||||||
start_idx, end_idx
|
outputs = self.forward(ray_bundle=ray_bundle, ray_dims=(image_height, image_width))
|
||||||
)
|
|
||||||
outputs = self.forward(
|
|
||||||
ray_bundle=ray_bundle, ray_dims=(image_height, image_width)
|
|
||||||
)
|
|
||||||
for output_name, output in outputs.items(): # type: ignore
|
for output_name, output in outputs.items(): # type: ignore
|
||||||
outputs_lists[output_name].append(output)
|
outputs_lists[output_name].append(output)
|
||||||
outputs = {}
|
outputs = {}
|
||||||
|
|||||||
Reference in new issue
Block a user