pylint and test cases for dataloader

This commit is contained in:
Ethan Weber committed 2022-04-23 19:45:53 +00:00
1 parent 391803759e
commit 80ad17700c
10 files changed
+94 -69

No files matched your search

+29 -19
View File
@@ -4,10 +4,10 @@ Code to test ray generation.
import imageio
import torch
from hydra import compose, initialize
from torch.utils.data import DataLoader
from tqdm import tqdm
from mattport.nerf.dataset.collate import CollateIterDataset, collate_batch_size_one
from mattport.nerf.dataset.image_dataset import ImageDataset, collate_batch
from mattport.nerf.dataset.utils import get_dataset_inputs
from mattport.nerf.field_modules.ray_generator import RayGenerator
@@ -19,11 +19,11 @@ def visualize_batch(batch):
"""Returns an image from a batch."""
assert "image" in batch, "You must call collate_batch with keep_full_image=True for this to work."
# set the color of the sampled rays
c, y, x = [i.flatten() for i in torch.split(batch.indices, 1, dim=-1)]
batch.image[c, y, x] = 1.0
c, y, x = [i.flatten() for i in torch.split(batch["local_indices"], 1, dim=-1)]
batch["image"][c, y, x] = 1.0
# batch.image is num_images, h, w, 3
images = torch.split(batch.image, 1, dim=0)
# batch["image"] is num_images, h, w, 3
images = torch.split(batch["image"], 1, dim=0)
image_list = [image[0] for image in images]
image = torch.cat(image_list, dim=1) # cat along the width dimension
image = (image * 255.0).to(torch.uint8)
@@ -33,23 +33,33 @@ def visualize_batch(batch):
def test_dataloader():
"""Testing for the dataloader from input dataset parameters to rays."""
with initialize(config_path="configs"):
cfg = compose(config_name="blender_lego.yaml")
data_directory = "data/blender/lego"
dataset_type = "blender"
downscale_factor = 1
num_images_to_sample_from = 8
num_times_to_repeat_images = 40
num_rays_per_batch = 1024
num_workers = 0
cfg.dataset.data_directory = get_absolute_path(
cfg.dataset.data_directory
) # TODO(ethan): create a helper function for this
dataset_inputs = get_dataset_inputs(**cfg.dataset)
dataset = ImageDataset(
data_directory = get_absolute_path(data_directory)
dataset_inputs = get_dataset_inputs(
data_directory=data_directory, dataset_type=dataset_type, downscale_factor=downscale_factor
)
image_dataset = ImageDataset(
image_filenames=dataset_inputs.image_filenames, downscale_factor=dataset_inputs.downscale_factor
)
iter_dataset = CollateIterDataset(
image_dataset,
collate_fn=lambda batch_list: collate_batch(batch_list, num_rays_per_batch, keep_full_image=True),
num_samples_to_collate=num_images_to_sample_from,
num_times_to_repeat=num_times_to_repeat_images,
)
dataloader = DataLoader(
dataset,
batch_size=cfg.dataloader.num_images_to_sample_from,
collate_fn=lambda batch: collate_batch(batch, cfg.dataloader.num_rays_per_batch, keep_full_image=True),
num_workers=cfg.dataloader.num_workers,
shuffle=True,
iter_dataset,
batch_size=1,
num_workers=num_workers,
collate_fn=collate_batch_size_one,
pin_memory=True,
)
dataloader_iter = iter(dataloader)
@@ -58,7 +68,7 @@ def test_dataloader():
num_batches = 10
for _ in tqdm(range(num_batches)):
batch = next(dataloader_iter)
ray_bundle = ray_generator.forward(batch.indices)
ray_bundle = ray_generator.forward(batch["indices"])
# visualize the batch
image = visualize_batch(batch)