| 8 | # Converts a Tensor into a Numpy array |
| 9 | # |imtype|: the desired type of the converted numpy array |
| 10 | def tensor2im(image_tensor, imtype=np.uint8, normalize=True): |
| 11 | if isinstance(image_tensor, list): |
| 12 | image_numpy = [] |
| 13 | for i in range(len(image_tensor)): |
| 14 | image_numpy.append(tensor2im(image_tensor[i], imtype, normalize)) |
| 15 | return image_numpy |
| 16 | image_numpy = image_tensor.cpu().float().numpy() |
| 17 | if normalize: |
| 18 | image_numpy = (np.transpose(image_numpy, (1, 2, 0)) + 1) / 2.0 * 255.0 |
| 19 | else: |
| 20 | image_numpy = np.transpose(image_numpy, (1, 2, 0)) * 255.0 |
| 21 | image_numpy = np.clip(image_numpy, 0, 255) |
| 22 | if image_numpy.shape[2] == 1 or image_numpy.shape[2] > 3: |
| 23 | image_numpy = image_numpy[:,:,0] |
| 24 | return image_numpy.astype(imtype) |
| 25 | |
| 26 | # Converts a one-hot tensor into a colorful label map |
| 27 | def tensor2label(label_tensor, n_label, imtype=np.uint8): |