import torch


def _to_format(image, out_format=None):
    """ return recentered image
    Args
        img         (torch.tensor)
        out_format  (str [None]) | float32, uint8
                None        centered torch tensor
                'float32'   np.ndarray, centered, clamped
                'uint8'     np.ndarray, centered, clamted
    """
    # unnormalize
    image = image.clone().detach().cpu()
    if out_format is None:
        return image

    _norm = 255 if out_format[0] == 'u' else 1
    image.add_(1).mul_(_norm/2).clamp_(0, _norm)
    image = image.permute(0, 2, 3, 1)[0]
    if _norm == 255:
        image = image.to(torch.uint8)
    return image.numpy()