Update docs

This commit is contained in:
Matt Tancik authored and Matthew Tancik committed 2022-06-07 10:06:46 -07:00
1 parent 4234762b4b
commit 8582238322
3 files changed
+43 -18

No files matched your search

+15 -11
View File
@@ -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)
+17 -7
View File
@@ -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
+11
View File
@@ -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