From 008cc8f144b65272645471c6748ac3fe2af1427c Mon Sep 17 00:00:00 2001 From: Terrance Wang Date: Tue, 13 Dec 2022 12:39:41 -0800 Subject: [PATCH] change normals grad calculation (#1108) * change normals grad calculation * changing normal calc --- nerfstudio/fields/base_field.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/nerfstudio/fields/base_field.py b/nerfstudio/fields/base_field.py index 043a29e01..c24d9659c 100644 --- a/nerfstudio/fields/base_field.py +++ b/nerfstudio/fields/base_field.py @@ -74,10 +74,15 @@ class Field(nn.Module): self._sample_locations.shape[:-1] == self._density_before_activation.shape[:-1] ), "Sample locations and density must have the same shape besides the last dimension." - self._density_before_activation.backward( - gradient=torch.ones_like(self._density_before_activation), inputs=self._sample_locations, retain_graph=True - ) - normals = -torch.nn.functional.normalize(self._sample_locations.grad, dim=-1) + normals = torch.autograd.grad( + self._density_before_activation, + self._sample_locations, + grad_outputs=torch.ones_like(self._density_before_activation), + retain_graph=True, + )[0] + + normals = -torch.nn.functional.normalize(normals, dim=-1) + return normals @abstractmethod