Image dataset implemented and ray generation working. (#23)

* working data loader code

* adding gitignore

* fixing linting errors

* set black line length in settings file

* update

* fixed filepath

* black format

* tqdm
This commit is contained in:
Ethan Weber authored and GitHub committed 2022-04-17 09:26:44 -07:00
1 parent 9bc83b0ed2
commit e38040ee4d
23 files changed
+529 -277

No files matched your search

+5 -4
View File
@@ -62,7 +62,7 @@ class Camera:
"""_summary_"""
return
def get_image_coords(self) -> TensorType["image_height", "image_width", 2]:
def get_image_coords(self, pixel_offset: float = 0.5) -> TensorType["image_height", "image_width", 2]:
"""_summary_
Returns:
@@ -71,7 +71,7 @@ class Camera:
image_height = self.get_image_height()
image_width = self.get_image_width()
image_coords = torch.meshgrid(torch.arange(image_height), torch.arange(image_width), indexing="ij")
image_coords = torch.stack(image_coords, dim=-1)
image_coords = torch.stack(image_coords, dim=-1) + pixel_offset # stored as (y, x) coordinates
return image_coords
@classmethod
@@ -179,12 +179,13 @@ class PinholeCamera(Camera):
cy = intrinsics[:, 1:2]
fx = intrinsics[:, cls.fx_index() : cls.fx_index() + 1]
fy = intrinsics[:, cls.fy_index() : cls.fy_index() + 1]
y = coords[:, 0:1] # (num_rays, 1)
x = coords[:, 1:2] # (num_rays, 1)
directions = torch.cat([(x - cx) / fx, -(y - cy) / fy, -torch.ones_like(x)], -1) # (num_rays, 3)
rotation = camera_to_world[:, :3, :3] # (num_rays, 3, 3)
directions = torch.bmm(rotation, directions.unsqueeze(-1)).squeeze(-1) # (num_rays, 3)
directions = torch.sum(
directions[:, None, :] * rotation, dim=-1
) # (num_rays, 1, 3) * (num_rays, 3, 3) -> (num_rays, 3)
directions = normalize(directions, dim=-1)
origins = camera_to_world[:, :3, 3] # (num_rays, 3)
return RayBundle(origins=origins, directions=directions)
+33 -10
View File
@@ -1,20 +1,13 @@
"""
Some ray datastructures.
"""
import random
from dataclasses import dataclass
from typing import Optional
from torchtyping import TensorType
@dataclass
class CameraRayBundle:
"""_summary_"""
origins: TensorType["image_height", "image_width", 3]
directions: TensorType["image_height", "image_width", 3]
camera_index: int = None
@dataclass
class RayBundle:
"""_summary_
@@ -27,7 +20,7 @@ class RayBundle:
directions: TensorType["num_rays", 3]
camera_indices: Optional[TensorType["num_rays"]] = None
def to_camera_ray_bundle(self, image_height, image_width) -> CameraRayBundle:
def to_camera_ray_bundle(self, image_height, image_width) -> "CameraRayBundle":
"""_summary_
Args:
@@ -42,3 +35,33 @@ class RayBundle:
directions=self.directions.view(image_height, image_width, 3),
)
return camera_ray_bundle
def __len__(self):
num_rays = self.origins.shape[0]
return num_rays
def sample(self, num_rays: int):
"""Returns a RayBundle as a subset of rays.
Args:
num_rays (int):
Returns:
RayBundle: _description_
"""
assert num_rays <= len(self)
indices = random.sample(range(len(self)), k=num_rays)
return RayBundle(
origins=self.origins[indices],
directions=self.directions[indices],
camera_indices=self.camera_indices[indices],
)
@dataclass
class CameraRayBundle:
"""_summary_"""
origins: TensorType["image_height", "image_width", 3]
directions: TensorType["image_height", "image_width", 3]
camera_index: int = None