def tensor2np(img, rgb2bgr=True, remove_batch=True, data_range=255, denormalize=False, change_range=True, imtype=np.uint8):
    '\n    Converts a Tensor array into a numpy image array.\n    Parameters:\n        img (tensor): the input image tensor array\n            4D(B,(3/1),H,W), 3D(C,H,W), or 2D(H,W), any range, RGB channel order\n        remove_batch (bool): choose if tensor of shape BCHW needs to be squeezed\n        denormalize (bool): Used to denormalize from [-1,1] range back to [0,1]\n        imtype (type): the desired type of the converted numpy array (np.uint8\n            default)\n    Output:\n        img (np array): 3D(H,W,C) or 2D(H,W), [0,255], np.uint8 (default)\n    '
    if (not isinstance(img, torch.Tensor)):
        raise TypeError('Got unexpected object type, expected torch.Tensor')
    n_dim = img.dim()
    img = img.float().cpu()
    if ((n_dim == 4) or (n_dim == 3)):
        if ((n_dim == 4) and remove_batch):
            if (img.shape[0] > 1):
                img = img[(0, ...)]
            else:
                img = img.squeeze()
                if (len(img.shape) < 3):
                    img = img.unsqueeze(dim=0)
        else:
            n_img = len(img)
            img = make_grid(img, nrow=int(math.sqrt(n_img)), normalize=False)
        if ((img.shape[0] == 3) and rgb2bgr):
            img_np = rgb_to_bgr(img).numpy()
        elif ((img.shape[0] == 4) and rgb2bgr):
            img_np = rgba_to_bgra(img).numpy()
        else:
            img_np = img.numpy()
        img_np = np.transpose(img_np, (1, 2, 0))
    elif (n_dim == 2):
        img_np = img.numpy()
    else:
        raise TypeError('Only support 4D, 3D and 2D tensor. But received with dimension: {:d}'.format(n_dim))
    if denormalize:
        img_np = denorm(img_np)
    if change_range:
        img_np = np.clip((data_range * img_np), 0, data_range).round()
    return img_np.astype(imtype)
Converts a Tensor array into a numpy image array.
Parameters:
    img (tensor): the input image tensor array
        4D(B,(3/1),H,W), 3D(C,H,W), or 2D(H,W), any range, RGB channel order
    remove_batch (bool): choose if tensor of shape BCHW needs to be squeezed
    denormalize (bool): Used to denormalize from [-1,1] range back to [0,1]
    imtype (type): the desired type of the converted numpy array (np.uint8
        default)
Output:
    img (np array): 3D(H,W,C) or 2D(H,W), [0,255], np.uint8 (default)
def tensor2np(img, rgb2bgr=True, remove_batch=True, data_range=255, denormalize=False, change_range=True, imtype=np.uint8):
    '\n    Converts a Tensor array into a numpy image array.\n    Parameters:\n        img (tensor): the input image tensor array\n            4D(B,(3/1),H,W), 3D(C,H,W), or 2D(H,W), any range, RGB channel order\n        remove_batch (bool): choose if tensor of shape BCHW needs to be squeezed\n        denormalize (bool): Used to denormalize from [-1,1] range back to [0,1]\n        imtype (type): the desired type of the converted numpy array (np.uint8\n            default)\n    Output:\n        img (np array): 3D(H,W,C) or 2D(H,W), [0,255], np.uint8 (default)\n    '
    if (not isinstance(img, torch.Tensor)):
        raise TypeError('Got unexpected object type, expected torch.Tensor')
    n_dim = img.dim()
    img = img.float().cpu()
    if ((n_dim == 4) or (n_dim == 3)):
        if ((n_dim == 4) and remove_batch):
            if (img.shape[0] > 1):
                img = img[(0, ...)]
            else:
                img = img.squeeze()
                if (len(img.shape) < 3):
                    img = img.unsqueeze(dim=0)
        else:
            n_img = len(img)
            img = make_grid(img, nrow=int(math.sqrt(n_img)), normalize=False)
        if ((img.shape[0] == 3) and rgb2bgr):
            img_np = rgb_to_bgr(img).numpy()
        elif ((img.shape[0] == 4) and rgb2bgr):
            img_np = rgba_to_bgra(img).numpy()
        else:
            img_np = img.numpy()
        img_np = np.transpose(img_np, (1, 2, 0))
    elif (n_dim == 2):
        img_np = img.numpy()
    else:
        raise TypeError('Only support 4D, 3D and 2D tensor. But received with dimension: {:d}'.format(n_dim))
    if denormalize:
        img_np = denorm(img_np)
    if change_range:
        img_np = np.clip((data_range * img_np), 0, data_range).round()
    return img_np.astype(imtype)