MCPcopy Create free account
hub / github.com/NVlabs/SPADE / tensor2im

Function tensor2im

util/util.py:64–95  ·  view source on GitHub ↗
(image_tensor, imtype=np.uint8, normalize=True, tile=False)

Source from the content-addressed store, hash-verified

62# Converts a Tensor into a Numpy array
63# |imtype|: the desired type of the converted numpy array
64def tensor2im(image_tensor, imtype=np.uint8, normalize=True, tile=False):
65 if isinstance(image_tensor, list):
66 image_numpy = []
67 for i in range(len(image_tensor)):
68 image_numpy.append(tensor2im(image_tensor[i], imtype, normalize))
69 return image_numpy
70
71 if image_tensor.dim() == 4:
72 # transform each image in the batch
73 images_np = []
74 for b in range(image_tensor.size(0)):
75 one_image = image_tensor[b]
76 one_image_np = tensor2im(one_image)
77 images_np.append(one_image_np.reshape(1, *one_image_np.shape))
78 images_np = np.concatenate(images_np, axis=0)
79 if tile:
80 images_tiled = tile_images(images_np)
81 return images_tiled
82 else:
83 return images_np
84
85 if image_tensor.dim() == 2:
86 image_tensor = image_tensor.unsqueeze(0)
87 image_numpy = image_tensor.detach().cpu().float().numpy()
88 if normalize:
89 image_numpy = (np.transpose(image_numpy, (1, 2, 0)) + 1) / 2.0 * 255.0
90 else:
91 image_numpy = np.transpose(image_numpy, (1, 2, 0)) * 255.0
92 image_numpy = np.clip(image_numpy, 0, 255)
93 if image_numpy.shape[2] == 1:
94 image_numpy = image_numpy[:, :, 0]
95 return image_numpy.astype(imtype)
96
97
98# Converts a one-hot tensor into a colorful label map

Callers 1

tensor2labelFunction · 0.85

Calls 1

tile_imagesFunction · 0.85

Tested by

no test coverage detected