| 3 | import torch.nn.functional as F |
| 4 | |
| 5 | class FreqEncoder(nn.Module): |
| 6 | def __init__(self, input_dim, max_freq_log2, N_freqs, |
| 7 | log_sampling=True, include_input=True, |
| 8 | periodic_fns=(torch.sin, torch.cos)): |
| 9 | |
| 10 | super().__init__() |
| 11 | |
| 12 | self.input_dim = input_dim |
| 13 | self.include_input = include_input |
| 14 | self.periodic_fns = periodic_fns |
| 15 | |
| 16 | self.output_dim = 0 |
| 17 | if self.include_input: |
| 18 | self.output_dim += self.input_dim |
| 19 | |
| 20 | self.output_dim += self.input_dim * N_freqs * len(self.periodic_fns) |
| 21 | |
| 22 | if log_sampling: |
| 23 | self.freq_bands = 2. ** torch.linspace(0., max_freq_log2, N_freqs) |
| 24 | else: |
| 25 | self.freq_bands = torch.linspace(2. ** 0., 2. ** max_freq_log2, N_freqs) |
| 26 | |
| 27 | self.freq_bands = self.freq_bands.numpy().tolist() |
| 28 | |
| 29 | def forward(self, input, **kwargs): |
| 30 | |
| 31 | out = [] |
| 32 | if self.include_input: |
| 33 | out.append(input) |
| 34 | |
| 35 | for i in range(len(self.freq_bands)): |
| 36 | freq = self.freq_bands[i] |
| 37 | for p_fn in self.periodic_fns: |
| 38 | out.append(p_fn(input * freq)) |
| 39 | |
| 40 | out = torch.cat(out, dim=-1) |
| 41 | |
| 42 | |
| 43 | return out |
| 44 | |
| 45 | def get_encoder(encoding, input_dim=3, |
| 46 | multires=6, |
nothing calls this directly
no outgoing calls
no test coverage detected