Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
kerrj authored Jan 13, 2025
2 parents 65c106c + 66cd681 commit 3ec5ce1
Show file tree
Hide file tree
Showing 29 changed files with 92 additions and 92 deletions.
2 changes: 1 addition & 1 deletion docs/nerfology/model_components/visualize_encoders.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@
"encoded_values = torch.moveaxis(encoded_values, 2, 0)\n",
"\n",
"for level in range(levels):\n",
" print(f\"Level: {level+1}\")\n",
" print(f\"Level: {level + 1}\")\n",
" media.show_images(encoded_values[level**2 : (level + 1) ** 2, ...], cmap=\"plasma\", border=True)"
]
}
Expand Down
18 changes: 9 additions & 9 deletions nerfstudio/cameras/cameras.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ def _init_get_camera_type(
elif isinstance(camera_type, int):
camera_type = torch.tensor([camera_type], device=self.device)
elif isinstance(camera_type, torch.Tensor):
assert not torch.is_floating_point(
camera_type
), f"camera_type tensor must be of type int, not: {camera_type.dtype}"
assert not torch.is_floating_point(camera_type), (
f"camera_type tensor must be of type int, not: {camera_type.dtype}"
)
camera_type = camera_type.to(self.device)
if camera_type.ndim == 0 or camera_type.shape[-1] != 1:
camera_type = camera_type.unsqueeze(-1)
Expand Down Expand Up @@ -400,14 +400,14 @@ def generate_rays(

# If the camera indices are an int, then we need to make sure that the camera batch is 1D
if isinstance(camera_indices, int):
assert (
len(cameras.shape) == 1
), "camera_indices must be a tensor if cameras are batched with more than 1 batch dimension"
assert len(cameras.shape) == 1, (
"camera_indices must be a tensor if cameras are batched with more than 1 batch dimension"
)
camera_indices = torch.tensor([camera_indices], device=cameras.device)

assert camera_indices.shape[-1] == len(
cameras.shape
), "camera_indices must have shape (num_rays:..., num_cameras_batch_dims)"
assert camera_indices.shape[-1] == len(cameras.shape), (
"camera_indices must have shape (num_rays:..., num_cameras_batch_dims)"
)

# If keep_shape is True, then we need to make sure that the camera indices in question
# are all the same height and width and can actually be batched while maintaining the image
Expand Down
4 changes: 2 additions & 2 deletions nerfstudio/data/datamanagers/full_images_datamanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ def undistort_idx(idx: int) -> Dict[str, torch.Tensor]:
data = dataset.get_data(idx, image_type=self.config.cache_images_type)
camera = dataset.cameras[idx].reshape(())
assert data["image"].shape[1] == camera.width.item() and data["image"].shape[0] == camera.height.item(), (
f'The size of image ({data["image"].shape[1]}, {data["image"].shape[0]}) loaded '
f'does not match the camera parameters ({camera.width.item(), camera.height.item()})'
f"The size of image ({data['image'].shape[1]}, {data['image'].shape[0]}) loaded "
f"does not match the camera parameters ({camera.width.item(), camera.height.item()})"
)
if camera.distortion_params is None or torch.all(camera.distortion_params == 0):
return data
Expand Down
2 changes: 1 addition & 1 deletion nerfstudio/data/dataparsers/dycheck_dataparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def downscale(img, scale: int) -> np.ndarray:
return img
height, width = img.shape[:2]
if height % scale > 0 or width % scale > 0:
raise ValueError(f"Image shape ({height},{width}) must be divisible by the" f" scale ({scale}).")
raise ValueError(f"Image shape ({height},{width}) must be divisible by the scale ({scale}).")
out_height, out_width = height // scale, width // scale
resized = cv2.resize(img, (out_width, out_height), cv2.INTER_AREA) # type: ignore
return resized
Expand Down
2 changes: 1 addition & 1 deletion nerfstudio/data/dataparsers/nerfstudio_dataparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ def _get_fname(self, filepath: Path, data_dir: Path, downsample_folder_prefix="i
while True:
if (max_res / 2 ** (df)) <= MAX_AUTO_RESOLUTION:
break
if not (data_dir / f"{downsample_folder_prefix}{2**(df+1)}" / filepath.name).exists():
if not (data_dir / f"{downsample_folder_prefix}{2 ** (df + 1)}" / filepath.name).exists():
break
df += 1

Expand Down
6 changes: 3 additions & 3 deletions nerfstudio/data/dataparsers/nuscenes_dataparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ def _generate_dataparser_outputs(self, split="train"):
)
cameras = ["CAM_" + camera for camera in self.config.cameras]

assert (
len(cameras) == 1
), "waiting on multiple camera support" # TODO: remove once multiple cameras are supported
assert len(cameras) == 1, (
"waiting on multiple camera support"
) # TODO: remove once multiple cameras are supported

# get samples for scene
samples = [
Expand Down
6 changes: 3 additions & 3 deletions nerfstudio/data/datasets/base_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ def get_data(self, image_idx: int, image_type: Literal["uint8", "float32"] = "fl
if self._dataparser_outputs.mask_filenames is not None:
mask_filepath = self._dataparser_outputs.mask_filenames[image_idx]
data["mask"] = get_image_mask_tensor_from_path(filepath=mask_filepath, scale_factor=self.scale_factor)
assert (
data["mask"].shape[:2] == data["image"].shape[:2]
), f"Mask and image have different shapes. Got {data['mask'].shape[:2]} and {data['image'].shape[:2]}"
assert data["mask"].shape[:2] == data["image"].shape[:2], (
f"Mask and image have different shapes. Got {data['mask'].shape[:2]} and {data['image'].shape[:2]}"
)
if self.mask_color:
data["image"] = torch.where(
data["mask"] == 1.0, data["image"], torch.ones_like(data["image"]) * torch.tensor(self.mask_color)
Expand Down
6 changes: 3 additions & 3 deletions nerfstudio/data/pixel_samplers.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,9 +556,9 @@ def sample_method( # pylint: disable=no-self-use
) -> Int[Tensor, "batch_size 3"]:
rays_to_sample = self.rays_to_sample
if batch_size is not None:
assert (
int(batch_size) % 2 == 0
), f"PairPixelSampler can only return batch sizes in multiples of two (got {batch_size})"
assert int(batch_size) % 2 == 0, (
f"PairPixelSampler can only return batch sizes in multiples of two (got {batch_size})"
)
rays_to_sample = batch_size // 2

if isinstance(mask, Tensor) and not self.config.ignore_mask:
Expand Down
12 changes: 7 additions & 5 deletions nerfstudio/data/utils/nerfstudio_collate.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from nerfstudio.cameras.cameras import Cameras

NERFSTUDIO_COLLATE_ERR_MSG_FORMAT = (
"default_collate: batch must contain tensors, numpy arrays, numbers, " "dicts, lists or anything in {}; found {}"
"default_collate: batch must contain tensors, numpy arrays, numbers, dicts, lists or anything in {}; found {}"
)
np_str_obj_array_pattern = re.compile(r"[SaUO]")

Expand Down Expand Up @@ -152,14 +152,16 @@ def nerfstudio_collate(batch: Any, extra_mappings: Union[Dict[type, Callable], N
assert all((isinstance(cam, Cameras) for cam in batch))
assert all((cam.distortion_params is None for cam in batch)) or all(
(cam.distortion_params is not None for cam in batch)
), "All cameras must have distortion parameters or none of them should have distortion parameters.\
), (
"All cameras must have distortion parameters or none of them should have distortion parameters.\
Generalized batching will be supported in the future."
)

if batch[0].metadata is not None:
metadata_keys = batch[0].metadata.keys()
assert all(
(cam.metadata.keys() == metadata_keys for cam in batch)
), "All cameras must have the same metadata keys."
assert all((cam.metadata.keys() == metadata_keys for cam in batch)), (
"All cameras must have the same metadata keys."
)
else:
assert all((cam.metadata is None for cam in batch)), "All cameras must have the same metadata keys."

Expand Down
6 changes: 3 additions & 3 deletions nerfstudio/engine/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ def __init__(
args: Optional[List] = None,
kwargs: Optional[Dict] = None,
):
assert (
"step" in signature(func).parameters.keys()
), f"'step: int' must be an argument in the callback function 'func': {func.__name__}"
assert "step" in signature(func).parameters.keys(), (
f"'step: int' must be an argument in the callback function 'func': {func.__name__}"
)
self.where_to_run = where_to_run
self.update_every_num_iters = update_every_num_iters
self.iters = iters
Expand Down
6 changes: 3 additions & 3 deletions nerfstudio/exporter/exporter_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ def generate_point_cloud(
CONSOLE.print(f"Please set --normal_output_name to one of: {outputs.keys()}", justify="center")
sys.exit(1)
normal = outputs[normal_output_name]
assert (
torch.min(normal) >= 0.0 and torch.max(normal) <= 1.0
), "Normal values from method output must be in [0, 1]"
assert torch.min(normal) >= 0.0 and torch.max(normal) <= 1.0, (
"Normal values from method output must be in [0, 1]"
)
normal = (normal * 2.0) - 1.0
point = ray_bundle.origins + ray_bundle.directions * depth
view_direction = ray_bundle.directions
Expand Down
6 changes: 3 additions & 3 deletions nerfstudio/field_components/encodings.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,9 @@ def __init__(
)

if self.tcnn_encoding is None:
assert (
interpolation is None or interpolation == "Linear"
), f"interpolation '{interpolation}' is not supported for torch encoding backend"
assert interpolation is None or interpolation == "Linear", (
f"interpolation '{interpolation}' is not supported for torch encoding backend"
)

def build_nn_modules(self) -> None:
"""Initialize the torch version of the hash encoding."""
Expand Down
6 changes: 3 additions & 3 deletions nerfstudio/fields/base_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ def get_normals(self) -> Float[Tensor, "*batch 3"]:
"""
assert self._sample_locations is not None, "Sample locations must be set before calling get_normals."
assert self._density_before_activation is not None, "Density must be set before calling get_normals."
assert (
self._sample_locations.shape[:-1] == self._density_before_activation.shape[:-1]
), "Sample locations and density must have the same shape besides the last dimension."
assert self._sample_locations.shape[:-1] == self._density_before_activation.shape[:-1], (
"Sample locations and density must have the same shape besides the last dimension."
)

normals = torch.autograd.grad(
self._density_before_activation,
Expand Down
6 changes: 3 additions & 3 deletions nerfstudio/model_components/ray_samplers.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,9 @@ def generate_ray_samples(
u = u.expand(size=(*cdf.shape[:-1], num_bins))
u = u.contiguous()

assert (
ray_samples.spacing_starts is not None and ray_samples.spacing_ends is not None
), "ray_sample spacing_starts and spacing_ends must be provided"
assert ray_samples.spacing_starts is not None and ray_samples.spacing_ends is not None, (
"ray_sample spacing_starts and spacing_ends must be provided"
)
assert ray_samples.spacing_to_euclidean_fn is not None, "ray_samples.spacing_to_euclidean_fn must be provided"
existing_bins = torch.cat(
[
Expand Down
6 changes: 3 additions & 3 deletions nerfstudio/models/base_surface_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ def get_outputs(self, ray_bundle: RayBundle) -> Dict[str, torch.Tensor]:
Returns:
Outputs of model. (ie. rendered colors)
"""
assert (
ray_bundle.metadata is not None and "directions_norm" in ray_bundle.metadata
), "directions_norm is required in ray_bundle.metadata"
assert ray_bundle.metadata is not None and "directions_norm" in ray_bundle.metadata, (
"directions_norm is required in ray_bundle.metadata"
)

samples_and_field_outputs = self.sample_and_forward_field(ray_bundle=ray_bundle)

Expand Down
6 changes: 3 additions & 3 deletions nerfstudio/models/tensorf.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,9 @@ def get_loss_dict(self, outputs, batch, metrics_dict=None) -> Dict[str, torch.Te
elif self.config.regularization == "tv":
density_plane_coef = self.field.density_encoding.plane_coef
color_plane_coef = self.field.color_encoding.plane_coef
assert isinstance(color_plane_coef, torch.Tensor) and isinstance(
density_plane_coef, torch.Tensor
), "TV reg only supported for TensoRF encoding types with plane_coef attribute"
assert isinstance(color_plane_coef, torch.Tensor) and isinstance(density_plane_coef, torch.Tensor), (
"TV reg only supported for TensoRF encoding types with plane_coef attribute"
)
loss_dict["tv_reg_density"] = tv_loss(density_plane_coef)
loss_dict["tv_reg_color"] = tv_loss(color_plane_coef)
elif self.config.regularization == "none":
Expand Down
6 changes: 3 additions & 3 deletions nerfstudio/pipelines/base_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,9 @@ def get_average_eval_image_metrics(
self, step: Optional[int] = None, output_path: Optional[Path] = None, get_std: bool = False
):
"""Get the average metrics for evaluation images."""
assert hasattr(
self.datamanager, "fixed_indices_eval_dataloader"
), "datamanager must have 'fixed_indices_eval_dataloader' attribute"
assert hasattr(self.datamanager, "fixed_indices_eval_dataloader"), (
"datamanager must have 'fixed_indices_eval_dataloader' attribute"
)
image_prefix = "eval"
return self.get_average_image_metrics(
self.datamanager.fixed_indices_eval_dataloader, image_prefix, step, output_path, get_std
Expand Down
6 changes: 3 additions & 3 deletions nerfstudio/pipelines/dynamic_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ def __init__(
grad_scaler: Optional[GradScaler] = None,
):
super().__init__(config, device, test_mode, world_size, local_rank)
assert isinstance(
self.datamanager, VanillaDataManager
), "DynamicBatchPipeline only works with VanillaDataManager."
assert isinstance(self.datamanager, VanillaDataManager), (
"DynamicBatchPipeline only works with VanillaDataManager."
)

self.dynamic_num_rays_per_batch = self.config.target_num_samples // self.config.max_num_samples_per_ray
self._update_pixel_samplers()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,7 @@ def _save_transforms(
summary_log.append(colmap_utils.get_matching_summary(num_frames, num_matched_frames))

else:
CONSOLE.log(
"[bold yellow]Warning: Could not find existing COLMAP results. " "Not generating transforms.json"
)
CONSOLE.log("[bold yellow]Warning: Could not find existing COLMAP results. Not generating transforms.json")
return summary_log

def _export_depth(self) -> Tuple[Optional[Dict[int, Path]], List[str]]:
Expand Down Expand Up @@ -224,7 +222,7 @@ def _run_colmap(self, mask_path: Optional[Path] = None):
)
elif sfm_tool == "hloc":
if mask_path is not None:
raise RuntimeError("Cannot use a mask with hloc. Please remove the cropping options " "and try again.")
raise RuntimeError("Cannot use a mask with hloc. Please remove the cropping options and try again.")

assert feature_type is not None
assert matcher_type is not None
Expand All @@ -241,7 +239,7 @@ def _run_colmap(self, mask_path: Optional[Path] = None):
use_single_camera_mode=self.use_single_camera_mode,
)
else:
raise RuntimeError("Invalid combination of sfm_tool, feature_type, and matcher_type, " "exiting")
raise RuntimeError("Invalid combination of sfm_tool, feature_type, and matcher_type, exiting")

def __post_init__(self) -> None:
super().__post_init__()
Expand Down
4 changes: 2 additions & 2 deletions nerfstudio/process_data/polycam_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ def polycam_to_json(
frame["cy"] = frame_json["cy"] - crop_border_pixels
frame["w"] = frame_json["width"] - crop_border_pixels * 2
frame["h"] = frame_json["height"] - crop_border_pixels * 2
frame["file_path"] = f"./images/frame_{i+1:05d}{image_filename.suffix}"
frame["file_path"] = f"./images/frame_{i + 1:05d}{image_filename.suffix}"
if use_depth:
frame["depth_file_path"] = f"./depth/frame_{i+1:05d}{depth_filenames[i].suffix}"
frame["depth_file_path"] = f"./depth/frame_{i + 1:05d}{depth_filenames[i].suffix}"
# Transform matrix to nerfstudio format. Please refer to the documentation for coordinate system conventions.
frame["transform_matrix"] = [
[frame_json["t_20"], frame_json["t_21"], frame_json["t_22"], frame_json["t_23"]],
Expand Down
4 changes: 2 additions & 2 deletions nerfstudio/process_data/process_data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def copy_images_list(

crop_cmd = ""
if crop_border_pixels is not None:
crop_cmd = f"crop=iw-{crop_border_pixels*2}:ih-{crop_border_pixels*2}[cropped];[cropped]"
crop_cmd = f"crop=iw-{crop_border_pixels * 2}:ih-{crop_border_pixels * 2}[cropped];[cropped]"
elif crop_factor != (0.0, 0.0, 0.0, 0.0):
height = 1 - crop_factor[0] - crop_factor[1]
width = 1 - crop_factor[2] - crop_factor[3]
Expand Down Expand Up @@ -484,7 +484,7 @@ def downscale_images(
run_command(ffmpeg_cmd, verbose=verbose)

CONSOLE.log("[bold green]:tada: Done downscaling images.")
downscale_text = [f"[bold blue]{2**(i+1)}x[/bold blue]" for i in range(num_downscales)]
downscale_text = [f"[bold blue]{2 ** (i + 1)}x[/bold blue]" for i in range(num_downscales)]
downscale_text = ", ".join(downscale_text[:-1]) + " and " + downscale_text[-1]
return f"We downsampled the images by {downscale_text}"

Expand Down
24 changes: 12 additions & 12 deletions nerfstudio/scripts/downloads/download_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,9 @@ def download(self, save_dir: Path):
PhototourismDownload(capture_name=capture_name).download(save_dir)
return

assert (
self.capture_name in phototourism_downloads
), f"Capture name {self.capture_name} not found in {phototourism_downloads.keys()}"
assert self.capture_name in phototourism_downloads, (
f"Capture name {self.capture_name} not found in {phototourism_downloads.keys()}"
)
url = phototourism_downloads[self.capture_name]
target_path = str(save_dir / "phototourism" / self.capture_name)
os.makedirs(target_path, exist_ok=True)
Expand Down Expand Up @@ -350,9 +350,9 @@ def download(self, save_dir: Path):
SDFstudioDemoDownload(dataset_name=dataset_name).download(save_dir)
return

assert (
self.dataset_name in sdfstudio_downloads
), f"Capture name {self.dataset_name} not found in {sdfstudio_downloads.keys()}"
assert self.dataset_name in sdfstudio_downloads, (
f"Capture name {self.dataset_name} not found in {sdfstudio_downloads.keys()}"
)

url = sdfstudio_downloads[self.dataset_name]

Expand Down Expand Up @@ -421,9 +421,9 @@ def download(self, save_dir: Path):
NeRFOSRDownload(capture_name=capture_name).download(save_dir)
return

assert (
self.capture_name in nerfosr_downloads
), f"Capture name {self.capture_name} not found in {nerfosr_downloads.keys()}"
assert self.capture_name in nerfosr_downloads, (
f"Capture name {self.capture_name} not found in {nerfosr_downloads.keys()}"
)
url = nerfosr_downloads[self.capture_name]
target_path = str(save_dir / "nerfosr" / self.capture_name)
os.makedirs(target_path, exist_ok=True)
Expand Down Expand Up @@ -475,9 +475,9 @@ def download(self, save_dir: Path) -> None:
Mill19Download(capture_name=capture_name).download(save_dir)
return

assert (
self.capture_name in mill19_downloads
), f"Capture name {self.capture_name} not found in {mill19_downloads.keys()}"
assert self.capture_name in mill19_downloads, (
f"Capture name {self.capture_name} not found in {mill19_downloads.keys()}"
)
url = mill19_downloads[self.capture_name]
target_path = save_dir / "mill19" / self.capture_name
target_path.mkdir(parents=True, exist_ok=True)
Expand Down
Loading

0 comments on commit 3ec5ce1

Please sign in to comment.