| |
| |
|
|
| from __future__ import print_function |
| import torch |
| import numpy as np |
| from PIL import Image |
| import numpy as np |
| import os |
| import torch.nn as nn |
|
|
| |
| |
| def tensor2im(image_tensor, imtype=np.uint8, normalize=True): |
| if isinstance(image_tensor, list): |
| image_numpy = [] |
| for i in range(len(image_tensor)): |
| image_numpy.append(tensor2im(image_tensor[i], imtype, normalize)) |
| return image_numpy |
| image_numpy = image_tensor.cpu().float().numpy() |
| if normalize: |
| image_numpy = (np.transpose(image_numpy, (1, 2, 0)) + 1) / 2.0 * 255.0 |
| else: |
| image_numpy = np.transpose(image_numpy, (1, 2, 0)) * 255.0 |
| image_numpy = np.clip(image_numpy, 0, 255) |
| if image_numpy.shape[2] == 1 or image_numpy.shape[2] > 3: |
| image_numpy = image_numpy[:, :, 0] |
| return image_numpy.astype(imtype) |
|
|
|
|
| |
| def tensor2label(label_tensor, n_label, imtype=np.uint8): |
| if n_label == 0: |
| return tensor2im(label_tensor, imtype) |
| label_tensor = label_tensor.cpu().float() |
| if label_tensor.size()[0] > 1: |
| label_tensor = label_tensor.max(0, keepdim=True)[1] |
| label_tensor = Colorize(n_label)(label_tensor) |
| label_numpy = np.transpose(label_tensor.numpy(), (1, 2, 0)) |
| return label_numpy.astype(imtype) |
|
|
|
|
| def save_image(image_numpy, image_path): |
| image_pil = Image.fromarray(image_numpy) |
| image_pil.save(image_path) |
|
|
|
|
| def mkdirs(paths): |
| if isinstance(paths, list) and not isinstance(paths, str): |
| for path in paths: |
| mkdir(path) |
| else: |
| mkdir(paths) |
|
|
|
|
| def mkdir(path): |
| if not os.path.exists(path): |
| os.makedirs(path) |
|
|