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)
| 42 | |
| 43 | |
| 44 | def tensor2im(input_image, imtype=np.uint8): |
| 45 | """"Converts a Tensor array into a numpy image array. |
| 46 | |
| 47 | Parameters: |
| 48 | input_image (tensor) -- the input image tensor array |
| 49 | imtype (type) -- the desired type of the converted numpy array |
| 50 | """ |
| 51 | if not isinstance(input_image, np.ndarray): |
| 52 | if isinstance(input_image, torch.Tensor): # get the data from a variable |
| 53 | image_tensor = input_image.data |
| 54 | else: |
| 55 | return input_image |
| 56 | image_numpy = image_tensor[0].clamp(-1.0, 1.0).cpu().float().numpy() # convert it into a numpy array |
| 57 | if image_numpy.shape[0] == 1: # grayscale to RGB |
| 58 | image_numpy = np.tile(image_numpy, (3, 1, 1)) |
| 59 | image_numpy = (np.transpose(image_numpy, (1, 2, 0)) + 1) / 2.0 * 255.0 # post-processing: tranpose and scaling |
| 60 | else: # if it is a numpy array, do nothing |
| 61 | image_numpy = input_image |
| 62 | return image_numpy.astype(imtype) |
| 63 | |
| 64 | |
| 65 | def diagnose_network(net, name='network'): |