Open conchincradle opened 1 year ago
!pip install IQA_pytorch
def mse(A,B):
return np.sqrt(np.sum(np.power((np.float32(A) - np.float32(B))/255, 2))/(256*256))
from IQA_pytorch import SSIM, GMSD, NLPD,LPIPSvgg, DISTS,MS_SSIM,VSI
img1 = cv2.imread("animal.png",0)
img1_tensor =transforms.ToTensor()(img1).unsqueeze(0)
X = img1_tensor
# X = X.repeat(1, 3, 1, 1)
Y = X
print(X.shape)
# D = SSIM(channels=1)
D = LPIPSvgg(channels=3)
# Calculate score of the image X with the reference Y
# X: (N,3,H,W)
# Y: (N,3,H,W)
# Tensor, data range: 0~1
score = D(X, Y, as_loss=False)
# set 'as_loss=True' to get a value as loss for optimizations.
print(score.item())
import torchvision.transforms as transforms
def mse(img1,img2):
t1 =transforms.ToTensor()(img1).unsqueeze(0)
t2 =transforms.ToTensor()(img2).unsqueeze(0)
score = D(t1,t2,as_loss=False)
return 1-score.item()
import cv2
dists1 = []
dists2 = []
dists3 = []
paths = ["animal","flower","foliage","fruit","landscape","manmade","shadow","texture","winter"]
for path in paths:
pre = "swirl/"
f1 = path + ".png"
f2 = pre + "14jnd_" + path +".png"
# u_filename1 = pre+"u" + path + ".png"
# u_filename2 = pre+"14jnd_u" + path +".png"
img1 = cv2.imread(f1,0)
img2 = cv2.imread(f2,0)
dists1.append(mse(img1,img2))
for path in paths:
pre = "blur/"
f1 = path + ".png"
f2 = pre + "14jnd_" + path +".png"
# u_filename1 = pre+"u" + path + ".png"
# u_filename2 = pre+"14jnd_u" + path +".png"
img1 = cv2.imread(f1,0)
img2 = cv2.imread(f2,0)
dists2.append(mse(img1,img2))
for path in paths:
pre = "noise/"
f1 = path + ".png"
nums = []
img1 = cv2.imread(f1,0)
for i in range(10):
f2 = pre + "14jnd_" + path +"_"+str(i)+".png"
# u_filename1 = pre+"u" + path + ".png"
# u_filename2 = pre+"14jnd_u" + path +".png"
img2 = cv2.imread(f2,0)
nums.append(mse(img1,img2))
dists3.append(np.mean(nums))
print(np.mean(dists1))
print(np.mean(dists2))
print(np.mean(dists3))
n = np.mean([np.mean(dists1),np.mean(dists2),np.mean(dists3)])
print(n)
print("--------------------------------")
import cv2
dists4 = []
dists5 = []
dists6 = []
paths = ["animal","flower","foliage","fruit","landscape","manmade","shadow","texture","winter"]
upaths = list(map(lambda x: "u" + x, paths))
for path in upaths:
pre = "swirl/"
f1 = path + ".png"
f2 = pre + "14jnd_" + path +".png"
# u_filename1 = pre+"u" + path + ".png"
# u_filename2 = pre+"14jnd_u" + path +".png"
img1 = cv2.imread(f1,0)
img2 = cv2.imread(f2,0)
dists4.append(mse(img1,img2))
for path in upaths:
pre = "blur/"
f1 = path + ".png"
f2 = pre + "14jnd_" + path +".png"
# u_filename1 = pre+"u" + path + ".png"
# u_filename2 = pre+"14jnd_u" + path +".png"
img1 = cv2.imread(f1,0)
img2 = cv2.imread(f2,0)
dists5.append(mse(img1,img2))
for path in upaths:
pre = "noise/"
f1 = path + ".png"
nums = []
img1 = cv2.imread(f1,0)
for i in range(10):
f2 = pre + "14jnd_" + path +"_"+str(i)+".png"
# u_filename1 = pre+"u" + path + ".png"
# u_filename2 = pre+"14jnd_u" + path +".png"
img2 = cv2.imread(f2,0)
nums.append(mse(img1,img2))
dists6.append(np.mean(nums))
print(np.mean(dists4))
print(np.mean(dists5))
print(np.mean(dists6))
un = np.mean([np.mean(dists4),np.mean(dists5),np.mean(dists6)])
print(un)
print(np.mean([n,un]))
For DISTS and LPIPSvgg using CUDA