File size: 757 Bytes
22b8701 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import torch
import numpy as np
import cv2
def tensor2img_denorm(tensor):
std = torch.tensor([0.229, 0.224, 0.225]).view(1, 3, 1, 1)
mean = torch.tensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1)
tensor = std * tensor.detach().cpu() + mean
img = tensor.numpy()
img = img.transpose(0, 2, 3, 1)[0]
img = np.clip(img * 255, 0.0, 255.0).astype(np.uint8)
return img
def tensor2img(tensor):
tensor = tensor.detach().cpu().numpy()
img = tensor.transpose(0, 2, 3, 1)[0]
img = np.clip(img * 255, 0.0, 255.0).astype(np.uint8)
return img
def show_tensor(tensor, name):
img = cv2.cvtColor(tensor2img(tensor), cv2.COLOR_RGB2BGR)
cv2.namedWindow(name, cv2.WINDOW_NORMAL)
cv2.imshow(name, img)
cv2.waitKey()
|