我想比较两个图像,但 ms_ssim 想要 4D 张量
https://pypi.org/project/pytorch-msssim/
我试过了
from PIL import Image
from tqdm import tqdm
from pytorch_msssim import ssim, ms_ssim, SSIM, MS_SSIM
import torchvision
import numpy as np
topil=torchvision.transforms.ToPILImage()
totensor=torchvision.transforms.ToTensor()
def ssimcompare(path1:str,path2:str)->float:
image1 = Image.open(path1)
image2 = Image.open(path2)
#it1=np.expand_dims(totensor(topil(np.array(image1))), axis=0)
#it2=np.expand_dims(totensor(topil(np.array(image2))), axis=0)
#it1=totensor(np.expand_dims(np.array(image1), axis=0))
#it2=totensor(np.expand_dims(np.array(image2), axis=0))
it1=totensor(np.array(image1))
it2=totensor(np.array(image2))
valor=ms_ssim( it1 , it2, data_range=255, size_average=False )
return valor
但我得到不同的错误
ValueError: Input images must be 4-d tensors.
TypeError: pic should be Tensor or ndarray. Got <class 'PIL.JpegImagePlugin.JpegImageFile'>.
AttributeError: 'numpy.ndarray' object has no attribute 'type'