import torch


def densify_features(x, shape):
    """
    Densify features from a sparse tensor

    Parameters
    ----------
    x : Sparse tensor
    shape : Dense shape [B,C,H,W]

    Returns
    -------
    Dense tensor containing sparse information
    """
    stride = x.tensor_stride
    coords, feats = x.C.long(), x.F
    shape = (shape[0], shape[2] // stride[0], shape[3] // stride[1], feats.shape[1])
    dense = torch.zeros(shape, device=x.device)
    dense[coords[:, 0],
          coords[:, 1] // stride[0],
          coords[:, 2] // stride[1]] = feats
    return dense.permute(0, 3, 1, 2).contiguous()