Files
nerfstudio/projects-lerf/lerf/lerf_renderers.py
T

31 lines
993 B
Python

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