图像读取为tensor
def img2tensor(path):
import torchvision.transforms as transforms
import cv2 as cv
img = cv.imread(path)
transf = transforms.ToTensor()
img_tensor = transf(img)
# print('opencv', img)
# print('torch', img_tensor)
return img_tensor
tensor存为图像
def tensor2img(img,name):
from torchvision import utils as vutils
vutils.save_image(img, name, normalize=True)