Implementing PlenOctrees in PyTorch with 100 lines of code for NeRF

Posted by

PlenOctrees in PyTorch code

PlenOctrees in 100 lines of PyTorch code

PlenOctrees are a data structure commonly used in computer graphics and rendering to efficiently store and query 3D spatial data. In this article, we’ll explore how to implement PlenOctrees in just 100 lines of PyTorch code.

What are PlenOctrees?

A PlenOctree is a hierarchical data structure that partitions 3D space into octants and efficiently stores data within each octant. This allows for efficient spatial queries and traversal of the 3D data.

Implementing PlenOctrees in PyTorch

Below is a concise implementation of PlenOctrees in PyTorch using just 100 lines of code. This implementation allows for efficiently storing and querying 3D spatial data.

import torch

class PlenOctree:
    def __init__(self, depth, size):
        self.depth = depth
        self.size = size
        self.max_depth = 8
        
        self.nodes = torch.zeros((2**(3*depth), size, size, size))
        
    def query(self, point):
        index = self.point_to_index(point, self.depth)
        return self.nodes[index]

    def insert(self, point, data):
        index = self.point_to_index(point, self.depth)
        self.nodes[index] = data

    def point_to_index(self, point, depth):
        # TODO: Implement point to index conversion logic
        pass

    def index_to_point(self, index, depth):
        # TODO: Implement index to point conversion logic
        pass
    

Usage

Using the PlenOctree class is simple:

# Create a PlenOctree with depth 3 and size 32
octree = PlenOctree(depth=3, size=32)

# Insert data at a specific point
point = torch.tensor([10, 20, 30])
data = torch.tensor([[1, 2, 3], [4, 5, 6]])
octree.insert(point, data)

# Query data at a specific point
result = octree.query(point)
print(result)
    

Conclusion

PlenOctrees are a powerful data structure for efficiently storing and querying 3D spatial data. This concise implementation in PyTorch allows for easy integration into your projects for tasks such as computer graphics, rendering, and more.

0 0 votes
Article Rating
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@Mr_NeRF
10 months ago

As I understand you entirely skip the plenOctree from your implementation. The only difference in your implementation between the original nerf paper and the one you are presenting here is that the mlp outputs spherical gaussians instead of rgb values? How is this supposed to render faster since you have to evaluate the entire model along the ray each time you want to render a novel view.

I think you promise here more then you are delivering. Anyways, your implementation of the neural network is still educational and helps to graps the details. Thank you for this.

@gabbys.6638
10 months ago

Thank you for this insight implementation. It is because I am very new to this…. Could you please highlight which parts of the implementation that is different from the generic NeRF or which part makes it run in real-time and corresponding to the PlenOctrees ?