diff --git a/pyrad/nerf/dataset/colmap_utils.py b/pyrad/nerf/dataset/colmap_utils.py index d4db0304a..e0c7db998 100644 --- a/pyrad/nerf/dataset/colmap_utils.py +++ b/pyrad/nerf/dataset/colmap_utils.py @@ -72,11 +72,13 @@ CAMERA_MODEL_NAMES = dict([(camera_model.model_name, camera_model) for camera_mo def read_next_bytes(fid, num_bytes, format_char_sequence, endian_character="<"): """Read and unpack the next bytes from a binary file. - :param fid: - :param num_bytes: Sum of combination of {2, 4, 8}, e.g. 2, 6, 16, 30, etc. - :param format_char_sequence: List of {c, e, f, d, h, H, i, I, l, L, q, Q}. - :param endian_character: Any of {@, =, <, >, !} - :return: Tuple of read and unpacked values. + + Args: + fid: + num_bytes: Sum of combination of {2, 4, 8}, e.g. 2, 6, 16, 30, etc. + format_char_sequence: List of {c, e, f, d, h, H, i, I, l, L, q, Q}. + endian_character: Any of {@, =, <, >, !} + Tuple of read and unpacked values. """ data = fid.read(num_bytes) return struct.unpack(endian_character + format_char_sequence, data) @@ -84,12 +86,14 @@ def read_next_bytes(fid, num_bytes, format_char_sequence, endian_character="<"): def write_next_bytes(fid, data, format_char_sequence, endian_character="<"): """pack and write to a binary file. - :param fid: - :param data: data to send, if multiple elements are sent at the same time, - they should be encapsuled either in a list or a tuple - :param format_char_sequence: List of {c, e, f, d, h, H, i, I, l, L, q, Q}. - should be the same length as the data list or tuple - :param endian_character: Any of {@, =, <, >, !} + + Args: + fid: + data: data to send, if multiple elements are sent at the same time, + they should be encapsuled either in a list or a tuple + format_char_sequence: List of {c, e, f, d, h, H, i, I, l, L, q, Q}. + should be the same length as the data list or tuple + endian_character: Any of {@, =, <, >, !} """ if isinstance(data, (list, tuple)): bytes = struct.pack(endian_character + format_char_sequence, *data) diff --git a/pyrad/nerf/image_sampler.py b/pyrad/nerf/image_sampler.py index 18eadf3e4..adbd29f3d 100644 --- a/pyrad/nerf/image_sampler.py +++ b/pyrad/nerf/image_sampler.py @@ -1,8 +1,10 @@ """ -Code for sampling images. +Code for sampling images from a dataset of images. """ +from abc import abstractmethod import random +from typing import Union import torch from torch.utils.data import default_collate @@ -11,11 +13,13 @@ from pyrad.utils.misc import get_dict_to_torch class ImageSampler(torch.utils.data.IterableDataset): - """Samples 'image_batch's.""" + """Samples image_batch's.""" + @abstractmethod def __getitem__(self, idx): raise NotImplementedError() + @abstractmethod def __iter__(self): raise NotImplementedError() @@ -23,6 +27,12 @@ class ImageSampler(torch.utils.data.IterableDataset): class CacheImageSampler(ImageSampler): """Collated image dataset that implements caching of images. Creates batches of the ImageDataset return type. + + Args: + dataset (torch.utils.data.Dataset): Dataset to sample from. + num_samples_to_collate (int): How many images are used to sample rays for each batch. + num_times_to_repeat_images (int): How often to collate new images. Defaults to every iteration. + device (Union[torch.device, str]): Device to perform computation. Defaults to "cpu". """ def __init__( @@ -30,7 +40,7 @@ class CacheImageSampler(ImageSampler): dataset: torch.utils.data.Dataset, num_samples_to_collate: int, num_times_to_repeat_images: int = 0, - device="cpu", + device: Union[torch.device, str] = "cpu", ): super().__init__() self.dataset = dataset @@ -45,15 +55,15 @@ class CacheImageSampler(ImageSampler): def __getitem__(self, idx): return self.dataset.__getitem__(idx) - def get_batch_list(self): + def _get_batch_list(self): """Returns a list of batches from the dataset attribute.""" indices = random.sample(range(len(self.dataset)), k=self.num_samples_to_collate) batch_list = [self.dataset.__getitem__(idx) for idx in indices] return batch_list - def get_collated_batch(self): + def _get_collated_batch(self): """Returns a collated batch.""" - batch_list = self.get_batch_list() + batch_list = self._get_batch_list() collated_batch = default_collate(batch_list) collated_batch = get_dict_to_torch(collated_batch, device=self.device) return collated_batch @@ -65,7 +75,7 @@ class CacheImageSampler(ImageSampler): ): # trigger a reset self.num_repeated = 0 - collated_batch = self.get_collated_batch() + collated_batch = self._get_collated_batch() # possibly save a cached item self.cached_collated_batch = collated_batch if self.num_times_to_repeat_images != 0 else None self.first_time = False diff --git a/pyrad/nerf/renderers.py b/pyrad/nerf/renderers.py index 22d60a3bd..3e3db18f8 100644 --- a/pyrad/nerf/renderers.py +++ b/pyrad/nerf/renderers.py @@ -1,5 +1,16 @@ """ Collection of renderers + +Example: + +.. code-block:: python + + field_outputs = field(ray_sampler.to_point_samples()) + weights = ray_sampler.get_weights(field_outputs[FieldHeadNames.DENSITY]) + + rgb_renderer = RGBRenderer() + rgb = rgb_renderer(rgb=field_outputs[FieldHeadNames.RGB], weights=weights) + """ import math from typing import Optional