MCPcopy Create free account
hub / github.com/Relento/lego_release / tensor2im

Function tensor2im

util/util.py:63–81  ·  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

61
62
63def tensor2im(input_image, imtype=np.uint8):
64 """"Converts a Tensor array into a numpy image array.
65
66 Parameters:
67 input_image (tensor) -- the input image tensor array
68 imtype (type) -- the desired type of the converted numpy array
69 """
70 if not isinstance(input_image, np.ndarray):
71 if isinstance(input_image, torch.Tensor): # get the data from a variable
72 image_tensor = input_image.data
73 else:
74 return input_image
75 image_numpy = image_tensor[0].cpu().float().numpy() # convert it into a numpy array
76 if image_numpy.shape[0] == 1: # grayscale to RGB
77 image_numpy = np.tile(image_numpy, (3, 1, 1))
78 image_numpy = (np.transpose(image_numpy, (1, 2, 0)) + 1) / 2.0 * 255.0 # post-processing: tranpose and scaling
79 else: # if it is a numpy array, do nothing
80 image_numpy = input_image
81 return image_numpy.astype(imtype)
82
83
84def diagnose_network(net, name='network'):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected