mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-03 06:18:44 +08:00
Merge branch 'main' into fix_gener_mesh_export
This commit is contained in:
2 files changed
+12
-13
No files matched your search
@@ -17,13 +17,7 @@ Code for sampling pixels.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import random
|
import random
|
||||||
|
|
||||||
import torch
|
|
||||||
from jaxtyping import Int
|
|
||||||
from torch import Tensor
|
|
||||||
|
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from nerfstudio.data.utils.pixel_sampling_utils import erode_mask
|
|
||||||
from typing import (
|
from typing import (
|
||||||
Dict,
|
Dict,
|
||||||
Optional,
|
Optional,
|
||||||
@@ -31,9 +25,14 @@ from typing import (
|
|||||||
Union,
|
Union,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
import torch
|
||||||
|
from jaxtyping import Int
|
||||||
|
from torch import Tensor
|
||||||
|
|
||||||
from nerfstudio.configs.base_config import (
|
from nerfstudio.configs.base_config import (
|
||||||
InstantiateConfig,
|
InstantiateConfig,
|
||||||
)
|
)
|
||||||
|
from nerfstudio.data.utils.pixel_sampling_utils import erode_mask
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@@ -398,19 +397,18 @@ class PairPixelSampler(PixelSampler): # pylint: disable=too-few-public-methods
|
|||||||
device: Union[torch.device, str] = "cpu",
|
device: Union[torch.device, str] = "cpu",
|
||||||
) -> Int[Tensor, "batch_size 3"]:
|
) -> Int[Tensor, "batch_size 3"]:
|
||||||
rays_to_sample = self.rays_to_sample
|
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})"
|
||||||
|
rays_to_sample = batch_size // 2
|
||||||
|
|
||||||
if isinstance(mask, Tensor):
|
if isinstance(mask, Tensor):
|
||||||
m = erode_mask(mask.permute(0, 3, 1, 2).float(), pixel_radius=self.radius)
|
m = erode_mask(mask.permute(0, 3, 1, 2).float(), pixel_radius=self.radius)
|
||||||
nonzero_indices = torch.nonzero(m[:, 0], as_tuple=False).to(device)
|
nonzero_indices = torch.nonzero(m[:, 0], as_tuple=False).to(device)
|
||||||
chosen_indices = random.sample(range(len(nonzero_indices)), k=rays_to_sample)
|
chosen_indices = random.sample(range(len(nonzero_indices)), k=rays_to_sample)
|
||||||
indices = nonzero_indices[chosen_indices]
|
indices = nonzero_indices[chosen_indices]
|
||||||
else:
|
else:
|
||||||
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})"
|
|
||||||
rays_to_sample = batch_size // 2
|
|
||||||
|
|
||||||
s = (rays_to_sample, 1)
|
s = (rays_to_sample, 1)
|
||||||
ns = torch.randint(0, num_images, s, dtype=torch.long, device=device)
|
ns = torch.randint(0, num_images, s, dtype=torch.long, device=device)
|
||||||
hs = torch.randint(self.radius, image_height - self.radius, s, dtype=torch.long, device=device)
|
hs = torch.randint(self.radius, image_height - self.radius, s, dtype=torch.long, device=device)
|
||||||
|
|||||||
@@ -464,6 +464,7 @@ class Trainer:
|
|||||||
|
|
||||||
self.optimizers.zero_grad_all()
|
self.optimizers.zero_grad_all()
|
||||||
cpu_or_cuda_str: str = self.device.split(":")[0]
|
cpu_or_cuda_str: str = self.device.split(":")[0]
|
||||||
|
cpu_or_cuda_str = "cpu" if cpu_or_cuda_str == "mps" else cpu_or_cuda_str
|
||||||
assert (
|
assert (
|
||||||
self.gradient_accumulation_steps > 0
|
self.gradient_accumulation_steps > 0
|
||||||
), f"gradient_accumulation_steps must be > 0, not {self.gradient_accumulation_steps}"
|
), f"gradient_accumulation_steps must be > 0, not {self.gradient_accumulation_steps}"
|
||||||
|
|||||||
Reference in new issue
Block a user