Convert a VarLenTensor to a dense representation without for-loop. Returns: dense (torch.Tensor): (N, L, C) dense tensor mask (torch.BoolTensor): (N, L) mask indicating valid positions
(self, max_length=None)
| 178 | return new_tensor |
| 179 | |
| 180 | def to_dense(self, max_length=None) -> torch.Tensor: |
| 181 | """ |
| 182 | Convert a VarLenTensor to a dense representation without for-loop. |
| 183 | |
| 184 | Returns: |
| 185 | dense (torch.Tensor): (N, L, C) dense tensor |
| 186 | mask (torch.BoolTensor): (N, L) mask indicating valid positions |
| 187 | """ |
| 188 | N = len(self) |
| 189 | L = max_length or self.seqlen.max().item() |
| 190 | spatial = self.feats.shape[1:] |
| 191 | idx = torch.arange(L, device=self.device).unsqueeze(0).expand(N, L) |
| 192 | mask = (idx < self.seqlen.unsqueeze(1)) |
| 193 | mapping = mask.reshape(-1).cumsum(dim=0) - 1 |
| 194 | dense = self.feats[mapping] |
| 195 | dense = dense.reshape(N, L, *spatial) |
| 196 | return dense, mask |
| 197 | |
| 198 | def __neg__(self) -> 'VarLenTensor': |
| 199 | return self.replace(-self.feats) |