import numpy as np import torch def tensor2im(input_image, imtype=np.uint8): if isinstance(input_image, torch.Tensor): input_image = torch.clamp(input_image, -1.0, 1.0) image_tensor = input_image.data else: return input_image.reshape(3, 512, 512).transpose() image_numpy = image_tensor[0].cpu().float().numpy() if image_numpy.shape[0] == 1: image_numpy = np.tile(image_numpy, (3, 1, 1)) image_numpy = (np.transpose(image_numpy, (1, 2, 0)) + 1) / 2.0 * 255.0 return image_numpy.astype(imtype)