mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-04 14:58:55 +08:00
Only load+undistort splatfacto images when they're used (#3043)
* Only load splatfacto dataset when it's used * ruff * typo fix * Fix #2817; don't undistort for `ns-viewer --viewer.max-num-display-images 0` * ruff
This commit is contained in:
1 parent
c572eb7f19
commit
3ac1b6bec7
2 files changed
+73
-95
No files matched your search
@@ -34,6 +34,7 @@ import numpy as np
|
|||||||
import torch
|
import torch
|
||||||
from rich.progress import track
|
from rich.progress import track
|
||||||
from torch.nn import Parameter
|
from torch.nn import Parameter
|
||||||
|
from typing_extensions import assert_never
|
||||||
|
|
||||||
from nerfstudio.cameras.camera_utils import fisheye624_project, fisheye624_unproject_helper
|
from nerfstudio.cameras.camera_utils import fisheye624_project, fisheye624_unproject_helper
|
||||||
from nerfstudio.cameras.cameras import Cameras, CameraType
|
from nerfstudio.cameras.cameras import Cameras, CameraType
|
||||||
@@ -115,7 +116,6 @@ class FullImageDatamanager(DataManager, Generic[TDataset]):
|
|||||||
style="bold yellow",
|
style="bold yellow",
|
||||||
)
|
)
|
||||||
self.config.cache_images = "cpu"
|
self.config.cache_images = "cpu"
|
||||||
self.cached_train, self.cached_eval = self.cache_images(self.config.cache_images)
|
|
||||||
self.exclude_batch_keys_from_device = self.train_dataset.exclude_batch_keys_from_device
|
self.exclude_batch_keys_from_device = self.train_dataset.exclude_batch_keys_from_device
|
||||||
if self.config.masks_on_gpu is True:
|
if self.config.masks_on_gpu is True:
|
||||||
self.exclude_batch_keys_from_device.remove("mask")
|
self.exclude_batch_keys_from_device.remove("mask")
|
||||||
@@ -129,104 +129,82 @@ class FullImageDatamanager(DataManager, Generic[TDataset]):
|
|||||||
|
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
def cache_images(self, cache_images_option):
|
@cached_property
|
||||||
cached_train = []
|
def cached_train(self) -> List[Dict[str, torch.Tensor]]:
|
||||||
cached_eval = []
|
"""Get the training images. Will load and undistort the images the
|
||||||
|
first time this (cached) property is accessed."""
|
||||||
|
return self._load_images("train", cache_images_device=self.config.cache_images)
|
||||||
|
|
||||||
def process_train_data(idx):
|
@cached_property
|
||||||
# cv2.undistort the images / cameras
|
def cached_eval(self) -> List[Dict[str, torch.Tensor]]:
|
||||||
data = self.train_dataset.get_data(idx, image_type=self.config.cache_images_type)
|
"""Get the eval images. Will load and undistort the images the
|
||||||
camera = self.train_dataset.cameras[idx].reshape(())
|
first time this (cached) property is accessed."""
|
||||||
K = camera.get_intrinsics_matrices().numpy()
|
return self._load_images("eval", cache_images_device=self.config.cache_images)
|
||||||
if camera.distortion_params is None:
|
|
||||||
return data
|
|
||||||
distortion_params = camera.distortion_params.numpy()
|
|
||||||
image = data["image"].numpy()
|
|
||||||
|
|
||||||
K, image, mask = _undistort_image(camera, distortion_params, data, image, K)
|
def _load_images(
|
||||||
data["image"] = torch.from_numpy(image)
|
self, split: Literal["train", "eval"], cache_images_device: Literal["cpu", "gpu"]
|
||||||
if mask is not None:
|
) -> List[Dict[str, torch.Tensor]]:
|
||||||
data["mask"] = mask
|
undistorted_images: List[Dict[str, torch.Tensor]] = []
|
||||||
|
|
||||||
self.train_dataset.cameras.fx[idx] = float(K[0, 0])
|
# Which dataset?
|
||||||
self.train_dataset.cameras.fy[idx] = float(K[1, 1])
|
if split == "train":
|
||||||
self.train_dataset.cameras.cx[idx] = float(K[0, 2])
|
dataset = self.train_dataset
|
||||||
self.train_dataset.cameras.cy[idx] = float(K[1, 2])
|
elif split == "eval":
|
||||||
self.train_dataset.cameras.width[idx] = image.shape[1]
|
dataset = self.eval_dataset
|
||||||
self.train_dataset.cameras.height[idx] = image.shape[0]
|
|
||||||
return data
|
|
||||||
|
|
||||||
def process_eval_data(idx):
|
|
||||||
# cv2.undistort the images / cameras
|
|
||||||
data = self.eval_dataset.get_data(idx, image_type=self.config.cache_images_type)
|
|
||||||
camera = self.eval_dataset.cameras[idx].reshape(())
|
|
||||||
K = camera.get_intrinsics_matrices().numpy()
|
|
||||||
if camera.distortion_params is None:
|
|
||||||
return data
|
|
||||||
distortion_params = camera.distortion_params.numpy()
|
|
||||||
image = data["image"].numpy()
|
|
||||||
|
|
||||||
K, image, mask = _undistort_image(camera, distortion_params, data, image, K)
|
|
||||||
data["image"] = torch.from_numpy(image)
|
|
||||||
if mask is not None:
|
|
||||||
data["mask"] = mask
|
|
||||||
|
|
||||||
self.eval_dataset.cameras.fx[idx] = float(K[0, 0])
|
|
||||||
self.eval_dataset.cameras.fy[idx] = float(K[1, 1])
|
|
||||||
self.eval_dataset.cameras.cx[idx] = float(K[0, 2])
|
|
||||||
self.eval_dataset.cameras.cy[idx] = float(K[1, 2])
|
|
||||||
self.eval_dataset.cameras.width[idx] = image.shape[1]
|
|
||||||
self.eval_dataset.cameras.height[idx] = image.shape[0]
|
|
||||||
return data
|
|
||||||
|
|
||||||
CONSOLE.log("Caching / undistorting train images")
|
|
||||||
with ThreadPoolExecutor(max_workers=2) as executor:
|
|
||||||
cached_train = list(
|
|
||||||
track(
|
|
||||||
executor.map(
|
|
||||||
process_train_data,
|
|
||||||
range(len(self.train_dataset)),
|
|
||||||
),
|
|
||||||
description="Caching / undistorting train images",
|
|
||||||
transient=True,
|
|
||||||
total=len(self.train_dataset),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
CONSOLE.log("Caching / undistorting eval images")
|
|
||||||
with ThreadPoolExecutor(max_workers=2) as executor:
|
|
||||||
cached_eval = list(
|
|
||||||
track(
|
|
||||||
executor.map(
|
|
||||||
process_eval_data,
|
|
||||||
range(len(self.eval_dataset)),
|
|
||||||
),
|
|
||||||
description="Caching / undistorting eval images",
|
|
||||||
transient=True,
|
|
||||||
total=len(self.eval_dataset),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
if cache_images_option == "gpu":
|
|
||||||
for cache in cached_train:
|
|
||||||
cache["image"] = cache["image"].to(self.device)
|
|
||||||
if "mask" in cache:
|
|
||||||
cache["mask"] = cache["mask"].to(self.device)
|
|
||||||
for cache in cached_eval:
|
|
||||||
cache["image"] = cache["image"].to(self.device)
|
|
||||||
if "mask" in cache:
|
|
||||||
cache["mask"] = cache["mask"].to(self.device)
|
|
||||||
else:
|
else:
|
||||||
for cache in cached_train:
|
assert_never(split)
|
||||||
cache["image"] = cache["image"].pin_memory()
|
|
||||||
if "mask" in cache:
|
|
||||||
cache["mask"] = cache["mask"].pin_memory()
|
|
||||||
for cache in cached_eval:
|
|
||||||
cache["image"] = cache["image"].pin_memory()
|
|
||||||
if "mask" in cache:
|
|
||||||
cache["mask"] = cache["mask"].pin_memory()
|
|
||||||
|
|
||||||
return cached_train, cached_eval
|
def undistort_idx(idx: int) -> Dict[str, torch.Tensor]:
|
||||||
|
data = dataset.get_data(idx, image_type=self.config.cache_images_type)
|
||||||
|
camera = dataset.cameras[idx].reshape(())
|
||||||
|
K = camera.get_intrinsics_matrices().numpy()
|
||||||
|
if camera.distortion_params is None:
|
||||||
|
return data
|
||||||
|
distortion_params = camera.distortion_params.numpy()
|
||||||
|
image = data["image"].numpy()
|
||||||
|
|
||||||
|
K, image, mask = _undistort_image(camera, distortion_params, data, image, K)
|
||||||
|
data["image"] = torch.from_numpy(image)
|
||||||
|
if mask is not None:
|
||||||
|
data["mask"] = mask
|
||||||
|
|
||||||
|
dataset.cameras.fx[idx] = float(K[0, 0])
|
||||||
|
dataset.cameras.fy[idx] = float(K[1, 1])
|
||||||
|
dataset.cameras.cx[idx] = float(K[0, 2])
|
||||||
|
dataset.cameras.cy[idx] = float(K[1, 2])
|
||||||
|
dataset.cameras.width[idx] = image.shape[1]
|
||||||
|
dataset.cameras.height[idx] = image.shape[0]
|
||||||
|
return data
|
||||||
|
|
||||||
|
CONSOLE.log(f"Caching / undistorting {split} images")
|
||||||
|
with ThreadPoolExecutor(max_workers=2) as executor:
|
||||||
|
undistorted_images = list(
|
||||||
|
track(
|
||||||
|
executor.map(
|
||||||
|
undistort_idx,
|
||||||
|
range(len(dataset)),
|
||||||
|
),
|
||||||
|
description=f"Caching / undistorting {split} images",
|
||||||
|
transient=True,
|
||||||
|
total=len(dataset),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Move to device.
|
||||||
|
if cache_images_device == "gpu":
|
||||||
|
for cache in undistorted_images:
|
||||||
|
cache["image"] = cache["image"].to(self.device)
|
||||||
|
if "mask" in cache:
|
||||||
|
cache["mask"] = cache["mask"].to(self.device)
|
||||||
|
elif cache_images_device == "cpu":
|
||||||
|
for cache in undistorted_images:
|
||||||
|
cache["image"] = cache["image"].pin_memory()
|
||||||
|
if "mask" in cache:
|
||||||
|
cache["mask"] = cache["mask"].pin_memory()
|
||||||
|
else:
|
||||||
|
assert_never(cache_images_device)
|
||||||
|
|
||||||
|
return undistorted_images
|
||||||
|
|
||||||
def create_train_dataset(self) -> TDataset:
|
def create_train_dataset(self) -> TDataset:
|
||||||
"""Sets up the data loaders for training"""
|
"""Sets up the data loaders for training"""
|
||||||
|
|||||||
@@ -138,7 +138,7 @@ def parse_object(
|
|||||||
return []
|
return []
|
||||||
ret = []
|
ret = []
|
||||||
# get a list of the properties of the object, sorted by whether things are instances of type_check
|
# get a list of the properties of the object, sorted by whether things are instances of type_check
|
||||||
obj_props = [(k, getattr(obj, k)) for k in dir(obj)]
|
obj_props = [(k, v) for k, v in vars(obj).items()]
|
||||||
for k, v in obj_props:
|
for k, v in obj_props:
|
||||||
if k[0] == "_":
|
if k[0] == "_":
|
||||||
continue
|
continue
|
||||||
|
|||||||
Reference in new issue
Block a user