octree-nn / ocnn-pytorch

Octree-based Sparse Convolutional Neural Networks
MIT License
150 stars 16 forks source link

AttributeError: type object 'Octree' has no attribute 'nnum' #5

Closed samanemami closed 1 year ago

samanemami commented 1 year ago

Considering the following written ResNet class;


import torch
import ocnn
from ocnn.octree import Octree

class ResNet(torch.nn.Module):
    r''' Octree-based ResNet for classification.
    '''

    def __init__(self, in_channels: int, out_channels: int, resblock_num: int,
                 stages: int, nempty: bool = False):
        super().__init__()
        self.in_channels = in_channels
        self.out_channels = out_channels
        self.resblk_num = resblock_num
        self.stages = stages
        self.nempty = nempty
        channels = [2 ** max(i+9-stages, 2) for i in range(stages)]

        self.conv1 = ocnn.modules.OctreeConvBnRelu(
            in_channels, channels[0], nempty=nempty)
        self.pool1 = ocnn.nn.OctreeMaxPool(nempty)
        self.resblocks = torch.nn.ModuleList([ocnn.modules.OctreeResBlocks(
            channels[i], channels[i+1], resblock_num, nempty=nempty)
            for i in range(stages-1)])
        self.pools = torch.nn.ModuleList([ocnn.nn.OctreeMaxPool(
            nempty) for i in range(stages-1)])
        self.global_pool = ocnn.nn.OctreeGlobalPool(nempty)
        # self.header = torch.nn.Linear(channels[-1], out_channels, bias=True)
        self.header = torch.nn.Sequential(
            ocnn.modules.FcBnRelu(channels[-1], 512),
            torch.nn.Dropout(p=0.5),
            torch.nn.Linear(512, out_channels))

    def forward(self, data: torch.Tensor, octree: Octree, depth: int):
        r''''''

        data = self.conv1(data, octree, depth)
        data = self.pool1(data, octree, depth)
        for i in range(self.stages-1):
            d = depth - i - 1
            data = self.resblocks[i](data, octree, d)
            data = self.pools[i](data, octree, d)
        data = self.global_pool(data, octree, depth-self.stages)
        data = self.header(data)
        return data

and the input ply file with the shape of torch.Size([841348, 3]). And the calling ResNet is as follows;

from ocnn.octree import Octree

model = ResNet(in_channels=3, out_channels=3, resblock_num=1,stages=2)
model.forward(data=tensor_cloud, octree= Octree, depth=8)

We will have the following AttributeError;

AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_22/1135799818.py in <module>
      2 
      3 model = ResNet(in_channels=3, out_channels=3, resblock_num=1,stages=2)
----> 4 model.forward(data=tensor_cloud, octree= Octree, depth=8)

/tmp/ipykernel_22/2884427206.py in forward(self, data, octree, depth)
     36         r''''''
     37 
---> 38         data = self.conv1(data, octree, depth)
     39         data = self.pool1(data, octree, depth)
     40         for i in range(self.stages-1):

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1108         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1109                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110             return forward_call(*input, **kwargs)
   1111         # Do not call functions when jit is used
   1112         full_backward_hooks, non_full_backward_hooks = [], []

/opt/conda/lib/python3.7/site-packages/ocnn/modules/modules.py in forward(self, data, octree, depth)
     70     r''''''
     71 
---> 72     out = self.conv(data, octree, depth)
     73     out = self.bn(out)
     74     out = self.relu(out)

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
   1108         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1109                 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110             return forward_call(*input, **kwargs)
   1111         # Do not call functions when jit is used
   1112         full_backward_hooks, non_full_backward_hooks = [], []

/opt/conda/lib/python3.7/site-packages/ocnn/nn/octree_conv.py in forward(self, data, octree, depth)
    366           data, self.weights, octree, depth, self.in_channels,
    367           self.out_channels, self.kernel_size, self.stride, self.nempty,
--> 368           self.max_buffer)
    369 
    370     if self.use_bias:

/opt/conda/lib/python3.7/site-packages/ocnn/nn/octree_conv.py in forward(ctx, data, weights, octree, depth, in_channels, out_channels, kernel_size, stride, nempty, max_buffer)
    238     octree_conv = _OctreeConv(
    239         in_channels, out_channels, kernel_size, stride, nempty, max_buffer)
--> 240     octree_conv.setup(octree, depth)
    241     out = octree_conv.check_and_init(data)
    242     out = octree_conv.forward_gemm(out, data, weights)

/opt/conda/lib/python3.7/site-packages/ocnn/nn/octree_conv.py in setup(self, octree, depth)
     86       self.out_h = octree.nnum_nempty[self.out_depth]
     87     else:
---> 88       self.in_h = octree.nnum[self.in_depth]
     89       self.out_h = octree.nnum[self.out_depth]
     90       if self.stride == 2:

AttributeError: type object 'Octree' has no attribute 'nnum'

And I think we have to call the OCtree properly not the object!

Any idea? Thanks

wang-ps commented 1 year ago

Please run the provided examples following the tutorials like Classification, Segmentation

samanemami commented 1 year ago

Thank you for the examples and datasets Of course, they are very clear and practical, But as your library is a capable library in terms of the point cloud, I intended to use the models from the model dir, such as the above code without any change. But if you only consider the above code (which is the same result as the one in the model), it does not work. I am not sure what element I am missing in the implementation