MCPcopy Create free account
hub / github.com/chenhaoxing/HDNet / tensor2im

Function tensor2im

util/util.py:9–27  ·  view source on GitHub ↗

Converts a Tensor array into a numpy image array. Parameters: input_image (tensor) -- the input image tensor array imtype (type) -- the desired type of the converted numpy array

(input_image, imtype=np.uint8)

Source from the content-addressed store, hash-verified

7
8
9def tensor2im(input_image, imtype=np.uint8):
10 """"Converts a Tensor array into a numpy image array.
11
12 Parameters:
13 input_image (tensor) -- the input image tensor array
14 imtype (type) -- the desired type of the converted numpy array
15 """
16 if not isinstance(input_image, np.ndarray):
17 if isinstance(input_image, torch.Tensor): # get the data from a variable
18 image_tensor = input_image.data
19 else:
20 return input_image
21 image_numpy = image_tensor[0].cpu().float().numpy() # convert it into a numpy array
22 if image_numpy.shape[0] == 1: # grayscale to RGB
23 image_numpy = np.tile(image_numpy, (3, 1, 1))
24 image_numpy = (np.transpose(image_numpy, (1, 2, 0)) + 1) / 2.0 * 255.0 # post-processing: tranpose and scaling
25 else: # if it is a numpy array, do nothing
26 image_numpy = input_image
27 return image_numpy.astype(imtype)
28
29
30def diagnose_network(net, name='network'):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected