MCPcopy Create free account
hub / github.com/Sin3DM/Sin3DM / SinusoidalEncoder

Class SinusoidalEncoder

src/encoding/blocks.py:7–42  ·  view source on GitHub ↗

Sinusoidal Positional Encoder used in Nerf.

Source from the content-addressed store, hash-verified

5
6
7class SinusoidalEncoder(nn.Module):
8 """Sinusoidal Positional Encoder used in Nerf."""
9
10 def __init__(self, x_dim, min_deg, max_deg, use_identity: bool = True):
11 super().__init__()
12 self.x_dim = x_dim
13 self.min_deg = min_deg
14 self.max_deg = max_deg
15 self.use_identity = use_identity
16 self.register_buffer(
17 "scales", torch.tensor([2**i for i in range(min_deg, max_deg)])
18 )
19
20 @property
21 def latent_dim(self) -> int:
22 return (
23 int(self.use_identity) + (self.max_deg - self.min_deg) * 2
24 ) * self.x_dim
25
26 def forward(self, x: torch.Tensor) -> torch.Tensor:
27 """
28 Args:
29 x: [..., x_dim]
30 Returns:
31 latent: [..., latent_dim]
32 """
33 if self.max_deg == self.min_deg:
34 return x
35 xb = torch.reshape(
36 (x[Ellipsis, None, :] * self.scales[:, None]),
37 list(x.shape[:-1]) + [(self.max_deg - self.min_deg) * self.x_dim],
38 )
39 latent = torch.sin(torch.cat([xb, xb + 0.5 * math.pi], dim=-1))
40 if self.use_identity:
41 latent = torch.cat([x] + [latent], dim=-1)
42 return latent
43
44
45class DecoderMLP(nn.Module):

Callers 2

__init__Method · 0.85
__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected