#IndexError: too many indices for tensor of dimension 1

def compute_mask_loss(self, mask_predict, positive_gt_idx, box_predicts, targets):
    mask_gt = targets['mask'].split(targets['batch_len'])
    box_gt = targets['target'].split(targets['batch_len'])
    loss_mask_predicts = list()
    loss_mask_target = list()
    for mg, bg, mp, pgi, bp in zip(mask_gt, box_gt, mask_predict, positive_gt_idx, box_predicts):
        cls_idx = bg[:, 0].long()[pgi]
        mg_t = mg[pgi]
        mp = mp[range(len(mp)), cls_idx, :]
        bp_extend = torch.cat([torch.arange(len(bp), device=bp.device)[:, None], bp], dim=-1)
        mt = roi_align(mg_t[:, None, :, :], bp_extend, (mp.shape[-1], mp.shape[-1]), 1.)[:, 0]
    loss_mask_predicts = torch.cat(loss_mask_predicts)
    loss_mask_target = torch.cat(loss_mask_target)
    mask_loss = self.bce(loss_mask_predicts, loss_mask_target)
    return mask_loss

I don’t know, which operation is raising this issue, but it could be the indexing of mg_t, in case it has a single dimension as seen here:

mg_t = torch.randn(10)
mg_t[:, None, :, :]
> IndexError: too many indices for tensor of dimension 1
1 Like