Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'Tensor' object has no attribute 'depth_map' #21

Open
macromogic opened this issue Oct 3, 2022 · 3 comments
Open

AttributeError: 'Tensor' object has no attribute 'depth_map' #21

macromogic opened this issue Oct 3, 2022 · 3 comments

Comments

@macromogic
Copy link

Hi. I wanted to train the depth2d part of the model, but it raises the following error:

Traceback (most recent call last):
  File "/home/student/.conda/envs/panoptic_cp/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home/student/.conda/envs/panoptic_cp/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/data/student/data/student/zhouyingquan/panoptic-reconstruction/tools/train_net.py", line 56, in <module>
    main()
  File "/data/student/data/student/zhouyingquan/panoptic-reconstruction/tools/train_net.py", line 51, in main
    trainer.do_train()
  File "/data/student/data/student/zhouyingquan/panoptic-reconstruction/lib/engine/trainer.py", line 91, in do_train
    losses, results = self.model(images, targets)
  File "/home/student/.conda/envs/panoptic_cp/lib/python3.8/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/data/student/data/student/zhouyingquan/panoptic-reconstruction/lib/modeling/panoptic_reconstruction.py", line 49, in forward
    depth_losses, depth_results = self.depth2d(image_features["blocks"], depth_targets)
  File "/home/student/.conda/envs/panoptic_cp/lib/python3.8/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/data/student/data/student/zhouyingquan/panoptic-reconstruction/lib/modeling/depth/depth_prediction.py", line 57, in forward
    valid_masks = torch.stack([(depth.depth_map != 0.0).bool() for depth in depth_target], dim=0)
  File "/data/student/data/student/zhouyingquan/panoptic-reconstruction/lib/modeling/depth/depth_prediction.py", line 57, in <listcomp>
    valid_masks = torch.stack([(depth.depth_map != 0.0).bool() for depth in depth_target], dim=0)
AttributeError: 'Tensor' object has no attribute 'depth_map'

I inspected the code and found the following code in lib/modeling/depth/depth_prediction.py:

    def forward(self, features, depth_target) -> ModuleResult:
        depth_pred, depth_feature = self.model(features)
        depth_return = [DepthMap(p_[0].cpu(), t_.get_intrinsic()) for p_, t_ in zip(depth_pred, depth_target)]
        depth_target = torch.stack([target.get_tensor() for target in depth_target]).float().to(config.MODEL.DEVICE).unsqueeze(1)
        ...
        if self.training:
            valid_masks = torch.stack([(depth.depth_map != 0.0).bool() for depth in depth_target], dim=0)

If my observations are correct, in the third line of the function, depth_targets has become a list of Tensors, so the attribute depth_map is lost. Is this a bug? How can I resolve this?

@unlugi
Copy link

unlugi commented Oct 3, 2022

I couldn't train the 2D part of the network from scratch either. Observed the exact error.
The provided code does not work for training the all parts of the pipeline.

Maybe changing depth.depth_map to depth might work. But then the rest of the code might complain.

@macromogic
Copy link
Author

macromogic commented Oct 4, 2022

I changed depth.depth_map to depth and, yes, the code did complain. 🙂 Below are the problems I met and solutions I adopted for myself:

  1. Shape mismatch when calculating loss in DepthPrediction.forward() (in file lib/modeling/depth_prediction.py)

    The author might not realized that the parameter depth_target has been shadowed by the local variable. In short there is no need to do another unsqueezing for valid_masks.

    Just modify the following lines:

    ## Before:
    valid_masks = torch.stack([(depth.depth_map != 0.0).bool() for depth in depth_target], dim=0)
    valid_masks.unsqueeze_(1)
    
    ## After:
    valid_masks = torch.stack([(depth != 0.0).bool() for depth in depth_target], dim=0)
    # valid_masks.unsqueeze_(1)
  2. Device mismatch in the RPN module in GeneralizedRCNN.forward() (in file lib/modeling/detector/generalized_rcnn.py):

    This error occurs because the instance ground truth is not put on the GPU. Change the following line:

    ## Before:
    bounding_boxes_gt = [target.get_field("instance2d") for target in targets]
    
    ## After:
    bounding_boxes_gt = [target.get_field("instance2d").to(config.MODEL.DEVICE) for target in targets]
  3. TypeError: 'module' object is not callable (for function smooth_l1_loss from module lib.layers):

    This function is defined in lib/layers/smooth_l1_loss.py, but the author forgot to export it in lib/layers/__init__.py. Adding the following line in the latter file solves the problem:

    from .smooth_l1_loss import smooth_l1_loss
  4. Shape mismatch when performing instance masking in UNetBlockHybridSparse.forward() (in file lib/modeling/backbone/unet_sparse.py):

    In my case, the network raised the error here:

    instance_prediction = torch.masked_fill(instance_prediction, frustum_mask.squeeze() == False, 0.0)

    Then I figured out that the shape of instance_prediction is [batch_size, config.MODEL.INSTANCE2D.MAX+1, 64, 64, 64], but for frustum_mask, it is [batch_size, 64, 64, 64]. If batch_size does not equal to 1 or config.MODEL.INSTANCE2D.MAX+1 (16 by default), the broadcasting fails. I resolved it by setting batch_size (i.e., config.DATALOADER.IMS_PER_BATCH) to 1. Other feasible solutions might be setting it to 16, or make a temporary unsqueezing for frustum_mask on dimension 1.

Hope my information helps!

@xheon
Copy link
Owner

xheon commented Oct 6, 2022

Hi all, sorry for the delay - I just returned from travelling. I will have a look at this issue the next days.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants