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

+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