mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-20 14:45:38 +08:00
31 lines
993 B
Python
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 |