Cameras TensorDataclass and Phototourism (#961)

This branch gives us the flexibility to use Cameras as a tensordataclass, giving arbitrary batch dimensions, slicing, etc all for free. It tests this with the phototourism dataset.

Tensordataclass changes:
Originally, the tensordataclass would look at tensors in an object and take everything in front of the last dimension to be the batch dimensions, but in the case of the camera_to_world matrices, everything except for the last TWO are the important dimensions, so we needed to have a way for the tensordataclass to acknowledge these specific fields where there are multiple data dimensions behind the batch dimensions. This is important for things like reshaping where we need to know which dimensions get reshaped and which dimensions get left alone.

Cameras changes:
Now subclass of tensordataclass, which required a change to the tensordataclass itself.
The possibility of extra batch dimensions is the hairy part that requires the majority of these changes. The extra batch dimensions aren't a hassle in every function except in ray generation. The ray generation is where this operates different relative to the old cameras. The reason for this is before, for each ray we wanted to generate, we could specify a single index into our batch of cameras, but if you have multiple camera dimensions, for each ray, you need to now specify one index per camera batch dimension, meaning our camera_indices we specify need to have one extra dimension at the end, a dimension of size Cameras.ndim (number of batch dimensions in the camera object).
I also added some neat logic in generate_rays where if you want your output raybundle to be batched, if you specify batched camera_indices, you will receive the same batch size for the output raybundle, meaning you can easily generate raybundles corresponding to things like image patches and whatnot.

Phototourism changes:
The difficulty here is that images in the phototourism dataset are of different sizes, which is difficult because we can no longer batch the image tensors. We also need to better utilize caching in the CacheDataloader since the vanilladatamanager defaults don't really use it as well as they could, and without properly utilizing it, the rays / sec are maybe 25% of the standard nerfstudio dataset speeds (which are 10% of the size of the phototourism datasets). To do this, we specify a collate function in the existing datamanager configs, and we make a subclass of the vanilladatamanagerconfig named VariableResDataManagerConfig which will have a custom collate function storing the images in a list, and using better defaults for caching in the dataloader, caching ~40 images at a time. The slowest part of using a list is pixel sampling, where we added a custom if case for if our pixel sampler receives a batch of images that are in a list, and the lack of ability to vectorize operations over a list means that caching in the dataloader is even more important.
This commit is contained in:
Ethan Weber authored and GitHub committed 2022-11-25 22:16:11 -08:00
1 parent 1eae0839b4
commit b23d76cc47
17 files changed
+1708 -235

No files matched your search

+79 -11
View File
@@ -29,6 +29,8 @@ class TensorDataclass:
Fields that are not Tensors will not be batched unless they are also a TensorDataclass.
Any fields that are dictionaries will have their Tensors or TensorDataclasses batched, and
dictionaries will have their tensors or TensorDataclasses considered in the initial broadcast.
Tensor fields must have at least 1 dimension, meaning that you must convert a field like torch.Tensor(1)
to torch.Tensor([1])
Example:
@@ -56,12 +58,24 @@ class TensorDataclass:
_shape: tuple
# A mapping from field-name (str): n (int)
# Any field OR any key in a dictionary field with this name (field-name) and a corresponding
# torch.Tensor will be assumed to have n dimensions after the batch dims. These n final dimensions
# will remain the same shape when doing reshapes, broadcasting, etc on the tensordataclass
_field_custom_dimensions: Dict[str, int] = {}
def __post_init__(self) -> None:
"""Finishes setting up the TensorDataclass
This will 1) find the broadcasted shape and 2) broadcast all fields to this shape 3)
set _shape to be the broadcasted shape.
"""
if self._field_custom_dimensions is not None:
for k, v in self._field_custom_dimensions.items():
assert (
isinstance(v, int) and v > 1
), f"Custom dimensions must be an integer greater than 1, since 1 is the default, received {k}: {v}"
if not dataclasses.is_dataclass(self):
raise TypeError("TensorDataclass must be a dataclass")
@@ -90,9 +104,13 @@ class TensorDataclass:
The batch shapes of all tensors in the dictionary.
"""
batch_shapes = []
for v in dict_.values():
for k, v in dict_.items():
if isinstance(v, torch.Tensor):
batch_shapes.append(v.shape[:-1])
if isinstance(self._field_custom_dimensions, dict) and k in self._field_custom_dimensions:
# pylint: disable=unsubscriptable-object
batch_shapes.append(v.shape[: -self._field_custom_dimensions[k]])
else:
batch_shapes.append(v.shape[:-1])
elif isinstance(v, TensorDataclass):
batch_shapes.append(v.shape)
elif isinstance(v, Dict):
@@ -111,7 +129,17 @@ class TensorDataclass:
new_dict = {}
for k, v in dict_.items():
if isinstance(v, torch.Tensor):
new_dict[k] = v.broadcast_to((*batch_shape, v.shape[-1]))
# If custom dimension key, then we need to
if isinstance(self._field_custom_dimensions, dict) and k in self._field_custom_dimensions:
# pylint: disable=unsubscriptable-object
new_dict[k] = v.broadcast_to(
(
*batch_shape,
*v.shape[-self._field_custom_dimensions[k] :],
)
)
else:
new_dict[k] = v.broadcast_to((*batch_shape, v.shape[-1]))
elif isinstance(v, TensorDataclass):
new_dict[k] = v.broadcast_to(batch_shape)
elif isinstance(v, Dict):
@@ -119,18 +147,26 @@ class TensorDataclass:
return new_dict
def __getitem__(self: TensorDataclassT, indices) -> TensorDataclassT:
if isinstance(indices, torch.Tensor):
if isinstance(indices, (torch.Tensor)):
return self._apply_fn_to_fields(lambda x: x[indices])
if isinstance(indices, (int, slice)):
if isinstance(indices, (int, slice, type(Ellipsis))):
indices = (indices,)
assert isinstance(indices, tuple)
tensor_fn = lambda x: x[indices + (slice(None),)]
dataclass_fn = lambda x: x[indices]
return self._apply_fn_to_fields(tensor_fn, dataclass_fn)
def custom_tensor_dims_fn(k, v):
custom_dims = self._field_custom_dimensions[k] # pylint: disable=unsubscriptable-object
return v[indices + ((slice(None),) * custom_dims)]
return self._apply_fn_to_fields(tensor_fn, dataclass_fn, custom_tensor_dims_fn=custom_tensor_dims_fn)
def __setitem__(self, indices, value) -> NoReturn:
raise RuntimeError("Index assignment is not supported for TensorDataclass")
def __len__(self) -> int:
if len(self._shape) == 0:
raise TypeError("len() of a 0-d tensor")
return self.shape[0]
def __bool__(self) -> bool:
@@ -173,7 +209,12 @@ class TensorDataclass:
shape = (shape,)
tensor_fn = lambda x: x.reshape((*shape, x.shape[-1]))
dataclass_fn = lambda x: x.reshape(shape)
return self._apply_fn_to_fields(tensor_fn, dataclass_fn)
def custom_tensor_dims_fn(k, v):
custom_dims = self._field_custom_dimensions[k] # pylint: disable=unsubscriptable-object
return v.reshape((*shape, *v.shape[-custom_dims:]))
return self._apply_fn_to_fields(tensor_fn, dataclass_fn, custom_tensor_dims_fn=custom_tensor_dims_fn)
def flatten(self: TensorDataclassT) -> TensorDataclassT:
"""Returns a new TensorDataclass with flattened batch dimensions
@@ -195,7 +236,14 @@ class TensorDataclass:
Returns:
A new TensorDataclass with the same data but with a new shape.
"""
return self._apply_fn_to_fields(lambda x: x.broadcast_to((*shape, x.shape[-1])))
def custom_tensor_dims_fn(k, v):
custom_dims = self._field_custom_dimensions[k] # pylint: disable=unsubscriptable-object
return v.broadcast_to((*shape, *v.shape[-custom_dims:]))
return self._apply_fn_to_fields(
lambda x: x.broadcast_to((*shape, x.shape[-1])), custom_tensor_dims_fn=custom_tensor_dims_fn
)
def to(self: TensorDataclassT, device) -> TensorDataclassT:
"""Returns a new TensorDataclass with the same data but on the specified device.
@@ -209,7 +257,10 @@ class TensorDataclass:
return self._apply_fn_to_fields(lambda x: x.to(device))
def _apply_fn_to_fields(
self: TensorDataclassT, fn: Callable, dataclass_fn: Optional[Callable] = None
self: TensorDataclassT,
fn: Callable,
dataclass_fn: Optional[Callable] = None,
custom_tensor_dims_fn: Optional[Callable] = None,
) -> TensorDataclassT:
"""Applies a function to all fields of the tensor dataclass.
@@ -231,12 +282,21 @@ class TensorDataclass:
"""
new_fields = self._apply_fn_to_dict(
{f.name: self.__getattribute__(f.name) for f in dataclasses.fields(self)}, fn, dataclass_fn
{f.name: self.__getattribute__(f.name) for f in dataclasses.fields(self)},
fn,
dataclass_fn,
custom_tensor_dims_fn,
)
return dataclasses.replace(self, **new_fields)
def _apply_fn_to_dict(self, dict_: Dict, fn: Callable, dataclass_fn: Optional[Callable] = None) -> Dict:
def _apply_fn_to_dict(
self,
dict_: Dict,
fn: Callable,
dataclass_fn: Optional[Callable] = None,
custom_tensor_dims_fn: Optional[Callable] = None,
) -> Dict:
"""A helper function for _apply_fn_to_fields, applying a function to all fields of dict_
Args:
@@ -254,6 +314,14 @@ class TensorDataclass:
if v is not None:
if isinstance(v, TensorDataclass) and dataclass_fn is not None:
new_dict[f] = dataclass_fn(v)
# This is the case when we have a custom dimensions tensor
elif (
isinstance(v, torch.Tensor)
and isinstance(self._field_custom_dimensions, dict)
and f in self._field_custom_dimensions
and custom_tensor_dims_fn is not None
):
new_dict[f] = custom_tensor_dims_fn(f, v)
elif isinstance(v, (torch.Tensor, TensorDataclass)):
new_dict[f] = fn(v)
elif isinstance(v, Dict):