prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Error while doing ns-export poisson for depth-nerfacto trained model on polycam data
  Hi all,
  
  I have been trying to do ns-export poisson on depth-nerfacto trained model on polycam data and get the following error: 
  
  In depth_nerfacto.py line 73 in get_outputs
  
  
  outputs["directions_norm"] = ray_bundle.metadata["directions_norm"]  as the following:
  
  TypeError: 'NoneType' object is not subscriptable
  
  Reference: 
  
  https://discord.com/channels/1025504971606724712/1073830375559806976/1074782026781175919
  
  
  Can you please help. Thanks in advance.
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `nerfstudio/models/depth_nerfacto.py`
  Content:
  ```
  1 # Copyright 2022 The Nerfstudio Team. All rights reserved.
  2 #
  3 # Licensed under the Apache License, Version 2.0 (the "License");
  4 # you may not use this file except in compliance with the License.
  5 # You may obtain a copy of the License at
  6 #
  7 #     http://www.apache.org/licenses/LICENSE-2.0
  8 #
  9 # Unless required by applicable law or agreed to in writing, software
  10 # distributed under the License is distributed on an "AS IS" BASIS,
  11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12 # See the License for the specific language governing permissions and
  13 # limitations under the License.
  14 
  15 """
  16 Nerfacto augmented with depth supervision.
  17 """
  18 
  19 from __future__ import annotations
  20 
  21 from dataclasses import dataclass, field
  22 from typing import Dict, Tuple, Type
  23 
  24 import torch
  25 
  26 from nerfstudio.cameras.rays import RayBundle
  27 from nerfstudio.model_components.losses import DepthLossType, depth_loss
  28 from nerfstudio.models.nerfacto import NerfactoModel, NerfactoModelConfig
  29 from nerfstudio.utils import colormaps
  30 
  31 
  32 @dataclass
  33 class DepthNerfactoModelConfig(NerfactoModelConfig):
  34     """Additional parameters for depth supervision."""
  35 
  36     _target: Type = field(default_factory=lambda: DepthNerfactoModel)
  37     depth_loss_mult: float = 1e-3
  38     """Lambda of the depth loss."""
  39     is_euclidean_depth: bool = False
  40     """Whether input depth maps are Euclidean distances (or z-distances)."""
  41     depth_sigma: float = 0.01
  42     """Uncertainty around depth values in meters (defaults to 1cm)."""
  43     should_decay_sigma: bool = False
  44     """Whether to exponentially decay sigma."""
  45     starting_depth_sigma: float = 0.2
  46     """Starting uncertainty around depth values in meters (defaults to 0.2m)."""
  47     sigma_decay_rate: float = 0.99985
  48     """Rate of exponential decay."""
  49     depth_loss_type: DepthLossType = DepthLossType.DS_NERF
  50     """Depth loss type."""
  51 
  52 
  53 class DepthNerfactoModel(NerfactoModel):
  54     """Depth loss augmented nerfacto model.
  55 
  56     Args:
  57         config: Nerfacto configuration to instantiate model
  58     """
  59 
  60     config: DepthNerfactoModelConfig
  61 
  62     def populate_modules(self):
  63         """Set the fields and modules."""
  64         super().populate_modules()
  65 
  66         if self.config.should_decay_sigma:
  67             self.depth_sigma = torch.tensor([self.config.starting_depth_sigma])
  68         else:
  69             self.depth_sigma = torch.tensor([self.config.depth_sigma])
  70 
  71     def get_outputs(self, ray_bundle: RayBundle):
  72         outputs = super().get_outputs(ray_bundle)
  73         outputs["directions_norm"] = ray_bundle.metadata["directions_norm"]
  74         return outputs
  75 
  76     def get_metrics_dict(self, outputs, batch):
  77         metrics_dict = super().get_metrics_dict(outputs, batch)
  78         if self.training:
  79             metrics_dict["depth_loss"] = 0.0
  80             sigma = self._get_sigma().to(self.device)
  81             termination_depth = batch["depth_image"].to(self.device)
  82             for i in range(len(outputs["weights_list"])):
  83                 metrics_dict["depth_loss"] += depth_loss(
  84                     weights=outputs["weights_list"][i],
  85                     ray_samples=outputs["ray_samples_list"][i],
  86                     termination_depth=termination_depth,
  87                     predicted_depth=outputs["depth"],
  88                     sigma=sigma,
  89                     directions_norm=outputs["directions_norm"],
  90                     is_euclidean=self.config.is_euclidean_depth,
  91                     depth_loss_type=self.config.depth_loss_type,
  92                 ) / len(outputs["weights_list"])
  93 
  94         return metrics_dict
  95 
  96     def get_loss_dict(self, outputs, batch, metrics_dict=None):
  97         loss_dict = super().get_loss_dict(outputs, batch, metrics_dict)
  98         if self.training:
  99             assert metrics_dict is not None and "depth_loss" in metrics_dict
  100             loss_dict["depth_loss"] = self.config.depth_loss_mult * metrics_dict["depth_loss"]
  101 
  102         return loss_dict
  103 
  104     def get_image_metrics_and_images(
  105         self, outputs: Dict[str, torch.Tensor], batch: Dict[str, torch.Tensor]
  106     ) -> Tuple[Dict[str, float], Dict[str, torch.Tensor]]:
  107         """Appends ground truth depth to the depth image."""
  108         metrics, images = super().get_image_metrics_and_images(outputs, batch)
  109         ground_truth_depth = batch["depth_image"]
  110         if not self.config.is_euclidean_depth:
  111             ground_truth_depth = ground_truth_depth * outputs["directions_norm"]
  112 
  113         ground_truth_depth_colormap = colormaps.apply_depth_colormap(ground_truth_depth)
  114         predicted_depth_colormap = colormaps.apply_depth_colormap(
  115             outputs["depth"],
  116             accumulation=outputs["accumulation"],
  117             near_plane=torch.min(ground_truth_depth),
  118             far_plane=torch.max(ground_truth_depth),
  119         )
  120         images["depth"] = torch.cat([ground_truth_depth_colormap, predicted_depth_colormap], dim=1)
  121         depth_mask = ground_truth_depth > 0
  122         metrics["depth_mse"] = torch.nn.functional.mse_loss(
  123             outputs["depth"][depth_mask], ground_truth_depth[depth_mask]
  124         )
  125         return metrics, images
  126 
  127     def _get_sigma(self):
  128         if not self.config.should_decay_sigma:
  129             return self.depth_sigma
  130 
  131         self.depth_sigma = torch.maximum(  # pylint: disable=attribute-defined-outside-init
  132             self.config.sigma_decay_rate * self.depth_sigma, torch.tensor([self.config.depth_sigma])
  133         )
  134         return self.depth_sigma
  135
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/nerfstudio/models/depth_nerfacto.py b/nerfstudio/models/depth_nerfacto.py
  --- a/nerfstudio/models/depth_nerfacto.py
  +++ b/nerfstudio/models/depth_nerfacto.py
  @@ -70,7 +70,8 @@
   
       def get_outputs(self, ray_bundle: RayBundle):
           outputs = super().get_outputs(ray_bundle)
  -        outputs["directions_norm"] = ray_bundle.metadata["directions_norm"]
  +        if ray_bundle.metadata is not None and "directions_norm" in ray_bundle.metadata:
  +            outputs["directions_norm"] = ray_bundle.metadata["directions_norm"]
           return outputs
   
       def get_metrics_dict(self, outputs, batch):
  
verification_info: {"golden_diff": "diff --git a/nerfstudio/models/depth_nerfacto.py b/nerfstudio/models/depth_nerfacto.py\n--- a/nerfstudio/models/depth_nerfacto.py\n+++ b/nerfstudio/models/depth_nerfacto.py\n@@ -70,7 +70,8 @@\n \n     def get_outputs(self, ray_bundle: RayBundle):\n         outputs = super().get_outputs(ray_bundle)\n-        outputs[\"directions_norm\"] = ray_bundle.metadata[\"directions_norm\"]\n+        if ray_bundle.metadata is not None and \"directions_norm\" in ray_bundle.metadata:\n+            outputs[\"directions_norm\"] = ray_bundle.metadata[\"directions_norm\"]\n         return outputs\n \n     def get_metrics_dict(self, outputs, batch):\n", "issue": "Error while doing ns-export poisson for depth-nerfacto trained model on polycam data\nHi all,\n\nI have been trying to do ns-export poisson on depth-nerfacto trained model on polycam data and get the following error: \n\nIn depth_nerfacto.py line 73 in get_outputs\n\n\noutputs[\"directions_norm\"] = ray_bundle.metadata[\"directions_norm\"]  as the following:\n\nTypeError: 'NoneType' object is not subscriptable\n\nReference: \n\nhttps://discord.com/channels/1025504971606724712/1073830375559806976/1074782026781175919\n\n\nCan you please help. Thanks in advance.\n\n", "before_files": [{"content": "# Copyright 2022 The Nerfstudio Team. All rights reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\n\"\"\"\nNerfacto augmented with depth supervision.\n\"\"\"\n\nfrom __future__ import annotations\n\nfrom dataclasses import dataclass, field\nfrom typing import Dict, Tuple, Type\n\nimport torch\n\nfrom nerfstudio.cameras.rays import RayBundle\nfrom nerfstudio.model_components.losses import DepthLossType, depth_loss\nfrom nerfstudio.models.nerfacto import NerfactoModel, NerfactoModelConfig\nfrom nerfstudio.utils import colormaps\n\n\n@dataclass\nclass DepthNerfactoModelConfig(NerfactoModelConfig):\n    \"\"\"Additional parameters for depth supervision.\"\"\"\n\n    _target: Type = field(default_factory=lambda: DepthNerfactoModel)\n    depth_loss_mult: float = 1e-3\n    \"\"\"Lambda of the depth loss.\"\"\"\n    is_euclidean_depth: bool = False\n    \"\"\"Whether input depth maps are Euclidean distances (or z-distances).\"\"\"\n    depth_sigma: float = 0.01\n    \"\"\"Uncertainty around depth values in meters (defaults to 1cm).\"\"\"\n    should_decay_sigma: bool = False\n    \"\"\"Whether to exponentially decay sigma.\"\"\"\n    starting_depth_sigma: float = 0.2\n    \"\"\"Starting uncertainty around depth values in meters (defaults to 0.2m).\"\"\"\n    sigma_decay_rate: float = 0.99985\n    \"\"\"Rate of exponential decay.\"\"\"\n    depth_loss_type: DepthLossType = DepthLossType.DS_NERF\n    \"\"\"Depth loss type.\"\"\"\n\n\nclass DepthNerfactoModel(NerfactoModel):\n    \"\"\"Depth loss augmented nerfacto model.\n\n    Args:\n        config: Nerfacto configuration to instantiate model\n    \"\"\"\n\n    config: DepthNerfactoModelConfig\n\n    def populate_modules(self):\n        \"\"\"Set the fields and modules.\"\"\"\n        super().populate_modules()\n\n        if self.config.should_decay_sigma:\n            self.depth_sigma = torch.tensor([self.config.starting_depth_sigma])\n        else:\n            self.depth_sigma = torch.tensor([self.config.depth_sigma])\n\n    def get_outputs(self, ray_bundle: RayBundle):\n        outputs = super().get_outputs(ray_bundle)\n        outputs[\"directions_norm\"] = ray_bundle.metadata[\"directions_norm\"]\n        return outputs\n\n    def get_metrics_dict(self, outputs, batch):\n        metrics_dict = super().get_metrics_dict(outputs, batch)\n        if self.training:\n            metrics_dict[\"depth_loss\"] = 0.0\n            sigma = self._get_sigma().to(self.device)\n            termination_depth = batch[\"depth_image\"].to(self.device)\n            for i in range(len(outputs[\"weights_list\"])):\n                metrics_dict[\"depth_loss\"] += depth_loss(\n                    weights=outputs[\"weights_list\"][i],\n                    ray_samples=outputs[\"ray_samples_list\"][i],\n                    termination_depth=termination_depth,\n                    predicted_depth=outputs[\"depth\"],\n                    sigma=sigma,\n                    directions_norm=outputs[\"directions_norm\"],\n                    is_euclidean=self.config.is_euclidean_depth,\n                    depth_loss_type=self.config.depth_loss_type,\n                ) / len(outputs[\"weights_list\"])\n\n        return metrics_dict\n\n    def get_loss_dict(self, outputs, batch, metrics_dict=None):\n        loss_dict = super().get_loss_dict(outputs, batch, metrics_dict)\n        if self.training:\n            assert metrics_dict is not None and \"depth_loss\" in metrics_dict\n            loss_dict[\"depth_loss\"] = self.config.depth_loss_mult * metrics_dict[\"depth_loss\"]\n\n        return loss_dict\n\n    def get_image_metrics_and_images(\n        self, outputs: Dict[str, torch.Tensor], batch: Dict[str, torch.Tensor]\n    ) -> Tuple[Dict[str, float], Dict[str, torch.Tensor]]:\n        \"\"\"Appends ground truth depth to the depth image.\"\"\"\n        metrics, images = super().get_image_metrics_and_images(outputs, batch)\n        ground_truth_depth = batch[\"depth_image\"]\n        if not self.config.is_euclidean_depth:\n            ground_truth_depth = ground_truth_depth * outputs[\"directions_norm\"]\n\n        ground_truth_depth_colormap = colormaps.apply_depth_colormap(ground_truth_depth)\n        predicted_depth_colormap = colormaps.apply_depth_colormap(\n            outputs[\"depth\"],\n            accumulation=outputs[\"accumulation\"],\n            near_plane=torch.min(ground_truth_depth),\n            far_plane=torch.max(ground_truth_depth),\n        )\n        images[\"depth\"] = torch.cat([ground_truth_depth_colormap, predicted_depth_colormap], dim=1)\n        depth_mask = ground_truth_depth > 0\n        metrics[\"depth_mse\"] = torch.nn.functional.mse_loss(\n            outputs[\"depth\"][depth_mask], ground_truth_depth[depth_mask]\n        )\n        return metrics, images\n\n    def _get_sigma(self):\n        if not self.config.should_decay_sigma:\n            return self.depth_sigma\n\n        self.depth_sigma = torch.maximum(  # pylint: disable=attribute-defined-outside-init\n            self.config.sigma_decay_rate * self.depth_sigma, torch.tensor([self.config.depth_sigma])\n        )\n        return self.depth_sigma\n", "path": "nerfstudio/models/depth_nerfacto.py"}], "after_files": [{"content": "# Copyright 2022 The Nerfstudio Team. All rights reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\n\"\"\"\nNerfacto augmented with depth supervision.\n\"\"\"\n\nfrom __future__ import annotations\n\nfrom dataclasses import dataclass, field\nfrom typing import Dict, Tuple, Type\n\nimport torch\n\nfrom nerfstudio.cameras.rays import RayBundle\nfrom nerfstudio.model_components.losses import DepthLossType, depth_loss\nfrom nerfstudio.models.nerfacto import NerfactoModel, NerfactoModelConfig\nfrom nerfstudio.utils import colormaps\n\n\n@dataclass\nclass DepthNerfactoModelConfig(NerfactoModelConfig):\n    \"\"\"Additional parameters for depth supervision.\"\"\"\n\n    _target: Type = field(default_factory=lambda: DepthNerfactoModel)\n    depth_loss_mult: float = 1e-3\n    \"\"\"Lambda of the depth loss.\"\"\"\n    is_euclidean_depth: bool = False\n    \"\"\"Whether input depth maps are Euclidean distances (or z-distances).\"\"\"\n    depth_sigma: float = 0.01\n    \"\"\"Uncertainty around depth values in meters (defaults to 1cm).\"\"\"\n    should_decay_sigma: bool = False\n    \"\"\"Whether to exponentially decay sigma.\"\"\"\n    starting_depth_sigma: float = 0.2\n    \"\"\"Starting uncertainty around depth values in meters (defaults to 0.2m).\"\"\"\n    sigma_decay_rate: float = 0.99985\n    \"\"\"Rate of exponential decay.\"\"\"\n    depth_loss_type: DepthLossType = DepthLossType.DS_NERF\n    \"\"\"Depth loss type.\"\"\"\n\n\nclass DepthNerfactoModel(NerfactoModel):\n    \"\"\"Depth loss augmented nerfacto model.\n\n    Args:\n        config: Nerfacto configuration to instantiate model\n    \"\"\"\n\n    config: DepthNerfactoModelConfig\n\n    def populate_modules(self):\n        \"\"\"Set the fields and modules.\"\"\"\n        super().populate_modules()\n\n        if self.config.should_decay_sigma:\n            self.depth_sigma = torch.tensor([self.config.starting_depth_sigma])\n        else:\n            self.depth_sigma = torch.tensor([self.config.depth_sigma])\n\n    def get_outputs(self, ray_bundle: RayBundle):\n        outputs = super().get_outputs(ray_bundle)\n        if ray_bundle.metadata is not None and \"directions_norm\" in ray_bundle.metadata:\n            outputs[\"directions_norm\"] = ray_bundle.metadata[\"directions_norm\"]\n        return outputs\n\n    def get_metrics_dict(self, outputs, batch):\n        metrics_dict = super().get_metrics_dict(outputs, batch)\n        if self.training:\n            metrics_dict[\"depth_loss\"] = 0.0\n            sigma = self._get_sigma().to(self.device)\n            termination_depth = batch[\"depth_image\"].to(self.device)\n            for i in range(len(outputs[\"weights_list\"])):\n                metrics_dict[\"depth_loss\"] += depth_loss(\n                    weights=outputs[\"weights_list\"][i],\n                    ray_samples=outputs[\"ray_samples_list\"][i],\n                    termination_depth=termination_depth,\n                    predicted_depth=outputs[\"depth\"],\n                    sigma=sigma,\n                    directions_norm=outputs[\"directions_norm\"],\n                    is_euclidean=self.config.is_euclidean_depth,\n                    depth_loss_type=self.config.depth_loss_type,\n                ) / len(outputs[\"weights_list\"])\n\n        return metrics_dict\n\n    def get_loss_dict(self, outputs, batch, metrics_dict=None):\n        loss_dict = super().get_loss_dict(outputs, batch, metrics_dict)\n        if self.training:\n            assert metrics_dict is not None and \"depth_loss\" in metrics_dict\n            loss_dict[\"depth_loss\"] = self.config.depth_loss_mult * metrics_dict[\"depth_loss\"]\n\n        return loss_dict\n\n    def get_image_metrics_and_images(\n        self, outputs: Dict[str, torch.Tensor], batch: Dict[str, torch.Tensor]\n    ) -> Tuple[Dict[str, float], Dict[str, torch.Tensor]]:\n        \"\"\"Appends ground truth depth to the depth image.\"\"\"\n        metrics, images = super().get_image_metrics_and_images(outputs, batch)\n        ground_truth_depth = batch[\"depth_image\"]\n        if not self.config.is_euclidean_depth:\n            ground_truth_depth = ground_truth_depth * outputs[\"directions_norm\"]\n\n        ground_truth_depth_colormap = colormaps.apply_depth_colormap(ground_truth_depth)\n        predicted_depth_colormap = colormaps.apply_depth_colormap(\n            outputs[\"depth\"],\n            accumulation=outputs[\"accumulation\"],\n            near_plane=torch.min(ground_truth_depth),\n            far_plane=torch.max(ground_truth_depth),\n        )\n        images[\"depth\"] = torch.cat([ground_truth_depth_colormap, predicted_depth_colormap], dim=1)\n        depth_mask = ground_truth_depth > 0\n        metrics[\"depth_mse\"] = torch.nn.functional.mse_loss(\n            outputs[\"depth\"][depth_mask], ground_truth_depth[depth_mask]\n        )\n        return metrics, images\n\n    def _get_sigma(self):\n        if not self.config.should_decay_sigma:\n            return self.depth_sigma\n\n        self.depth_sigma = torch.maximum(  # pylint: disable=attribute-defined-outside-init\n            self.config.sigma_decay_rate * self.depth_sigma, torch.tensor([self.config.depth_sigma])\n        )\n        return self.depth_sigma\n", "path": "nerfstudio/models/depth_nerfacto.py"}]}