mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-27 11:30:11 +08:00
refactored dataset names + multi-gpu running (#321)
* refactored dataset names + multi-gpu running * update comments + docs
This commit is contained in:
1 parent
bb2a528eb4
commit
b0f6d75cdf
16 files changed
+310
-304
No files matched your search
@@ -24,12 +24,16 @@ from typing import Dict, List, Optional, Tuple, Union
|
||||
import torch
|
||||
from torch import nn
|
||||
from torch.nn import Parameter
|
||||
from torch.utils.data.distributed import DistributedSampler
|
||||
|
||||
from nerfactory.cameras.rays import RayBundle
|
||||
from nerfactory.configs import base as cfg
|
||||
from nerfactory.dataloaders.eval import EvalDataloader, FixedIndicesEvalDataloader
|
||||
from nerfactory.dataloaders.image_dataloader import (
|
||||
CacheImageDataloader,
|
||||
EvalDataloader,
|
||||
FixedIndicesEvalDataloader,
|
||||
)
|
||||
from nerfactory.dataloaders.image_dataset import ImageDataset, PanopticImageDataset
|
||||
from nerfactory.dataloaders.image_sampler import CacheImageSampler
|
||||
from nerfactory.dataloaders.pixel_sampler import PixelSampler
|
||||
from nerfactory.dataloaders.structs import DatasetInputs
|
||||
from nerfactory.models.modules.ray_generator import RayGenerator
|
||||
@@ -37,17 +41,17 @@ from nerfactory.utils.callbacks import TrainingCallback, TrainingCallbackAttribu
|
||||
from nerfactory.utils.misc import IterableWrapper
|
||||
|
||||
|
||||
class Dataloader(nn.Module):
|
||||
"""Generic dataloader's abstract class
|
||||
class DataManager(nn.Module):
|
||||
"""Generic data manager's abstract class
|
||||
|
||||
This version of the dataloader is designed be a monolithic way to load data and latents,
|
||||
This version of the data manager is designed be a monolithic way to load data and latents,
|
||||
especially since this may contain learnable parameters which need to be shared across the train
|
||||
and test dataloaders. The idea is that we have setup methods for train and eval separately and
|
||||
and test data managers. The idea is that we have setup methods for train and eval separately and
|
||||
this can be a combined train/eval if you want.
|
||||
|
||||
Usage:
|
||||
To get data, use the next_train and next_eval functions.
|
||||
This dataloader's next_train and next_eval methods will return 2 things:
|
||||
This data manager's next_train and next_eval methods will return 2 things:
|
||||
1. A Raybundle: This will contain the rays we are sampling, with latents and
|
||||
conditionals attached (everything needed at inference)
|
||||
2. A "batch" of auxilury information: This will contain the mask, the ground truth
|
||||
@@ -72,8 +76,8 @@ class Dataloader(nn.Module):
|
||||
get_eval_iterable: utility that gets a clean pythonic iterator for your eval data
|
||||
|
||||
Args:
|
||||
use_train: whether this dataloader is being used for training
|
||||
use_eval: whether this dataloader is being used for evaluation
|
||||
use_train: whether this data manager is being used for training
|
||||
use_eval: whether this data manager is being used for evaluation
|
||||
|
||||
Attributes:
|
||||
use_train (bool): whether or not we are using train
|
||||
@@ -95,9 +99,9 @@ class Dataloader(nn.Module):
|
||||
eval_dataloader: Optional[EvalDataloader] = None
|
||||
|
||||
def __init__(self, use_train: bool, use_eval: bool):
|
||||
"""Constructor for the Dataloader class.
|
||||
"""Constructor for the DataManager class.
|
||||
|
||||
Subclassed Dataloaders will likely need to override this constructor.
|
||||
Subclassed DataManagers will likely need to override this constructor.
|
||||
|
||||
If you aren't manually calling the setup_train and setup_eval functions from an overriden
|
||||
constructor, that you call super().__init__(use_train, use_eval) BEFORE you initialize any
|
||||
@@ -140,10 +144,10 @@ class Dataloader(nn.Module):
|
||||
as __iter__ and __next__ methods respectivley.
|
||||
|
||||
This basically is just a little utility if you want to do something like:
|
||||
| for ray_bundle, batch in dataloader.get_train_iterable():
|
||||
| for ray_bundle, batch in data_manager.get_train_iterable():
|
||||
| <eval code here>
|
||||
since the returned IterableWrapper is just an iterator with the __iter__ and __next__
|
||||
methods (methods bound to our Dataloader instance in this case) specified in the constructor.
|
||||
methods (methods bound to our DataManager instance in this case) specified in the constructor.
|
||||
"""
|
||||
return IterableWrapper(self.iter_train, self.next_train, length)
|
||||
|
||||
@@ -152,38 +156,38 @@ class Dataloader(nn.Module):
|
||||
as __iter__ and __next__ methods respectivley.
|
||||
|
||||
This basically is just a little utility if you want to do something like:
|
||||
| for ray_bundle, batch in dataloader.get_eval_iterable():
|
||||
| for ray_bundle, batch in data_manager.get_eval_iterable():
|
||||
| <eval code here>
|
||||
since the returned IterableWrapper is just an iterator with the __iter__ and __next__
|
||||
methods (methods bound to our Dataloader instance in this case) specified in the constructor.
|
||||
methods (methods bound to our DataManager instance in this case) specified in the constructor.
|
||||
"""
|
||||
return IterableWrapper(self.iter_eval, self.next_eval, length)
|
||||
|
||||
@abstractmethod
|
||||
def setup_train(self):
|
||||
"""Sets up the dataloader for training.
|
||||
"""Sets up the data manager for training.
|
||||
|
||||
Here you will define any subclass specific object attributes from the attribute"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def setup_eval(self):
|
||||
"""Sets up the dataloader for evaluation"""
|
||||
"""Sets up the data manager for evaluation"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def next_train(self) -> Tuple:
|
||||
"""Returns the next batch of data from the train dataloader.
|
||||
"""Returns the next batch of data from the train data manager.
|
||||
|
||||
This will be a tuple of all the information that this dataloader outputs.
|
||||
This will be a tuple of all the information that this data manager outputs.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def next_eval(self) -> Tuple:
|
||||
"""Returns the next batch of data from the eval dataloader.
|
||||
"""Returns the next batch of data from the eval data manager.
|
||||
|
||||
This will be a tuple of all the information that this dataloader outputs.
|
||||
This will be a tuple of all the information that this data manager outputs.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@@ -195,16 +199,16 @@ class Dataloader(nn.Module):
|
||||
|
||||
@abstractmethod
|
||||
def get_param_groups(self) -> Dict[str, List[Parameter]]: # pylint: disable=no-self-use
|
||||
"""Get the param groups for the dataloader.
|
||||
"""Get the param groups for the data manager.
|
||||
|
||||
Returns:
|
||||
A list of dictionaries containing the dataloader's param groups.
|
||||
A list of dictionaries containing the data manager's param groups.
|
||||
"""
|
||||
return {}
|
||||
|
||||
|
||||
class VanillaDataloader(Dataloader): # pylint: disable=abstract-method
|
||||
"""Basic stored dataloader implementation.
|
||||
class VanillaDataManager(DataManager): # pylint: disable=abstract-method
|
||||
"""Basic stored data manager implementation.
|
||||
|
||||
This is pretty much a port over from our old dataloading utilities, and is a little jank
|
||||
under the hood. We may clean this up a little bit under the hood with more standard dataloading
|
||||
@@ -212,17 +216,22 @@ class VanillaDataloader(Dataloader): # pylint: disable=abstract-method
|
||||
only the constructor is likely to change in the future, or maybe passing in step number to the
|
||||
next_train and next_eval functions."""
|
||||
|
||||
config: cfg.VanillaDataloaderConfig
|
||||
config: cfg.VanillaDataManagerConfig
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config: cfg.VanillaDataloaderConfig,
|
||||
config: cfg.VanillaDataManagerConfig,
|
||||
device: Union[torch.device, str] = "cpu",
|
||||
test_mode: bool = False,
|
||||
world_size: int = 1,
|
||||
local_rank: int = 0,
|
||||
**kwargs, # pylint: disable=unused-argument
|
||||
):
|
||||
self.config = config
|
||||
self.device = device
|
||||
self.world_size = world_size
|
||||
self.local_rank = local_rank
|
||||
self.sampler = None
|
||||
|
||||
dataset_train = config.train_dataset.setup()
|
||||
self.train_datasetinputs = dataset_train.get_dataset_inputs(split="train")
|
||||
@@ -239,43 +248,68 @@ class VanillaDataloader(Dataloader): # pylint: disable=abstract-method
|
||||
super().__init__(use_train, use_eval)
|
||||
|
||||
def setup_train(self):
|
||||
"""Sets up the dataloader for training"""
|
||||
"""Sets up the data manager for training"""
|
||||
assert self.train_datasetinputs is not None
|
||||
if self.config.image_dataset_type == "rgb":
|
||||
self.train_image_dataset = ImageDataset(**self.train_datasetinputs.as_dict())
|
||||
self.train_image_dataset = ImageDataset(
|
||||
image_filenames=self.train_datasetinputs.image_filenames,
|
||||
downscale_factor=self.train_datasetinputs.downscale_factor,
|
||||
alpha_color=self.train_datasetinputs.alpha_color,
|
||||
)
|
||||
elif self.config.image_dataset_type == "panoptic":
|
||||
self.train_image_dataset = PanopticImageDataset(**self.train_datasetinputs.as_dict())
|
||||
else:
|
||||
raise ValueError(f"Unknown image dataset type {self.config.image_dataset_type}")
|
||||
self.train_image_sampler = CacheImageSampler(
|
||||
self.train_image_dataset,
|
||||
num_images_to_sample_from=self.config.train_num_images_to_sample_from,
|
||||
device=self.device,
|
||||
) # TODO(ethan): pass this in
|
||||
self.iter_train_image_sampler = iter(self.train_image_sampler)
|
||||
|
||||
if self.world_size > 1:
|
||||
self.train_image_dataloader = CacheImageDataloader(
|
||||
self.train_image_dataset,
|
||||
num_images_to_sample_from=self.config.train_num_images_to_sample_from,
|
||||
device=self.device,
|
||||
num_workers=self.world_size * 4,
|
||||
pin_memory=True,
|
||||
) # TODO(ethan): pass this in
|
||||
else:
|
||||
self.train_image_dataloader = CacheImageDataloader(
|
||||
self.train_image_dataset,
|
||||
num_images_to_sample_from=self.config.train_num_images_to_sample_from,
|
||||
device=self.device,
|
||||
)
|
||||
self.iter_train_image_dataloader = iter(self.train_image_dataloader)
|
||||
self.train_pixel_sampler = PixelSampler(self.config.train_num_rays_per_batch)
|
||||
self.train_ray_generator = RayGenerator(self.train_cameras)
|
||||
|
||||
def setup_eval(self):
|
||||
"""Sets up the dataloader for evaluation"""
|
||||
"""Sets up the data manager for evaluation"""
|
||||
assert self.eval_datasetinputs is not None
|
||||
if self.config.image_dataset_type == "rgb":
|
||||
self.eval_image_dataset = ImageDataset(**self.eval_datasetinputs.as_dict())
|
||||
self.eval_image_dataset = ImageDataset(
|
||||
image_filenames=self.eval_datasetinputs.image_filenames,
|
||||
downscale_factor=self.eval_datasetinputs.downscale_factor,
|
||||
alpha_color=self.eval_datasetinputs.alpha_color,
|
||||
)
|
||||
elif self.config.image_dataset_type == "panoptic":
|
||||
self.eval_image_dataset = PanopticImageDataset(**self.eval_datasetinputs.as_dict())
|
||||
|
||||
if self.world_size > 1:
|
||||
self.sampler = DistributedSampler(
|
||||
self.train_image_dataset, num_replicas=self.world_size, rank=self.local_rank, shuffle=True, seed=42
|
||||
)
|
||||
|
||||
self.eval_dataloader = FixedIndicesEvalDataloader(
|
||||
image_dataset=self.eval_image_dataset,
|
||||
cameras=self.eval_cameras,
|
||||
num_rays_per_chunk=self.config.eval_num_rays_per_chunk,
|
||||
image_indices=self.config.eval_image_indices,
|
||||
device=self.device,
|
||||
num_workers=0 if self.world_size == 1 else self.world_size * 4,
|
||||
)
|
||||
|
||||
def next_train(self) -> Tuple[RayBundle, Dict]:
|
||||
"""Returns the next batch of data from the train dataloader.
|
||||
The RayBundle can be shaped in whatever way."""
|
||||
self.train_count += 1
|
||||
image_batch = next(self.iter_train_image_sampler)
|
||||
image_batch = next(self.iter_train_image_dataloader)
|
||||
batch = self.train_pixel_sampler.sample(image_batch)
|
||||
ray_indices = batch["indices"]
|
||||
ray_bundle = self.train_ray_generator(ray_indices)
|
||||
|
||||
@@ -52,10 +52,10 @@ from nerfactory.utils.misc import get_hash_str_from_dict
|
||||
|
||||
|
||||
@dataclass
|
||||
class Dataset:
|
||||
class DataParser:
|
||||
"""A dataset."""
|
||||
|
||||
def __init__(self, config: cfg.DatasetConfig):
|
||||
def __init__(self, config: cfg.DataParserConfig):
|
||||
super().__init__()
|
||||
self.config = config
|
||||
|
||||
@@ -130,7 +130,7 @@ class Dataset:
|
||||
|
||||
|
||||
@dataclass
|
||||
class Blender(Dataset):
|
||||
class Blender(DataParser):
|
||||
"""Blender Dataset
|
||||
Some of this code comes from https://github.com/yenchenlin/nerf-pytorch/blob/master/load_blender.py#L37.
|
||||
|
||||
@@ -141,7 +141,7 @@ class Blender(Dataset):
|
||||
downscale_factor: How much to downscale images. Defaults to 1.
|
||||
"""
|
||||
|
||||
def __init__(self, config: cfg.BlenderDatasetConfig):
|
||||
def __init__(self, config: cfg.BlenderDataParserConfig):
|
||||
super().__init__(config=config)
|
||||
self.data_directory: Path = config.data_directory
|
||||
self.scale_factor: float = config.scale_factor
|
||||
@@ -200,7 +200,7 @@ class Blender(Dataset):
|
||||
|
||||
|
||||
@dataclass
|
||||
class InstantNGP(Dataset):
|
||||
class InstantNGP(DataParser):
|
||||
"""Instant NGP Dataset
|
||||
|
||||
Args:
|
||||
@@ -210,7 +210,7 @@ class InstantNGP(Dataset):
|
||||
scene_scale: How much to scale the scene. Defaults to 0.33
|
||||
"""
|
||||
|
||||
config: cfg.InstantNGPDatasetConfig
|
||||
config: cfg.InstantNGPDataParserConfig
|
||||
|
||||
def _generate_dataset_inputs(self, split="train"):
|
||||
|
||||
@@ -288,7 +288,7 @@ class InstantNGP(Dataset):
|
||||
|
||||
|
||||
@dataclass
|
||||
class Mipnerf360(Dataset):
|
||||
class Mipnerf360(DataParser):
|
||||
"""MipNeRF 360 Dataset
|
||||
|
||||
Args:
|
||||
@@ -299,7 +299,7 @@ class Mipnerf360(Dataset):
|
||||
aabb_scale: Scene scale, Defaults to 1.0.
|
||||
"""
|
||||
|
||||
config: cfg.MipNerf360DatasetConfig
|
||||
config: cfg.MipNerf360DataParserConfig
|
||||
|
||||
@classmethod
|
||||
def normalize_orientation(cls, poses: np.ndarray):
|
||||
@@ -412,7 +412,7 @@ class Mipnerf360(Dataset):
|
||||
|
||||
|
||||
@dataclass
|
||||
class Record3D(Dataset):
|
||||
class Record3D(DataParser):
|
||||
"""Record3D Dataset
|
||||
|
||||
Args:
|
||||
@@ -424,7 +424,7 @@ class Record3D(Dataset):
|
||||
more, images will be sampled approximately evenly. Defaults to 150.
|
||||
"""
|
||||
|
||||
config: cfg.Record3DDatasetConfig
|
||||
config: cfg.Record3DDataParserConfig
|
||||
|
||||
def _generate_dataset_inputs(self, split: str = "train") -> DatasetInputs:
|
||||
abs_dir = get_absolute_path(self.config.data_directory)
|
||||
@@ -534,7 +534,7 @@ class Record3D(Dataset):
|
||||
|
||||
|
||||
@dataclass
|
||||
class Friends(Dataset):
|
||||
class Friends(DataParser):
|
||||
"""Friends Dataset
|
||||
|
||||
Args:
|
||||
@@ -13,7 +13,7 @@
|
||||
# limitations under the License.
|
||||
|
||||
"""
|
||||
Data loader.
|
||||
Code for sampling images from a dataset of images.
|
||||
"""
|
||||
|
||||
import random
|
||||
@@ -21,6 +21,8 @@ from abc import abstractmethod
|
||||
from typing import Dict, Optional, Tuple, Union
|
||||
|
||||
import torch
|
||||
from torch.utils.data import default_collate
|
||||
from torch.utils.data.dataloader import DataLoader
|
||||
|
||||
from nerfactory.cameras.cameras import Cameras
|
||||
from nerfactory.cameras.rays import RayBundle
|
||||
@@ -28,7 +30,76 @@ from nerfactory.dataloaders.image_dataset import ImageDataset
|
||||
from nerfactory.utils.misc import get_dict_to_torch
|
||||
|
||||
|
||||
class EvalDataloader:
|
||||
class CacheImageDataloader(DataLoader):
|
||||
"""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, optional): How many images to sample rays for each batch. Defaults to all images.
|
||||
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__(
|
||||
self,
|
||||
dataset: ImageDataset,
|
||||
num_images_to_sample_from: int = -1,
|
||||
num_times_to_repeat_images: int = 0,
|
||||
device: Union[torch.device, str] = "cpu",
|
||||
**kwargs,
|
||||
):
|
||||
self.dataset = dataset
|
||||
self.num_times_to_repeat_images = num_times_to_repeat_images
|
||||
self.cache_all_images = num_images_to_sample_from == -1
|
||||
self.num_images_to_sample_from = len(self.dataset) if self.cache_all_images else num_images_to_sample_from
|
||||
self.device = device
|
||||
|
||||
self.num_repeated = self.num_times_to_repeat_images # starting value
|
||||
self.first_time = True
|
||||
|
||||
self.cached_collated_batch = None
|
||||
if self.cache_all_images:
|
||||
self.cached_collated_batch = self._get_collated_batch()
|
||||
super().__init__(dataset=dataset, **kwargs)
|
||||
|
||||
def __getitem__(self, idx):
|
||||
return self.dataset.__getitem__(idx)
|
||||
|
||||
def _get_batch_list(self):
|
||||
"""Returns a list of batches from the dataset attribute."""
|
||||
indices = random.sample(range(len(self.dataset)), k=self.num_images_to_sample_from)
|
||||
|
||||
batch_list = [self.dataset.__getitem__(idx) for idx in indices]
|
||||
return batch_list
|
||||
|
||||
def _get_collated_batch(self):
|
||||
"""Returns a collated batch."""
|
||||
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
|
||||
|
||||
def __iter__(self):
|
||||
while True:
|
||||
if self.cache_all_images:
|
||||
collated_batch = self.cached_collated_batch
|
||||
elif self.first_time or (
|
||||
self.num_times_to_repeat_images != -1 and self.num_repeated >= self.num_times_to_repeat_images
|
||||
):
|
||||
# trigger a reset
|
||||
self.num_repeated = 0
|
||||
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
|
||||
else:
|
||||
collated_batch = self.cached_collated_batch
|
||||
self.num_repeated += 1
|
||||
yield collated_batch
|
||||
|
||||
|
||||
class EvalDataloader(DataLoader):
|
||||
"""Evaluation dataloader base class
|
||||
|
||||
Args:
|
||||
@@ -46,12 +117,12 @@ class EvalDataloader:
|
||||
device: Union[torch.device, str] = "cpu",
|
||||
**kwargs,
|
||||
):
|
||||
super().__init__()
|
||||
self.image_dataset = image_dataset
|
||||
self.cameras = cameras
|
||||
self.num_rays_per_chunk = num_rays_per_chunk
|
||||
self.device = device
|
||||
self.kwargs = kwargs
|
||||
super().__init__(dataset=image_dataset)
|
||||
|
||||
@abstractmethod
|
||||
def __iter__(self):
|
||||
@@ -39,7 +39,6 @@ class ImageDataset(Dataset):
|
||||
image_filenames: List[Path],
|
||||
downscale_factor: int = 1,
|
||||
alpha_color: Optional[TensorType[3]] = None,
|
||||
**kwargs,
|
||||
):
|
||||
"""_summary_
|
||||
|
||||
@@ -53,7 +52,6 @@ class ImageDataset(Dataset):
|
||||
self.image_filenames = image_filenames
|
||||
self.downscale_factor = downscale_factor
|
||||
self.alpha_color = alpha_color
|
||||
self.kwargs = kwargs
|
||||
|
||||
def __len__(self):
|
||||
return len(self.image_filenames)
|
||||
|
||||
@@ -1,116 +0,0 @@
|
||||
# Copyright 2022 The Plenoptix Team. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
"""
|
||||
Code for sampling images from a dataset of images.
|
||||
"""
|
||||
|
||||
import random
|
||||
from abc import abstractmethod
|
||||
from typing import Union
|
||||
|
||||
import torch
|
||||
from torch.utils.data import IterableDataset, default_collate
|
||||
|
||||
from nerfactory.dataloaders.image_dataset import ImageDataset
|
||||
from nerfactory.utils.misc import get_dict_to_torch
|
||||
|
||||
|
||||
class ImageSampler(IterableDataset):
|
||||
"""Samples image_batch's.
|
||||
|
||||
Attributes:
|
||||
dataset (torch.utils.data.Dataset): Dataset to sample from.
|
||||
"""
|
||||
|
||||
# dataset: ImageDataset
|
||||
def __init__(self, dataset: ImageDataset):
|
||||
super().__init__()
|
||||
self.dataset = dataset
|
||||
|
||||
@abstractmethod
|
||||
def __getitem__(self, idx):
|
||||
raise NotImplementedError()
|
||||
|
||||
@abstractmethod
|
||||
def __iter__(self):
|
||||
raise NotImplementedError()
|
||||
|
||||
|
||||
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, optional): How many images to sample rays for each batch. Defaults to all images.
|
||||
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__(
|
||||
self,
|
||||
dataset: ImageDataset,
|
||||
num_images_to_sample_from: int = -1,
|
||||
num_times_to_repeat_images: int = 0,
|
||||
device: Union[torch.device, str] = "cpu",
|
||||
):
|
||||
super().__init__(dataset=dataset)
|
||||
self.dataset = dataset
|
||||
self.cache_all_images = num_images_to_sample_from == -1
|
||||
self.num_images_to_sample_from = len(self.dataset) if self.cache_all_images else num_images_to_sample_from
|
||||
self.num_times_to_repeat_images = num_times_to_repeat_images
|
||||
self.device = device
|
||||
|
||||
self.num_repeated = self.num_times_to_repeat_images # starting value
|
||||
self.first_time = True
|
||||
|
||||
self.cached_collated_batch = None
|
||||
if self.cache_all_images:
|
||||
self.cached_collated_batch = self._get_collated_batch()
|
||||
|
||||
def __getitem__(self, idx):
|
||||
return self.dataset.__getitem__(idx)
|
||||
|
||||
def _get_batch_list(self):
|
||||
"""Returns a list of batches from the dataset attribute."""
|
||||
indices = random.sample(range(len(self.dataset)), k=self.num_images_to_sample_from)
|
||||
|
||||
batch_list = [self.dataset.__getitem__(idx) for idx in indices]
|
||||
return batch_list
|
||||
|
||||
def _get_collated_batch(self):
|
||||
"""Returns a collated batch."""
|
||||
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
|
||||
|
||||
def __iter__(self):
|
||||
while True:
|
||||
if self.cache_all_images:
|
||||
collated_batch = self.cached_collated_batch
|
||||
elif self.first_time or (
|
||||
self.num_times_to_repeat_images != -1 and self.num_repeated >= self.num_times_to_repeat_images
|
||||
):
|
||||
# trigger a reset
|
||||
self.num_repeated = 0
|
||||
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
|
||||
else:
|
||||
collated_batch = self.cached_collated_batch
|
||||
self.num_repeated += 1
|
||||
yield collated_batch
|
||||
Reference in new issue
Block a user