mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-02 13:59:10 +08:00
All lerf-changes moved to projects-lerf, except viewer changes
This commit is contained in:
1 parent
18208f7aaa
commit
a87b7c3b53
6 files changed
+52
-43
No files matched your search
@@ -40,9 +40,6 @@ class FieldHeadNames(Enum):
|
|||||||
SDF = "sdf"
|
SDF = "sdf"
|
||||||
ALPHA = "alpha"
|
ALPHA = "alpha"
|
||||||
GRADIENT = "gradient"
|
GRADIENT = "gradient"
|
||||||
HASHGRID = "hashgrid"
|
|
||||||
CLIP = "clip"
|
|
||||||
DINO = "dino"
|
|
||||||
|
|
||||||
|
|
||||||
class FieldHead(FieldComponent):
|
class FieldHead(FieldComponent):
|
||||||
|
|||||||
@@ -342,31 +342,3 @@ class NormalsRenderer(nn.Module):
|
|||||||
if normalize:
|
if normalize:
|
||||||
n = safe_normalize(n)
|
n = safe_normalize(n)
|
||||||
return n
|
return n
|
||||||
|
|
||||||
class CLIPRenderer(nn.Module):
|
|
||||||
"""Calculate CLIP embeddings along ray."""
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def forward(
|
|
||||||
cls,
|
|
||||||
embeds: TensorType["bs":..., "num_samples", "num_classes"],
|
|
||||||
weights: TensorType["bs":..., "num_samples", 1],
|
|
||||||
) -> TensorType["bs":..., "num_classes"]:
|
|
||||||
"""Calculate semantics along the ray."""
|
|
||||||
output = torch.sum(weights * embeds, dim=-2)
|
|
||||||
output = output / torch.linalg.norm(output, dim=-1, keepdim=True)
|
|
||||||
return output
|
|
||||||
|
|
||||||
|
|
||||||
class MeanRenderer(nn.Module):
|
|
||||||
"""Calculate average of embeddings along ray."""
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def forward(
|
|
||||||
cls,
|
|
||||||
embeds: TensorType["bs":..., "num_samples", "num_classes"],
|
|
||||||
weights: TensorType["bs":..., "num_samples", 1],
|
|
||||||
) -> TensorType["bs":..., "num_classes"]:
|
|
||||||
"""Calculate semantics along the ray."""
|
|
||||||
output = torch.sum(weights * embeds, dim=-2)
|
|
||||||
return output
|
|
||||||
@@ -12,14 +12,13 @@ from nerfstudio.data.scene_box import SceneBox
|
|||||||
from nerfstudio.field_components.field_heads import FieldHeadNames
|
from nerfstudio.field_components.field_heads import FieldHeadNames
|
||||||
from nerfstudio.field_components.spatial_distortions import SceneContraction
|
from nerfstudio.field_components.spatial_distortions import SceneContraction
|
||||||
from nerfstudio.model_components.ray_samplers import PDFSampler
|
from nerfstudio.model_components.ray_samplers import PDFSampler
|
||||||
from nerfstudio.model_components.renderers import (
|
from nerfstudio.model_components.renderers import DepthRenderer
|
||||||
CLIPRenderer,
|
|
||||||
DepthRenderer,
|
|
||||||
MeanRenderer,
|
|
||||||
)
|
|
||||||
from nerfstudio.models.nerfacto import NerfactoModel, NerfactoModelConfig
|
from nerfstudio.models.nerfacto import NerfactoModel, NerfactoModelConfig
|
||||||
|
|
||||||
from lerf.encoders.image_encoder import BaseImageEncoder
|
from lerf.encoders.image_encoder import BaseImageEncoder
|
||||||
from lerf.lerf_field import LERFField
|
from lerf.lerf_field import LERFField
|
||||||
|
from lerf.lerf_renderers import CLIPRenderer, MeanRenderer
|
||||||
|
from lerf.lerf_fieldheadnames import LERFFieldHeadNames
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@@ -108,15 +107,15 @@ class LERFModel(NerfactoModel):
|
|||||||
|
|
||||||
if self.training:
|
if self.training:
|
||||||
outputs["clip"] = self.renderer_clip(
|
outputs["clip"] = self.renderer_clip(
|
||||||
embeds=lerf_field_outputs[FieldHeadNames.CLIP], weights=lerf_weights.detach()
|
embeds=lerf_field_outputs[LERFFieldHeadNames.CLIP], weights=lerf_weights.detach()
|
||||||
)
|
)
|
||||||
outputs["dino"] = self.renderer_mean(
|
outputs["dino"] = self.renderer_mean(
|
||||||
embeds=lerf_field_outputs[FieldHeadNames.DINO], weights=lerf_weights.detach()
|
embeds=lerf_field_outputs[LERFFieldHeadNames.DINO], weights=lerf_weights.detach()
|
||||||
)
|
)
|
||||||
|
|
||||||
if not self.training:
|
if not self.training:
|
||||||
max_across, best_scales = self.get_max_across(
|
max_across, best_scales = self.get_max_across(
|
||||||
lerf_samples, lerf_weights, lerf_field_outputs[FieldHeadNames.HASHGRID], clip_scales.shape
|
lerf_samples, lerf_weights, lerf_field_outputs[LERFFieldHeadNames.HASHGRID], clip_scales.shape
|
||||||
)
|
)
|
||||||
multiphrase = max_across[0]
|
multiphrase = max_across[0]
|
||||||
# normalization for sanity check TODO(cmk) remove
|
# normalization for sanity check TODO(cmk) remove
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ from nerfstudio.field_components.spatial_distortions import (
|
|||||||
)
|
)
|
||||||
from nerfstudio.fields.base_field import Field
|
from nerfstudio.fields.base_field import Field
|
||||||
|
|
||||||
|
from lerf.lerf_fieldheadnames import LERFFieldHeadNames
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import tinycudann as tcnn
|
import tinycudann as tcnn
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@@ -76,7 +78,7 @@ class LERFField(Field):
|
|||||||
)
|
)
|
||||||
return enc
|
return enc
|
||||||
|
|
||||||
def get_outputs(self, ray_samples: RaySamples, clip_scales):
|
def get_outputs(self, ray_samples: RaySamples, clip_scales) -> Dict[LERFFieldHeadNames, TensorType]:
|
||||||
# random scales, one scale
|
# random scales, one scale
|
||||||
outputs = {}
|
outputs = {}
|
||||||
|
|
||||||
@@ -87,13 +89,13 @@ class LERFField(Field):
|
|||||||
xs = [e(positions.view(-1, 3)) for e in self.clip_encs]
|
xs = [e(positions.view(-1, 3)) for e in self.clip_encs]
|
||||||
x = torch.concat(xs, dim=-1)
|
x = torch.concat(xs, dim=-1)
|
||||||
|
|
||||||
outputs[FieldHeadNames.HASHGRID] = x.view(*ray_samples.frustums.shape, -1)
|
outputs[LERFFieldHeadNames.HASHGRID] = x.view(*ray_samples.frustums.shape, -1)
|
||||||
|
|
||||||
clip_pass = self.clip_net(torch.cat([x, clip_scales.view(-1, 1)], dim=-1)).view(*ray_samples.frustums.shape, -1)
|
clip_pass = self.clip_net(torch.cat([x, clip_scales.view(-1, 1)], dim=-1)).view(*ray_samples.frustums.shape, -1)
|
||||||
outputs[FieldHeadNames.CLIP] = clip_pass / clip_pass.norm(dim=-1, keepdim=True)
|
outputs[LERFFieldHeadNames.CLIP] = clip_pass / clip_pass.norm(dim=-1, keepdim=True)
|
||||||
|
|
||||||
dino_pass = self.dino_net(x).view(*ray_samples.frustums.shape, -1)
|
dino_pass = self.dino_net(x).view(*ray_samples.frustums.shape, -1)
|
||||||
outputs[FieldHeadNames.DINO] = dino_pass
|
outputs[LERFFieldHeadNames.DINO] = dino_pass
|
||||||
|
|
||||||
return outputs
|
return outputs
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
from enum import Enum
|
||||||
|
# from nerfstudio.field_components.field_heads import FieldHeadNames
|
||||||
|
|
||||||
|
class LERFFieldHeadNames(Enum):
|
||||||
|
"""Possible field outputs"""
|
||||||
|
HASHGRID = "hashgrid"
|
||||||
|
CLIP = "clip"
|
||||||
|
DINO = "dino"
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
import torch
|
||||||
|
from torch import nn
|
||||||
|
from torchtyping import TensorType
|
||||||
|
|
||||||
|
class CLIPRenderer(nn.Module):
|
||||||
|
"""Calculate CLIP embeddings along ray."""
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def forward(
|
||||||
|
cls,
|
||||||
|
embeds: TensorType["bs":..., "num_samples", "num_classes"],
|
||||||
|
weights: TensorType["bs":..., "num_samples", 1],
|
||||||
|
) -> TensorType["bs":..., "num_classes"]:
|
||||||
|
"""Calculate semantics along the ray."""
|
||||||
|
output = torch.sum(weights * embeds, dim=-2)
|
||||||
|
output = output / torch.linalg.norm(output, dim=-1, keepdim=True)
|
||||||
|
return output
|
||||||
|
|
||||||
|
|
||||||
|
class MeanRenderer(nn.Module):
|
||||||
|
"""Calculate average of embeddings along ray."""
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def forward(
|
||||||
|
cls,
|
||||||
|
embeds: TensorType["bs":..., "num_samples", "num_classes"],
|
||||||
|
weights: TensorType["bs":..., "num_samples", 1],
|
||||||
|
) -> TensorType["bs":..., "num_classes"]:
|
||||||
|
"""Calculate semantics along the ray."""
|
||||||
|
output = torch.sum(weights * embeds, dim=-2)
|
||||||
|
return output
|
||||||
Reference in new issue
Block a user