def torch2np(tensor):
    """
    Convert from torch tensor to numpy convention.
    If 4D -> [b, c, h, w] to [b, h, w, c]
    If 3D -> [c, h, w] to [h, w, c]

    :param tensor: Torch tensor
    :return: Numpy array
    """
    d = tensor.dim()
    perm = [0, 2, 3, 1] if d == 4 else \
           [1, 2, 0] if d == 3 else \
           [0, 1]
    return tensor.permute(perm).detach().cpu().numpy()