def img_normalize(img, val_range=None):
    """ Normalize the tensor into (0, 1).

    Args:
        tensor (torch.Tensor): input tensor.
        val_range (tuple): range in the form (min_val, max_val).
    Returns:
        torch.Tensor: normalized tensor
    """
    t = img.clone()
    if val_range:
        mm, mx = val_range[0], val_range[1]
    else:
        mm, mx = t.min(), t.max()
    try:
        return t.add_(-mm).div_(mx - mm)
    except RuntimeError:
        return img