mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-07 00:09:14 +08:00
resizing image
This commit is contained in:
1 parent
f090d9a001
commit
5099a4f7c5
11 files changed
+374
-45
No files matched your search
@@ -6,6 +6,7 @@ from typing import List
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
import PIL
|
||||
from PIL import Image
|
||||
from torch.utils.data import default_collate
|
||||
|
||||
@@ -15,8 +16,9 @@ from mattport.utils.misc import DotDict
|
||||
class ImageDataset(torch.utils.data.Dataset):
|
||||
"""Dataset that returns images."""
|
||||
|
||||
def __init__(self, image_filenames: List[str], downscale_factor: float = 1.0):
|
||||
def __init__(self, image_filenames: List[str], downscale_factor: int = 1):
|
||||
super().__init__()
|
||||
assert isinstance(downscale_factor, int)
|
||||
self.image_filenames = image_filenames
|
||||
self.downscale_factor = downscale_factor
|
||||
|
||||
@@ -33,7 +35,21 @@ class ImageDataset(torch.utils.data.Dataset):
|
||||
np.uint8: an image of shape (H, W, 3 or 4)
|
||||
"""
|
||||
image_filename = self.image_filenames[image_idx]
|
||||
image = np.array(Image.open(image_filename)) # shape is (h, w, 3 or 4)
|
||||
pil_image = Image.open(image_filename)
|
||||
if self.downscale_factor != 1.0:
|
||||
image_width, image_height = pil_image.size
|
||||
if image_width % self.downscale_factor != 0:
|
||||
raise ValueError(
|
||||
f"Image width {image_width} is not divisible by downscale_factor {self.downscale_factor}"
|
||||
)
|
||||
if image_height % self.downscale_factor != 0:
|
||||
raise ValueError(
|
||||
f"Image height {image_height} is not divisible by downscale_factor {self.downscale_factor}"
|
||||
)
|
||||
pil_image = pil_image.resize(
|
||||
(image_width // self.downscale_factor, image_height // self.downscale_factor), PIL.Image.BILINEAR
|
||||
)
|
||||
image = np.array(pil_image) # shape is (h, w, 3 or 4)
|
||||
assert len(image.shape) == 3
|
||||
assert image.shape[2] in [3, 4], f"Image shape of {image.shape} is in correct."
|
||||
return image
|
||||
|
||||
@@ -62,7 +62,7 @@ def load_blender_data(basedir, downscale_factor=1.0, split="train"):
|
||||
dataset_inputs = DatasetInputs(
|
||||
image_filenames=image_filenames,
|
||||
downscale_factor=downscale_factor,
|
||||
intrinsics=intrinsics,
|
||||
intrinsics=intrinsics * 1.0 / downscale_factor, # downscaling the intrinsics here
|
||||
camera_to_world=camera_to_world,
|
||||
)
|
||||
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
"""
|
||||
Multi Layer Perceptron
|
||||
"""
|
||||
from typing import Optional
|
||||
from tkinter import X
|
||||
from typing import List, Optional
|
||||
|
||||
import torch
|
||||
from torch import nn
|
||||
from torchtyping import TensorType
|
||||
|
||||
from mattport.nerf.field_modules.base import FieldModule
|
||||
|
||||
|
||||
@@ -16,7 +19,8 @@ class MLP(FieldModule):
|
||||
in_dim: int,
|
||||
out_dim: int,
|
||||
num_layers: int,
|
||||
layer_width: int,
|
||||
layer_width: int = None,
|
||||
skip_connections: List[int] = [],
|
||||
activation: Optional[nn.Module] = nn.ReLU(),
|
||||
out_activation: Optional[nn.Module] = None,
|
||||
) -> None:
|
||||
@@ -36,26 +40,28 @@ class MLP(FieldModule):
|
||||
self.out_dim = out_dim
|
||||
self.num_layers = num_layers
|
||||
self.layer_width = layer_width
|
||||
self.skip_connections = skip_connections
|
||||
self.activation = activation
|
||||
self.out_activation = out_activation
|
||||
self.net = None
|
||||
self.build_nn_modules()
|
||||
|
||||
def build_nn_modules(self) -> None:
|
||||
"""Initialize mulilayer perceptron."""
|
||||
"""Initialize multi-layer perceptron."""
|
||||
layers = []
|
||||
for i in range(self.num_layers):
|
||||
if i == 0:
|
||||
layers.append(nn.Linear(self.in_dim, self.layer_width))
|
||||
else:
|
||||
layers.append(nn.Linear(self.layer_width, self.layer_width))
|
||||
if self.activation is not None:
|
||||
layers.append(self.activation)
|
||||
layers.append(nn.Linear(self.layer_width, self.out_dim))
|
||||
if self.out_activation is not None:
|
||||
layers.append(self.out_activation)
|
||||
|
||||
self.net = nn.Sequential(*layers)
|
||||
if self.num_layers == 1:
|
||||
layers.append(nn.Linear(self.in_dim, self.out_dim))
|
||||
else:
|
||||
for i in range(self.num_layers - 1):
|
||||
if i == 0:
|
||||
assert i not in self.skip_connections, "Skip connection at layer 0 doesn't make sense."
|
||||
layers.append(nn.Linear(self.in_dim, self.layer_width))
|
||||
elif i in self.skip_connections:
|
||||
layers.append(nn.Linear(self.layer_width + self.in_dim, self.layer_width))
|
||||
else:
|
||||
layers.append(nn.Linear(self.layer_width, self.layer_width))
|
||||
layers.append(nn.Linear(self.layer_width, self.out_dim))
|
||||
self.layers = nn.ModuleList(layers)
|
||||
|
||||
def forward(self, in_tensor: TensorType[..., "in_dim"]) -> TensorType[..., "out_dim"]:
|
||||
"""Process input with a multilayer perceptron.
|
||||
@@ -66,5 +72,11 @@ class MLP(FieldModule):
|
||||
Returns:
|
||||
TensorType[..., "out_dim"]: Network output
|
||||
"""
|
||||
|
||||
return self.net(in_tensor)
|
||||
x = in_tensor
|
||||
for i, layer in enumerate(self.layers):
|
||||
if i in self.skip_connections:
|
||||
x = torch.cat([in_tensor, x], -1)
|
||||
x = layer(x)
|
||||
if self.activation:
|
||||
x = self.activation(x)
|
||||
return x
|
||||
@@ -34,7 +34,7 @@ class Node:
|
||||
class Graph(nn.Module):
|
||||
"""_summary_"""
|
||||
|
||||
def __init__(self, intrinsics=None, camera_to_world=None) -> None:
|
||||
def __init__(self, intrinsics=None, camera_to_world=None, **kwargs) -> None:
|
||||
super().__init__()
|
||||
self.intrinsics = intrinsics
|
||||
self.camera_to_world = camera_to_world
|
||||
|
||||
@@ -24,18 +24,26 @@ from mattport.structures.rays import RaySamples
|
||||
class NeRFField(nn.Module):
|
||||
"""NeRF module"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
def __init__(self, num_layers=8, layer_width=256, skip_connections=[4]) -> None:
|
||||
super().__init__()
|
||||
self.num_layers = num_layers
|
||||
self.layer_width = layer_width
|
||||
self.skip_connections = skip_connections
|
||||
|
||||
self.encoding_xyz = NeRFEncoding(in_dim=3, num_frequencies=10, min_freq_exp=0.0, max_freq_exp=8.0)
|
||||
self.encoding_dir = NeRFEncoding(in_dim=3, num_frequencies=6, min_freq_exp=0.0, max_freq_exp=4.0)
|
||||
self.encoding_dir = NeRFEncoding(in_dim=3, num_frequencies=4, min_freq_exp=0.0, max_freq_exp=4.0)
|
||||
self.mlp_base = MLP(
|
||||
in_dim=self.encoding_xyz.get_out_dim(), out_dim=64, num_layers=8, layer_width=64, activation=nn.ReLU()
|
||||
in_dim=self.encoding_xyz.get_out_dim(),
|
||||
out_dim=self.layer_width,
|
||||
num_layers=self.num_layers,
|
||||
layer_width=self.layer_width,
|
||||
skip_connections=self.skip_connections,
|
||||
activation=nn.ReLU(),
|
||||
)
|
||||
self.mlp_rgb = MLP(
|
||||
in_dim=self.mlp_base.get_out_dim() + self.encoding_dir.get_out_dim(),
|
||||
out_dim=64,
|
||||
num_layers=2,
|
||||
layer_width=64,
|
||||
out_dim=self.layer_width // 2,
|
||||
num_layers=1,
|
||||
activation=nn.ReLU(),
|
||||
)
|
||||
self.field_output_rgb = RGBFieldHead(in_dim=self.mlp_rgb.get_out_dim())
|
||||
@@ -67,16 +75,31 @@ class NeRFField(nn.Module):
|
||||
class NeRFGraph(Graph):
|
||||
"""Vanilla NeRF graph"""
|
||||
|
||||
def __init__(self, intrinsics=None, camera_to_world=None) -> None:
|
||||
super().__init__(intrinsics=intrinsics, camera_to_world=camera_to_world)
|
||||
def __init__(
|
||||
self,
|
||||
intrinsics=None,
|
||||
camera_to_world=None,
|
||||
near_plane=1.0,
|
||||
far_plane=6.0,
|
||||
num_coarse_samples=64,
|
||||
num_importance_samples=128,
|
||||
**kwargs
|
||||
) -> None:
|
||||
self.near_plane = near_plane
|
||||
self.far_plane = far_plane
|
||||
self.num_coarse_samples = num_coarse_samples
|
||||
self.num_importance_samples = num_importance_samples
|
||||
super().__init__(intrinsics=intrinsics, camera_to_world=camera_to_world, **kwargs)
|
||||
|
||||
def populate_modules(self):
|
||||
# ray generator
|
||||
self.ray_generator = RayGenerator(self.intrinsics, self.camera_to_world)
|
||||
|
||||
# samplers
|
||||
self.sampler_uniform = UniformSampler(near_plane=0.1, far_plane=6.0, num_samples=64)
|
||||
self.sampler_pdf = PDFSampler(num_samples=64)
|
||||
self.sampler_uniform = UniformSampler(
|
||||
near_plane=self.near_plane, far_plane=self.far_plane, num_samples=self.num_coarse_samples
|
||||
)
|
||||
self.sampler_pdf = PDFSampler(num_samples=self.num_importance_samples)
|
||||
|
||||
# field
|
||||
self.field_coarse = NeRFField()
|
||||
@@ -95,7 +118,7 @@ class NeRFGraph(Graph):
|
||||
Dict[str, List[Parameter]]: Mapping of different parameter groups
|
||||
"""
|
||||
param_groups = {}
|
||||
param_groups["cameras"] = list(self.ray_generator.parameters())
|
||||
# param_groups["cameras"] = list(self.ray_generator.parameters())
|
||||
param_groups["fields"] = list(self.field_coarse.parameters()) + list(self.field_fine.parameters())
|
||||
return param_groups
|
||||
|
||||
|
||||
@@ -137,7 +137,7 @@ class Trainer:
|
||||
batch = next(iter(self.train_dataloader))
|
||||
loss_dict = self.train_iteration(batch, step)
|
||||
if step != 0 and step % self.config.steps_per_log == 0:
|
||||
self.tensorboard_writer.write_scalar_dict(loss_dict, step, group="train")
|
||||
self.tensorboard_writer.write_scalar_dict(loss_dict, step, group="Loss", prefix="train-")
|
||||
logging.info(f"{step}/{num_iterations} with losses {loss_dict}")
|
||||
# TODO: add the learning rates to tensorboard/logging
|
||||
if self.config.steps_per_save and step != 0 and step % self.config.steps_per_save == 0:
|
||||
|
||||
@@ -56,7 +56,7 @@ class Writer:
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def write_scalar_dict(self, scalar_dict: Dict[str, float], step: int, group: str = None) -> None:
|
||||
def write_scalar_dict(self, scalar_dict: Dict[str, float], step: int, group: str = None, prefix: str = None) -> None:
|
||||
"""Function that writes out all scalars from a given dictionary to the logger
|
||||
|
||||
Args:
|
||||
@@ -64,7 +64,7 @@ class Writer:
|
||||
"""
|
||||
if self.is_main_thread:
|
||||
for name, scalar in scalar_dict.items():
|
||||
self.write_scalar(name, scalar, step, group=group)
|
||||
self.write_scalar(name, scalar, step, group=group, prefix=prefix)
|
||||
|
||||
|
||||
class TensorboardWriter(Writer):
|
||||
@@ -97,7 +97,7 @@ class TensorboardWriter(Writer):
|
||||
self.tb_writer.add_text(name, x)
|
||||
|
||||
@check_main_thread
|
||||
def write_scalar(self, name: str, scalar: float, step: float, group: str = None) -> None:
|
||||
def write_scalar(self, name: str, scalar: float, step: float, group: str = None, prefix: str = None) -> None:
|
||||
"""Tensorboard method to write a single scalar value to the logger
|
||||
|
||||
Args:
|
||||
@@ -106,8 +106,9 @@ class TensorboardWriter(Writer):
|
||||
y (float): y value to write
|
||||
group (str)): a prefix to group tensorboard scalars
|
||||
"""
|
||||
prefix = f"{group}/" if group else ""
|
||||
self.tb_writer.add_scalar(f"{prefix}{name}", scalar, step)
|
||||
group_s = f"{group}/" if group else ""
|
||||
prefix_s = f"{prefix}" if prefix else ""
|
||||
self.tb_writer.add_scalar(f"{group_s}{prefix_s}{name}", scalar, step)
|
||||
|
||||
|
||||
class LocalWriter(Writer):
|
||||
|
||||
Reference in new issue
Block a user